@legendapp/state 3.0.0-alpha.3 → 3.0.0-alpha.30
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/CHANGELOG.md +831 -1
- package/LICENSE +21 -1
- package/README.md +141 -1
- package/babel.js +0 -2
- package/babel.mjs +0 -2
- package/config/enable$GetSet.js +2 -1
- package/config/enable$GetSet.mjs +2 -1
- package/config/enableReactTracking.js +2 -1
- package/config/enableReactTracking.mjs +2 -1
- package/config/enableReactUse.js +2 -1
- package/config/enableReactUse.mjs +2 -1
- package/config/enable_PeekAssign.js +2 -1
- package/config/enable_PeekAssign.mjs +2 -1
- package/config.d.mts +13 -0
- package/config.d.ts +13 -0
- package/config.js +2052 -0
- package/config.mjs +2050 -0
- package/helpers/trackHistory.js +2 -2
- package/helpers/trackHistory.mjs +2 -2
- package/index.d.mts +21 -302
- package/index.d.ts +21 -302
- package/index.js +274 -318
- package/index.mjs +275 -317
- package/observableInterfaces-Dilj6F92.d.mts +282 -0
- package/observableInterfaces-Dilj6F92.d.ts +282 -0
- package/package.json +11 -1
- package/persist-plugins/async-storage.d.mts +6 -3
- package/persist-plugins/async-storage.d.ts +6 -3
- package/persist-plugins/async-storage.js +12 -4
- package/persist-plugins/async-storage.mjs +12 -5
- package/persist-plugins/indexeddb.d.mts +6 -4
- package/persist-plugins/indexeddb.d.ts +6 -4
- package/persist-plugins/indexeddb.js +16 -6
- package/persist-plugins/indexeddb.mjs +16 -7
- package/persist-plugins/mmkv.d.mts +5 -1
- package/persist-plugins/mmkv.d.ts +5 -1
- package/persist-plugins/mmkv.js +14 -5
- package/persist-plugins/mmkv.mjs +14 -6
- package/react.d.mts +18 -14
- package/react.d.ts +18 -14
- package/react.js +57 -32
- package/react.mjs +58 -33
- package/sync-plugins/_transformObjectFields.d.mts +31 -0
- package/sync-plugins/_transformObjectFields.d.ts +31 -0
- package/sync-plugins/_transformObjectFields.js +114 -0
- package/sync-plugins/_transformObjectFields.mjs +110 -0
- package/sync-plugins/crud.d.mts +15 -23
- package/sync-plugins/crud.d.ts +15 -23
- package/sync-plugins/crud.js +213 -134
- package/sync-plugins/crud.mjs +214 -135
- package/sync-plugins/fetch.js +12 -8
- package/sync-plugins/fetch.mjs +13 -9
- package/sync-plugins/firebase.d.mts +26 -0
- package/sync-plugins/firebase.d.ts +26 -0
- package/sync-plugins/firebase.js +373 -0
- package/sync-plugins/firebase.mjs +368 -0
- package/sync-plugins/keel.d.mts +27 -10
- package/sync-plugins/keel.d.ts +27 -10
- package/sync-plugins/keel.js +40 -21
- package/sync-plugins/keel.mjs +40 -21
- package/sync-plugins/supabase.d.mts +12 -7
- package/sync-plugins/supabase.d.ts +12 -7
- package/sync-plugins/supabase.js +24 -13
- package/sync-plugins/supabase.mjs +25 -14
- package/sync-plugins/tanstack-query.d.mts +2 -2
- package/sync-plugins/tanstack-query.d.ts +2 -2
- package/sync-plugins/tanstack-query.js +3 -2
- package/sync-plugins/tanstack-query.mjs +3 -2
- package/sync-plugins/tanstack-react-query.d.mts +1 -1
- package/sync-plugins/tanstack-react-query.d.ts +1 -1
- package/sync.d.mts +68 -197
- package/sync.d.ts +68 -197
- package/sync.js +448 -283
- package/sync.mjs +454 -289
- package/types/babel.d.ts +12 -1
- package/.DS_Store +0 -0
- /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/config/enable$GetSet.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('@legendapp/state');
|
|
4
|
+
var config = require('@legendapp/state/config');
|
|
4
5
|
|
|
5
6
|
// src/config/enable$GetSet.ts
|
|
6
7
|
function enable$GetSet() {
|
|
7
|
-
|
|
8
|
+
config.configureLegendState({
|
|
8
9
|
observableProperties: {
|
|
9
10
|
$: {
|
|
10
11
|
get(node) {
|
package/config/enable$GetSet.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('@legendapp/state');
|
|
4
|
+
var config = require('@legendapp/state/config');
|
|
4
5
|
var react$1 = require('@legendapp/state/react');
|
|
5
6
|
var react = require('react');
|
|
6
7
|
|
|
@@ -22,7 +23,7 @@ function enableReactTracking({ auto, warnUnobserved }) {
|
|
|
22
23
|
}
|
|
23
24
|
return false;
|
|
24
25
|
};
|
|
25
|
-
|
|
26
|
+
config.configureLegendState({
|
|
26
27
|
observableFunctions: {
|
|
27
28
|
get: (node, options) => {
|
|
28
29
|
if (needsSelector()) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isObject, internal, tracking } from '@legendapp/state';
|
|
2
|
+
import { configureLegendState } from '@legendapp/state/config';
|
|
2
3
|
import { useSelector } from '@legendapp/state/react';
|
|
3
4
|
import { createContext, __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED, useContext } from 'react';
|
|
4
5
|
|
package/config/enableReactUse.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('@legendapp/state');
|
|
4
|
+
var config = require('@legendapp/state/config');
|
|
4
5
|
var react = require('@legendapp/state/react');
|
|
5
6
|
|
|
6
7
|
// src/config/enableReactUse.ts
|
|
7
8
|
var didWarn = false;
|
|
8
9
|
function enableReactUse() {
|
|
9
|
-
|
|
10
|
+
config.configureLegendState({
|
|
10
11
|
observableFunctions: {
|
|
11
12
|
use: (node, options) => {
|
|
12
13
|
if (process.env.NODE_ENV === "development" && !didWarn) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { internal } from '@legendapp/state';
|
|
2
|
+
import { configureLegendState } from '@legendapp/state/config';
|
|
2
3
|
import { useSelector } from '@legendapp/state/react';
|
|
3
4
|
|
|
4
5
|
// src/config/enableReactUse.ts
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var state = require('@legendapp/state');
|
|
4
|
+
var config = require('@legendapp/state/config');
|
|
4
5
|
|
|
5
6
|
// src/config/enable_PeekAssign.ts
|
|
6
7
|
function enable_PeekAssign() {
|
|
7
|
-
|
|
8
|
+
config.configureLegendState({
|
|
8
9
|
observableProperties: {
|
|
9
10
|
_: {
|
|
10
11
|
get(node) {
|
package/config.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { N as NodeInfo } from './observableInterfaces-Dilj6F92.mjs';
|
|
2
|
+
|
|
3
|
+
declare function configureLegendState({ observableFunctions, observableProperties, jsonReplacer, jsonReviver, }: {
|
|
4
|
+
observableFunctions?: Record<string, (node: NodeInfo, ...args: any[]) => any>;
|
|
5
|
+
observableProperties?: Record<string, {
|
|
6
|
+
get: (node: NodeInfo) => any;
|
|
7
|
+
set: (node: NodeInfo, value: any) => any;
|
|
8
|
+
}>;
|
|
9
|
+
jsonReplacer?: (this: any, key: string, value: any) => any;
|
|
10
|
+
jsonReviver?: (this: any, key: string, value: any) => any;
|
|
11
|
+
}): void;
|
|
12
|
+
|
|
13
|
+
export { configureLegendState };
|
package/config.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { N as NodeInfo } from './observableInterfaces-Dilj6F92.js';
|
|
2
|
+
|
|
3
|
+
declare function configureLegendState({ observableFunctions, observableProperties, jsonReplacer, jsonReviver, }: {
|
|
4
|
+
observableFunctions?: Record<string, (node: NodeInfo, ...args: any[]) => any>;
|
|
5
|
+
observableProperties?: Record<string, {
|
|
6
|
+
get: (node: NodeInfo) => any;
|
|
7
|
+
set: (node: NodeInfo, value: any) => any;
|
|
8
|
+
}>;
|
|
9
|
+
jsonReplacer?: (this: any, key: string, value: any) => any;
|
|
10
|
+
jsonReviver?: (this: any, key: string, value: any) => any;
|
|
11
|
+
}): void;
|
|
12
|
+
|
|
13
|
+
export { configureLegendState };
|