@legendapp/state 0.10.0 → 0.11.0-beta.2
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/index.d.ts +2 -2
- package/index.js +110 -71
- package/index.js.map +1 -1
- package/index.mjs +111 -68
- package/index.mjs.map +1 -1
- package/internal.d.ts +1 -1
- package/internal.js +0 -4
- package/internal.js.map +1 -1
- package/internal.mjs +1 -1
- package/local-storage.js +1 -1
- package/local-storage.js.map +1 -1
- package/local-storage.mjs +1 -1
- package/local-storage.mjs.map +1 -1
- package/mmkv.js +1 -1
- package/mmkv.js.map +1 -1
- package/mmkv.mjs +1 -1
- package/mmkv.mjs.map +1 -1
- package/package.json +1 -1
- package/persist.js +3 -3
- package/persist.js.map +1 -1
- package/persist.mjs +3 -3
- package/persist.mjs.map +1 -1
- package/react.d.ts +3 -2
- package/react.js +87 -121
- package/react.js.map +1 -1
- package/react.mjs +86 -121
- package/react.mjs.map +1 -1
- package/src/globals.d.ts +7 -4
- package/src/helpers.d.ts +1 -2
- package/src/observableInterfaces.d.ts +8 -11
- package/src/react/listenWhileCalling.d.ts +2 -0
- package/src/react/observer.d.ts +2 -0
- package/src/react/useComputed.d.ts +1 -0
- package/src/react/useForceRender.d.ts +1 -0
- package/src/react/useObservable.d.ts +9 -0
- package/src/state.d.ts +3 -5
- package/src/react/useNewObservable.d.ts +0 -11
- package/src/react/useObservables.d.ts +0 -11
package/persist.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.mjs","sources":["../../src/persist/configureObservablePersistence.ts","../../src/persist/local-storage.ts","../../src/persist/persistHelpers.ts","../../src/persist/persistObservable.ts"],"sourcesContent":[null,null,null,null],"names":[],"mappings":";;AASO,MAAM,8BAA8B,GAAW,GAAG;AAEnD,SAAU,8BAA8B,CAAC,OAAgB,EAAA;AAC3D,IAAA,MAAM,CAAC,MAAM,CAAC,8BAA8B,EAAE,OAAO,CAAC,CAAC;AAC3D;;MCXa,6BAA6B,CAAA;AAA1C,IAAA,WAAA,GAAA;QACY,IAAI,CAAA,IAAA,GAAwB,EAAE,CAAC;KA4B1C;AA1BU,IAAA,GAAG,CAAC,EAAU,EAAA;QACjB,IAAI,OAAO,YAAY,KAAK,WAAW;AAAE,YAAA,OAAO,SAAS,CAAC;QAC1D,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,SAAS,EAAE;YAC7B,IAAI;gBACA,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AACvC,gBAAA,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,SAAS,CAAC;AAChD,aAAA;YAAC,OAAM,EAAA,EAAA;AACJ,gBAAA,OAAO,CAAC,KAAK,CAAC,+
|
|
1
|
+
{"version":3,"file":"persist.mjs","sources":["../../src/persist/configureObservablePersistence.ts","../../src/persist/local-storage.ts","../../src/persist/persistHelpers.ts","../../src/persist/persistObservable.ts"],"sourcesContent":[null,null,null,null],"names":[],"mappings":";;AASO,MAAM,8BAA8B,GAAW,GAAG;AAEnD,SAAU,8BAA8B,CAAC,OAAgB,EAAA;AAC3D,IAAA,MAAM,CAAC,MAAM,CAAC,8BAA8B,EAAE,OAAO,CAAC,CAAC;AAC3D;;MCXa,6BAA6B,CAAA;AAA1C,IAAA,WAAA,GAAA;QACY,IAAI,CAAA,IAAA,GAAwB,EAAE,CAAC;KA4B1C;AA1BU,IAAA,GAAG,CAAC,EAAU,EAAA;QACjB,IAAI,OAAO,YAAY,KAAK,WAAW;AAAE,YAAA,OAAO,SAAS,CAAC;QAC1D,IAAI,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,SAAS,EAAE;YAC7B,IAAI;gBACA,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AACvC,gBAAA,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,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E,QAAQ,EAAE,UAAU,CAAC,CAAC;AAErD,IAAA,IAAI,MAAM,EAAE;QACR,IAAI,CAAC,iBAAiB,EAAE;AACpB,YAAA,MAAM,IAAI,KAAK,CAAC,sCAAsC,CAAC,CAAC;AAC3D,SAAA;;AAED,QAAA,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,iBAAiB,CAAC,EAAE;YACzC,eAAe,CAAC,GAAG,CAAC,iBAAiB,EAAE,IAAI,iBAAiB,EAAE,CAAC,CAAC;AACnE,SAAA;QACD,UAAU,CAAC,iBAAiB,GAAG,eAAe,CAAC,GAAG,CAAC,iBAAiB,CAA4B,CAAC;AAEjG,QAAA,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,MAAK;YAC/B,UAAU,CAAC,iBAAiB,CAAC,MAAM,CAC/B,GAAG,EACH,cAAc,EACd,MAAK;AACD,gBAAA,QAAQ,CAAC,GAAG,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;aACxC,EACD,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,CACxC,CAAC;AACN,SAAC,CAAC,CAAC;AACN,KAAA;AAED,IAAA,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,CAAC;AAE3E,IAAA,OAAO,QAAQ,CAAC;AACpB;;;;"}
|
package/react.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export * from './src/react/
|
|
2
|
-
export * from './src/react/
|
|
1
|
+
export * from './src/react/observer';
|
|
2
|
+
export * from './src/react/useComputed';
|
|
3
|
+
export * from './src/react/useObservable';
|
package/react.js
CHANGED
|
@@ -2,145 +2,111 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var state = require('@legendapp/state');
|
|
6
5
|
var react = require('react');
|
|
6
|
+
var state = require('@legendapp/state');
|
|
7
|
+
|
|
8
|
+
function listenWhileCalling(fn, listeners, updateFn) {
|
|
9
|
+
var _a;
|
|
10
|
+
// Cache previous tracking nodes since this might be nested from another observing component
|
|
11
|
+
const trackingPrev = state.tracking.nodes;
|
|
12
|
+
// Reset tracking nodes
|
|
13
|
+
state.tracking.nodes = new Map();
|
|
14
|
+
// Calling the function fills up the tracking nodes
|
|
15
|
+
const ret = fn();
|
|
16
|
+
const nodes = state.tracking.nodes;
|
|
17
|
+
// Restore previous tracking nodes
|
|
18
|
+
state.tracking.nodes = trackingPrev;
|
|
19
|
+
// Listen to any nodes not already listened
|
|
20
|
+
for (let tracked of nodes) {
|
|
21
|
+
const { node, shallow } = tracked[1];
|
|
22
|
+
// Listen to this path if not already listening
|
|
23
|
+
if (!((_a = node.listeners) === null || _a === void 0 ? void 0 : _a.has(updateFn))) {
|
|
24
|
+
listeners.add(shallow ? state.onChangeShallow(node, updateFn) : state.onChange(node, updateFn));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return ret;
|
|
28
|
+
}
|
|
7
29
|
|
|
8
30
|
function useForceRender() {
|
|
9
31
|
const [, forceRender] = react.useReducer((s) => s + 1, 0);
|
|
10
|
-
return () => forceRender();
|
|
32
|
+
return react.useCallback(() => forceRender(), []);
|
|
11
33
|
}
|
|
12
|
-
/**
|
|
13
|
-
* A React hook that listens to observables and returns their values.
|
|
14
|
-
*
|
|
15
|
-
* @param fn A function that returns a single observable, an array of observables, or a flat object of observables
|
|
16
|
-
* @param deps If present, useObservables will re-activate if the values in the list change. By default it will never reactivate, which is the same as a [] argument.
|
|
17
34
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (ref.current.listeners.size > 0) {
|
|
32
|
-
ref.current.listeners.forEach((dispose) => dispose());
|
|
33
|
-
ref.current.listeners.clear();
|
|
34
|
-
}
|
|
35
|
-
let previousPrimitives;
|
|
36
|
-
const updateFromSelector = () => {
|
|
37
|
-
// Compute returns a string concatenated with all of the primitive values in the arguments
|
|
38
|
-
// so we can easily compare that to the previous result and re-render only if it's different.
|
|
39
|
-
const args = ref.current.fn();
|
|
40
|
-
const { primitives, value } = compute(args);
|
|
41
|
-
if (previousPrimitives !== primitives) {
|
|
42
|
-
previousPrimitives = primitives;
|
|
43
|
-
ref.current.value = value;
|
|
44
|
-
forceRender();
|
|
35
|
+
const hasSymbol = typeof Symbol === 'function' && Symbol.for;
|
|
36
|
+
// Extracting the forwardRef inspired by https://github.com/mobxjs/mobx/blob/main/packages/mobx-react-lite/src/observer.ts
|
|
37
|
+
const ReactForwardRefSymbol = hasSymbol
|
|
38
|
+
? Symbol.for('react.forward_ref')
|
|
39
|
+
: typeof react.forwardRef === 'function' && react.forwardRef((props) => null)['$$typeof'];
|
|
40
|
+
function observer(component) {
|
|
41
|
+
// Unwrap forwardRef on the component
|
|
42
|
+
let useForwardRef;
|
|
43
|
+
if (ReactForwardRefSymbol && component['$$typeof'] === ReactForwardRefSymbol) {
|
|
44
|
+
useForwardRef = true;
|
|
45
|
+
component = component['render'];
|
|
46
|
+
if (process.env.NODE_ENV === 'development' && typeof component !== 'function') {
|
|
47
|
+
throw new Error(`[legend-state] \`render\` property of ForwardRef was not a function`);
|
|
45
48
|
}
|
|
49
|
+
}
|
|
50
|
+
const componentName = component.displayName || component.name;
|
|
51
|
+
// Create a wrapper observer component
|
|
52
|
+
let observer = function (props, ref) {
|
|
53
|
+
const refListeners = react.useRef();
|
|
54
|
+
if (!refListeners.current)
|
|
55
|
+
refListeners.current = new Set();
|
|
56
|
+
const forceRender = useForceRender();
|
|
57
|
+
// Clean up listeners on the way out
|
|
58
|
+
react.useEffect(() => () => refListeners.current.forEach((dispose) => dispose()), []);
|
|
59
|
+
// Set up all the listeners while rendering the component
|
|
60
|
+
return listenWhileCalling(() => component(props, ref), refListeners.current, forceRender);
|
|
46
61
|
};
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
const update = () => {
|
|
58
|
-
state.tracking.is = true;
|
|
59
|
-
state.tracking.nodes = [];
|
|
60
|
-
// tracking.nodes is updated any time a primitive is accessed or get() is called
|
|
61
|
-
// so after calling fn(), tracking.nodes will be filled with those selectors
|
|
62
|
-
const args = ref.current.fn();
|
|
63
|
-
const selectorNodes = state.tracking.nodes;
|
|
64
|
-
state.tracking.nodes = [];
|
|
65
|
-
// compute calls symbolGet on all of the observable arguments so afterwards
|
|
66
|
-
// tracking.nodes will be filled with all of the observables in the arguments
|
|
67
|
-
const { primitives, value } = compute(args);
|
|
68
|
-
state.tracking.is = false;
|
|
69
|
-
// Set up listeners on all nodes
|
|
70
|
-
// Selectors only need to check for changes in the primitives while
|
|
71
|
-
// observables need to check observable arguments to add listeners to any
|
|
72
|
-
// that weren't there before
|
|
73
|
-
updateListeners(selectorNodes, updateFromSelector);
|
|
74
|
-
updateListeners(state.tracking.nodes, update);
|
|
75
|
-
ref.current.value = value;
|
|
76
|
-
// Re-render when called by a change handler
|
|
77
|
-
if (!ref.current.isFirst) {
|
|
78
|
-
previousPrimitives = primitives;
|
|
79
|
-
forceRender();
|
|
80
|
-
}
|
|
81
|
-
ref.current.isFirst = false;
|
|
82
|
-
};
|
|
83
|
-
update();
|
|
62
|
+
if (componentName !== '') {
|
|
63
|
+
observer.displayName = componentName;
|
|
64
|
+
}
|
|
65
|
+
// Wrap back in forwardRef if necessary
|
|
66
|
+
if (useForwardRef) {
|
|
67
|
+
observer = react.forwardRef(observer);
|
|
68
|
+
}
|
|
69
|
+
return react.memo(observer);
|
|
84
70
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
isPrim = state.isPrimitive(args);
|
|
102
|
-
if (isPrim || args[state.symbolIsObservable] || args[state.symbolShallow]) {
|
|
103
|
-
// Process a single value
|
|
104
|
-
if (isPrim)
|
|
105
|
-
primitives += args;
|
|
106
|
-
value = isPrim ? args : state.getObservableRawValue(args);
|
|
107
|
-
}
|
|
108
|
-
else if (state.isObject(args)) {
|
|
109
|
-
// Process a flat object of observables
|
|
110
|
-
value = {};
|
|
111
|
-
const keys = Object.keys(args);
|
|
112
|
-
const length = keys.length;
|
|
113
|
-
for (let i = 0; i < length; i++) {
|
|
114
|
-
const key = keys[i];
|
|
115
|
-
isPrim = state.isPrimitive(args[key]);
|
|
116
|
-
if (isPrim)
|
|
117
|
-
primitives += args[key];
|
|
118
|
-
value[key] = isPrim ? args[key] : state.getObservableRawValue(args[key]);
|
|
119
|
-
}
|
|
71
|
+
|
|
72
|
+
function useComputed(selector, deps) {
|
|
73
|
+
const ref = react.useRef();
|
|
74
|
+
if (!ref.current)
|
|
75
|
+
ref.current = new Set();
|
|
76
|
+
// Do all the computed magic inside a useMemo so we can get an initial value
|
|
77
|
+
const initial = react.useMemo(() => {
|
|
78
|
+
let prevValue;
|
|
79
|
+
const onChange = () => {
|
|
80
|
+
const v = selector();
|
|
81
|
+
// If the selector value is different than previously
|
|
82
|
+
if (v !== prevValue) {
|
|
83
|
+
prevValue = v;
|
|
84
|
+
setValue(v);
|
|
120
85
|
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
|
|
86
|
+
};
|
|
87
|
+
// Set up all the listeners while computing the value
|
|
88
|
+
prevValue = listenWhileCalling(selector, ref.current, onChange);
|
|
89
|
+
return prevValue;
|
|
90
|
+
}, deps || []);
|
|
91
|
+
const [value, setValue] = react.useState(initial);
|
|
92
|
+
// Clean up listeners on the way out
|
|
93
|
+
react.useEffect(() => () => ref.current.forEach((dispose) => dispose()), []);
|
|
94
|
+
return value;
|
|
124
95
|
}
|
|
125
96
|
|
|
126
97
|
/**
|
|
127
98
|
* A React hook that creates a new observable and can optionally listen or persist its state.
|
|
128
99
|
*
|
|
129
|
-
* @param
|
|
130
|
-
* @param observe (optional) Listen to the observable for changes
|
|
131
|
-
* @param persist (optional) PersistOptions for peristing the observable to state
|
|
100
|
+
* @param initialValue The initial value of the observable or a function that returns the initial value
|
|
132
101
|
*
|
|
133
|
-
* @see https://www.legendapp.com/dev/state/react/#
|
|
102
|
+
* @see https://www.legendapp.com/dev/state/react/#useObservable
|
|
134
103
|
*/
|
|
135
|
-
function
|
|
104
|
+
function useObservable(initialValue) {
|
|
136
105
|
// Create the observable from the default value
|
|
137
|
-
|
|
138
|
-
if (observe !== false) {
|
|
139
|
-
useObservables(() => [obs]);
|
|
140
|
-
}
|
|
141
|
-
return [obs, obs[state.symbolGet]];
|
|
106
|
+
return react.useMemo(() => state.observable(state.isFunction(initialValue) ? initialValue() : initialValue), []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
142
107
|
}
|
|
143
108
|
|
|
144
|
-
exports.
|
|
145
|
-
exports.
|
|
109
|
+
exports.observer = observer;
|
|
110
|
+
exports.useComputed = useComputed;
|
|
111
|
+
exports.useObservable = useObservable;
|
|
146
112
|
//# sourceMappingURL=react.js.map
|
package/react.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.js","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"react.js","sources":["../../src/react/listenWhileCalling.ts","../../src/react/useForceRender.ts","../../src/react/observer.ts","../../src/react/useComputed.ts","../../src/react/useObservable.ts"],"sourcesContent":[null,null,null,null,null],"names":["tracking","onChangeShallow","onChange","useReducer","useCallback","forwardRef","useRef","useEffect","memo","useMemo","useState","observable","isFunction"],"mappings":";;;;;;;SAEgB,kBAAkB,CAAI,EAAW,EAAE,SA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AAC;AACxC;;;;;;"}
|
package/react.mjs
CHANGED
|
@@ -1,141 +1,106 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useReducer, useCallback, forwardRef, memo, useRef, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { tracking, onChangeShallow, onChange, observable, isFunction } from '@legendapp/state';
|
|
3
|
+
|
|
4
|
+
function listenWhileCalling(fn, listeners, updateFn) {
|
|
5
|
+
var _a;
|
|
6
|
+
// Cache previous tracking nodes since this might be nested from another observing component
|
|
7
|
+
const trackingPrev = tracking.nodes;
|
|
8
|
+
// Reset tracking nodes
|
|
9
|
+
tracking.nodes = new Map();
|
|
10
|
+
// Calling the function fills up the tracking nodes
|
|
11
|
+
const ret = fn();
|
|
12
|
+
const nodes = tracking.nodes;
|
|
13
|
+
// Restore previous tracking nodes
|
|
14
|
+
tracking.nodes = trackingPrev;
|
|
15
|
+
// Listen to any nodes not already listened
|
|
16
|
+
for (let tracked of nodes) {
|
|
17
|
+
const { node, shallow } = tracked[1];
|
|
18
|
+
// Listen to this path if not already listening
|
|
19
|
+
if (!((_a = node.listeners) === null || _a === void 0 ? void 0 : _a.has(updateFn))) {
|
|
20
|
+
listeners.add(shallow ? onChangeShallow(node, updateFn) : onChange(node, updateFn));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return ret;
|
|
24
|
+
}
|
|
3
25
|
|
|
4
26
|
function useForceRender() {
|
|
5
27
|
const [, forceRender] = useReducer((s) => s + 1, 0);
|
|
6
|
-
return () => forceRender();
|
|
28
|
+
return useCallback(() => forceRender(), []);
|
|
7
29
|
}
|
|
8
|
-
/**
|
|
9
|
-
* A React hook that listens to observables and returns their values.
|
|
10
|
-
*
|
|
11
|
-
* @param fn A function that returns a single observable, an array of observables, or a flat object of observables
|
|
12
|
-
* @param deps If present, useObservables will re-activate if the values in the list change. By default it will never reactivate, which is the same as a [] argument.
|
|
13
30
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (ref.current.listeners.size > 0) {
|
|
28
|
-
ref.current.listeners.forEach((dispose) => dispose());
|
|
29
|
-
ref.current.listeners.clear();
|
|
30
|
-
}
|
|
31
|
-
let previousPrimitives;
|
|
32
|
-
const updateFromSelector = () => {
|
|
33
|
-
// Compute returns a string concatenated with all of the primitive values in the arguments
|
|
34
|
-
// so we can easily compare that to the previous result and re-render only if it's different.
|
|
35
|
-
const args = ref.current.fn();
|
|
36
|
-
const { primitives, value } = compute(args);
|
|
37
|
-
if (previousPrimitives !== primitives) {
|
|
38
|
-
previousPrimitives = primitives;
|
|
39
|
-
ref.current.value = value;
|
|
40
|
-
forceRender();
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const updateListeners = (nodes, updateFn) => {
|
|
44
|
-
var _a;
|
|
45
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
46
|
-
const { node, shallow } = nodes[i];
|
|
47
|
-
// Listen to this path if not already listening
|
|
48
|
-
if (!((_a = node.listeners) === null || _a === void 0 ? void 0 : _a.has(updateFn))) {
|
|
49
|
-
ref.current.listeners.add(shallow ? onChangeShallow(node, updateFn) : onChange(node, updateFn));
|
|
50
|
-
}
|
|
31
|
+
const hasSymbol = typeof Symbol === 'function' && Symbol.for;
|
|
32
|
+
// Extracting the forwardRef inspired by https://github.com/mobxjs/mobx/blob/main/packages/mobx-react-lite/src/observer.ts
|
|
33
|
+
const ReactForwardRefSymbol = hasSymbol
|
|
34
|
+
? Symbol.for('react.forward_ref')
|
|
35
|
+
: typeof forwardRef === 'function' && forwardRef((props) => null)['$$typeof'];
|
|
36
|
+
function observer(component) {
|
|
37
|
+
// Unwrap forwardRef on the component
|
|
38
|
+
let useForwardRef;
|
|
39
|
+
if (ReactForwardRefSymbol && component['$$typeof'] === ReactForwardRefSymbol) {
|
|
40
|
+
useForwardRef = true;
|
|
41
|
+
component = component['render'];
|
|
42
|
+
if (process.env.NODE_ENV === 'development' && typeof component !== 'function') {
|
|
43
|
+
throw new Error(`[legend-state] \`render\` property of ForwardRef was not a function`);
|
|
51
44
|
}
|
|
45
|
+
}
|
|
46
|
+
const componentName = component.displayName || component.name;
|
|
47
|
+
// Create a wrapper observer component
|
|
48
|
+
let observer = function (props, ref) {
|
|
49
|
+
const refListeners = useRef();
|
|
50
|
+
if (!refListeners.current)
|
|
51
|
+
refListeners.current = new Set();
|
|
52
|
+
const forceRender = useForceRender();
|
|
53
|
+
// Clean up listeners on the way out
|
|
54
|
+
useEffect(() => () => refListeners.current.forEach((dispose) => dispose()), []);
|
|
55
|
+
// Set up all the listeners while rendering the component
|
|
56
|
+
return listenWhileCalling(() => component(props, ref), refListeners.current, forceRender);
|
|
52
57
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// compute calls symbolGet on all of the observable arguments so afterwards
|
|
62
|
-
// tracking.nodes will be filled with all of the observables in the arguments
|
|
63
|
-
const { primitives, value } = compute(args);
|
|
64
|
-
tracking.is = false;
|
|
65
|
-
// Set up listeners on all nodes
|
|
66
|
-
// Selectors only need to check for changes in the primitives while
|
|
67
|
-
// observables need to check observable arguments to add listeners to any
|
|
68
|
-
// that weren't there before
|
|
69
|
-
updateListeners(selectorNodes, updateFromSelector);
|
|
70
|
-
updateListeners(tracking.nodes, update);
|
|
71
|
-
ref.current.value = value;
|
|
72
|
-
// Re-render when called by a change handler
|
|
73
|
-
if (!ref.current.isFirst) {
|
|
74
|
-
previousPrimitives = primitives;
|
|
75
|
-
forceRender();
|
|
76
|
-
}
|
|
77
|
-
ref.current.isFirst = false;
|
|
78
|
-
};
|
|
79
|
-
update();
|
|
58
|
+
if (componentName !== '') {
|
|
59
|
+
observer.displayName = componentName;
|
|
60
|
+
}
|
|
61
|
+
// Wrap back in forwardRef if necessary
|
|
62
|
+
if (useForwardRef) {
|
|
63
|
+
observer = forwardRef(observer);
|
|
64
|
+
}
|
|
65
|
+
return memo(observer);
|
|
80
66
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
isPrim = isPrimitive(args);
|
|
98
|
-
if (isPrim || args[symbolIsObservable] || args[symbolShallow]) {
|
|
99
|
-
// Process a single value
|
|
100
|
-
if (isPrim)
|
|
101
|
-
primitives += args;
|
|
102
|
-
value = isPrim ? args : getObservableRawValue(args);
|
|
103
|
-
}
|
|
104
|
-
else if (isObject(args)) {
|
|
105
|
-
// Process a flat object of observables
|
|
106
|
-
value = {};
|
|
107
|
-
const keys = Object.keys(args);
|
|
108
|
-
const length = keys.length;
|
|
109
|
-
for (let i = 0; i < length; i++) {
|
|
110
|
-
const key = keys[i];
|
|
111
|
-
isPrim = isPrimitive(args[key]);
|
|
112
|
-
if (isPrim)
|
|
113
|
-
primitives += args[key];
|
|
114
|
-
value[key] = isPrim ? args[key] : getObservableRawValue(args[key]);
|
|
115
|
-
}
|
|
67
|
+
|
|
68
|
+
function useComputed(selector, deps) {
|
|
69
|
+
const ref = useRef();
|
|
70
|
+
if (!ref.current)
|
|
71
|
+
ref.current = new Set();
|
|
72
|
+
// Do all the computed magic inside a useMemo so we can get an initial value
|
|
73
|
+
const initial = useMemo(() => {
|
|
74
|
+
let prevValue;
|
|
75
|
+
const onChange = () => {
|
|
76
|
+
const v = selector();
|
|
77
|
+
// If the selector value is different than previously
|
|
78
|
+
if (v !== prevValue) {
|
|
79
|
+
prevValue = v;
|
|
80
|
+
setValue(v);
|
|
116
81
|
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
82
|
+
};
|
|
83
|
+
// Set up all the listeners while computing the value
|
|
84
|
+
prevValue = listenWhileCalling(selector, ref.current, onChange);
|
|
85
|
+
return prevValue;
|
|
86
|
+
}, deps || []);
|
|
87
|
+
const [value, setValue] = useState(initial);
|
|
88
|
+
// Clean up listeners on the way out
|
|
89
|
+
useEffect(() => () => ref.current.forEach((dispose) => dispose()), []);
|
|
90
|
+
return value;
|
|
120
91
|
}
|
|
121
92
|
|
|
122
93
|
/**
|
|
123
94
|
* A React hook that creates a new observable and can optionally listen or persist its state.
|
|
124
95
|
*
|
|
125
|
-
* @param
|
|
126
|
-
* @param observe (optional) Listen to the observable for changes
|
|
127
|
-
* @param persist (optional) PersistOptions for peristing the observable to state
|
|
96
|
+
* @param initialValue The initial value of the observable or a function that returns the initial value
|
|
128
97
|
*
|
|
129
|
-
* @see https://www.legendapp.com/dev/state/react/#
|
|
98
|
+
* @see https://www.legendapp.com/dev/state/react/#useObservable
|
|
130
99
|
*/
|
|
131
|
-
function
|
|
100
|
+
function useObservable(initialValue) {
|
|
132
101
|
// Create the observable from the default value
|
|
133
|
-
|
|
134
|
-
if (observe !== false) {
|
|
135
|
-
useObservables(() => [obs]);
|
|
136
|
-
}
|
|
137
|
-
return [obs, obs[symbolGet]];
|
|
102
|
+
return useMemo(() => observable(isFunction(initialValue) ? initialValue() : initialValue), []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
138
103
|
}
|
|
139
104
|
|
|
140
|
-
export {
|
|
105
|
+
export { observer, useComputed, useObservable };
|
|
141
106
|
//# sourceMappingURL=react.mjs.map
|
package/react.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"react.mjs","sources":["../../src/react/
|
|
1
|
+
{"version":3,"file":"react.mjs","sources":["../../src/react/listenWhileCalling.ts","../../src/react/useForceRender.ts","../../src/react/observer.ts","../../src/react/useComputed.ts","../../src/react/useObservable.ts"],"sourcesContent":[null,null,null,null,null],"names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,EAAE,CAC6B,CAAC;AACxC;;;;"}
|
package/src/globals.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NodeValue } from './observableInterfaces';
|
|
2
2
|
export declare const symbolDateModified: unique symbol;
|
|
3
|
-
export declare const symbolShallow: unique symbol;
|
|
4
|
-
export declare const symbolGet: unique symbol;
|
|
5
3
|
export declare const symbolIsObservable: unique symbol;
|
|
4
|
+
export declare const nextNodeID: {
|
|
5
|
+
current: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function getOutputValue(node: NodeValue): any;
|
|
8
|
+
export declare function get(node: NodeValue): any;
|
|
9
|
+
export declare function observe(node: NodeValue, shallow?: boolean): any;
|
|
6
10
|
export declare function getNodeValue(node: NodeValue): any;
|
|
7
11
|
export declare function getChildNode(node: NodeValue, key: string | number): NodeValue;
|
|
8
|
-
export declare function getObservableRawValue<T>(obs: ObservableTypeRender<T>): T;
|
package/src/helpers.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { ObservableType
|
|
2
|
-
export declare function shallow(obs: ObservableType): Shallow;
|
|
1
|
+
import type { ObservableType } from './observableInterfaces';
|
|
3
2
|
export declare function isObservable(obs: any): boolean;
|
|
4
3
|
export declare function mergeIntoObservable(target: ObservableType | object, ...sources: any[]): any;
|