@solid-primitives/deep 0.0.102 → 0.2.0
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/README.md +124 -13
- package/dist/index.cjs +130 -12
- package/dist/index.d.ts +73 -20
- package/dist/index.js +129 -13
- package/package.json +24 -11
package/README.md
CHANGED
|
@@ -7,9 +7,13 @@
|
|
|
7
7
|
[](https://turborepo.org/)
|
|
8
8
|
[](https://bundlephobia.com/package/@solid-primitives/deep)
|
|
9
9
|
[](https://www.npmjs.com/package/@solid-primitives/deep)
|
|
10
|
-
[](https://github.com/solidjs-community/solid-primitives#contribution-process)
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
Primitives for tracking and observing nested reactive objects in Solid.
|
|
13
|
+
|
|
14
|
+
- [`trackDeep`](#trackDeep) - Tracks all properties of a store by iterating over them recursively.
|
|
15
|
+
- [`trackStore`](#trackStore) - A more performant alternative to `trackDeep` utilizing specific store implementations.
|
|
16
|
+
- [`captureStoreUpdates`](#captureStoreUpdates) - A utility function that captures all updates to a store and returns them as an array.
|
|
13
17
|
|
|
14
18
|
## Installation
|
|
15
19
|
|
|
@@ -21,31 +25,138 @@ yarn add @solid-primitives/deep
|
|
|
21
25
|
pnpm add @solid-primitives/deep
|
|
22
26
|
```
|
|
23
27
|
|
|
24
|
-
##
|
|
28
|
+
## `trackDeep`
|
|
29
|
+
|
|
30
|
+
Tracks all properties of a store by iterating over them recursively.
|
|
31
|
+
|
|
32
|
+
It's a slightly more performant alternative to tracing a store with `JSON.stringify`, that won't throw when encountering circular references or `BigInt` values.
|
|
33
|
+
|
|
34
|
+
Since it iterates over all properties of a store, it's not recommended to use this on large stores under rapid updates.
|
|
35
|
+
|
|
36
|
+
### How to use it
|
|
25
37
|
|
|
26
|
-
You can
|
|
38
|
+
You can call this function with any store under a tracking scope and it will iterate over all properties of the store and track them.
|
|
27
39
|
|
|
28
40
|
```ts
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
41
|
+
import { trackDeep } from "@solid-primitives/deep";
|
|
42
|
+
|
|
43
|
+
const [state, setState] = createStore({ name: "John", age: 42 });
|
|
44
|
+
|
|
45
|
+
createEffect(() => {
|
|
46
|
+
trackDeep(state);
|
|
47
|
+
/* execute some logic whenever the state changes */
|
|
48
|
+
});
|
|
37
49
|
```
|
|
38
50
|
|
|
39
51
|
Or since this has a composable design, you can create _derivative_ functions and use them similar to derivative signals.
|
|
40
52
|
|
|
41
53
|
```ts
|
|
42
|
-
const deeplyTrackedStore = () =>
|
|
54
|
+
const deeplyTrackedStore = () => trackDeep(sign);
|
|
43
55
|
createEffect(() => {
|
|
44
56
|
console.log("Store is: ", deeplyTrackedStore());
|
|
45
57
|
// ^ this causes a re-execution of the effect on deep changes of properties
|
|
46
58
|
});
|
|
47
59
|
```
|
|
48
60
|
|
|
61
|
+
`trackDeep` will traverse any "wrappable" object _(objects that solid stores will wrap with proxies)_, even if it's not a solid store.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
createEffect(() => {
|
|
65
|
+
// will also work:
|
|
66
|
+
trackDeep({ myStore: state });
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
> **Warning** If you `unwrap` a store, it will no longer be tracked by `trackDeep` nor `trackStore`!
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
const unwrapped = unwrap(state);
|
|
74
|
+
|
|
75
|
+
createEffect(() => {
|
|
76
|
+
// This will NOT work:
|
|
77
|
+
trackDeep(unwrapped);
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## `trackStore`
|
|
82
|
+
|
|
83
|
+
A much more performant alternative to `trackDeep` that is utilizing memoization and specific store implementations of solid stores.
|
|
84
|
+
|
|
85
|
+
You should consider using this instead of other tracking methods, for large stores, stores that are updated rapidly or tracked in many effects.
|
|
86
|
+
|
|
87
|
+
### How to use it
|
|
88
|
+
|
|
89
|
+
It can be used in almost the same way as `trackDeep`, the only difference is that it requires a store to be directly passed in. So it won't work with objects that contain stores.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { trackStore } from "@solid-primitives/deep";
|
|
93
|
+
|
|
94
|
+
const [state, setState] = createStore({ name: "John", age: 42 });
|
|
95
|
+
|
|
96
|
+
createEffect(() => {
|
|
97
|
+
trackStore(state);
|
|
98
|
+
/* execute some logic whenever the state changes */
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## `captureStoreUpdates`
|
|
103
|
+
|
|
104
|
+
Creates a function for tracking and capturing updates to a store.
|
|
105
|
+
|
|
106
|
+
It could be useful for implementing [undo/redo functionality](https://primitives.solidjs.community/package/history) or for turning a store into a immutable stream of updates.
|
|
107
|
+
|
|
108
|
+
### How to use it
|
|
109
|
+
|
|
110
|
+
Each execution of the returned function will return an array of updates to the store since the last execution.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const [state, setState] = createStore({ todos: [] });
|
|
114
|
+
|
|
115
|
+
const getDelta = captureStoreUpdates(state);
|
|
116
|
+
|
|
117
|
+
getDelta(); // [{ path: [], value: { todos: [] } }]
|
|
118
|
+
|
|
119
|
+
setState("todos", ["foo"]);
|
|
120
|
+
|
|
121
|
+
getDelta(); // [{ path: ["todos"], value: ["foo"] }]
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The returned function will track all updates to a store (just like [`trackStore`](#trackStore)), so it can be used inside a tracking scope.
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
const [state, setState] = createStore({ todos: [] });
|
|
128
|
+
|
|
129
|
+
const getDelta = captureStoreUpdates(state);
|
|
130
|
+
|
|
131
|
+
createEffect(() => {
|
|
132
|
+
const delta = getDelta();
|
|
133
|
+
/* execute some logic whenever the state changes */
|
|
134
|
+
console.log(delta);
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The returned function is not a signal - it won't get updated by itself, it has to be called manually, or under a tracking scope to capture new updates.
|
|
139
|
+
|
|
140
|
+
But it can be turned into a signal by using `createMemo`:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
const [state, setState] = createStore({ todos: [] });
|
|
144
|
+
|
|
145
|
+
const delta = createMemo(captureStoreUpdates(state));
|
|
146
|
+
|
|
147
|
+
// both of these effects will receive the same delta
|
|
148
|
+
createEffect(() => {
|
|
149
|
+
console.log(delta());
|
|
150
|
+
});
|
|
151
|
+
createEffect(() => {
|
|
152
|
+
console.log(delta());
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Demo
|
|
157
|
+
|
|
158
|
+
See a demo of this primitive in action [here](https://primitives.solidjs.community/playground/deep).
|
|
159
|
+
|
|
49
160
|
## Changelog
|
|
50
161
|
|
|
51
162
|
See [CHANGELOG.md](./CHANGELOG.md)
|
package/dist/index.cjs
CHANGED
|
@@ -1,23 +1,141 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var solidJs = require('solid-js');
|
|
4
|
+
var store = require('solid-js/store');
|
|
5
|
+
var memo = require('@solid-primitives/memo');
|
|
6
|
+
var web = require('solid-js/web');
|
|
4
7
|
|
|
5
|
-
// src/
|
|
6
|
-
function
|
|
7
|
-
|
|
8
|
+
// src/track-deep.ts
|
|
9
|
+
function trackDeep(store) {
|
|
10
|
+
traverse(store, /* @__PURE__ */ new Set());
|
|
11
|
+
return store;
|
|
8
12
|
}
|
|
9
|
-
function
|
|
10
|
-
|
|
13
|
+
function traverse(value, seen) {
|
|
14
|
+
let isArray;
|
|
15
|
+
let proto;
|
|
16
|
+
if (value != null && typeof value === "object" && !seen.has(value) && ((isArray = Array.isArray(value)) || value[solidJs.$PROXY] || !(proto = Object.getPrototypeOf(value)) || proto === Object.prototype)) {
|
|
11
17
|
seen.add(value);
|
|
12
|
-
for (const
|
|
13
|
-
|
|
18
|
+
for (const child of isArray ? value : Object.values(value))
|
|
19
|
+
traverse(child, seen);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.deepTrack = trackDeep;
|
|
23
|
+
var EQUALS_FALSE = { equals: false };
|
|
24
|
+
var TrackStoreCache = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
var TrackVersion = 0;
|
|
26
|
+
function createLazyMemo(calc) {
|
|
27
|
+
let isReading = false, isStale = true, alreadyTracked = false, version = 0;
|
|
28
|
+
const [track, trigger] = solidJs.createSignal(void 0, EQUALS_FALSE), memo = solidJs.createMemo(() => isReading ? calc() : isStale = !track(), void 0, {
|
|
29
|
+
equals: () => alreadyTracked
|
|
30
|
+
});
|
|
31
|
+
return () => {
|
|
32
|
+
isReading = true;
|
|
33
|
+
if (isStale)
|
|
34
|
+
isStale = trigger();
|
|
35
|
+
alreadyTracked = version === TrackVersion;
|
|
36
|
+
version = TrackVersion;
|
|
37
|
+
memo();
|
|
38
|
+
isReading = false;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function getTrackStoreNode(node) {
|
|
42
|
+
let track = TrackStoreCache.get(node);
|
|
43
|
+
if (!track) {
|
|
44
|
+
solidJs.createRoot(() => {
|
|
45
|
+
const unwrapped = store.unwrap(node);
|
|
46
|
+
track = createLazyMemo(() => {
|
|
47
|
+
node[solidJs.$TRACK];
|
|
48
|
+
for (const [key, child] of Object.entries(unwrapped)) {
|
|
49
|
+
let childNode;
|
|
50
|
+
if (child != null && typeof child === "object" && ((childNode = child[solidJs.$PROXY]) || (childNode = solidJs.untrack(() => node[key])) && solidJs.$TRACK in childNode)) {
|
|
51
|
+
getTrackStoreNode(childNode)?.();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
TrackStoreCache.set(node, track);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return track;
|
|
59
|
+
}
|
|
60
|
+
function trackStore(store) {
|
|
61
|
+
TrackVersion++;
|
|
62
|
+
solidJs.$TRACK in store && getTrackStoreNode(store)?.();
|
|
63
|
+
return store;
|
|
64
|
+
}
|
|
65
|
+
function* entries(obj) {
|
|
66
|
+
if (Array.isArray(obj)) {
|
|
67
|
+
for (let i = 0; i < obj.length; i++) {
|
|
68
|
+
yield [i, obj[i]];
|
|
14
69
|
}
|
|
70
|
+
} else {
|
|
71
|
+
yield* Object.entries(obj)[Symbol.iterator]();
|
|
15
72
|
}
|
|
16
|
-
return value;
|
|
17
73
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
74
|
+
var StoreNodeChildrenCache = /* @__PURE__ */ new WeakMap();
|
|
75
|
+
function getStoreNodechildren(node) {
|
|
76
|
+
let signal = StoreNodeChildrenCache.get(node);
|
|
77
|
+
if (!signal) {
|
|
78
|
+
const unwrapped = store.unwrap(node), isArray = Array.isArray(unwrapped);
|
|
79
|
+
signal = solidJs.createRoot(
|
|
80
|
+
() => memo.createLazyMemo(() => {
|
|
81
|
+
node[solidJs.$TRACK];
|
|
82
|
+
const children = isArray ? [] : {};
|
|
83
|
+
for (const [key, child] of entries(unwrapped)) {
|
|
84
|
+
let childNode;
|
|
85
|
+
if (child != null && typeof child === "object" && ((childNode = child[solidJs.$PROXY]) || (childNode = solidJs.untrack(() => node[key])) && solidJs.$TRACK in childNode)) {
|
|
86
|
+
children[key] = childNode;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return children;
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
StoreNodeChildrenCache.set(node, signal);
|
|
93
|
+
}
|
|
94
|
+
return signal();
|
|
95
|
+
}
|
|
96
|
+
var CurrentUpdates;
|
|
97
|
+
var SeenNodes;
|
|
98
|
+
function newCacheNode(children) {
|
|
99
|
+
const record = { ...children };
|
|
100
|
+
for (const [key, node] of entries(children)) {
|
|
101
|
+
if (SeenNodes.has(node))
|
|
102
|
+
continue;
|
|
103
|
+
SeenNodes.add(node);
|
|
104
|
+
record[key] = newCacheNode(getStoreNodechildren(node));
|
|
105
|
+
}
|
|
106
|
+
return { children, record };
|
|
107
|
+
}
|
|
108
|
+
function compareStoreWithCache(node, parent, key, path) {
|
|
109
|
+
if (SeenNodes.has(node))
|
|
110
|
+
return;
|
|
111
|
+
SeenNodes.add(node);
|
|
112
|
+
const cacheNode = parent[key], children = getStoreNodechildren(node);
|
|
113
|
+
if (cacheNode && children === cacheNode.children) {
|
|
114
|
+
for (const [key2, child] of entries(children)) {
|
|
115
|
+
compareStoreWithCache(child, cacheNode.record, key2, [...path, key2]);
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
parent[key] = newCacheNode(children);
|
|
119
|
+
CurrentUpdates.push({ path, value: node });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function captureStoreUpdates(store) {
|
|
123
|
+
if (web.isServer || !(solidJs.$TRACK in store)) {
|
|
124
|
+
if (web.isDev) {
|
|
125
|
+
console.warn("createStoreDelta expects a store, got", store);
|
|
126
|
+
}
|
|
127
|
+
let init = true;
|
|
128
|
+
return () => init ? (init = false, [{ path: [], value: store }]) : [];
|
|
129
|
+
}
|
|
130
|
+
const cache = {};
|
|
131
|
+
return () => {
|
|
132
|
+
CurrentUpdates = [];
|
|
133
|
+
SeenNodes = /* @__PURE__ */ new WeakSet();
|
|
134
|
+
compareStoreWithCache(store, cache, "root", []);
|
|
135
|
+
return CurrentUpdates;
|
|
136
|
+
};
|
|
21
137
|
}
|
|
22
138
|
|
|
23
|
-
exports.
|
|
139
|
+
exports.captureStoreUpdates = captureStoreUpdates;
|
|
140
|
+
exports.trackDeep = trackDeep;
|
|
141
|
+
exports.trackStore = trackStore;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,80 @@
|
|
|
1
1
|
import { Store } from 'solid-js/store';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* export function deepTrack<T>(
|
|
7
|
-
* store: Store<T>
|
|
8
|
-
* ): T;
|
|
9
|
-
* ```
|
|
4
|
+
* Tracks all properties of a {@link store} by iterating over them recursively.
|
|
5
|
+
*
|
|
10
6
|
* @param store reactive store dependency
|
|
11
|
-
* @returns same
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* );
|
|
7
|
+
* @returns same {@link store} that was passed in
|
|
8
|
+
*
|
|
9
|
+
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#trackDeep
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* createEffect(on(
|
|
14
|
+
* () => trackDeep(store),
|
|
15
|
+
* () => {
|
|
16
|
+
* // this effect will run when any property of store changes
|
|
17
|
+
* }
|
|
18
|
+
* ));
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function trackDeep<T extends Store<object>>(store: T): T;
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Renamed to {@link trackDeep}
|
|
24
|
+
*/
|
|
25
|
+
declare const deepTrack: typeof trackDeep;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Tracks all nested changes to passed {@link store}.
|
|
29
|
+
*
|
|
30
|
+
* @param store a reactive store proxy
|
|
31
|
+
* @returns same {@link store} that was passed in
|
|
32
|
+
*
|
|
33
|
+
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#trackStore
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* createEffect(on(
|
|
38
|
+
* () => trackStore(store),
|
|
39
|
+
* () => {
|
|
40
|
+
* // this effect will run when any property of store changes
|
|
41
|
+
* }
|
|
42
|
+
* ));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare function trackStore<T extends object>(store: Store<T>): T;
|
|
46
|
+
|
|
47
|
+
type Static<T = unknown> = {
|
|
48
|
+
[K in number | string]: T;
|
|
49
|
+
} | T[];
|
|
50
|
+
type AllNestedObjects<T> = T extends Static<infer U> ? AllNestedObjects<U> | T : never;
|
|
51
|
+
type NestedUpdate<T> = {
|
|
52
|
+
path: (string | number)[];
|
|
53
|
+
value: AllNestedObjects<T>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Creates a function for tracking and capturing updates to a {@link store}.
|
|
57
|
+
*
|
|
58
|
+
* Each execution of the returned function will return an array of updates to the store since the last execution.
|
|
59
|
+
*
|
|
60
|
+
* @param store - The store to track.
|
|
61
|
+
* @returns A function that returns an array of updates to the store since the last execution.
|
|
62
|
+
*
|
|
63
|
+
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#captureStoreUpdates
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const [state, setState] = createStore({ todos: [] });
|
|
68
|
+
*
|
|
69
|
+
* const getDelta = captureStoreUpdates(state);
|
|
70
|
+
*
|
|
71
|
+
* getDelta(); // [{ path: [], value: { todos: [] } }]
|
|
72
|
+
*
|
|
73
|
+
* setState("todos", ["foo"]);
|
|
74
|
+
*
|
|
75
|
+
* getDelta(); // [{ path: ["todos"], value: ["foo"] }]
|
|
23
76
|
* ```
|
|
24
77
|
*/
|
|
25
|
-
declare function
|
|
78
|
+
declare function captureStoreUpdates<T extends Static>(store: T): () => NestedUpdate<T>[];
|
|
26
79
|
|
|
27
|
-
export { deepTrack };
|
|
80
|
+
export { AllNestedObjects, NestedUpdate, captureStoreUpdates, deepTrack, trackDeep, trackStore };
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,137 @@
|
|
|
1
|
-
import { $PROXY } from 'solid-js';
|
|
1
|
+
import { $PROXY, $TRACK, createRoot, createSignal, createMemo, untrack } from 'solid-js';
|
|
2
|
+
import { unwrap } from 'solid-js/store';
|
|
3
|
+
import { createLazyMemo as createLazyMemo$1 } from '@solid-primitives/memo';
|
|
4
|
+
import { isServer, isDev } from 'solid-js/web';
|
|
2
5
|
|
|
3
|
-
// src/
|
|
4
|
-
function
|
|
5
|
-
|
|
6
|
+
// src/track-deep.ts
|
|
7
|
+
function trackDeep(store) {
|
|
8
|
+
traverse(store, /* @__PURE__ */ new Set());
|
|
9
|
+
return store;
|
|
6
10
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
11
|
+
function traverse(value, seen) {
|
|
12
|
+
let isArray;
|
|
13
|
+
let proto;
|
|
14
|
+
if (value != null && typeof value === "object" && !seen.has(value) && ((isArray = Array.isArray(value)) || value[$PROXY] || !(proto = Object.getPrototypeOf(value)) || proto === Object.prototype)) {
|
|
9
15
|
seen.add(value);
|
|
10
|
-
for (const
|
|
11
|
-
|
|
16
|
+
for (const child of isArray ? value : Object.values(value))
|
|
17
|
+
traverse(child, seen);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
var deepTrack = trackDeep;
|
|
21
|
+
var EQUALS_FALSE = { equals: false };
|
|
22
|
+
var TrackStoreCache = /* @__PURE__ */ new WeakMap();
|
|
23
|
+
var TrackVersion = 0;
|
|
24
|
+
function createLazyMemo(calc) {
|
|
25
|
+
let isReading = false, isStale = true, alreadyTracked = false, version = 0;
|
|
26
|
+
const [track, trigger] = createSignal(void 0, EQUALS_FALSE), memo = createMemo(() => isReading ? calc() : isStale = !track(), void 0, {
|
|
27
|
+
equals: () => alreadyTracked
|
|
28
|
+
});
|
|
29
|
+
return () => {
|
|
30
|
+
isReading = true;
|
|
31
|
+
if (isStale)
|
|
32
|
+
isStale = trigger();
|
|
33
|
+
alreadyTracked = version === TrackVersion;
|
|
34
|
+
version = TrackVersion;
|
|
35
|
+
memo();
|
|
36
|
+
isReading = false;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function getTrackStoreNode(node) {
|
|
40
|
+
let track = TrackStoreCache.get(node);
|
|
41
|
+
if (!track) {
|
|
42
|
+
createRoot(() => {
|
|
43
|
+
const unwrapped = unwrap(node);
|
|
44
|
+
track = createLazyMemo(() => {
|
|
45
|
+
node[$TRACK];
|
|
46
|
+
for (const [key, child] of Object.entries(unwrapped)) {
|
|
47
|
+
let childNode;
|
|
48
|
+
if (child != null && typeof child === "object" && ((childNode = child[$PROXY]) || (childNode = untrack(() => node[key])) && $TRACK in childNode)) {
|
|
49
|
+
getTrackStoreNode(childNode)?.();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
TrackStoreCache.set(node, track);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return track;
|
|
57
|
+
}
|
|
58
|
+
function trackStore(store) {
|
|
59
|
+
TrackVersion++;
|
|
60
|
+
$TRACK in store && getTrackStoreNode(store)?.();
|
|
61
|
+
return store;
|
|
62
|
+
}
|
|
63
|
+
function* entries(obj) {
|
|
64
|
+
if (Array.isArray(obj)) {
|
|
65
|
+
for (let i = 0; i < obj.length; i++) {
|
|
66
|
+
yield [i, obj[i]];
|
|
12
67
|
}
|
|
68
|
+
} else {
|
|
69
|
+
yield* Object.entries(obj)[Symbol.iterator]();
|
|
13
70
|
}
|
|
14
|
-
return value;
|
|
15
71
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
72
|
+
var StoreNodeChildrenCache = /* @__PURE__ */ new WeakMap();
|
|
73
|
+
function getStoreNodechildren(node) {
|
|
74
|
+
let signal = StoreNodeChildrenCache.get(node);
|
|
75
|
+
if (!signal) {
|
|
76
|
+
const unwrapped = unwrap(node), isArray = Array.isArray(unwrapped);
|
|
77
|
+
signal = createRoot(
|
|
78
|
+
() => createLazyMemo$1(() => {
|
|
79
|
+
node[$TRACK];
|
|
80
|
+
const children = isArray ? [] : {};
|
|
81
|
+
for (const [key, child] of entries(unwrapped)) {
|
|
82
|
+
let childNode;
|
|
83
|
+
if (child != null && typeof child === "object" && ((childNode = child[$PROXY]) || (childNode = untrack(() => node[key])) && $TRACK in childNode)) {
|
|
84
|
+
children[key] = childNode;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return children;
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
StoreNodeChildrenCache.set(node, signal);
|
|
91
|
+
}
|
|
92
|
+
return signal();
|
|
93
|
+
}
|
|
94
|
+
var CurrentUpdates;
|
|
95
|
+
var SeenNodes;
|
|
96
|
+
function newCacheNode(children) {
|
|
97
|
+
const record = { ...children };
|
|
98
|
+
for (const [key, node] of entries(children)) {
|
|
99
|
+
if (SeenNodes.has(node))
|
|
100
|
+
continue;
|
|
101
|
+
SeenNodes.add(node);
|
|
102
|
+
record[key] = newCacheNode(getStoreNodechildren(node));
|
|
103
|
+
}
|
|
104
|
+
return { children, record };
|
|
105
|
+
}
|
|
106
|
+
function compareStoreWithCache(node, parent, key, path) {
|
|
107
|
+
if (SeenNodes.has(node))
|
|
108
|
+
return;
|
|
109
|
+
SeenNodes.add(node);
|
|
110
|
+
const cacheNode = parent[key], children = getStoreNodechildren(node);
|
|
111
|
+
if (cacheNode && children === cacheNode.children) {
|
|
112
|
+
for (const [key2, child] of entries(children)) {
|
|
113
|
+
compareStoreWithCache(child, cacheNode.record, key2, [...path, key2]);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
parent[key] = newCacheNode(children);
|
|
117
|
+
CurrentUpdates.push({ path, value: node });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function captureStoreUpdates(store) {
|
|
121
|
+
if (isServer || !($TRACK in store)) {
|
|
122
|
+
if (isDev) {
|
|
123
|
+
console.warn("createStoreDelta expects a store, got", store);
|
|
124
|
+
}
|
|
125
|
+
let init = true;
|
|
126
|
+
return () => init ? (init = false, [{ path: [], value: store }]) : [];
|
|
127
|
+
}
|
|
128
|
+
const cache = {};
|
|
129
|
+
return () => {
|
|
130
|
+
CurrentUpdates = [];
|
|
131
|
+
SeenNodes = /* @__PURE__ */ new WeakSet();
|
|
132
|
+
compareStoreWithCache(store, cache, "root", []);
|
|
133
|
+
return CurrentUpdates;
|
|
134
|
+
};
|
|
19
135
|
}
|
|
20
136
|
|
|
21
|
-
export { deepTrack };
|
|
137
|
+
export { captureStoreUpdates, deepTrack, trackDeep, trackStore };
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/deep",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Primitives for tracking and observing nested reactive objects in Solid.",
|
|
5
5
|
"author": "Samuel Burbano <me@iosamuel.dev>",
|
|
6
|
-
"contributors": [
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Damian Tarnawski <gthetarnav@gmail.com>"
|
|
8
|
+
],
|
|
7
9
|
"license": "MIT",
|
|
8
10
|
"homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#readme",
|
|
9
11
|
"repository": {
|
|
@@ -15,16 +17,21 @@
|
|
|
15
17
|
},
|
|
16
18
|
"primitive": {
|
|
17
19
|
"name": "deep",
|
|
18
|
-
"stage":
|
|
20
|
+
"stage": 1,
|
|
19
21
|
"list": [
|
|
20
|
-
"
|
|
22
|
+
"trackDeep",
|
|
23
|
+
"trackStore",
|
|
24
|
+
"captureStoreUpdates"
|
|
21
25
|
],
|
|
22
26
|
"category": "Reactivity"
|
|
23
27
|
},
|
|
24
28
|
"keywords": [
|
|
25
29
|
"solid",
|
|
26
30
|
"primitives",
|
|
27
|
-
"deep"
|
|
31
|
+
"deep",
|
|
32
|
+
"store",
|
|
33
|
+
"reactivity",
|
|
34
|
+
"delta"
|
|
28
35
|
],
|
|
29
36
|
"private": false,
|
|
30
37
|
"sideEffects": false,
|
|
@@ -43,15 +50,21 @@
|
|
|
43
50
|
},
|
|
44
51
|
"require": "./dist/index.cjs"
|
|
45
52
|
},
|
|
53
|
+
"typesVersions": {},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@solid-primitives/memo": "^1.3.1"
|
|
56
|
+
},
|
|
46
57
|
"peerDependencies": {
|
|
47
58
|
"solid-js": "^1.6.12"
|
|
48
59
|
},
|
|
49
|
-
"
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"solid-js": "1.7.5"
|
|
62
|
+
},
|
|
50
63
|
"scripts": {
|
|
51
|
-
"dev": "
|
|
52
|
-
"page": "vite build dev",
|
|
64
|
+
"dev": "jiti ../../scripts/dev.ts",
|
|
53
65
|
"build": "jiti ../../scripts/build.ts",
|
|
54
|
-
"
|
|
55
|
-
"test
|
|
66
|
+
"vitest": "vitest -c ../../configs/vitest.config.ts",
|
|
67
|
+
"test": "pnpm run vitest",
|
|
68
|
+
"test:ssr": "pnpm run vitest --mode ssr"
|
|
56
69
|
}
|
|
57
70
|
}
|