@octanejs/valtio 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/LICENSE +21 -0
- package/README.md +36 -0
- package/package.json +57 -0
- package/src/index.ts +3 -0
- package/src/internal.ts +26 -0
- package/src/react/utils.ts +36 -0
- package/src/react.ts +75 -0
- package/src/vanilla/utils.ts +1 -0
- package/src/vanilla.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dominic Gannaway
|
|
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,36 @@
|
|
|
1
|
+
# @octanejs/valtio
|
|
2
|
+
|
|
3
|
+
[Valtio](https://github.com/pmndrs/valtio) for the
|
|
4
|
+
[Octane](https://github.com/octanejs/octane) UI framework.
|
|
5
|
+
|
|
6
|
+
The package reuses Valtio's framework-independent `valtio/vanilla` core and
|
|
7
|
+
reimplements only `useSnapshot` with Octane hooks. Existing Valtio components
|
|
8
|
+
can usually migrate by changing the import:
|
|
9
|
+
|
|
10
|
+
```tsx
|
|
11
|
+
import { proxy, useSnapshot } from '@octanejs/valtio';
|
|
12
|
+
|
|
13
|
+
const state = proxy({ count: 0 });
|
|
14
|
+
|
|
15
|
+
function Counter() @{
|
|
16
|
+
const snap = useSnapshot(state);
|
|
17
|
+
<button onClick={() => state.count++}>{snap.count as string}</button>
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Entry points
|
|
22
|
+
|
|
23
|
+
| import | what you get |
|
|
24
|
+
| --- | --- |
|
|
25
|
+
| `@octanejs/valtio` | Valtio's vanilla API plus the Octane-bound `useSnapshot` |
|
|
26
|
+
| `@octanejs/valtio/react` | the Octane-bound `useSnapshot` |
|
|
27
|
+
| `@octanejs/valtio/react/utils` | `useProxy`, for one proxy reference in renders and callbacks |
|
|
28
|
+
| `@octanejs/valtio/vanilla` | `proxy`, `snapshot`, `subscribe`, `ref`, and vanilla types |
|
|
29
|
+
| `@octanejs/valtio/vanilla/utils` | `subscribeKey`, `watch`, `devtools`, proxy collections, and other vanilla utilities |
|
|
30
|
+
|
|
31
|
+
`useSnapshot` retains Valtio's property-access tracking through `proxy-compare`.
|
|
32
|
+
Read from the returned snapshot and mutate the source proxy. The optional
|
|
33
|
+
`{ sync: true }` subscription mode is supported.
|
|
34
|
+
|
|
35
|
+
Current scope and verification status are tracked in
|
|
36
|
+
[`status.json`](./status.json).
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@octanejs/valtio",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22"
|
|
8
|
+
},
|
|
9
|
+
"octane": {
|
|
10
|
+
"hookSlots": {
|
|
11
|
+
"manual": [
|
|
12
|
+
"src"
|
|
13
|
+
]
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"description": "Valtio bindings for the Octane renderer, built on valtio/vanilla with an Octane-native useSnapshot hook.",
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Dominic Gannaway",
|
|
19
|
+
"email": "dg@domgan.com"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/octanejs/octane.git",
|
|
27
|
+
"directory": "packages/valtio"
|
|
28
|
+
},
|
|
29
|
+
"main": "src/index.ts",
|
|
30
|
+
"module": "src/index.ts",
|
|
31
|
+
"types": "src/index.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"src",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./src/index.ts",
|
|
38
|
+
"./react": "./src/react.ts",
|
|
39
|
+
"./react/utils": "./src/react/utils.ts",
|
|
40
|
+
"./vanilla": "./src/vanilla.ts",
|
|
41
|
+
"./vanilla/utils": "./src/vanilla/utils.ts"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"proxy-compare": "^3.0.1",
|
|
45
|
+
"valtio": "^2.3.2"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"octane": "0.1.17"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"vitest": "^4.1.10",
|
|
52
|
+
"octane": "0.1.17"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"test": "vitest run --root ../.. --project valtio"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/index.ts
ADDED
package/src/internal.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const subSlotCache = new Map<symbol, Map<string, symbol>>();
|
|
2
|
+
const bareTagCache = new Map<string, symbol>();
|
|
3
|
+
|
|
4
|
+
export function subSlot(slot: symbol | undefined, tag: string): symbol {
|
|
5
|
+
if (slot === undefined) {
|
|
6
|
+
let bare = bareTagCache.get(tag);
|
|
7
|
+
if (bare === undefined) {
|
|
8
|
+
bare = Symbol.for(`@octanejs/valtio:${tag}`);
|
|
9
|
+
bareTagCache.set(tag, bare);
|
|
10
|
+
}
|
|
11
|
+
return bare;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let byTag = subSlotCache.get(slot);
|
|
15
|
+
if (byTag === undefined) {
|
|
16
|
+
byTag = new Map();
|
|
17
|
+
subSlotCache.set(slot, byTag);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let child = byTag.get(tag);
|
|
21
|
+
if (child === undefined) {
|
|
22
|
+
child = Symbol.for(`${slot.description ?? ''}:${tag}`);
|
|
23
|
+
byTag.set(tag, child);
|
|
24
|
+
}
|
|
25
|
+
return child;
|
|
26
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { useLayoutEffect } from 'octane';
|
|
2
|
+
import { subSlot } from '../internal';
|
|
3
|
+
import { useSnapshot } from '../react';
|
|
4
|
+
import type { UseSnapshotOptions } from '../react';
|
|
5
|
+
|
|
6
|
+
const DUMMY_SYMBOL = Symbol();
|
|
7
|
+
|
|
8
|
+
export function useProxy<T extends object>(
|
|
9
|
+
proxyObject: T,
|
|
10
|
+
...rest: [options?: UseSnapshotOptions, slot?: symbol]
|
|
11
|
+
): T {
|
|
12
|
+
const tail = rest[rest.length - 1];
|
|
13
|
+
const slot = typeof tail === 'symbol' ? tail : undefined;
|
|
14
|
+
const options = (typeof rest[0] === 'object' ? rest[0] : undefined) as
|
|
15
|
+
UseSnapshotOptions | undefined;
|
|
16
|
+
const currentSnapshot = useSnapshot(proxyObject, options, subSlot(slot, 'snapshot')) as T;
|
|
17
|
+
|
|
18
|
+
// Register an empty access so a component that reads only in callbacks does
|
|
19
|
+
// not subscribe to every property.
|
|
20
|
+
currentSnapshot[DUMMY_SYMBOL as keyof T];
|
|
21
|
+
|
|
22
|
+
let isRendering = true;
|
|
23
|
+
useLayoutEffect(
|
|
24
|
+
() => {
|
|
25
|
+
isRendering = false;
|
|
26
|
+
},
|
|
27
|
+
null,
|
|
28
|
+
subSlot(slot, 'render-effect'),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return new Proxy(proxyObject, {
|
|
32
|
+
get(target, property) {
|
|
33
|
+
return isRendering ? currentSnapshot[property as keyof T] : target[property as keyof T];
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
package/src/react.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useMemo, useRef, useSyncExternalStore } from 'octane';
|
|
2
|
+
import { createProxy as createProxyToCompare, isChanged } from 'proxy-compare';
|
|
3
|
+
import { snapshot, subscribe } from 'valtio/vanilla';
|
|
4
|
+
import type { Snapshot } from 'valtio/vanilla';
|
|
5
|
+
import { subSlot } from './internal';
|
|
6
|
+
|
|
7
|
+
export type UseSnapshotOptions = {
|
|
8
|
+
sync?: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const targetCache = new WeakMap<object, unknown>();
|
|
12
|
+
|
|
13
|
+
export function useSnapshot<T extends object>(
|
|
14
|
+
proxyObject: T,
|
|
15
|
+
...rest: [options?: UseSnapshotOptions, slot?: symbol]
|
|
16
|
+
): Snapshot<T> {
|
|
17
|
+
const tail = rest[rest.length - 1];
|
|
18
|
+
const slot = typeof tail === 'symbol' ? tail : undefined;
|
|
19
|
+
const options = (typeof rest[0] === 'object' ? rest[0] : undefined) as
|
|
20
|
+
UseSnapshotOptions | undefined;
|
|
21
|
+
const notifyInSync = options?.sync;
|
|
22
|
+
|
|
23
|
+
const affected = useMemo(
|
|
24
|
+
() => new WeakMap<object, unknown>(),
|
|
25
|
+
[proxyObject],
|
|
26
|
+
subSlot(slot, 'affected'),
|
|
27
|
+
);
|
|
28
|
+
const lastSnapshot = useRef<Snapshot<T> | undefined>(undefined, subSlot(slot, 'last-snapshot'));
|
|
29
|
+
let inRender = true;
|
|
30
|
+
|
|
31
|
+
const currSnapshot = useSyncExternalStore(
|
|
32
|
+
useCallback(
|
|
33
|
+
(callback) => {
|
|
34
|
+
const unsubscribe = subscribe(proxyObject, callback, notifyInSync);
|
|
35
|
+
callback();
|
|
36
|
+
return unsubscribe;
|
|
37
|
+
},
|
|
38
|
+
[proxyObject, notifyInSync],
|
|
39
|
+
subSlot(slot, 'subscribe'),
|
|
40
|
+
),
|
|
41
|
+
() => {
|
|
42
|
+
const nextSnapshot = snapshot(proxyObject);
|
|
43
|
+
try {
|
|
44
|
+
if (
|
|
45
|
+
!inRender &&
|
|
46
|
+
lastSnapshot.current &&
|
|
47
|
+
!isChanged(lastSnapshot.current, nextSnapshot, affected, new WeakMap())
|
|
48
|
+
) {
|
|
49
|
+
return lastSnapshot.current;
|
|
50
|
+
}
|
|
51
|
+
} catch {
|
|
52
|
+
// Accessed promises and other throwing values must not break snapshots.
|
|
53
|
+
}
|
|
54
|
+
return nextSnapshot;
|
|
55
|
+
},
|
|
56
|
+
() => snapshot(proxyObject),
|
|
57
|
+
subSlot(slot, 'external-store'),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
inRender = false;
|
|
61
|
+
useLayoutEffect(
|
|
62
|
+
() => {
|
|
63
|
+
lastSnapshot.current = currSnapshot;
|
|
64
|
+
},
|
|
65
|
+
null,
|
|
66
|
+
subSlot(slot, 'last-snapshot-effect'),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const proxyCache = useMemo(
|
|
70
|
+
() => new WeakMap<object, unknown>(),
|
|
71
|
+
[],
|
|
72
|
+
subSlot(slot, 'proxy-cache'),
|
|
73
|
+
);
|
|
74
|
+
return createProxyToCompare(currSnapshot, affected, proxyCache, targetCache) as Snapshot<T>;
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'valtio/vanilla/utils';
|
package/src/vanilla.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from 'valtio/vanilla';
|