@legendapp/state 3.0.0-alpha.4 → 3.0.0-alpha.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. package/.DS_Store +0 -0
  2. package/CHANGELOG.md +831 -1
  3. package/LICENSE +21 -1
  4. package/README.md +141 -1
  5. package/babel.js +0 -2
  6. package/babel.mjs +0 -2
  7. package/config/configureLegendState.d.mts +13 -0
  8. package/config/configureLegendState.d.ts +13 -0
  9. package/config/configureLegendState.js +45 -0
  10. package/config/configureLegendState.mjs +43 -0
  11. package/config/enable$GetSet.js +2 -1
  12. package/config/enable$GetSet.mjs +2 -1
  13. package/config/enableReactTracking.js +2 -1
  14. package/config/enableReactTracking.mjs +2 -1
  15. package/config/enableReactUse.js +2 -1
  16. package/config/enableReactUse.mjs +2 -1
  17. package/config/enable_PeekAssign.js +2 -1
  18. package/config/enable_PeekAssign.mjs +2 -1
  19. package/helpers/trackHistory.js +2 -2
  20. package/helpers/trackHistory.mjs +2 -2
  21. package/index.d.mts +106 -81
  22. package/index.d.ts +106 -81
  23. package/index.js +347 -335
  24. package/index.mjs +344 -333
  25. package/package.json +36 -1
  26. package/persist-plugins/async-storage.d.mts +6 -3
  27. package/persist-plugins/async-storage.d.ts +6 -3
  28. package/persist-plugins/async-storage.js +8 -4
  29. package/persist-plugins/async-storage.mjs +8 -5
  30. package/persist-plugins/indexeddb.d.mts +6 -4
  31. package/persist-plugins/indexeddb.d.ts +6 -4
  32. package/persist-plugins/indexeddb.js +35 -15
  33. package/persist-plugins/indexeddb.mjs +35 -16
  34. package/persist-plugins/mmkv.d.mts +5 -1
  35. package/persist-plugins/mmkv.d.ts +5 -1
  36. package/persist-plugins/mmkv.js +10 -5
  37. package/persist-plugins/mmkv.mjs +10 -6
  38. package/react-reactive/enableReactComponents.d.mts +9 -0
  39. package/react-reactive/enableReactComponents.d.ts +9 -0
  40. package/react-reactive/enableReactComponents.js +19 -0
  41. package/react-reactive/enableReactComponents.mjs +17 -0
  42. package/react-reactive/enableReactNativeComponents.d.mts +22 -0
  43. package/react-reactive/enableReactNativeComponents.d.ts +22 -0
  44. package/react-reactive/enableReactNativeComponents.js +53 -0
  45. package/react-reactive/enableReactNativeComponents.mjs +51 -0
  46. package/react-reactive/enableReactive.d.mts +5 -0
  47. package/react-reactive/enableReactive.d.ts +5 -0
  48. package/react-reactive/enableReactive.js +24 -0
  49. package/react-reactive/enableReactive.mjs +22 -0
  50. package/react-reactive/enableReactive.native.d.mts +5 -0
  51. package/react-reactive/enableReactive.native.d.ts +5 -0
  52. package/react-reactive/enableReactive.native.js +58 -0
  53. package/react-reactive/enableReactive.native.mjs +56 -0
  54. package/react-reactive/enableReactive.web.d.mts +5 -0
  55. package/react-reactive/enableReactive.web.d.ts +5 -0
  56. package/react-reactive/enableReactive.web.js +58 -0
  57. package/react-reactive/enableReactive.web.mjs +56 -0
  58. package/react.d.mts +39 -34
  59. package/react.d.ts +39 -34
  60. package/react.js +54 -27
  61. package/react.mjs +55 -28
  62. package/sync-plugins/crud.d.mts +21 -24
  63. package/sync-plugins/crud.d.ts +21 -24
  64. package/sync-plugins/crud.js +241 -140
  65. package/sync-plugins/crud.mjs +243 -142
  66. package/sync-plugins/fetch.js +12 -8
  67. package/sync-plugins/fetch.mjs +13 -9
  68. package/sync-plugins/firebase.d.mts +27 -0
  69. package/sync-plugins/firebase.d.ts +27 -0
  70. package/sync-plugins/firebase.js +373 -0
  71. package/sync-plugins/firebase.mjs +368 -0
  72. package/sync-plugins/keel.d.mts +43 -26
  73. package/sync-plugins/keel.d.ts +43 -26
  74. package/sync-plugins/keel.js +145 -100
  75. package/sync-plugins/keel.mjs +147 -100
  76. package/sync-plugins/supabase.d.mts +19 -9
  77. package/sync-plugins/supabase.d.ts +19 -9
  78. package/sync-plugins/supabase.js +52 -22
  79. package/sync-plugins/supabase.mjs +53 -23
  80. package/sync-plugins/tanstack-query.d.mts +2 -2
  81. package/sync-plugins/tanstack-query.d.ts +2 -2
  82. package/sync-plugins/tanstack-query.js +22 -5
  83. package/sync-plugins/tanstack-query.mjs +22 -5
  84. package/sync-plugins/tanstack-react-query.d.mts +1 -1
  85. package/sync-plugins/tanstack-react-query.d.ts +1 -1
  86. package/sync-plugins/tanstack-react-query.js +8 -1
  87. package/sync-plugins/tanstack-react-query.mjs +8 -1
  88. package/sync.d.mts +74 -200
  89. package/sync.d.ts +74 -200
  90. package/sync.js +510 -307
  91. package/sync.mjs +516 -313
  92. package/types/babel.d.ts +12 -1
  93. /package/config/{enable_GetSet.d.mts → enable$GetSet.d.mts} +0 -0
  94. /package/config/{enable_GetSet.d.ts → enable$GetSet.d.ts} +0 -0
