@legendapp/state 3.0.0-beta.4 → 3.0.0-beta.40

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 (79) hide show
  1. package/.DS_Store +0 -0
  2. package/README.md +2 -2
  3. package/config/enableReactComponents.js +3 -1
  4. package/config/enableReactComponents.mjs +3 -1
  5. package/config/enableReactTracking.d.mts +2 -1
  6. package/config/enableReactTracking.d.ts +2 -1
  7. package/config/enableReactTracking.js +32 -13
  8. package/config/enableReactTracking.mjs +32 -13
  9. package/index.d.mts +46 -8
  10. package/index.d.ts +46 -8
  11. package/index.js +267 -75
  12. package/index.mjs +267 -75
  13. package/package.json +35 -1
  14. package/persist-plugins/async-storage.js +17 -9
  15. package/persist-plugins/async-storage.mjs +17 -9
  16. package/persist-plugins/expo-sqlite.d.mts +19 -0
  17. package/persist-plugins/expo-sqlite.d.ts +19 -0
  18. package/persist-plugins/expo-sqlite.js +72 -0
  19. package/persist-plugins/expo-sqlite.mjs +69 -0
  20. package/persist-plugins/indexeddb.js +13 -3
  21. package/persist-plugins/indexeddb.mjs +13 -3
  22. package/react-native.d.mts +4 -0
  23. package/react-native.d.ts +4 -0
  24. package/react-native.js +53 -0
  25. package/react-native.mjs +40 -0
  26. package/react-reactive/Components.d.mts +19 -0
  27. package/react-reactive/Components.d.ts +19 -0
  28. package/react-reactive/Components.js +53 -0
  29. package/react-reactive/Components.mjs +40 -0
  30. package/react-reactive/enableReactComponents.d.mts +3 -2
  31. package/react-reactive/enableReactComponents.d.ts +3 -2
  32. package/react-reactive/enableReactComponents.js +10 -3
  33. package/react-reactive/enableReactComponents.mjs +10 -3
  34. package/react-reactive/enableReactNativeComponents.d.mts +3 -20
  35. package/react-reactive/enableReactNativeComponents.d.ts +3 -20
  36. package/react-reactive/enableReactNativeComponents.js +8 -3
  37. package/react-reactive/enableReactNativeComponents.mjs +8 -3
  38. package/react-reactive/enableReactive.js +10 -3
  39. package/react-reactive/enableReactive.mjs +10 -3
  40. package/react-reactive/enableReactive.native.js +8 -3
  41. package/react-reactive/enableReactive.native.mjs +8 -3
  42. package/react-reactive/enableReactive.web.js +8 -3
  43. package/react-reactive/enableReactive.web.mjs +8 -3
  44. package/react-web.d.mts +7 -0
  45. package/react-web.d.ts +7 -0
  46. package/react-web.js +39 -0
  47. package/react-web.mjs +37 -0
  48. package/react.d.mts +59 -26
  49. package/react.d.ts +59 -26
  50. package/react.js +136 -87
  51. package/react.mjs +135 -89
  52. package/sync-plugins/crud.d.mts +24 -9
  53. package/sync-plugins/crud.d.ts +24 -9
  54. package/sync-plugins/crud.js +267 -123
  55. package/sync-plugins/crud.mjs +268 -124
  56. package/sync-plugins/firebase.d.mts +7 -3
  57. package/sync-plugins/firebase.d.ts +7 -3
  58. package/sync-plugins/firebase.js +214 -64
  59. package/sync-plugins/firebase.mjs +215 -65
  60. package/sync-plugins/keel.d.mts +12 -13
  61. package/sync-plugins/keel.d.ts +12 -13
  62. package/sync-plugins/keel.js +60 -52
  63. package/sync-plugins/keel.mjs +61 -48
  64. package/sync-plugins/supabase.d.mts +10 -5
  65. package/sync-plugins/supabase.d.ts +10 -5
  66. package/sync-plugins/supabase.js +90 -33
  67. package/sync-plugins/supabase.mjs +91 -34
  68. package/sync-plugins/tanstack-query.d.mts +3 -3
  69. package/sync-plugins/tanstack-query.d.ts +3 -3
  70. package/sync-plugins/tanstack-query.js +1 -1
  71. package/sync-plugins/tanstack-query.mjs +1 -1
  72. package/sync.d.mts +17 -8
  73. package/sync.d.ts +17 -8
  74. package/sync.js +448 -307
  75. package/sync.mjs +446 -307
  76. package/trace.js +5 -6
  77. package/trace.mjs +5 -6
  78. package/types/reactive-native.d.ts +19 -0
  79. package/types/reactive-web.d.ts +7 -0
