@legendapp/state 3.0.0-alpha.4 → 3.0.0-alpha.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 (94) hide show
  1. package/.DS_Store +0 -0
  2. package/CHANGELOG.md +831 -1
  3. package/LICENSE +21 -1
  4. package/README.md +141 -1
  5. package/babel.js +0 -2
  6. package/babel.mjs +0 -2
  7. package/config/configureLegendState.d.mts +13 -0
  8. package/config/configureLegendState.d.ts +13 -0
  9. package/config/configureLegendState.js +45 -0
  10. package/config/configureLegendState.mjs +43 -0
  11. package/config/enable$GetSet.js +2 -1
  12. package/config/enable$GetSet.mjs +2 -1
  13. package/config/enableReactTracking.js +2 -1
  14. package/config/enableReactTracking.mjs +2 -1
  15. package/config/enableReactUse.js +2 -1
  16. package/config/enableReactUse.mjs +2 -1
  17. package/config/enable_PeekAssign.js +2 -1
  18. package/config/enable_PeekAssign.mjs +2 -1
  19. package/helpers/trackHistory.js +2 -2
  20. package/helpers/trackHistory.mjs +2 -2
  21. package/index.d.mts +106 -81
  22. package/index.d.ts +106 -81
  23. package/index.js +347 -335
  24. package/index.mjs +344 -333
  25. package/package.json +36 -1
  26. package/persist-plugins/async-storage.d.mts +6 -3
  27. package/persist-plugins/async-storage.d.ts +6 -3
  28. package/persist-plugins/async-storage.js +8 -4
  29. package/persist-plugins/async-storage.mjs +8 -5
  30. package/persist-plugins/indexeddb.d.mts +6 -4
  31. package/persist-plugins/indexeddb.d.ts +6 -4
  32. package/persist-plugins/indexeddb.js +35 -15
  33. package/persist-plugins/indexeddb.mjs +35 -16
  34. package/persist-plugins/mmkv.d.mts +5 -1
  35. package/persist-plugins/mmkv.d.ts +5 -1
  36. package/persist-plugins/mmkv.js +10 -5
  37. package/persist-plugins/mmkv.mjs +10 -6
  38. package/react-reactive/enableReactComponents.d.mts +9 -0
  39. package/react-reactive/enableReactComponents.d.ts +9 -0
  40. package/react-reactive/enableReactComponents.js +19 -0
  41. package/react-reactive/enableReactComponents.mjs +17 -0
  42. package/react-reactive/enableReactNativeComponents.d.mts +22 -0
  43. package/react-reactive/enableReactNativeComponents.d.ts +22 -0
  44. package/react-reactive/enableReactNativeComponents.js +53 -0
  45. package/react-reactive/enableReactNativeComponents.mjs +51 -0
  46. package/react-reactive/enableReactive.d.mts +5 -0
  47. package/react-reactive/enableReactive.d.ts +5 -0
  48. package/react-reactive/enableReactive.js +24 -0
  49. package/react-reactive/enableReactive.mjs +22 -0
  50. package/react-reactive/enableReactive.native.d.mts +5 -0
  51. package/react-reactive/enableReactive.native.d.ts +5 -0
  52. package/react-reactive/enableReactive.native.js +58 -0
  53. package/react-reactive/enableReactive.native.mjs +56 -0
  54. package/react-reactive/enableReactive.web.d.mts +5 -0
  55. package/react-reactive/enableReactive.web.d.ts +5 -0
  56. package/react-reactive/enableReactive.web.js +58 -0
  57. package/react-reactive/enableReactive.web.mjs +56 -0
  58. package/react.d.mts +39 -34
  59. package/react.d.ts +39 -34
  60. package/react.js +54 -27
  61. package/react.mjs +55 -28
  62. package/sync-plugins/crud.d.mts +21 -24
  63. package/sync-plugins/crud.d.ts +21 -24
  64. package/sync-plugins/crud.js +241 -140
  65. package/sync-plugins/crud.mjs +243 -142
  66. package/sync-plugins/fetch.js +12 -8
  67. package/sync-plugins/fetch.mjs +13 -9
  68. package/sync-plugins/firebase.d.mts +27 -0
  69. package/sync-plugins/firebase.d.ts +27 -0
  70. package/sync-plugins/firebase.js +373 -0
  71. package/sync-plugins/firebase.mjs +368 -0
  72. package/sync-plugins/keel.d.mts +43 -26
  73. package/sync-plugins/keel.d.ts +43 -26
  74. package/sync-plugins/keel.js +143 -100
  75. package/sync-plugins/keel.mjs +145 -100
  76. package/sync-plugins/supabase.d.mts +19 -9
  77. package/sync-plugins/supabase.d.ts +19 -9
  78. package/sync-plugins/supabase.js +52 -22
  79. package/sync-plugins/supabase.mjs +53 -23
  80. package/sync-plugins/tanstack-query.d.mts +2 -2
  81. package/sync-plugins/tanstack-query.d.ts +2 -2
  82. package/sync-plugins/tanstack-query.js +22 -5
  83. package/sync-plugins/tanstack-query.mjs +22 -5
  84. package/sync-plugins/tanstack-react-query.d.mts +1 -1
  85. package/sync-plugins/tanstack-react-query.d.ts +1 -1
  86. package/sync-plugins/tanstack-react-query.js +8 -1
  87. package/sync-plugins/tanstack-react-query.mjs +8 -1
  88. package/sync.d.mts +74 -200
  89. package/sync.d.ts +74 -200
  90. package/sync.js +510 -307
  91. package/sync.mjs +516 -313
  92. package/types/babel.d.ts +12 -1
  93. /package/config/{enable_GetSet.d.mts → enable$GetSet.d.mts} +0 -0
  94. /package/config/{enable_GetSet.d.ts → enable$GetSet.d.ts} +0 -0