package/react.js CHANGED
@@ -3,6 +3,7 @@
3
3
  var state = require('@legendapp/state');
4
4
  var React = require('react');
5
5
  var index_js = require('use-sync-external-store/shim/index.js');
6
+ var enableReactive = require('@legendapp/state/react-reactive/enableReactive');
6
7
 
7
8
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
8
9
 
@@ -111,15 +112,6 @@ function useSelector(selector, options) {
111
112
  const { subscribe, getVersion, run } = selectorFn;
112
113
  value = run(selector);
113
114
  index_js.useSyncExternalStore(subscribe, getVersion, getVersion);
114
- if (options == null ? void 0 : options.suspense) {
115
- if (state.isPromise(value) || !value && state.isObservable(selector) && state.syncState(selector).isLoaded.get() === false) {
116
- if (React__default.default.use) {
117
- React__default.default.use(value);
118
- } else {
119
- throw value;
120
- }
121
- }
122
- }
123
115
  } catch (err) {
124
116
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && ((_a = err == null ? void 0 : err.message) == null ? void 0 : _a.includes("Rendered more"))) {
125
117
  console.warn(
@@ -128,6 +120,17 @@ function useSelector(selector, options) {
128
120
  }
129
121
  throw err;
130
122
  }
123
+ if (options == null ? void 0 : options.suspense) {
124
+ const isProm = state.isPromise(value);
125
+ if (state.isPromise(value) || !value && state.isObservable(selector)) {
126
+ const vProm = isProm ? value : state.when(selector);
127
+ if (React__default.default.use) {
128
+ React__default.default.use(vProm);
129
+ } else {
130
+ throw vProm;
131
+ }
132
+ }
133
+ }
131
134
  return value;
132
135
  }
133
136
 