package/react.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isFunction, isEmpty, observable, isPromise, isObservable, when, computeSelector, isArray, isMap, isObservableValueReady, linked, observe, whenReady, internal, trackSelector, isPrimitive } from '@legendapp/state';
1
+ import { isFunction, isEmpty, observable, computeSelector, isArray, isMap, isObservableValueReady, isPromise, getNode as getNode$1, linked, observe, when, whenReady, internal, trackSelector, isPrimitive, isObservable } 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
4
  import { enableReactive } from '@legendapp/state/react-reactive/enableReactive';
@@ -39,6 +39,7 @@ function createSelectorFunctions(options, isPaused$) {
39
39
  } = trackSelector(
40
40
  _selector,
41
41
  _update,
42
+ options,
42
43
  void 0,
43
44
  void 0,
44
45
  /*createResubscribe*/
@@ -94,12 +95,24 @@ function createSelectorFunctions(options, isPaused$) {
94
95
  }
95
96
  };
96
97
  }
98
+ function doSuspense(selector) {
99
+ const vProm = when(selector);
100
+ if (React.use) {
101
+ React.use(vProm);
102
+ } else {
103
+ throw vProm;
104
+ }
105
+ }
97
106
  function useSelector(selector, options) {
98
107
  var _a;
108
+ let value;
99
109
  if (reactGlobals.inObserver && isObservable(selector) && !(options == null ? void 0 : options.suspense)) {
100
- return computeSelector(selector);
110
+ value = computeSelector(selector, options);
111
+ if ((options == null ? void 0 : options.suspense) && value === void 0) {
112
+ doSuspense(selector);
113
+ }
114
+ return value;
101
115
  }
102
- let value;
103
116
  try {
104
117
  const isPaused$ = useContext(getPauseContext());
105
118
  const selectorFn = useMemo(() => createSelectorFunctions(options, isPaused$), []);
@@ -114,16 +127,8 @@ function useSelector(selector, options) {
114
127
  }
115
128
  throw err;
116
129
  }
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
- }
130
+ if ((options == null ? void 0 : options.suspense) && value === void 0) {
131
+ doSuspense(selector);
127
132
  }
128
133
  return value;
129
134
  }
@@ -134,7 +139,7 @@ function Computed({ children }) {
134
139
  }
135
140
  var hasSymbol = typeof Symbol === "function" && Symbol.for;
136
141
  var didWarnProps = false;
137
- function createReactiveComponent(component, observe3, reactive2, bindKeys) {
142
+ function createReactiveComponent(component, observe3, reactive2, keysReactive, bindKeys) {
138
143
  const ReactForwardRefSymbol = hasSymbol ? Symbol.for("react.forward_ref") : (
139
144
  // eslint-disable-next-line react/display-name, @typescript-eslint/no-unused-vars
140
145
  typeof forwardRef === "function" && forwardRef((props) => null)["$$typeof"]
@@ -159,6 +164,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
159
164
  throw new Error(`[legend-state] \`render\` property of ForwardRef was not a function`);
160
165
  }
161
166
  }
167
+ const keysReactiveSet = keysReactive ? new Set(keysReactive) : void 0;
162
168
  const proxyHandler = {
163
169
  apply(target, thisArg, argArray) {
164
170
  if (reactive2) {
@@ -168,9 +174,10 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
168
174
  for (let i = 0; i < keys.length; i++) {
169
175
  const key = keys[i];
170
176
  const p = props[key];
177
+ const isReactiveKey = keysReactiveSet && keysReactiveSet.has(key);
171
178
  if (key === "children" && (isFunction(p) || isObservable(p))) {
172
- props[key] = useSelector(p, { skipCheck: true });
173
- } else if (key.startsWith("$") || key.endsWith("$")) {
179
+ propsOut[key] = useSelector(p, { skipCheck: true });
180
+ } else if (isReactiveKey || key.startsWith("$") || key.endsWith("$")) {
174
181
  if (process.env.NODE_ENV === "development" && !didWarnProps && key.endsWith("$")) {
175
182
  didWarnProps = true;
176
183
  console.warn(
@@ -180,7 +187,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
180
187
  )}. See https://legendapp.com/open-source/state/migrating for more details.`
181
188
  );
182
189
  }
183
- const k = key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
190
+ const k = isReactiveKey ? key : key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
184
191
  const bind = bindKeys == null ? void 0 : bindKeys[k];
185
192
  const shouldBind = bind && isObservable(p);
186
193
  propsOut[k] = shouldBind && (bind == null ? void 0 : bind.selector) ? bind.selector(propsOut, p) : useSelector(p);
@@ -198,7 +205,9 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
198
205
  process.env.NODE_ENV === "development" ? handlerFn : useCallback(handlerFn, [props[bind.handler], bindKeys]);
199
206
  }
200
207
  }
201
- delete propsOut[key];
208
+ if (!isReactiveKey) {
209
+ delete propsOut[key];
210
+ }
202
211
  } else if (propsOut[key] === void 0) {
203
212
  propsOut[key] = p;
204
213
  }
@@ -235,11 +244,11 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
235
244
  function observer(component) {
236
245
  return createReactiveComponent(component, true);
237
246
  }
238
- function reactive(component, bindKeys) {
239
- return createReactiveComponent(component, false, true, bindKeys);
247
+ function reactive(component, keys, bindKeys) {
248
+ return createReactiveComponent(component, false, true, keys, bindKeys);
240
249
  }
241
- function reactiveObserver(component, bindKeys) {
242
- return createReactiveComponent(component, true, true, bindKeys);
250
+ function reactiveObserver(component, keys, bindKeys) {
251
+ return createReactiveComponent(component, true, true, keys, bindKeys);
243
252
  }
244
253
  function reactiveComponents(components) {
245
254
  return new Proxy(
@@ -330,7 +339,10 @@ function For({
330
339
  }
331
340
  return out;
332
341
  }
333
- var Memo = memo(Computed, () => true);
342
+ var Memo = memo(
343
+ Computed,
344
+ (prev, next) => next.scoped ? prev.children === next.children : true
345
+ );
334
346
 
335
347
  // src/react/configureReactive.ts
336
348
  var ReactiveFns = /* @__PURE__ */ new Map();
@@ -365,7 +377,7 @@ var Reactive = new Proxy(
365
377
  }
366
378
  return createElement(Component, propsOut);
367
379
  });
368
- target[p] = reactive(render, ReactiveFnBinders.get(p));
380
+ target[p] = reactive(render, [], ReactiveFnBinders.get(p));
369
381
  }
370
382
  return target[p];
371
383
  }
@@ -393,38 +405,6 @@ function Switch({
393
405
  const child = children[useSelector(value)];
394
406
  return (_b = child ? child() : (_a = children["default"]) == null ? void 0 : _a.call(children)) != null ? _b : null;
395
407
  }
396
- function useObservable(initialValue, deps) {
397
- var _a;
398
- const ref = useRef({});
399
- ref.current.value = initialValue;
400
- const depsObs$ = deps ? useObservable(deps) : void 0;
401
- if (!((_a = ref.current) == null ? void 0 : _a.obs$)) {
402
- const value = depsObs$ ? isFunction(initialValue) && initialValue.length === 1 ? (p) => {
403
- depsObs$.get();
404
- return ref.current.value(p);
405
- } : () => {
406
- depsObs$.get();
407
- return computeSelector(ref.current.value);
408
- } : initialValue;
409
- ref.current.obs$ = observable(value);
410
- }
411
- if (depsObs$) {
412
- depsObs$.set(deps);
413
- }
414
- return ref.current.obs$;
415
- }
416
-
417
- // src/react/useComputed.ts
418
- function useComputed(get, set, deps) {
419
- if (!deps && isArray(set)) {
420
- deps = set;
421
- set = void 0;
422
- }
423
- return useObservable(
424
- set ? linked({ get, set: ({ value }) => set(value) }) : get,
425
- deps
426
- );
427
- }
428
408
  var useEffectOnce = (effect, deps) => {
429
409
  if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") {
430
410
  const refDispose = useRef({ num: 0 });
@@ -453,6 +433,8 @@ var useEffectOnce = (effect, deps) => {
453
433
  useEffect(effect, deps);
454
434
  }
455
435
  };
436
+
437
+ // src/react/useMount.ts
456
438
  function useMount(fn) {
457
439
  return useEffectOnce(() => {
458
440
  const ret = fn();
@@ -463,6 +445,54 @@ function useMount(fn) {
463
445
  }
464
446
  var useMountOnce = useMount;
465
447
 
448
+ // src/react/useUnmount.ts
449
+ function useUnmount(fn) {
450
+ return useMount(() => fn);
451
+ }
452
+ var useUnmountOnce = useUnmount;
453
+
454
+ // src/react/useObservable.ts
455
+ var { deactivateNode } = internal;
456
+ function useObservable(initialValue, deps) {
457
+ var _a;
458
+ const ref = useRef({});
459
+ ref.current.value = initialValue;
460
+ const depsObs$ = deps ? useObservable(deps) : void 0;
461
+ if (!((_a = ref.current) == null ? void 0 : _a.obs$)) {
462
+ const value = depsObs$ ? isFunction(initialValue) && initialValue.length === 1 ? (p) => {
463
+ depsObs$.get();
464
+ return ref.current.value(p);
465
+ } : () => {
466
+ depsObs$.get();
467
+ return computeSelector(ref.current.value);
468
+ } : initialValue;
469
+ ref.current.obs$ = observable(value);
470
+ }
471
+ if (depsObs$) {
472
+ depsObs$.set(deps);
473
+ }
474
+ useUnmount(() => {
475
+ const obs = ref.current.obs$;
476
+ if (obs) {
477
+ const node = getNode$1(obs);
478
+ deactivateNode(node);
479
+ }
480
+ });
481
+ return ref.current.obs$;
482
+ }
483
+
484
+ // src/react/useComputed.ts
485
+ function useComputed(get, set, deps) {
486
+ if (!deps && isArray(set)) {
487
+ deps = set;
488
+ set = void 0;
489
+ }
490
+ return useObservable(
491
+ set ? linked({ get, set: ({ value }) => set(value) }) : get,
492
+ deps
493
+ );
494
+ }
495
+
466
496
  // src/react/useIsMounted.ts
467
497
  function useIsMounted() {
468
498
  const obs = useObservable(false);
@@ -474,43 +504,50 @@ function useIsMounted() {
474
504
  return obs;
475
505
  }
476
506
  function useObservableReducer(reducer, initializerArg, initializer) {
477
- const obs = useObservable(
478
- () => initializerArg !== void 0 && isFunction(initializerArg) ? initializer(initializerArg) : initializerArg
479
- );
507
+ const obs = useObservable(() => {
508
+ if (initializer) {
509
+ return initializer(initializerArg);
510
+ }
511
+ if (isFunction(initializerArg)) {
512
+ return initializerArg();
513
+ }
514
+ return initializerArg;
515
+ });
480
516
  const dispatch = (action) => {
481
517
  obs.set(reducer(obs.get(), action));
482
518
  };
483
519
  return [obs, dispatch];
484
520
  }
485
-
486
- // src/react/useUnmount.ts
487
- function useUnmount(fn) {
488
- return useMount(() => fn);
489
- }
490
- var useUnmountOnce = useUnmount;
491
-
492
- // src/react/useObserve.ts
493
- function useObserve(selector, reactionOrOptions, options) {
521
+ function useObserve(selector, reactionOrOptionsOrDeps, options, deps) {
494
522
  let reaction;
495
- if (isFunction(reactionOrOptions)) {
496
- reaction = reactionOrOptions;
523
+ if (isFunction(reactionOrOptionsOrDeps)) {
524
+ reaction = reactionOrOptionsOrDeps;
525
+ } else if (Array.isArray(reactionOrOptionsOrDeps)) {
526
+ deps = reactionOrOptionsOrDeps;
527
+ options = void 0;
497
528
  } else {
498
- options = reactionOrOptions;
529
+ options = reactionOrOptionsOrDeps;
499
530
  }
500
- const deps = options == null ? void 0 : options.deps;
531
+ if (reaction && Array.isArray(options)) {
532
+ deps = options;
533
+ options = void 0;
534
+ }
535
+ deps = deps || (options == null ? void 0 : options.deps);
501
536
  const depsObs$ = deps ? useObservable(deps) : void 0;
537
+ const ref = useRef({});
538
+ ref.current.selector = selector;
539
+ ref.current.reaction = reaction;
502
540
  if (depsObs$) {
503
541
  depsObs$.set(deps);
504
542
  }
505
- const ref = useRef({});
506
- ref.current.selector = deps ? () => {
507
- depsObs$ == null ? void 0 : depsObs$.get();
508
- return computeSelector(selector);
509
- } : selector;
510
- ref.current.reaction = reaction;
511
543
  if (!ref.current.dispose) {
512
544
  ref.current.dispose = observe(
513
- (e) => computeSelector(ref.current.selector, e),
545
+ (e) => {
546
+ var _a;
547
+ depsObs$ == null ? void 0 : depsObs$.get();
548
+ const selector2 = (_a = ref.current) == null ? void 0 : _a.selector;
549
+ return computeSelector(selector2, void 0, e);
550
+ },
514
551
  (e) => {
515
552
  var _a, _b;
516
553
  return (_b = (_a = ref.current).reaction) == null ? void 0 : _b.call(_a, e);
@@ -524,26 +561,35 @@ function useObserve(selector, reactionOrOptions, options) {
524
561
  });
525
562
  return ref.current.dispose;
526
563
  }
527
- function useObserveEffect(selector, reactionOrOptions, options) {
564
+ function useObserveEffect(selector, reactionOrOptionsOrDeps, options, deps) {
528
565
  let reaction;
529
- if (isFunction(reactionOrOptions)) {
530
- reaction = reactionOrOptions;
566
+ if (isFunction(reactionOrOptionsOrDeps)) {
567
+ reaction = reactionOrOptionsOrDeps;
568
+ } else if (Array.isArray(reactionOrOptionsOrDeps)) {
569
+ deps = reactionOrOptionsOrDeps;
570
+ options = void 0;
531
571
  } else {
532
- options = reactionOrOptions;
572
+ options = reactionOrOptionsOrDeps;
533
573
  }
534
- const deps = options == null ? void 0 : options.deps;
574
+ if (reaction && Array.isArray(options)) {
575
+ deps = options;
576
+ options = void 0;
577
+ }
578
+ deps = deps || (options == null ? void 0 : options.deps);
579
+ const ref = useRef({});
535
580
  const depsObs$ = deps ? useObservable(deps) : void 0;
581
+ ref.current.selector = selector;
582
+ ref.current.reaction = reaction;
536
583
  if (depsObs$) {
537
584
  depsObs$.set(deps);
538
585
  }
539
- const ref = useRef({ selector });
540
- ref.current = { selector, reaction };
541
586
  useMountOnce(
542
587
  () => observe(
543
588
  (e) => {
544
- const { selector: selector2 } = ref.current;
589
+ var _a;
545
590
  depsObs$ == null ? void 0 : depsObs$.get();
546
- return isFunction(selector2) ? selector2(e) : selector2;
591
+ const selector2 = (_a = ref.current) == null ? void 0 : _a.selector;
592
+ return computeSelector(selector2, void 0, e);
547
593
  },
548
594
  (e) => {
549
595
  var _a, _b;
@@ -560,4 +606,4 @@ function useWhenReady(predicate, effect) {
560
606
  return useMemo(() => whenReady(predicate, effect), []);
561
607
  }
562
608
 
563
- export { Computed, For, Memo, Reactive, Show, Switch, configureReactive, hasSymbol, observer, reactive, reactiveComponents, reactiveObserver, useComputed, useEffectOnce, useIsMounted, useMount, useMountOnce, useObservable, useObservableReducer, useObserve, useObserveEffect, usePauseProvider, useSelector, useUnmount, useUnmountOnce, useWhen, useWhenReady };
609
+ export { Computed, For, Memo, Reactive, Show, Switch, configureReactive, hasSymbol, observer, reactive, reactiveComponents, reactiveObserver, useSelector as use$, useComputed, useEffectOnce, useIsMounted, useObservable as useLocalObservable, useMount, useMountOnce, useObservable, useObservableReducer, useObserve, useObserveEffect, usePauseProvider, useSelector, useUnmount, useUnmountOnce, useSelector as useValue, useWhen, useWhenReady };
@@ -1,17 +1,25 @@
1
1
  import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
2
- import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
2
+ import { SyncedGetParams, SyncedErrorParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
3
3
 
4
4
  type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
5
5
  type CrudResult<T> = T;
6
+ interface SyncedCrudPropsNoRead<TLocal, TAsOption extends CrudAsOption> {
7
+ get?: never | undefined;
8
+ list?: never | undefined;
9
+ initial?: InitialValue<TLocal, TAsOption>;
10
+ as?: TAsOption;
11
+ }
6
12
  interface SyncedCrudPropsSingle<TRemote extends object, TLocal> {
7
- get?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
8
- initial?: InitialValue<TLocal, 'value'>;
13
+ get: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
14
+ list?: never | undefined;
15
+ initial?: InitialValue<Partial<NoInfer<TLocal>>, 'value'>;
9
16
  as?: never | 'value';
10
17
  }
11
18
  interface SyncedCrudPropsMany<TRemote extends object, TLocal, TAsOption extends CrudAsOption> {
12
- list?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
19
+ list: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
20
+ get?: never | undefined;
13
21
  as?: TAsOption;
14
- initial?: InitialValue<TLocal, TAsOption>;
22
+ initial?: InitialValue<Partial<NoInfer<TLocal>>, TAsOption>;
15
23
  }
16
24
  interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
17
25
  saved: TLocal;
@@ -23,7 +31,11 @@ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
23
31
  interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
24
32
  type: 'create' | 'update' | 'delete';
25
33
  }
26
- interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet'> {
34
+ interface CrudErrorParams extends Omit<SyncedErrorParams, 'source'> {
35
+ source: 'list' | 'get' | 'create' | 'update' | 'delete' | 'unknown';
36
+ }
37
+ type CrudOnErrorFn = (error: Error, params: CrudErrorParams) => void;
38
+ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet' | 'onError'> {
27
39
  create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
28
40
  update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
29
41
  delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
@@ -38,6 +50,7 @@ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends
38
50
  generateId?: () => string | number;
39
51
  subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
40
52
  waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
53
+ onError?: (error: Error, params: CrudErrorParams) => void;
41
54
  }
42
55
  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[];
43
56
  type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
@@ -45,7 +58,9 @@ type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption ex
45
58
  } ? number : string, TLocal> : TAsOption extends 'object' ? Record<TLocal extends {
46
59
  id: number;
47
60
  } ? number : string, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
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'>>;
61
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsNoRead<TRemote, TAsOption> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, TAsOption>;
62
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: SyncedCrudPropsSingle<TRemote, TLocal> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
63
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsMany<TRemote, TLocal, TAsOption> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
64
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: (SyncedCrudPropsSingle<TRemote, TLocal> | SyncedCrudPropsMany<TRemote, TLocal, TAsOption>) & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, TAsOption>;
50
65
 
51
- export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
66
+ export { type CrudAsOption, type CrudErrorParams, type CrudOnErrorFn, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsNoRead, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
@@ -1,17 +1,25 @@
1
1
  import { WaitForSetFnParams, ObservableParam, ObservableEvent } from '@legendapp/state';
2
- import { SyncedGetParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
2
+ import { SyncedGetParams, SyncedErrorParams, SyncedOptions, SyncedSetParams, SyncedSubscribeParams } from '@legendapp/state/sync';
3
3
 
4
4
  type CrudAsOption = 'Map' | 'object' | 'value' | 'array';
5
5
  type CrudResult<T> = T;
6
+ interface SyncedCrudPropsNoRead<TLocal, TAsOption extends CrudAsOption> {
7
+ get?: never | undefined;
8
+ list?: never | undefined;
9
+ initial?: InitialValue<TLocal, TAsOption>;
10
+ as?: TAsOption;
11
+ }
6
12
  interface SyncedCrudPropsSingle<TRemote extends object, TLocal> {
7
- get?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
8
- initial?: InitialValue<TLocal, 'value'>;
13
+ get: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote | null>> | CrudResult<TRemote | null>;
14
+ list?: never | undefined;
15
+ initial?: InitialValue<Partial<NoInfer<TLocal>>, 'value'>;
9
16
  as?: never | 'value';
10
17
  }
11
18
  interface SyncedCrudPropsMany<TRemote extends object, TLocal, TAsOption extends CrudAsOption> {
12
- list?: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
19
+ list: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
20
+ get?: never | undefined;
13
21
  as?: TAsOption;
14
- initial?: InitialValue<TLocal, TAsOption>;
22
+ initial?: InitialValue<Partial<NoInfer<TLocal>>, TAsOption>;
15
23
  }
16
24
  interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
17
25
  saved: TLocal;
@@ -23,7 +31,11 @@ interface SyncedCrudOnSavedParams<TRemote extends object, TLocal> {
23
31
  interface WaitForSetCrudFnParams<T> extends WaitForSetFnParams<T> {
24
32
  type: 'create' | 'update' | 'delete';
25
33
  }
26
- interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet'> {
34
+ interface CrudErrorParams extends Omit<SyncedErrorParams, 'source'> {
35
+ source: 'list' | 'get' | 'create' | 'update' | 'delete' | 'unknown';
36
+ }
37
+ type CrudOnErrorFn = (error: Error, params: CrudErrorParams) => void;
38
+ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends Omit<SyncedOptions<TRemote, TLocal>, 'get' | 'set' | 'initial' | 'subscribe' | 'waitForSet' | 'onError'> {
27
39
  create?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<CrudResult<TRemote> | null | undefined | void>;
28
40
  update?(input: Partial<TRemote>, params: SyncedSetParams<TRemote>): Promise<CrudResult<Partial<TRemote> | null | undefined | void>>;
29
41
  delete?(input: TRemote, params: SyncedSetParams<TRemote>): Promise<any>;
@@ -38,6 +50,7 @@ interface SyncedCrudPropsBase<TRemote extends object, TLocal = TRemote> extends
38
50
  generateId?: () => string | number;
39
51
  subscribe?: (params: SyncedSubscribeParams<TRemote[]>) => (() => void) | void;
40
52
  waitForSet?: ((params: WaitForSetCrudFnParams<TLocal>) => any) | Promise<any> | ObservableParam<any> | ObservableEvent;
53
+ onError?: (error: Error, params: CrudErrorParams) => void;
41
54
  }
42
55
  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[];
43
56
  type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption extends 'Map' ? Map<TLocal extends {
@@ -45,7 +58,9 @@ type SyncedCrudReturnType<TLocal, TAsOption extends CrudAsOption> = TAsOption ex
45
58
  } ? number : string, TLocal> : TAsOption extends 'object' ? Record<TLocal extends {
46
59
  id: number;
47
60
  } ? number : string, TLocal> : TAsOption extends 'value' ? TLocal : TLocal[];
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'>>;
61
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsNoRead<TRemote, TAsOption> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, TAsOption>;
62
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote>(props: SyncedCrudPropsSingle<TRemote, TLocal> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, 'value'>;
63
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: SyncedCrudPropsMany<TRemote, TLocal, TAsOption> & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, Exclude<TAsOption, 'value'>>;
64
+ declare function syncedCrud<TRemote extends object, TLocal = TRemote, TAsOption extends CrudAsOption = 'object'>(props: (SyncedCrudPropsSingle<TRemote, TLocal> | SyncedCrudPropsMany<TRemote, TLocal, TAsOption>) & SyncedCrudPropsBase<TRemote, TLocal>): SyncedCrudReturnType<TLocal, TAsOption>;
50
65
 
51
- export { type CrudAsOption, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };
66
+ export { type CrudAsOption, type CrudErrorParams, type CrudOnErrorFn, type CrudResult, type SyncedCrudOnSavedParams, type SyncedCrudPropsBase, type SyncedCrudPropsMany, type SyncedCrudPropsNoRead, type SyncedCrudPropsSingle, type SyncedCrudReturnType, type WaitForSetCrudFnParams, syncedCrud };