package/LICENSE CHANGED
@@ -1 +1,21 @@
1
- LICENSE
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
- README.md
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>&nbsp;&nbsp;&nbsp;&nbsp;</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;
@@ -0,0 +1,13 @@
1
+ import { NodeInfo } from '@legendapp/state';
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 };
@@ -0,0 +1,13 @@
1
+ import { NodeInfo } from '@legendapp/state';
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 };
@@ -0,0 +1,45 @@
1
+ 'use strict';
2
+
3
+ var state = require('@legendapp/state');
4
+
5
+ // src/config/configureLegendState.ts
6
+ var { globalState, observableProperties: _observableProperties, observableFns, ObservablePrimitiveClass } = state.internal;
7
+ function configureLegendState({
8
+ observableFunctions,
9
+ observableProperties,
10
+ jsonReplacer,
11
+ jsonReviver
12
+ }) {
13
+ if (observableFunctions) {
14
+ for (const key in observableFunctions) {
15
+ const fn = observableFunctions[key];
16
+ observableFns.set(key, fn);
17
+ ObservablePrimitiveClass.prototype[key] = function(...args) {
18
+ return fn.call(this, this._node, ...args);
19
+ };
20
+ }
21
+ }
22
+ if (observableProperties) {
23
+ for (const key in observableProperties) {
24
+ const fns = observableProperties[key];
25
+ _observableProperties.set(key, fns);
26
+ Object.defineProperty(ObservablePrimitiveClass.prototype, key, {
27
+ configurable: true,
28
+ get() {
29
+ return fns.get.call(this, this._node);
30
+ },
31
+ set(value) {
32
+ return fns.set.call(this, this._node, value);
33
+ }
34
+ });
35
+ }
36
+ }
37
+ if (jsonReplacer) {
38
+ globalState.replacer = jsonReplacer;
39
+ }
40
+ if (jsonReviver) {
41
+ globalState.reviver = jsonReviver;
42
+ }
43
+ }
44
+
45
+ exports.configureLegendState = configureLegendState;
@@ -0,0 +1,43 @@
1
+ import { internal } from '@legendapp/state';
2
+
3
+ // src/config/configureLegendState.ts
4
+ var { globalState, observableProperties: _observableProperties, observableFns, ObservablePrimitiveClass } = internal;
5
+ function configureLegendState({
6
+ observableFunctions,
7
+ observableProperties,
8
+ jsonReplacer,
9
+ jsonReviver
10
+ }) {
11
+ if (observableFunctions) {
12
+ for (const key in observableFunctions) {
13
+ const fn = observableFunctions[key];
14
+ observableFns.set(key, fn);
15
+ ObservablePrimitiveClass.prototype[key] = function(...args) {
16
+ return fn.call(this, this._node, ...args);
17
+ };
18
+ }
19
+ }
20
+ if (observableProperties) {
21
+ for (const key in observableProperties) {
22
+ const fns = observableProperties[key];
23
+ _observableProperties.set(key, fns);
24
+ Object.defineProperty(ObservablePrimitiveClass.prototype, key, {
25
+ configurable: true,
26
+ get() {
27
+ return fns.get.call(this, this._node);
28
+ },
29
+ set(value) {
30
+ return fns.set.call(this, this._node, value);
31
+ }
32
+ });
33
+ }
34
+ }
35
+ if (jsonReplacer) {
36
+ globalState.replacer = jsonReplacer;
37
+ }
38
+ if (jsonReviver) {
39
+ globalState.reviver = jsonReviver;
40
+ }
41
+ }
42
+
43
+ export { configureLegendState };
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  var state = require('@legendapp/state');
4
+ var configureLegendState = require('@legendapp/state/config/configureLegendState');
4
5
 
