@solid-primitives/deep 0.0.102 → 0.1.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 +65 -13
- package/dist/index.cjs +55 -13
- package/dist/index.d.ts +38 -18
- package/dist/index.js +55 -14
- package/package.json +14 -9
package/README.md
CHANGED
|
@@ -7,9 +7,12 @@
|
|
|
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 changes in stores.
|
|
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.
|
|
13
16
|
|
|
14
17
|
## Installation
|
|
15
18
|
|
|
@@ -21,31 +24,80 @@ yarn add @solid-primitives/deep
|
|
|
21
24
|
pnpm add @solid-primitives/deep
|
|
22
25
|
```
|
|
23
26
|
|
|
24
|
-
##
|
|
27
|
+
## `trackDeep`
|
|
28
|
+
|
|
29
|
+
Tracks all properties of a store by iterating over them recursively.
|
|
30
|
+
|
|
31
|
+
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.
|
|
32
|
+
|
|
33
|
+
Since it iterates over all properties of a store, it's not recommended to use this on large stores under rapid updates.
|
|
34
|
+
|
|
35
|
+
### How to use it
|
|
25
36
|
|
|
26
|
-
You can
|
|
37
|
+
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
38
|
|
|
28
39
|
```ts
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
40
|
+
import { trackDeep } from "@solid-primitives/deep";
|
|
41
|
+
|
|
42
|
+
const [state, setState] = createSignal({ name: "John", age: 42 });
|
|
43
|
+
|
|
44
|
+
createEffect(() => {
|
|
45
|
+
trackDeep(state);
|
|
46
|
+
/* execute some logic whenever the state changes */
|
|
47
|
+
});
|
|
37
48
|
```
|
|
38
49
|
|
|
39
50
|
Or since this has a composable design, you can create _derivative_ functions and use them similar to derivative signals.
|
|
40
51
|
|
|
41
52
|
```ts
|
|
42
|
-
const deeplyTrackedStore = () =>
|
|
53
|
+
const deeplyTrackedStore = () => trackDeep(sign);
|
|
43
54
|
createEffect(() => {
|
|
44
55
|
console.log("Store is: ", deeplyTrackedStore());
|
|
45
56
|
// ^ this causes a re-execution of the effect on deep changes of properties
|
|
46
57
|
});
|
|
47
58
|
```
|
|
48
59
|
|
|
60
|
+
`trackDeep` will traverse any "wrappable" object _(objects that solid stores will wrap with proxies)_, even if it's not a solid store.
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
createEffect(() => {
|
|
64
|
+
// will also work:
|
|
65
|
+
trackDeep({ myStore: state });
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
> **Warning** If you `unwrap` a store, it will no longer be tracked by `trackDeep` nor `trackStore`!
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const unwrapped = unwrap(state);
|
|
73
|
+
|
|
74
|
+
createEffect(() => {
|
|
75
|
+
// This will NOT work:
|
|
76
|
+
trackDeep(unwrapped);
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## `trackStore`
|
|
81
|
+
|
|
82
|
+
A much more performant alternative to `trackDeep` that is utilizing memoization and specific store implementations of solid stores.
|
|
83
|
+
|
|
84
|
+
You should consider using this instead of other tracking methods, for large stores, stores that are updated rapidly or tracked in many effects.
|
|
85
|
+
|
|
86
|
+
### How to use it
|
|
87
|
+
|
|
88
|
+
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.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
import { trackStore } from "@solid-primitives/deep";
|
|
92
|
+
|
|
93
|
+
const [state, setState] = createSignal({ name: "John", age: 42 });
|
|
94
|
+
|
|
95
|
+
createEffect(() => {
|
|
96
|
+
trackStore(state);
|
|
97
|
+
/* execute some logic whenever the state changes */
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
49
101
|
## Changelog
|
|
50
102
|
|
|
51
103
|
See [CHANGELOG.md](./CHANGELOG.md)
|
package/dist/index.cjs
CHANGED
|
@@ -1,23 +1,65 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var solidJs = require('solid-js');
|
|
4
|
+
var store = require('solid-js/store');
|
|
4
5
|
|
|
5
|
-
// src/
|
|
6
|
-
function
|
|
7
|
-
|
|
6
|
+
// src/track-deep.ts
|
|
7
|
+
function trackDeep(store) {
|
|
8
|
+
traverse(store, /* @__PURE__ */ new Set());
|
|
9
|
+
return store;
|
|
8
10
|
}
|
|
9
|
-
function
|
|
10
|
-
|
|
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[solidJs.$PROXY] || !(proto = Object.getPrototypeOf(value)) || proto === Object.prototype)) {
|
|
11
15
|
seen.add(value);
|
|
12
|
-
for (const
|
|
13
|
-
|
|
14
|
-
}
|
|
16
|
+
for (const child of isArray ? value : Object.values(value))
|
|
17
|
+
traverse(child, seen);
|
|
15
18
|
}
|
|
16
|
-
return value;
|
|
17
19
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
exports.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] = solidJs.createSignal(void 0, EQUALS_FALSE), memo = solidJs.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(value) {
|
|
40
|
+
let track = TrackStoreCache.get(value);
|
|
41
|
+
if (!track) {
|
|
42
|
+
solidJs.createRoot(() => {
|
|
43
|
+
const unwrapped = store.unwrap(value);
|
|
44
|
+
track = createLazyMemo(() => {
|
|
45
|
+
value[solidJs.$TRACK];
|
|
46
|
+
for (const [key, child] of Object.entries(unwrapped)) {
|
|
47
|
+
let obj;
|
|
48
|
+
if (child != null && typeof child === "object" && ((obj = child[solidJs.$PROXY]) || (obj = solidJs.untrack(() => value[key])) && solidJs.$TRACK in obj)) {
|
|
49
|
+
getTrackStoreNode(obj)?.();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
TrackStoreCache.set(value, track);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return track;
|
|
57
|
+
}
|
|
58
|
+
function trackStore(store) {
|
|
59
|
+
TrackVersion++;
|
|
60
|
+
solidJs.$TRACK in store && getTrackStoreNode(store)?.();
|
|
61
|
+
return store;
|
|
21
62
|
}
|
|
22
63
|
|
|
23
|
-
exports.
|
|
64
|
+
exports.trackDeep = trackDeep;
|
|
65
|
+
exports.trackStore = trackStore;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,27 +1,47 @@
|
|
|
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
|
|
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
|
|
12
32
|
*
|
|
13
|
-
*
|
|
14
|
-
* createEffect(
|
|
15
|
-
* on(
|
|
16
|
-
* deepTrack(store),
|
|
17
|
-
* () => {
|
|
18
|
-
* // this effect will run when any property of store changes
|
|
19
|
-
* }
|
|
20
|
-
* )
|
|
33
|
+
* @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/deep#trackStore
|
|
21
34
|
*
|
|
22
|
-
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* createEffect(on(
|
|
38
|
+
* () => trackStore(store),
|
|
39
|
+
* () => {
|
|
40
|
+
* // this effect will run when any property of store changes
|
|
41
|
+
* }
|
|
42
|
+
* ));
|
|
23
43
|
* ```
|
|
24
44
|
*/
|
|
25
|
-
declare function
|
|
45
|
+
declare function trackStore<T extends object>(store: Store<T>): T;
|
|
26
46
|
|
|
27
|
-
export { deepTrack };
|
|
47
|
+
export { deepTrack, trackDeep, trackStore };
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,62 @@
|
|
|
1
|
-
import { $PROXY } from 'solid-js';
|
|
1
|
+
import { $PROXY, $TRACK, createRoot, createSignal, createMemo, untrack } from 'solid-js';
|
|
2
|
+
import { unwrap } from 'solid-js/store';
|
|
2
3
|
|
|
3
|
-
// src/
|
|
4
|
-
function
|
|
5
|
-
|
|
4
|
+
// src/track-deep.ts
|
|
5
|
+
function trackDeep(store) {
|
|
6
|
+
traverse(store, /* @__PURE__ */ new Set());
|
|
7
|
+
return store;
|
|
6
8
|
}
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
+
function traverse(value, seen) {
|
|
10
|
+
let isArray;
|
|
11
|
+
let proto;
|
|
12
|
+
if (value != null && typeof value === "object" && !seen.has(value) && ((isArray = Array.isArray(value)) || value[$PROXY] || !(proto = Object.getPrototypeOf(value)) || proto === Object.prototype)) {
|
|
9
13
|
seen.add(value);
|
|
10
|
-
for (const
|
|
11
|
-
|
|
12
|
-
}
|
|
14
|
+
for (const child of isArray ? value : Object.values(value))
|
|
15
|
+
traverse(child, seen);
|
|
13
16
|
}
|
|
14
|
-
return value;
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
var deepTrack = trackDeep;
|
|
19
|
+
var EQUALS_FALSE = { equals: false };
|
|
20
|
+
var TrackStoreCache = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
var TrackVersion = 0;
|
|
22
|
+
function createLazyMemo(calc) {
|
|
23
|
+
let isReading = false, isStale = true, alreadyTracked = false, version = 0;
|
|
24
|
+
const [track, trigger] = createSignal(void 0, EQUALS_FALSE), memo = createMemo(() => isReading ? calc() : isStale = !track(), void 0, {
|
|
25
|
+
equals: () => alreadyTracked
|
|
26
|
+
});
|
|
27
|
+
return () => {
|
|
28
|
+
isReading = true;
|
|
29
|
+
if (isStale)
|
|
30
|
+
isStale = trigger();
|
|
31
|
+
alreadyTracked = version === TrackVersion;
|
|
32
|
+
version = TrackVersion;
|
|
33
|
+
memo();
|
|
34
|
+
isReading = false;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function getTrackStoreNode(value) {
|
|
38
|
+
let track = TrackStoreCache.get(value);
|
|
39
|
+
if (!track) {
|
|
40
|
+
createRoot(() => {
|
|
41
|
+
const unwrapped = unwrap(value);
|
|
42
|
+
track = createLazyMemo(() => {
|
|
43
|
+
value[$TRACK];
|
|
44
|
+
for (const [key, child] of Object.entries(unwrapped)) {
|
|
45
|
+
let obj;
|
|
46
|
+
if (child != null && typeof child === "object" && ((obj = child[$PROXY]) || (obj = untrack(() => value[key])) && $TRACK in obj)) {
|
|
47
|
+
getTrackStoreNode(obj)?.();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
TrackStoreCache.set(value, track);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return track;
|
|
55
|
+
}
|
|
56
|
+
function trackStore(store) {
|
|
57
|
+
TrackVersion++;
|
|
58
|
+
$TRACK in store && getTrackStoreNode(store)?.();
|
|
59
|
+
return store;
|
|
19
60
|
}
|
|
20
61
|
|
|
21
|
-
export { deepTrack };
|
|
62
|
+
export { deepTrack, trackDeep, trackStore };
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solid-primitives/deep",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Deep trigger effects for Stores.",
|
|
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,19 @@
|
|
|
15
17
|
},
|
|
16
18
|
"primitive": {
|
|
17
19
|
"name": "deep",
|
|
18
|
-
"stage":
|
|
20
|
+
"stage": 1,
|
|
19
21
|
"list": [
|
|
20
|
-
"
|
|
22
|
+
"trackDeep",
|
|
23
|
+
"trackStore"
|
|
21
24
|
],
|
|
22
25
|
"category": "Reactivity"
|
|
23
26
|
},
|
|
24
27
|
"keywords": [
|
|
25
28
|
"solid",
|
|
26
29
|
"primitives",
|
|
27
|
-
"deep"
|
|
30
|
+
"deep",
|
|
31
|
+
"store",
|
|
32
|
+
"reactivity"
|
|
28
33
|
],
|
|
29
34
|
"private": false,
|
|
30
35
|
"sideEffects": false,
|
|
@@ -48,10 +53,10 @@
|
|
|
48
53
|
},
|
|
49
54
|
"typesVersions": {},
|
|
50
55
|
"scripts": {
|
|
51
|
-
"dev": "
|
|
52
|
-
"page": "vite build dev",
|
|
56
|
+
"dev": "jiti ../../scripts/dev.ts",
|
|
53
57
|
"build": "jiti ../../scripts/build.ts",
|
|
54
|
-
"
|
|
55
|
-
"test
|
|
58
|
+
"vitest": "vitest -c ../../configs/vitest.config.ts",
|
|
59
|
+
"test": "pnpm run vitest",
|
|
60
|
+
"test:ssr": "pnpm run vitest --mode ssr"
|
|
56
61
|
}
|
|
57
62
|
}
|