@@ -334,8 +337,27 @@ function For({
334
337
  return out;
335
338
  }
336
339
  var Memo = React.memo(Computed, () => true);
340
+
341
+ // src/react/configureReactive.ts
337
342
  var ReactiveFns = /* @__PURE__ */ new Map();
338
343
  var ReactiveFnBinders = /* @__PURE__ */ new Map();
344
+ function configureReactive({
345
+ components,
346
+ binders
347
+ }) {
348
+ if (components) {
349
+ for (const key in components) {
350
+ ReactiveFns.set(key, components[key]);
351
+ }
352
+ }
353
+ if (binders) {
354
+ for (const key in binders) {
355
+ ReactiveFnBinders.set(key, binders[key]);
356
+ }
357
+ }
358
+ }
359
+
360
+ // src/react/Reactive.tsx
339
361
  var Reactive = new Proxy(
340
362
  {},
341
363
  {
@@ -355,26 +377,14 @@ var Reactive = new Proxy(
355
377
  }
356
378
  }
357
379
  );
358
- function configureReactive({
359
- components,
360
- binders
361
- }) {
362
- if (components) {
363
- for (const key in components) {
364
- ReactiveFns.set(key, components[key]);
365
- }
366
- }
367
- if (binders) {
368
- for (const key in binders) {
369
- ReactiveFnBinders.set(key, binders[key]);
370
- }
371
- }
380
+ if (process.env.NODE_ENV !== "test") {
381
+ enableReactive.enableReactive(configureReactive);
372
382
  }
373
- function Show({ if: if_, ifReady, else: else_, wrap, children }) {
383
+ function Show({ if: if_, ifReady, else: else_, $value, wrap, children }) {
374
384
  const value = useSelector(if_ != null ? if_ : ifReady);
375
385
  const show = ifReady !== void 0 ? state.isObservableValueReady(value) : value;
376
386
  const child = useSelector(
377
- show ? state.isFunction(children) ? () => children(value) : children : else_ != null ? else_ : null,
387
+ show ? state.isFunction(children) ? () => children($value ? $value.get() : value) : children : else_ != null ? else_ : null,
378
388
  { skipCheck: true }
379
389
  );
380
390
  return wrap ? React.createElement(wrap, void 0, child) : child;
@@ -395,7 +405,10 @@ function useObservable(initialValue, deps) {
395
405
  ref.current.value = initialValue;
396
406
  const depsObs$ = deps ? useObservable(deps) : void 0;
397
407
  if (!((_a = ref.current) == null ? void 0 : _a.obs$)) {
398
- const value = depsObs$ ? () => {
408
+ const value = depsObs$ ? state.isFunction(initialValue) && initialValue.length === 1 ? (p) => {
409
+ depsObs$.get();
410
+ return ref.current.value(p);
411
+ } : () => {
399
412
  depsObs$.get();
400
413
  return state.computeSelector(ref.current.value);
401
414
  } : initialValue;
@@ -490,8 +503,16 @@ function useObserve(selector, reactionOrOptions, options) {
490
503
  } else {
491
504
  options = reactionOrOptions;
492
505
  }
506
+ const deps = options == null ? void 0 : options.deps;
507
+ const depsObs$ = deps ? useObservable(deps) : void 0;
508
+ if (depsObs$) {
509
+ depsObs$.set(deps);
510
+ }
493
511
  const ref = React.useRef({});
494
- ref.current.selector = selector;
512
+ ref.current.selector = deps ? () => {
513
+ depsObs$ == null ? void 0 : depsObs$.get();
514
+ return state.computeSelector(selector);
515
+ } : selector;
495
516
  ref.current.reaction = reaction;
496
517
  if (!ref.current.dispose) {
497
518
  ref.current.dispose = state.observe(
@@ -516,12 +537,18 @@ function useObserveEffect(selector, reactionOrOptions, options) {
516
537
  } else {
517
538
  options = reactionOrOptions;
518
539
  }
540
+ const deps = options == null ? void 0 : options.deps;
541
+ const depsObs$ = deps ? useObservable(deps) : void 0;
542
+ if (depsObs$) {
543
+ depsObs$.set(deps);
544
+ }
519
545
  const ref = React.useRef({ selector });
520
546
  ref.current = { selector, reaction };
521
547
  useMountOnce(
522
548
  () => state.observe(
523
549
  (e) => {
524
550
  const { selector: selector2 } = ref.current;
551
+ depsObs$ == null ? void 0 : depsObs$.get();
525
552
  return state.isFunction(selector2) ? selector2(e) : selector2;
526
553
  },
527
554
  (e) => {
package/react.mjs CHANGED
@@ -1,6 +1,7 @@
1
- import { isFunction, isEmpty, observable, isPromise, isObservable, syncState, computeSelector, isArray, isMap, isObservableValueReady, linked, observe, when, whenReady, internal, trackSelector, isPrimitive } from '@legendapp/state';
1
+ import { isFunction, isEmpty, observable, isPromise, isObservable, when, computeSelector, isArray, isMap, isObservableValueReady, linked, observe, whenReady, internal, trackSelector, isPrimitive } from '@legendapp/state';
2
2
  import React, { memo, forwardRef, createElement, useState, useContext, useMemo, useRef, useEffect, createContext, useCallback } from 'react';
3
3
  import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
4
+ import { enableReactive } from '@legendapp/state/react-reactive/enableReactive';
4
5
 
5
6
  // src/react/Computed.tsx
6
7
 
@@ -105,15 +106,6 @@ function useSelector(selector, options) {
105
106
  const { subscribe, getVersion, run } = selectorFn;
106
107
  value = run(selector);
107
108
  useSyncExternalStore(subscribe, getVersion, getVersion);
108
- if (options == null ? void 0 : options.suspense) {
109
- if (isPromise(value) || !value && isObservable(selector) && syncState(selector).isLoaded.get() === false) {
110
- if (React.use) {
111
- React.use(value);
112
- } else {
113
- throw value;
114
- }
115
- }
116
- }
117
109
  } catch (err) {
118
110
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && ((_a = err == null ? void 0 : err.message) == null ? void 0 : _a.includes("Rendered more"))) {
119
111
  console.warn(
@@ -122,6 +114,17 @@ function useSelector(selector, options) {
122
114
  }
123
115
  throw err;
124
116
  }
117
+ if (options == null ? void 0 : options.suspense) {
118
+ const isProm = isPromise(value);
119
+ if (isPromise(value) || !value && isObservable(selector)) {
120
+ const vProm = isProm ? value : when(selector);
121
+ if (React.use) {
122
+ React.use(vProm);
123
+ } else {
124
+ throw vProm;
125
+ }
126
+ }
127
+ }
125
128
  return value;
126
129
  }
127
130
 
@@ -328,8 +331,27 @@ function For({
328
331
  return out;
329
332
  }
330
333
  var Memo = memo(Computed, () => true);
334
+
335
+ // src/react/configureReactive.ts
331
336
  var ReactiveFns = /* @__PURE__ */ new Map();
332
337
  var ReactiveFnBinders = /* @__PURE__ */ new Map();
338
+ function configureReactive({
339
+ components,
340
+ binders
341
+ }) {
342
+ if (components) {
343
+ for (const key in components) {
344
+ ReactiveFns.set(key, components[key]);
345
+ }
346
+ }
347
+ if (binders) {
348
+ for (const key in binders) {
349
+ ReactiveFnBinders.set(key, binders[key]);
350
+ }
351
+ }
352
+ }
353
+
354
+ // src/react/Reactive.tsx
333
355
  var Reactive = new Proxy(
334
356
  {},
335
357
  {
@@ -349,26 +371,14 @@ var Reactive = new Proxy(
349
371
  }
350
372
  }
351
373
  );
352
- function configureReactive({
353
- components,
354
- binders
355
- }) {
356
- if (components) {
357
- for (const key in components) {
358
- ReactiveFns.set(key, components[key]);
359
- }
360
- }
361
- if (binders) {
362
- for (const key in binders) {
363
- ReactiveFnBinders.set(key, binders[key]);
364
- }
365
- }
374
+ if (process.env.NODE_ENV !== "test") {
375
+ enableReactive(configureReactive);
366
376
  }
367
- function Show({ if: if_, ifReady, else: else_, wrap, children }) {
377
+ function Show({ if: if_, ifReady, else: else_, $value, wrap, children }) {
368
378
  const value = useSelector(if_ != null ? if_ : ifReady);
369
379
  const show = ifReady !== void 0 ? isObservableValueReady(value) : value;
370
380
  const child = useSelector(
371
- show ? isFunction(children) ? () => children(value) : children : else_ != null ? else_ : null,
381
+ show ? isFunction(children) ? () => children($value ? $value.get() : value) : children : else_ != null ? else_ : null,
372
382
  { skipCheck: true }
373
383
  );
374
384
  return wrap ? createElement(wrap, void 0, child) : child;
@@ -389,7 +399,10 @@ function useObservable(initialValue, deps) {
389
399
  ref.current.value = initialValue;
390
400
  const depsObs$ = deps ? useObservable(deps) : void 0;
391
401
  if (!((_a = ref.current) == null ? void 0 : _a.obs$)) {
392
- const value = depsObs$ ? () => {
402
+ const value = depsObs$ ? isFunction(initialValue) && initialValue.length === 1 ? (p) => {
403
+ depsObs$.get();
404
+ return ref.current.value(p);
405
+ } : () => {
393
406
  depsObs$.get();
394
407
  return computeSelector(ref.current.value);
395
408
  } : initialValue;
@@ -484,8 +497,16 @@ function useObserve(selector, reactionOrOptions, options) {
484
497
  } else {
485
498
  options = reactionOrOptions;
486
499
  }
500
+ const deps = options == null ? void 0 : options.deps;
501
+ const depsObs$ = deps ? useObservable(deps) : void 0;
502
+ if (depsObs$) {
503
+ depsObs$.set(deps);
504
+ }
487
505
  const ref = useRef({});
488
- ref.current.selector = selector;
506
+ ref.current.selector = deps ? () => {
507
+ depsObs$ == null ? void 0 : depsObs$.get();
508
+ return computeSelector(selector);
509
+ } : selector;
489
510
  ref.current.reaction = reaction;
490
511
  if (!ref.current.dispose) {
491
512
  ref.current.dispose = observe(
@@ -510,12 +531,18 @@ function useObserveEffect(selector, reactionOrOptions, options) {
510
531
  } else {
511
532
  options = reactionOrOptions;
512
533
  }
534
+ const deps = options == null ? void 0 : options.deps;
535
+ const depsObs$ = deps ? useObservable(deps) : void 0;
536
+ if (depsObs$) {
537
+ depsObs$.set(deps);
538
+ }
513
539
  const ref = useRef({ selector });
514
540
  ref.current = { selector, reaction };
515
541
  useMountOnce(
516
542
  () => observe(
517
543
  (e) => {
518
544
  const { selector: selector2 } = ref.current;
545
+ depsObs$ == null ? void 0 : depsObs$.get();
519
546
  return isFunction(selector2) ? selector2(e) : selector2;
520
547
  },
521
548
  (e) => {
@@ -1,42 +1,43 @@
1
- import { SyncedGetParams, SyncedOptions, SyncedSetParams } from '@legendapp/state/sync';
1
+ import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
2
+ import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
2
3
 
3
4
  type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
4
5
  type CrudResult<T> = T;
5
- interface SyncedCrudPropsSingle<TRemote, TLocal> {
6
- get?: (params: SyncedGetParams) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
6
+ interface SyncedCrudPropsSingle<TRemote extends object, TLocal> {
7
+ get?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
7
8
  initial?: InitialValue<TLocal, 'value'>;
8
9
  as?: never | 'value';
9
10
  }
10
- interface SyncedCrudPropsMany<TRemote, TLocal, TAsOption extends CrudAsOption> {
11
- list?: (params: SyncedGetParams) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
11
+ interface SyncedCrudPropsMany<TRemote extends object, TLocal, TAsOption extends CrudAsOption> {
12
+ list?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
12
13
  as?: TAsOption;
13
14
  initial?: InitialValue<TLocal, TAsOption>;
14
15
  }
15
- interface SyncedCrudOnSavedParams<TRemote extends {
16
- id: string | number;
17
- }, TLocal> {
16
+ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
18
17
  saved: TLocal;
19
18
  input: TRemote;
20
19
  currentValue: TLocal;
21
20
  isCreate: boolean;
22
21
  props: SyncedCrudPropsBase<TRemote, TLocal>;
23
22
  }
24
- interface SyncedCrudPropsBase<TRemote extends {
25
- id: string | number;
26
- }, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial'> {
27
- create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined>;
28
- update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined>>;
29
- delete?(input: {
30
- id: TRemote['id'];
31
- }, params: SyncedSetParams<TRemote>): Promise<CrudResult<any>>;
23
+ interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
24
+ type: 'create' | 'update' | 'delete';
25
+ }
26
+ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet'> {
27
+ create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
28
+ update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
29
+ delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
32
30
  onSaved?(params: SyncedCrudOnSavedParams<TRemote, TLocal>): Partial<TLocal> | void;
33
- onSavedUpdate?: 'createdUpdatedAt';
31
+ fieldId?: string;
34
32
  fieldUpdatedAt?: string;
35
33
  fieldCreatedAt?: string;
36
34
  fieldDeleted?: string;
35
+ fieldDeletedList?: string;
37
36
  updatePartial?: boolean;
38
37
  changesSince?: 'all' | 'last-sync';
39
38
  generateId?: () => string | number;
39
+ subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
40
+ waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
40
41
  }
41
42
  type InitialValue<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<string | number, TLocal> : TAsOption extends 'object' ? Record<string | number, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
42
43
  type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
@@ -44,11 +45,7 @@ type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption ex
44
45
  } ? number : string, TLocal> : TAsOption extends 'object' ? Record<TLocal extends {
45
46
  id: number;
46
47
  } ? number : string, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
47
- declare function syncedCrud<TRemote extends {
48
- id: string | number;
49
- }, TLocal = TRemote>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
50
- declare function syncedCrud<TRemote extends {
51
- id: string | number;
52
- }, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsMany<TRemote, TLocal, TAsOption>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
48
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
49
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsMany<TRemote, TLocal, TAsOption>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
53
50
 
54
- export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, syncedCrud };
51
+ export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
@@ -1,42 +1,43 @@
1
- import { SyncedGetParams, SyncedOptions, SyncedSetParams } from '@legendapp/state/sync';
1
+ import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
2
+ import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
2
3
 
3
4
  type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
4
5
  type CrudResult<T> = T;
5
- interface SyncedCrudPropsSingle<TRemote, TLocal> {
6
- get?: (params: SyncedGetParams) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
6
+ interface SyncedCrudPropsSingle<TRemote extends object, TLocal> {
7
+ get?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
7
8
  initial?: InitialValue<TLocal, 'value'>;
8
9
  as?: never | 'value';
9
10
  }
10
- interface SyncedCrudPropsMany<TRemote, TLocal, TAsOption extends CrudAsOption> {
11
- list?: (params: SyncedGetParams) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
11
+ interface SyncedCrudPropsMany<TRemote extends object, TLocal, TAsOption extends CrudAsOption> {
12
+ list?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
12
13
  as?: TAsOption;
13
14
  initial?: InitialValue<TLocal, TAsOption>;
14
15
  }
15
- interface SyncedCrudOnSavedParams<TRemote extends {
16
- id: string | number;
17
- }, TLocal> {
16
+ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
18
17
  saved: TLocal;
19
18
  input: TRemote;
20
19
  currentValue: TLocal;
21
20
  isCreate: boolean;
22
21
  props: SyncedCrudPropsBase<TRemote, TLocal>;
23
22
  }
24
- interface SyncedCrudPropsBase<TRemote extends {
25
- id: string | number;
26
- }, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial'> {
27
- create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined>;
28
- update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined>>;
29
- delete?(input: {
30
- id: TRemote['id'];
31
- }, params: SyncedSetParams<TRemote>): Promise<CrudResult<any>>;
23
+ interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
24
+ type: 'create' | 'update' | 'delete';
25
+ }
26
+ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet'> {
27
+ create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
28
+ update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
29
+ delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
32
30
  onSaved?(params: SyncedCrudOnSavedParams<TRemote, TLocal>): Partial<TLocal> | void;
33
- onSavedUpdate?: 'createdUpdatedAt';
31
+ fieldId?: string;
34
32
  fieldUpdatedAt?: string;
35
33
  fieldCreatedAt?: string;
36
34
  fieldDeleted?: string;
35
+ fieldDeletedList?: string;
37
36
  updatePartial?: boolean;
38
37
  changesSince?: 'all' | 'last-sync';
39
38
  generateId?: () => string | number;
39
+ subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
40
+ waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
40
41
  }
41
42
  type InitialValue<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<string | number, TLocal> : TAsOption extends 'object' ? Record<string | number, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
42
43
  type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
@@ -44,11 +45,7 @@ type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption ex
44
45
  } ? number : string, TLocal> : TAsOption extends 'object' ? Record<TLocal extends {
45
46
  id: number;
46
47
  } ? number : string, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
47
- declare function syncedCrud<TRemote extends {
48
- id: string | number;
49
- }, TLocal = TRemote>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
50
- declare function syncedCrud<TRemote extends {
51
- id: string | number;
52
- }, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsMany<TRemote, TLocal, TAsOption>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
48
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsSingle<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
49
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsBase<TRemote, TLocal> & SyncedCrudPropsMany<TRemote, TLocal, TAsOption>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
53
50
 
54
- export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, syncedCrud };
51
+ export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };