@firsthandjs/deep 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/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Firsthand contributors
|
|
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
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @firsthandjs/deep
|
|
2
|
+
|
|
3
|
+
Deep reactivity for Firsthand: a proxy where every property, at any depth, is a
|
|
4
|
+
signal — the shape Vue calls `reactive()`.
|
|
5
|
+
|
|
6
|
+
**Documentation:** [guide](https://github.com/firsthandjs/firsthand/blob/main/docs/guide/02-reactivity.md#deep-state) · [API reference](https://github.com/firsthandjs/firsthand/blob/main/docs/reference/deep.md) · [all docs](https://github.com/firsthandjs/firsthand/blob/main/docs/README.md)
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
npm install @firsthandjs/deep
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
0.72 kB gzip. It depends on `@firsthandjs/core` and changes nothing about
|
|
13
|
+
`signal`.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { effect } from '@firsthandjs/core';
|
|
17
|
+
import { deepSignal } from '@firsthandjs/deep';
|
|
18
|
+
|
|
19
|
+
const state = deepSignal({ user: { name: 'Ada' }, todos: [] as string[] });
|
|
20
|
+
|
|
21
|
+
effect(() => console.log(state.user.name)); // subscribes to that property
|
|
22
|
+
state.user.name = 'Grace'; // and only that effect re-runs
|
|
23
|
+
state.todos.push('write the docs'); // arrays too, as one update
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Why it exists
|
|
27
|
+
|
|
28
|
+
A signal holds one value and notices when that value is **replaced**:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const form = signal({ user: { name: 'Ada' } });
|
|
32
|
+
form.value.user.name = 'Grace'; // invisible: the signal holds the same object
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Replacing the object is right for small state and tedious for a form, a
|
|
36
|
+
document or a settings tree. `deepSignal` tracks per property instead, so an
|
|
37
|
+
effect that read `state.user.name` ignores a change to `state.user.age`, and a
|
|
38
|
+
write to a property nobody has read costs nothing at all.
|
|
39
|
+
|
|
40
|
+
## Only objects and arrays
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
deepSignal(new Map()); // compile error
|
|
44
|
+
deepSignal(new Date()); // compile error
|
|
45
|
+
deepSignal(new Point()); // compile error
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Those read their own internals through `this`, and inside a proxy `this` is the
|
|
49
|
+
proxy — a proxied `Map` throws on `get`. Keep them in a `signal` and replace
|
|
50
|
+
them on each edit; one held inside deep state still works, it is simply not
|
|
51
|
+
reactive itself.
|
|
52
|
+
|
|
53
|
+
## The rest of the surface
|
|
54
|
+
|
|
55
|
+
| Export | What it does |
|
|
56
|
+
| ----------------- | -------------------------------------------------- |
|
|
57
|
+
| `deepSignal(obj)` | The proxy. Same type in, same type out |
|
|
58
|
+
| `raw(value)` | The object behind a proxy, for clones and requests |
|
|
59
|
+
| `isDeep(value)` | Whether something is a proxy this package made |
|
|
60
|
+
|
|
61
|
+
Full rules — `Object.keys`, `in`, `delete`, every array mutator, identity — are
|
|
62
|
+
in the [API reference](https://github.com/firsthandjs/firsthand/blob/main/docs/reference/deep.md),
|
|
63
|
+
and the reasoning is in [ADR-0018](https://github.com/firsthandjs/firsthand/blob/main/docs/adr/0018-deep-reactivity-as-its-own-package.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What may be made deeply reactive: an object or an array.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately not `T extends object`. A function, a `Date`, a `Map`, a
|
|
5
|
+
* `Promise` and a class instance all satisfy `object`, and proxying them
|
|
6
|
+
* silently breaks them in ways that only show up at runtime — a `Map` reads its
|
|
7
|
+
* internal slots through `this`, which a proxy is not. Restricting the type
|
|
8
|
+
* makes that a compile error instead of a bug.
|
|
9
|
+
*/
|
|
10
|
+
export type Deep = Record<PropertyKey, unknown> | unknown[];
|
|
11
|
+
/**
|
|
12
|
+
* Makes an object or array deeply reactive.
|
|
13
|
+
*
|
|
14
|
+
* Returns a proxy with the same shape as what you passed: read it, write it,
|
|
15
|
+
* spread it, iterate it. Reads inside an `effect`, a `computed` or a DOM
|
|
16
|
+
* binding subscribe to exactly the properties they touched; writes notify
|
|
17
|
+
* exactly those readers.
|
|
18
|
+
*
|
|
19
|
+
* Calling it twice with the same object returns the same proxy, and calling it
|
|
20
|
+
* on a proxy returns that proxy, so identity is stable and `===` means what it
|
|
21
|
+
* looks like.
|
|
22
|
+
*/
|
|
23
|
+
export declare function deepSignal<T extends Deep>(value: T): T;
|
|
24
|
+
/**
|
|
25
|
+
* The object a deep proxy wraps, or the value itself if it is not one.
|
|
26
|
+
*
|
|
27
|
+
* For handing state to something that must not be tracked — a structured
|
|
28
|
+
* clone, a `fetch` body, a library that stores what it is given.
|
|
29
|
+
*/
|
|
30
|
+
export declare function raw<T>(value: T): T;
|
|
31
|
+
/** Whether a value is a proxy this package created. */
|
|
32
|
+
export declare function isDeep(value: unknown): boolean;
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAkCA;;;;;;;;GAQG;AACH,MAAM,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,OAAO,EAAE,CAAC;AAgK5D;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,IAAI,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAStD;AAED;;;;;GAKG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAMlC;AAED,uDAAuD;AACvD,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAI9C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{batch as b,signal as h}from"@firsthandjs/core";var p=Symbol.for("firsthand.deep.raw"),y=new WeakMap,f=new WeakMap,u=Symbol.for("firsthand.deep.keys");function j(n,e){let t=f.get(n);t===void 0&&(t=new Map,f.set(n,t));let o=t.get(e);return o===void 0&&(o=h(0),t.set(e,o)),o}function c(n,e){j(n,e).value}function r(n,e){let t=f.get(n)?.get(e);t!==void 0&&t.value++}function A(n){if(typeof n!="object"||n===null)return!1;if(Array.isArray(n))return!0;let e=Object.getPrototypeOf(n);return e===Object.prototype||e===null}var P=new Set(["push","pop","shift","unshift","splice","sort","reverse","fill","copyWithin"]),m={get(n,e,t){if(e===p)return n;let o=Reflect.get(n,e,t);return typeof e=="symbol"?o:Array.isArray(n)&&typeof o=="function"&&P.has(e)?function(...i){return b(()=>o.apply(t,i))}:(c(n,e),A(o)?R(o):o)},set(n,e,t,o){let s=Array.isArray(n),i=Object.prototype.hasOwnProperty.call(n,e),a=Reflect.get(n,e,o),w=s?n.length:0,l=d(t);return Reflect.set(n,e,l,o)?(i?Object.is(a,l)||r(n,e):(r(n,u),r(n,e)),s&&e!=="length"&&n.length!==w&&r(n,"length"),!0):!1},deleteProperty(n,e){let t=Object.prototype.hasOwnProperty.call(n,e),o=Reflect.deleteProperty(n,e);return o&&t&&(r(n,e),r(n,u)),o},has(n,e){return typeof e!="symbol"&&c(n,e),Reflect.has(n,e)},ownKeys(n){return c(n,Array.isArray(n)?"length":u),Reflect.ownKeys(n)}};function R(n){let e=d(n),t=y.get(e);if(t!==void 0)return t;let o=new Proxy(e,m);return y.set(e,o),o}function d(n){if(typeof n!="object"||n===null)return n;let e=n[p];return e===void 0?n:e}function O(n){return typeof n=="object"&&n!==null&&n[p]!==void 0}export{R as deepSignal,O as isDeep,d as raw};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@firsthandjs/deep",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Deep reactivity for Firsthand: a proxy where every property, at any depth, is a signal.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@firsthandjs/core": "0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.11.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/firsthandjs/firsthand.git",
|
|
34
|
+
"directory": "packages/deep"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/firsthandjs/firsthand/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/firsthandjs/firsthand#readme",
|
|
40
|
+
"keywords": [
|
|
41
|
+
"firsthand",
|
|
42
|
+
"reactive",
|
|
43
|
+
"proxy",
|
|
44
|
+
"deep",
|
|
45
|
+
"state",
|
|
46
|
+
"signals"
|
|
47
|
+
]
|
|
48
|
+
}
|