@legendapp/state 3.0.0-alpha.6 → 3.0.0-alpha.8
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/CHANGELOG.md +831 -1
- package/LICENSE +21 -1
- package/README.md +141 -1
- package/babel.js +0 -2
- package/babel.mjs +0 -2
- package/index.js +22 -20
- package/index.mjs +22 -20
- package/package.json +1 -1
- package/sync.js +27 -19
- package/sync.mjs +27 -19
- package/types/babel.d.ts +12 -1
- /package/config/{enable_GetSet.d.mts → enable$GetSet.d.mts} +0 -0
- /package/config/{enable_GetSet.d.ts → enable$GetSet.d.ts} +0 -0
package/LICENSE
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Moo.do LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1 +1,141 @@
|
|
|
1
|
-
|
|
1
|
+
# Legend-State
|
|
2
|
+
|
|
3
|
+
Legend-State is a super fast all-in-one state and sync library that lets you write less code to make faster apps. Legend-State has four primary goals:
|
|
4
|
+
|
|
5
|
+
### 1. 🦄 As easy as possible to use
|
|
6
|
+
|
|
7
|
+
There is no boilerplate and there are no contexts, actions, reducers, dispatchers, sagas, thunks, or epics. It doesn't modify your data at all, and you can just call `get()` to get the raw data and `set()` to change it.
|
|
8
|
+
|
|
9
|
+
In React components you can call `use()` on any observable to get the raw data and automatically re-render whenever it changes.
|
|
10
|
+
|
|
11
|
+
```jsx
|
|
12
|
+
import { observable, observe } from "@legendapp/state"
|
|
13
|
+
import { observer } from "@legendapp/state/react"
|
|
14
|
+
|
|
15
|
+
const settings$ = observable({ theme: 'dark' })
|
|
16
|
+
|
|
17
|
+
// get returns the raw data
|
|
18
|
+
settings$.theme.get() // 'dark'
|
|
19
|
+
// set sets
|
|
20
|
+
settings$.theme.set('light')
|
|
21
|
+
|
|
22
|
+
// Computed observables with just a function
|
|
23
|
+
const isDark$ = observable(() => settings$.theme.get() === 'dark')
|
|
24
|
+
|
|
25
|
+
// observing contexts re-run when tracked observables change
|
|
26
|
+
observe(() => {
|
|
27
|
+
console.log(settings$.theme.get())
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const Component = observer(function Component() {
|
|
31
|
+
const theme = state$.settings.theme.get()
|
|
32
|
+
|
|
33
|
+
return <div>Theme: {theme}</div>
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. ⚡️ The fastest React state library
|
|
38
|
+
|
|
39
|
+
Legend-State beats every other state library on just about every metric and is so optimized for arrays that it even beats vanilla JS on the "swap" and "replace all rows" benchmarks. At only `4kb` and with the massive reduction in boilerplate code, you'll have big savings in file size too.
|
|
40
|
+
|
|
41
|
+
<p>
|
|
42
|
+
<img src="https://www.legendapp.com/img/dev/state/times.png" />
|
|
43
|
+
</p>
|
|
44
|
+
|
|
45
|
+
See [Fast 🔥](https://www.legendapp.com/open-source/state/v3/intro/fast/) for more details of why Legend-State is so fast.
|
|
46
|
+
|
|
47
|
+
### 3. 🔥 Fine-grained reactivity for minimal renders
|
|
48
|
+
|
|
49
|
+
Legend-State lets you make your renders super fine-grained, so your apps will be much faster because React has to do less work. The best way to be fast is to render less, less often.
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
function FineGrained() {
|
|
53
|
+
const count$ = useObservable(0)
|
|
54
|
+
|
|
55
|
+
useInterval(() => {
|
|
56
|
+
count$.set(v => v + 1)
|
|
57
|
+
}, 600)
|
|
58
|
+
|
|
59
|
+
// The text updates itself so the component doesn't re-render
|
|
60
|
+
return (
|
|
61
|
+
<div>
|
|
62
|
+
Count: <Memo>{count$}</Memo>
|
|
63
|
+
</div>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 4. 💾 Powerful sync and persistence
|
|
69
|
+
|
|
70
|
+
Legend-State includes a powerful [sync and persistence system](../../usage/persist-sync). It easily enables local-first apps by optimistically applying all changes locally first, retrying changes even after restart until they eventually sync, and syncing minimal diffs. We use Legend-State as the sync systems in [Legend](https://legendapp.com) and [Bravely](https://bravely.io), so it is by necessity very full featured while being simple to set up.
|
|
71
|
+
|
|
72
|
+
Local persistence plugins for the browser and React Native are included, with sync plugins for [Keel](https://www.keel.so), [Supabase](https://www.supabase.com), [TanStack Query](https://tanstack.com/query), and `fetch`.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
const state$ = observable(
|
|
76
|
+
users: syncedKeel({
|
|
77
|
+
list: queries.getUsers,
|
|
78
|
+
create: mutations.createUsers,
|
|
79
|
+
update: mutations.updateUsers,
|
|
80
|
+
delete: mutations.deleteUsers,
|
|
81
|
+
persist: { name: 'users', retrySync: true },
|
|
82
|
+
debounceSet: 500,
|
|
83
|
+
retry: {
|
|
84
|
+
infinite: true,
|
|
85
|
+
},
|
|
86
|
+
changesSince: 'last-sync',
|
|
87
|
+
}),
|
|
88
|
+
// direct link to my user within the users observable
|
|
89
|
+
me: () => state$.users['myuid']
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
observe(() => {
|
|
93
|
+
// get() activates through to state$.users and starts syncing.
|
|
94
|
+
// it updates itself and re-runs observers when name changes
|
|
95
|
+
const name = me$.name.get()
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
// Setting a value goes through to state$.users and saves update to server
|
|
99
|
+
me$.name.set('Annyong')
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Install
|
|
103
|
+
|
|
104
|
+
`bun add @legendapp/state` or `npm install @legendapp/state` or `yarn add @legendapp/state`
|
|
105
|
+
|
|
106
|
+
## Highlights
|
|
107
|
+
|
|
108
|
+
- ✨ Super easy to use 😌
|
|
109
|
+
- ✨ Super fast ⚡️
|
|
110
|
+
- ✨ Super small at 4kb 🐥
|
|
111
|
+
- ✨ Fine-grained reactivity 🔥
|
|
112
|
+
- ✨ No boilerplate
|
|
113
|
+
- ✨ Designed for maximum performance and scalability
|
|
114
|
+
- ✨ React components re-render only on changes
|
|
115
|
+
- ✨ Very strongly typed with TypeScript
|
|
116
|
+
- ✨ Persistence plugins for automatically saving/loading from storage
|
|
117
|
+
- ✨ State can be global or within components
|
|
118
|
+
|
|
119
|
+
[Read more](https://www.legendapp.com/open-source/state/v3/intro/why/) about why Legend-State might be right for you.
|
|
120
|
+
|
|
121
|
+
## Documentation
|
|
122
|
+
|
|
123
|
+
See [the documentation site](https://www.legendapp.com/open-source/state/).
|
|
124
|
+
|
|
125
|
+
## Community
|
|
126
|
+
|
|
127
|
+
Join us on [Discord](https://discord.gg/5CBaNtADNX) to get involved with the Legend community.
|
|
128
|
+
|
|
129
|
+
## 👩⚖️ License
|
|
130
|
+
|
|
131
|
+
[MIT](LICENSE)
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
Legend-State is created and maintained by [Jay Meistrich](https://github.com/jmeistrich) with [Legend](https://www.legendapp.com) and [Bravely](https://www.bravely.io).
|
|
136
|
+
|
|
137
|
+
<p>
|
|
138
|
+
<a href="https://www.legendapp.com"><img src="https://www.legendapp.com/img/LogoTextOnWhite.png" height="56" alt="Legend" /></a>
|
|
139
|
+
<span> </span>
|
|
140
|
+
<a href="https://www.bravely.io"><img src="https://www.legendapp.com/img/bravely-logo.png" height="56" alt="Bravely" /></a>
|
|
141
|
+
</p>
|
package/babel.js
CHANGED
|
@@ -15,7 +15,6 @@ function babel_default() {
|
|
|
15
15
|
const s = specifiers[i].imported.name;
|
|
16
16
|
if (!hasLegendImport && (s === "Computed" || s === "Memo" || s === "Show")) {
|
|
17
17
|
hasLegendImport = true;
|
|
18
|
-
path.skip();
|
|
19
18
|
break;
|
|
20
19
|
}
|
|
21
20
|
}
|
|
@@ -25,7 +24,6 @@ function babel_default() {
|
|
|
25
24
|
JSXElement: {
|
|
26
25
|
enter(path) {
|
|
27
26
|
if (!hasLegendImport) {
|
|
28
|
-
path.skip();
|
|
29
27
|
return;
|
|
30
28
|
}
|
|
31
29
|
const openingElement = path.node.openingElement;
|
package/babel.mjs
CHANGED
|
@@ -13,7 +13,6 @@ function babel_default() {
|
|
|
13
13
|
const s = specifiers[i].imported.name;
|
|
14
14
|
if (!hasLegendImport && (s === "Computed" || s === "Memo" || s === "Show")) {
|
|
15
15
|
hasLegendImport = true;
|
|
16
|
-
path.skip();
|
|
17
16
|
break;
|
|
18
17
|
}
|
|
19
18
|
}
|
|
@@ -23,7 +22,6 @@ function babel_default() {
|
|
|
23
22
|
JSXElement: {
|
|
24
23
|
enter(path) {
|
|
25
24
|
if (!hasLegendImport) {
|
|
26
|
-
path.skip();
|
|
27
25
|
return;
|
|
28
26
|
}
|
|
29
27
|
const openingElement = path.node.openingElement;
|
package/index.js
CHANGED
|
@@ -2038,7 +2038,7 @@ function setToObservable(node, value) {
|
|
|
2038
2038
|
return value;
|
|
2039
2039
|
}
|
|
2040
2040
|
function recursivelyAutoActivate(obj, node) {
|
|
2041
|
-
if (isObject(obj) || isArray(obj)) {
|
|
2041
|
+
if ((isObject(obj) || isArray(obj)) && !obj[symbolOpaque]) {
|
|
2042
2042
|
const pathStack = [];
|
|
2043
2043
|
const getNodeAtPath2 = () => {
|
|
2044
2044
|
var _a;
|
|
@@ -2056,27 +2056,29 @@ function recursivelyAutoActivate(obj, node) {
|
|
|
2056
2056
|
}
|
|
2057
2057
|
function recursivelyAutoActivateInner(obj, pathStack, getNodeAtPath2) {
|
|
2058
2058
|
var _a;
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2059
|
+
if ((isObject(obj) || isArray(obj)) && !obj[symbolOpaque]) {
|
|
2060
|
+
for (const key in obj) {
|
|
2061
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
2062
|
+
const value = obj[key];
|
|
2063
|
+
if (isObservable(value)) {
|
|
2064
|
+
const childNode = getNodeAtPath2();
|
|
2065
|
+
extractFunctionOrComputed(childNode, key, value);
|
|
2066
|
+
delete childNode.lazy;
|
|
2067
|
+
} else {
|
|
2068
|
+
const linkedOptions = isFunction(value) && ((_a = value.prototype) == null ? void 0 : _a[symbolLinked]);
|
|
2069
|
+
if (linkedOptions) {
|
|
2070
|
+
const activate = linkedOptions.activate;
|
|
2071
|
+
if (!activate || activate === "auto") {
|
|
2072
|
+
const childNode = getNodeAtPath2();
|
|
2073
|
+
peek(getChildNode(childNode, key, value));
|
|
2074
|
+
}
|
|
2073
2075
|
}
|
|
2074
2076
|
}
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2077
|
+
if (typeof value === "object") {
|
|
2078
|
+
pathStack.push({ key, value });
|
|
2079
|
+
recursivelyAutoActivateInner(value, pathStack, getNodeAtPath2);
|
|
2080
|
+
pathStack.pop();
|
|
2081
|
+
}
|
|
2080
2082
|
}
|
|
2081
2083
|
}
|
|
2082
2084
|
}
|
package/index.mjs
CHANGED
|
@@ -2036,7 +2036,7 @@ function setToObservable(node, value) {
|
|
|
2036
2036
|
return value;
|
|
2037
2037
|
}
|
|
2038
2038
|
function recursivelyAutoActivate(obj, node) {
|
|
2039
|
-
if (isObject(obj) || isArray(obj)) {
|
|
2039
|
+
if ((isObject(obj) || isArray(obj)) && !obj[symbolOpaque]) {
|
|
2040
2040
|
const pathStack = [];
|
|
2041
2041
|
const getNodeAtPath2 = () => {
|
|
2042
2042
|
var _a;
|
|
@@ -2054,27 +2054,29 @@ function recursivelyAutoActivate(obj, node) {
|
|
|
2054
2054
|
}
|
|
2055
2055
|
function recursivelyAutoActivateInner(obj, pathStack, getNodeAtPath2) {
|
|
2056
2056
|
var _a;
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2057
|
+
if ((isObject(obj) || isArray(obj)) && !obj[symbolOpaque]) {
|
|
2058
|
+
for (const key in obj) {
|
|
2059
|
+
if (hasOwnProperty.call(obj, key)) {
|
|
2060
|
+
const value = obj[key];
|
|
2061
|
+
if (isObservable(value)) {
|
|
2062
|
+
const childNode = getNodeAtPath2();
|
|
2063
|
+
extractFunctionOrComputed(childNode, key, value);
|
|
2064
|
+
delete childNode.lazy;
|
|
2065
|
+
} else {
|
|
2066
|
+
const linkedOptions = isFunction(value) && ((_a = value.prototype) == null ? void 0 : _a[symbolLinked]);
|
|
2067
|
+
if (linkedOptions) {
|
|
2068
|
+
const activate = linkedOptions.activate;
|
|
2069
|
+
if (!activate || activate === "auto") {
|
|
2070
|
+
const childNode = getNodeAtPath2();
|
|
2071
|
+
peek(getChildNode(childNode, key, value));
|
|
2072
|
+
}
|
|
2071
2073
|
}
|
|
2072
2074
|
}
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2075
|
+
if (typeof value === "object") {
|
|
2076
|
+
pathStack.push({ key, value });
|
|
2077
|
+
recursivelyAutoActivateInner(value, pathStack, getNodeAtPath2);
|
|
2078
|
+
pathStack.pop();
|
|
2079
|
+
}
|
|
2078
2080
|
}
|
|
2079
2081
|
}
|
|
2080
2082
|
}
|
package/package.json
CHANGED
package/sync.js
CHANGED
|
@@ -484,21 +484,24 @@ async function doChangeLocal(changeInfo) {
|
|
|
484
484
|
const { value$: obs, syncState, localState, syncOptions } = queuedChange;
|
|
485
485
|
const { pluginPersist } = localState;
|
|
486
486
|
const persist = syncOptions.persist;
|
|
487
|
-
const
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
if (changesLocal.length > 0) {
|
|
495
|
-
let promiseSet = pluginPersist.set(table, changesLocal, configLocal);
|
|
496
|
-
if (promiseSet) {
|
|
497
|
-
promiseSet = promiseSet.then(() => {
|
|
498
|
-
promisesLocalSaves.delete(promiseSet);
|
|
487
|
+
const saveLocal = !!(persist == null ? void 0 : persist.name);
|
|
488
|
+
if (saveLocal) {
|
|
489
|
+
const { table, config: configLocal } = parseLocalConfig(persist);
|
|
490
|
+
const shouldSaveMetadata = persist == null ? void 0 : persist.retrySync;
|
|
491
|
+
if (saveRemote && shouldSaveMetadata) {
|
|
492
|
+
await updateMetadataImmediate(obs, localState, syncState, syncOptions, {
|
|
493
|
+
pending: localState.pendingChanges
|
|
499
494
|
});
|
|
500
|
-
|
|
501
|
-
|
|
495
|
+
}
|
|
496
|
+
if (changesLocal.length > 0) {
|
|
497
|
+
let promiseSet = pluginPersist.set(table, changesLocal, configLocal);
|
|
498
|
+
if (promiseSet) {
|
|
499
|
+
promiseSet = promiseSet.then(() => {
|
|
500
|
+
promisesLocalSaves.delete(promiseSet);
|
|
501
|
+
});
|
|
502
|
+
promisesLocalSaves.add(promiseSet);
|
|
503
|
+
await promiseSet;
|
|
504
|
+
}
|
|
502
505
|
}
|
|
503
506
|
}
|
|
504
507
|
}
|
|
@@ -513,6 +516,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
513
516
|
const { table, config: configLocal } = parseLocalConfig(persist);
|
|
514
517
|
const { allowSetIfGetError, onBeforeSet, onSetError, waitForSet, onAfterSet } = syncOptions || {};
|
|
515
518
|
const shouldSaveMetadata = persist == null ? void 0 : persist.retrySync;
|
|
519
|
+
const saveLocal = !!(persist == null ? void 0 : persist.name);
|
|
516
520
|
if (changesRemote.length > 0) {
|
|
517
521
|
await state.when(() => syncState.isLoaded.get() || allowSetIfGetError && syncState.error.get());
|
|
518
522
|
if (waitForSet) {
|
|
@@ -547,7 +551,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
547
551
|
if (pathStrs.length > 0) {
|
|
548
552
|
let transformedChanges = void 0;
|
|
549
553
|
const metadata = {};
|
|
550
|
-
if (
|
|
554
|
+
if (saveLocal) {
|
|
551
555
|
const pendingMetadata = (_b = pluginPersist.getMetadata(table, configLocal)) == null ? void 0 : _b.pending;
|
|
552
556
|
const pending = localState.pendingChanges;
|
|
553
557
|
for (let i = 0; i < pathStrs.length; i++) {
|
|
@@ -584,7 +588,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
584
588
|
}
|
|
585
589
|
onChangeRemote(() => state.mergeIntoObservable(obs, ...allChanges));
|
|
586
590
|
}
|
|
587
|
-
if (
|
|
591
|
+
if (saveLocal) {
|
|
588
592
|
if (shouldSaveMetadata && !state.isEmpty(metadata)) {
|
|
589
593
|
updateMetadata(obs, localState, syncState, syncOptions, metadata);
|
|
590
594
|
}
|
|
@@ -618,10 +622,11 @@ function onObsChange(value$, syncState, localState, syncOptions, { changes, load
|
|
|
618
622
|
async function loadLocal(value$, syncOptions, syncState, localState) {
|
|
619
623
|
var _a, _b, _c;
|
|
620
624
|
const { persist } = syncOptions;
|
|
621
|
-
|
|
625
|
+
const node = getNode(value$);
|
|
626
|
+
const nodeValue = getNodeValue(getNode(node.state));
|
|
627
|
+
if (persist == null ? void 0 : persist.name) {
|
|
622
628
|
const PersistPlugin = persist.plugin || ((_a = observableSyncConfiguration.persist) == null ? void 0 : _a.plugin);
|
|
623
629
|
const { table, config } = parseLocalConfig(persist);
|
|
624
|
-
const node = getNode(value$);
|
|
625
630
|
if (!PersistPlugin) {
|
|
626
631
|
throw new Error("Local persist is not configured");
|
|
627
632
|
}
|
|
@@ -673,10 +678,13 @@ async function loadLocal(value$, syncOptions, syncState, localState) {
|
|
|
673
678
|
}
|
|
674
679
|
state.internal.globalState.isLoadingLocal = false;
|
|
675
680
|
}
|
|
676
|
-
|
|
681
|
+
nodeValue.clearPersist = () => Promise.all([
|
|
677
682
|
persistPlugin.deleteTable(table, config),
|
|
678
683
|
persistPlugin.deleteMetadata(table, config)
|
|
679
684
|
]);
|
|
685
|
+
} else {
|
|
686
|
+
nodeValue.clearPersist = () => {
|
|
687
|
+
};
|
|
680
688
|
}
|
|
681
689
|
syncState.isPersistLoaded.set(true);
|
|
682
690
|
}
|
package/sync.mjs
CHANGED
|
@@ -482,21 +482,24 @@ async function doChangeLocal(changeInfo) {
|
|
|
482
482
|
const { value$: obs, syncState, localState, syncOptions } = queuedChange;
|
|
483
483
|
const { pluginPersist } = localState;
|
|
484
484
|
const persist = syncOptions.persist;
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (changesLocal.length > 0) {
|
|
493
|
-
let promiseSet = pluginPersist.set(table, changesLocal, configLocal);
|
|
494
|
-
if (promiseSet) {
|
|
495
|
-
promiseSet = promiseSet.then(() => {
|
|
496
|
-
promisesLocalSaves.delete(promiseSet);
|
|
485
|
+
const saveLocal = !!(persist == null ? void 0 : persist.name);
|
|
486
|
+
if (saveLocal) {
|
|
487
|
+
const { table, config: configLocal } = parseLocalConfig(persist);
|
|
488
|
+
const shouldSaveMetadata = persist == null ? void 0 : persist.retrySync;
|
|
489
|
+
if (saveRemote && shouldSaveMetadata) {
|
|
490
|
+
await updateMetadataImmediate(obs, localState, syncState, syncOptions, {
|
|
491
|
+
pending: localState.pendingChanges
|
|
497
492
|
});
|
|
498
|
-
|
|
499
|
-
|
|
493
|
+
}
|
|
494
|
+
if (changesLocal.length > 0) {
|
|
495
|
+
let promiseSet = pluginPersist.set(table, changesLocal, configLocal);
|
|
496
|
+
if (promiseSet) {
|
|
497
|
+
promiseSet = promiseSet.then(() => {
|
|
498
|
+
promisesLocalSaves.delete(promiseSet);
|
|
499
|
+
});
|
|
500
|
+
promisesLocalSaves.add(promiseSet);
|
|
501
|
+
await promiseSet;
|
|
502
|
+
}
|
|
500
503
|
}
|
|
501
504
|
}
|
|
502
505
|
}
|
|
@@ -511,6 +514,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
511
514
|
const { table, config: configLocal } = parseLocalConfig(persist);
|
|
512
515
|
const { allowSetIfGetError, onBeforeSet, onSetError, waitForSet, onAfterSet } = syncOptions || {};
|
|
513
516
|
const shouldSaveMetadata = persist == null ? void 0 : persist.retrySync;
|
|
517
|
+
const saveLocal = !!(persist == null ? void 0 : persist.name);
|
|
514
518
|
if (changesRemote.length > 0) {
|
|
515
519
|
await when(() => syncState.isLoaded.get() || allowSetIfGetError && syncState.error.get());
|
|
516
520
|
if (waitForSet) {
|
|
@@ -545,7 +549,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
545
549
|
if (pathStrs.length > 0) {
|
|
546
550
|
let transformedChanges = void 0;
|
|
547
551
|
const metadata = {};
|
|
548
|
-
if (
|
|
552
|
+
if (saveLocal) {
|
|
549
553
|
const pendingMetadata = (_b = pluginPersist.getMetadata(table, configLocal)) == null ? void 0 : _b.pending;
|
|
550
554
|
const pending = localState.pendingChanges;
|
|
551
555
|
for (let i = 0; i < pathStrs.length; i++) {
|
|
@@ -582,7 +586,7 @@ async function doChangeRemote(changeInfo) {
|
|
|
582
586
|
}
|
|
583
587
|
onChangeRemote(() => mergeIntoObservable(obs, ...allChanges));
|
|
584
588
|
}
|
|
585
|
-
if (
|
|
589
|
+
if (saveLocal) {
|
|
586
590
|
if (shouldSaveMetadata && !isEmpty(metadata)) {
|
|
587
591
|
updateMetadata(obs, localState, syncState, syncOptions, metadata);
|
|
588
592
|
}
|
|
@@ -616,10 +620,11 @@ function onObsChange(value$, syncState, localState, syncOptions, { changes, load
|
|
|
616
620
|
async function loadLocal(value$, syncOptions, syncState, localState) {
|
|
617
621
|
var _a, _b, _c;
|
|
618
622
|
const { persist } = syncOptions;
|
|
619
|
-
|
|
623
|
+
const node = getNode(value$);
|
|
624
|
+
const nodeValue = getNodeValue(getNode(node.state));
|
|
625
|
+
if (persist == null ? void 0 : persist.name) {
|
|
620
626
|
const PersistPlugin = persist.plugin || ((_a = observableSyncConfiguration.persist) == null ? void 0 : _a.plugin);
|
|
621
627
|
const { table, config } = parseLocalConfig(persist);
|
|
622
|
-
const node = getNode(value$);
|
|
623
628
|
if (!PersistPlugin) {
|
|
624
629
|
throw new Error("Local persist is not configured");
|
|
625
630
|
}
|
|
@@ -671,10 +676,13 @@ async function loadLocal(value$, syncOptions, syncState, localState) {
|
|
|
671
676
|
}
|
|
672
677
|
internal.globalState.isLoadingLocal = false;
|
|
673
678
|
}
|
|
674
|
-
|
|
679
|
+
nodeValue.clearPersist = () => Promise.all([
|
|
675
680
|
persistPlugin.deleteTable(table, config),
|
|
676
681
|
persistPlugin.deleteMetadata(table, config)
|
|
677
682
|
]);
|
|
683
|
+
} else {
|
|
684
|
+
nodeValue.clearPersist = () => {
|
|
685
|
+
};
|
|
678
686
|
}
|
|
679
687
|
syncState.isPersistLoaded.set(true);
|
|
680
688
|
}
|
package/types/babel.d.ts
CHANGED
|
@@ -1 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ObservableParam } from '@legendapp/state';
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import type { Computed, Memo } from '@legendapp/state/react';
|
|
5
|
+
declare module '@legendapp/state/react' {
|
|
6
|
+
export declare const Computed: (props: {
|
|
7
|
+
children: ObservableParam | (() => ReactNode) | ReactNode;
|
|
8
|
+
}) => React.ReactElement;
|
|
9
|
+
export declare const Memo: (props: {
|
|
10
|
+
children: ObservableParam | (() => ReactNode) | ReactNode;
|
|
11
|
+
}) => React.ReactElement;
|
|
12
|
+
}
|
|
File without changes
|
|
File without changes
|