@legendapp/state 3.0.0-beta.2 → 3.0.0-beta.21
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.
- package/.DS_Store +0 -0
- package/config/enableReactComponents.js +3 -1
- package/config/enableReactComponents.mjs +3 -1
- package/config/enableReactTracking.d.mts +2 -1
- package/config/enableReactTracking.d.ts +2 -1
- package/config/enableReactTracking.js +32 -13
- package/config/enableReactTracking.mjs +32 -13
- package/index.d.mts +13 -4
- package/index.d.ts +13 -4
- package/index.js +70 -22
- package/index.mjs +70 -22
- package/package.json +22 -1
- package/persist-plugins/async-storage.js +17 -9
- package/persist-plugins/async-storage.mjs +17 -9
- package/react-native.d.mts +4 -0
- package/react-native.d.ts +4 -0
- package/react-native.js +53 -0
- package/react-native.mjs +40 -0
- package/react-reactive/Components.d.mts +19 -0
- package/react-reactive/Components.d.ts +19 -0
- package/react-reactive/Components.js +53 -0
- package/react-reactive/Components.mjs +40 -0
- package/react-reactive/enableReactComponents.d.mts +3 -2
- package/react-reactive/enableReactComponents.d.ts +3 -2
- package/react-reactive/enableReactComponents.js +10 -3
- package/react-reactive/enableReactComponents.mjs +10 -3
- package/react-reactive/enableReactNativeComponents.d.mts +3 -20
- package/react-reactive/enableReactNativeComponents.d.ts +3 -20
- package/react-reactive/enableReactNativeComponents.js +8 -3
- package/react-reactive/enableReactNativeComponents.mjs +8 -3
- package/react-reactive/enableReactive.js +10 -3
- package/react-reactive/enableReactive.mjs +10 -3
- package/react-reactive/enableReactive.native.js +8 -3
- package/react-reactive/enableReactive.native.mjs +8 -3
- package/react-reactive/enableReactive.web.js +8 -3
- package/react-reactive/enableReactive.web.mjs +8 -3
- package/react-web.d.mts +6 -0
- package/react-web.d.ts +6 -0
- package/react-web.js +39 -0
- package/react-web.mjs +37 -0
- package/react.d.mts +38 -20
- package/react.d.ts +38 -20
- package/react.js +36 -23
- package/react.mjs +37 -25
- package/sync-plugins/crud.d.mts +24 -9
- package/sync-plugins/crud.d.ts +24 -9
- package/sync-plugins/crud.js +199 -108
- package/sync-plugins/crud.mjs +200 -109
- package/sync-plugins/firebase.d.mts +7 -3
- package/sync-plugins/firebase.d.ts +7 -3
- package/sync-plugins/firebase.js +4 -2
- package/sync-plugins/firebase.mjs +4 -2
- package/sync-plugins/keel.d.mts +9 -13
- package/sync-plugins/keel.d.ts +9 -13
- package/sync-plugins/keel.js +52 -41
- package/sync-plugins/keel.mjs +53 -37
- package/sync-plugins/supabase.d.mts +7 -3
- package/sync-plugins/supabase.d.ts +7 -3
- package/sync-plugins/supabase.js +87 -31
- package/sync-plugins/supabase.mjs +88 -32
- package/sync-plugins/tanstack-query.d.mts +5 -5
- package/sync-plugins/tanstack-query.d.ts +5 -5
- package/sync-plugins/tanstack-query.js +10 -1
- package/sync-plugins/tanstack-query.mjs +10 -1
- package/sync-plugins/tanstack-react-query.d.mts +4 -2
- package/sync-plugins/tanstack-react-query.d.ts +4 -2
- package/sync.d.mts +16 -8
- package/sync.d.ts +16 -8
- package/sync.js +267 -174
- package/sync.mjs +266 -174
- package/trace.js +5 -6
- package/trace.mjs +5 -6
- package/types/reactive-native.d.ts +19 -0
- package/types/reactive-web.d.ts +7 -0
package/react.js
CHANGED
|
@@ -45,6 +45,7 @@ function createSelectorFunctions(options, isPaused$) {
|
|
|
45
45
|
} = state.trackSelector(
|
|
46
46
|
_selector,
|
|
47
47
|
_update,
|
|
48
|
+
options,
|
|
48
49
|
void 0,
|
|
49
50
|
void 0,
|
|
50
51
|
/*createResubscribe*/
|
|
@@ -100,12 +101,24 @@ function createSelectorFunctions(options, isPaused$) {
|
|
|
100
101
|
}
|
|
101
102
|
};
|
|
102
103
|
}
|
|
104
|
+
function doSuspense(selector) {
|
|
105
|
+
const vProm = state.when(selector);
|
|
106
|
+
if (React__default.default.use) {
|
|
107
|
+
React__default.default.use(vProm);
|
|
108
|
+
} else {
|
|
109
|
+
throw vProm;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
103
112
|
function useSelector(selector, options) {
|
|
104
113
|
var _a;
|
|
114
|
+
let value;
|
|
105
115
|
if (reactGlobals.inObserver && state.isObservable(selector) && !(options == null ? void 0 : options.suspense)) {
|
|
106
|
-
|
|
116
|
+
value = state.computeSelector(selector, options);
|
|
117
|
+
if ((options == null ? void 0 : options.suspense) && value === void 0) {
|
|
118
|
+
doSuspense(selector);
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
107
121
|
}
|
|
108
|
-
let value;
|
|
109
122
|
try {
|
|
110
123
|
const isPaused$ = React.useContext(getPauseContext());
|
|
111
124
|
const selectorFn = React.useMemo(() => createSelectorFunctions(options, isPaused$), []);
|
|
@@ -120,16 +133,8 @@ function useSelector(selector, options) {
|
|
|
120
133
|
}
|
|
121
134
|
throw err;
|
|
122
135
|
}
|
|
123
|
-
if (options == null ? void 0 : options.suspense) {
|
|
124
|
-
|
|
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
|
-
}
|
|
136
|
+
if ((options == null ? void 0 : options.suspense) && value === void 0) {
|
|
137
|
+
doSuspense(selector);
|
|
133
138
|
}
|
|
134
139
|
return value;
|
|
135
140
|
}
|
|
@@ -140,7 +145,7 @@ function Computed({ children }) {
|
|
|
140
145
|
}
|
|
141
146
|
var hasSymbol = typeof Symbol === "function" && Symbol.for;
|
|
142
147
|
var didWarnProps = false;
|
|
143
|
-
function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
148
|
+
function createReactiveComponent(component, observe3, reactive2, keysReactive, bindKeys) {
|
|
144
149
|
const ReactForwardRefSymbol = hasSymbol ? Symbol.for("react.forward_ref") : (
|
|
145
150
|
// eslint-disable-next-line react/display-name, @typescript-eslint/no-unused-vars
|
|
146
151
|
typeof React.forwardRef === "function" && React.forwardRef((props) => null)["$$typeof"]
|
|
@@ -165,6 +170,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
165
170
|
throw new Error(`[legend-state] \`render\` property of ForwardRef was not a function`);
|
|
166
171
|
}
|
|
167
172
|
}
|
|
173
|
+
const keysReactiveSet = keysReactive ? new Set(keysReactive) : void 0;
|
|
168
174
|
const proxyHandler = {
|
|
169
175
|
apply(target, thisArg, argArray) {
|
|
170
176
|
if (reactive2) {
|
|
@@ -174,9 +180,10 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
174
180
|
for (let i = 0; i < keys.length; i++) {
|
|
175
181
|
const key = keys[i];
|
|
176
182
|
const p = props[key];
|
|
183
|
+
const isReactiveKey = keysReactiveSet && keysReactiveSet.has(key);
|
|
177
184
|
if (key === "children" && (state.isFunction(p) || state.isObservable(p))) {
|
|
178
185
|
props[key] = useSelector(p, { skipCheck: true });
|
|
179
|
-
} else if (key.startsWith("$") || key.endsWith("$")) {
|
|
186
|
+
} else if (isReactiveKey || key.startsWith("$") || key.endsWith("$")) {
|
|
180
187
|
if (process.env.NODE_ENV === "development" && !didWarnProps && key.endsWith("$")) {
|
|
181
188
|
didWarnProps = true;
|
|
182
189
|
console.warn(
|
|
@@ -186,7 +193,7 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
186
193
|
)}. See https://legendapp.com/open-source/state/migrating for more details.`
|
|
187
194
|
);
|
|
188
195
|
}
|
|
189
|
-
const k = key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
|
|
196
|
+
const k = isReactiveKey ? key : key.endsWith("$") ? key.slice(0, -1) : key.slice(1);
|
|
190
197
|
const bind = bindKeys == null ? void 0 : bindKeys[k];
|
|
191
198
|
const shouldBind = bind && state.isObservable(p);
|
|
192
199
|
propsOut[k] = shouldBind && (bind == null ? void 0 : bind.selector) ? bind.selector(propsOut, p) : useSelector(p);
|
|
@@ -204,7 +211,9 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
204
211
|
process.env.NODE_ENV === "development" ? handlerFn : React.useCallback(handlerFn, [props[bind.handler], bindKeys]);
|
|
205
212
|
}
|
|
206
213
|
}
|
|
207
|
-
|
|
214
|
+
if (!isReactiveKey) {
|
|
215
|
+
delete propsOut[key];
|
|
216
|
+
}
|
|
208
217
|
} else if (propsOut[key] === void 0) {
|
|
209
218
|
propsOut[key] = p;
|
|
210
219
|
}
|
|
@@ -241,11 +250,11 @@ function createReactiveComponent(component, observe3, reactive2, bindKeys) {
|
|
|
241
250
|
function observer(component) {
|
|
242
251
|
return createReactiveComponent(component, true);
|
|
243
252
|
}
|
|
244
|
-
function reactive(component, bindKeys) {
|
|
245
|
-
return createReactiveComponent(component, false, true, bindKeys);
|
|
253
|
+
function reactive(component, keys, bindKeys) {
|
|
254
|
+
return createReactiveComponent(component, false, true, keys, bindKeys);
|
|
246
255
|
}
|
|
247
|
-
function reactiveObserver(component, bindKeys) {
|
|
248
|
-
return createReactiveComponent(component, true, true, bindKeys);
|
|
256
|
+
function reactiveObserver(component, keys, bindKeys) {
|
|
257
|
+
return createReactiveComponent(component, true, true, keys, bindKeys);
|
|
249
258
|
}
|
|
250
259
|
function reactiveComponents(components) {
|
|
251
260
|
return new Proxy(
|
|
@@ -336,7 +345,10 @@ function For({
|
|
|
336
345
|
}
|
|
337
346
|
return out;
|
|
338
347
|
}
|
|
339
|
-
var Memo = React.memo(
|
|
348
|
+
var Memo = React.memo(
|
|
349
|
+
Computed,
|
|
350
|
+
(prev, next) => next.scoped ? prev.children === next.children : true
|
|
351
|
+
);
|
|
340
352
|
|
|
341
353
|
// src/react/configureReactive.ts
|
|
342
354
|
var ReactiveFns = /* @__PURE__ */ new Map();
|
|
@@ -371,7 +383,7 @@ var Reactive = new Proxy(
|
|
|
371
383
|
}
|
|
372
384
|
return React.createElement(Component, propsOut);
|
|
373
385
|
});
|
|
374
|
-
target[p] = reactive(render, ReactiveFnBinders.get(p));
|
|
386
|
+
target[p] = reactive(render, [], ReactiveFnBinders.get(p));
|
|
375
387
|
}
|
|
376
388
|
return target[p];
|
|
377
389
|
}
|
|
@@ -516,7 +528,7 @@ function useObserve(selector, reactionOrOptions, options) {
|
|
|
516
528
|
ref.current.reaction = reaction;
|
|
517
529
|
if (!ref.current.dispose) {
|
|
518
530
|
ref.current.dispose = state.observe(
|
|
519
|
-
(e) => state.computeSelector(ref.current.selector, e),
|
|
531
|
+
(e) => state.computeSelector(ref.current.selector, void 0, e),
|
|
520
532
|
(e) => {
|
|
521
533
|
var _a, _b;
|
|
522
534
|
return (_b = (_a = ref.current).reaction) == null ? void 0 : _b.call(_a, e);
|
|
@@ -578,6 +590,7 @@ exports.observer = observer;
|
|
|
578
590
|
exports.reactive = reactive;
|
|
579
591
|
exports.reactiveComponents = reactiveComponents;
|
|
580
592
|
exports.reactiveObserver = reactiveObserver;
|
|
593
|
+
exports.use$ = useSelector;
|
|
581
594
|
exports.useComputed = useComputed;
|
|
582
595
|
exports.useEffectOnce = useEffectOnce;
|
|
583
596
|
exports.useIsMounted = useIsMounted;
|
package/react.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isFunction, isEmpty, observable,
|
|
1
|
+
import { isFunction, isEmpty, observable, computeSelector, isArray, isMap, isObservableValueReady, linked, isPromise, 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
|
-
|
|
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
|
-
|
|
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
179
|
props[key] = useSelector(p, { skipCheck: true });
|
|
173
|
-
} else if (key.startsWith("$") || key.endsWith("$")) {
|
|
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
|
-
|
|
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(
|
|
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
|
}
|
|
@@ -510,7 +522,7 @@ function useObserve(selector, reactionOrOptions, options) {
|
|
|
510
522
|
ref.current.reaction = reaction;
|
|
511
523
|
if (!ref.current.dispose) {
|
|
512
524
|
ref.current.dispose = observe(
|
|
513
|
-
(e) => computeSelector(ref.current.selector, e),
|
|
525
|
+
(e) => computeSelector(ref.current.selector, void 0, e),
|
|
514
526
|
(e) => {
|
|
515
527
|
var _a, _b;
|
|
516
528
|
return (_b = (_a = ref.current).reaction) == null ? void 0 : _b.call(_a, e);
|
|
@@ -560,4 +572,4 @@ function useWhenReady(predicate, effect) {
|
|
|
560
572
|
return useMemo(() => whenReady(predicate, effect), []);
|
|
561
573
|
}
|
|
562
574
|
|
|
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 };
|
|
575
|
+
export { Computed, For, Memo, Reactive, Show, Switch, configureReactive, hasSymbol, observer, reactive, reactiveComponents, reactiveObserver, useSelector as use$, useComputed, useEffectOnce, useIsMounted, useMount, useMountOnce, useObservable, useObservableReducer, useObserve, useObserveEffect, usePauseProvider, useSelector, useUnmount, useUnmountOnce, useWhen, useWhenReady };
|
package/sync-plugins/crud.d.mts
CHANGED
|
@@ -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
|
|
8
|
-
|
|
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
|
|
19
|
+
list: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
|
|
20
|
+
get?: never | undefined;
|
|
13
21
|
as?: TAsOption;
|
|
14
|
-
initial?: InitialValue<TLocal
|
|
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
|
|
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:
|
|
49
|
-
declare function syncedCrud<TRemote extends object, TLocal = TRemote
|
|
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 };
|
package/sync-plugins/crud.d.ts
CHANGED
|
@@ -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
|
|
8
|
-
|
|
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
|
|
19
|
+
list: (params: SyncedGetParams<TRemote>) => Promise<CrudResult<TRemote[] | null>> | CrudResult<TRemote[] | null>;
|
|
20
|
+
get?: never | undefined;
|
|
13
21
|
as?: TAsOption;
|
|
14
|
-
initial?: InitialValue<TLocal
|
|
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
|
|
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:
|
|
49
|
-
declare function syncedCrud<TRemote extends object, TLocal = TRemote
|
|
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 };
|