5
6
  // src/config/enable$GetSet.ts
6
7
  function enable$GetSet() {
7
- state.configureLegendState({
8
+ configureLegendState.configureLegendState({
8
9
  observableProperties: {
9
10
  $: {
10
11
  get(node) {
@@ -1,4 +1,5 @@
1
- import { configureLegendState, internal } from '@legendapp/state';
1
+ import { internal } from '@legendapp/state';
2
+ import { configureLegendState } from '@legendapp/state/config/configureLegendState';
2
3
 
3
4
  // src/config/enable$GetSet.ts
4
5
  function enable$GetSet() {
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var state = require('@legendapp/state');
4
+ var configureLegendState = require('@legendapp/state/config/configureLegendState');
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
- state.configureLegendState({
26
+ configureLegendState.configureLegendState({
26
27
  observableFunctions: {
27
28
  get: (node, options) => {
28
29
  if (needsSelector()) {
@@ -1,4 +1,5 @@
1
- import { configureLegendState, isObject, internal, tracking } from '@legendapp/state';
1
+ import { isObject, internal, tracking } from '@legendapp/state';
2
+ import { configureLegendState } from '@legendapp/state/config/configureLegendState';
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
 
@@ -1,12 +1,13 @@
1
1
  'use strict';
2
2
 
3
3
  var state = require('@legendapp/state');
4
+ var configureLegendState = require('@legendapp/state/config/configureLegendState');
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
- state.configureLegendState({
10
+ configureLegendState.configureLegendState({
10
11
  observableFunctions: {
11
12
  use: (node, options) => {
12
13
  if (process.env.NODE_ENV === "development" && !didWarn) {
@@ -1,4 +1,5 @@
1
- import { configureLegendState, internal } from '@legendapp/state';
1
+ import { internal } from '@legendapp/state';
2
+ import { configureLegendState } from '@legendapp/state/config/configureLegendState';
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 configureLegendState = require('@legendapp/state/config/configureLegendState');
4
5
 
5
6
  // src/config/enable_PeekAssign.ts
6
7
  function enable_PeekAssign() {
7
- state.configureLegendState({
8
+ configureLegendState.configureLegendState({
8
9
  observableProperties: {
9
10
  _: {
10
11
  get(node) {
@@ -1,4 +1,5 @@
1
- import { configureLegendState, internal } from '@legendapp/state';
1
+ import { internal } from '@legendapp/state';
2
+ import { configureLegendState } from '@legendapp/state/config/configureLegendState';
2
3
 
3
4
  // src/config/enable_PeekAssign.ts
4
5
  function enable_PeekAssign() {
@@ -5,8 +5,8 @@ var state = require('@legendapp/state');
5
5
  // src/helpers/trackHistory.ts
6
6
  function trackHistory(value$, targetObservable) {
7
7
  const history = targetObservable != null ? targetObservable : state.observable();
8
- value$.onChange(({ loading, remote, changes }) => {
9
- if (!loading && !remote) {
8
+ value$.onChange(({ isFromPersist, isFromSync, changes }) => {
9
+ if (!isFromPersist && !isFromSync) {
10
10
  const time = Date.now().toString();
11
11
  for (let i = 0; i < changes.length; i++) {
12
12
  const { path, prevAtPath, pathTypes } = changes[i];
@@ -3,8 +3,8 @@ import { observable, constructObjectWithPath, mergeIntoObservable } from '@legen
3
3
  // src/helpers/trackHistory.ts
4
4
  function trackHistory(value$, targetObservable) {
5
5
  const history = targetObservable != null ? targetObservable : observable();
6
- value$.onChange(({ loading, remote, changes }) => {
7
- if (!loading && !remote) {
6
+ value$.onChange(({ isFromPersist, isFromSync, changes }) => {
7
+ if (!isFromPersist && !isFromSync) {
8
8
  const time = Date.now().toString();
9
9
  for (let i = 0; i < changes.length; i++) {
10
10
  const { path, prevAtPath, pathTypes } = changes[i];