@pyreon/reactivity 0.14.0 → 0.16.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 +4 -1
- package/lib/analysis/index.js.html +1 -1
- package/lib/index.js +230 -47
- package/lib/types/index.d.ts +95 -3
- package/package.json +5 -4
- package/src/batch.ts +155 -32
- package/src/computed.ts +21 -6
- package/src/createSelector.ts +44 -12
- package/src/effect.ts +149 -14
- package/src/env.d.ts +6 -0
- package/src/index.ts +18 -3
- package/src/manifest.ts +372 -5
- package/src/reconcile.ts +9 -1
- package/src/resource.ts +19 -1
- package/src/scope.ts +38 -0
- package/src/signal.ts +29 -12
- package/src/store.ts +111 -11
- package/src/tests/batch.test.ts +605 -0
- package/src/tests/computed.test.ts +86 -0
- package/src/tests/createSelector.test.ts +59 -0
- package/src/tests/effect.test.ts +65 -0
- package/src/tests/fanout-repro.test.ts +179 -0
- package/src/tests/manifest-snapshot.test.ts +17 -1
- package/src/tests/resource.test.ts +93 -0
- package/src/tests/scope.test.ts +29 -0
- package/src/tests/signal.test.ts +108 -0
- package/src/tests/store.test.ts +54 -0
- package/src/tests/vue-parity.test.ts +191 -0
- package/lib/index.js.map +0 -1
- package/lib/types/index.d.ts.map +0 -1
package/src/store.ts
CHANGED
|
@@ -17,8 +17,60 @@ import { type Signal, signal } from './signal'
|
|
|
17
17
|
|
|
18
18
|
// WeakMap: raw object → its reactive proxy (ensures each raw object gets one proxy)
|
|
19
19
|
const proxyCache = new WeakMap<object, object>()
|
|
20
|
+
// Separate cache for shallow proxies — same raw can produce different proxies
|
|
21
|
+
// depending on shallow vs deep mode, so we can't share proxyCache.
|
|
22
|
+
const shallowProxyCache = new WeakMap<object, object>()
|
|
20
23
|
|
|
21
24
|
const IS_STORE = Symbol('pyreon.store')
|
|
25
|
+
const IS_RAW = Symbol('pyreon.raw')
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Mark an object as RAW — `createStore` and `shallowReactive` will return it
|
|
29
|
+
* unwrapped. Useful when storing class instances, third-party objects, or
|
|
30
|
+
* other shapes that shouldn't be deeply proxied (Vue 3 parity).
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const cm = markRaw(new CodeMirrorView(...))
|
|
34
|
+
* const store = createStore({ editor: cm })
|
|
35
|
+
* store.editor === cm // true (not wrapped)
|
|
36
|
+
*
|
|
37
|
+
* Note: marking is one-way — there's no `unmarkRaw`. Mark BEFORE the object
|
|
38
|
+
* enters a store; marking after wrap doesn't unwrap an existing proxy.
|
|
39
|
+
*/
|
|
40
|
+
export function markRaw<T extends object>(value: T): T {
|
|
41
|
+
Object.defineProperty(value, IS_RAW, {
|
|
42
|
+
value: true,
|
|
43
|
+
enumerable: false,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: false,
|
|
46
|
+
})
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Returns true if the value was marked with `markRaw()`. */
|
|
51
|
+
function isMarkedRaw(value: object): boolean {
|
|
52
|
+
return (value as Record<symbol, unknown>)[IS_RAW] === true
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Built-in object types that have internal slots and fail the Proxy
|
|
56
|
+
// internal-slot check on every method call (`Map.prototype.set` called on a
|
|
57
|
+
// Proxy → `TypeError: Method ... called on incompatible receiver`). Returning
|
|
58
|
+
// the raw instance keeps these usable but at the cost of fine-grained
|
|
59
|
+
// reactivity for their contents — write replace-the-whole-Map style if you
|
|
60
|
+
// need reactivity (`store.users = new Map(store.users)`). A future PR can
|
|
61
|
+
// add Vue-style collection-aware wrapping for Map/Set if demand emerges.
|
|
62
|
+
function isBuiltinNonProxiable(obj: object): boolean {
|
|
63
|
+
return (
|
|
64
|
+
obj instanceof Map ||
|
|
65
|
+
obj instanceof Set ||
|
|
66
|
+
obj instanceof WeakMap ||
|
|
67
|
+
obj instanceof WeakSet ||
|
|
68
|
+
obj instanceof Date ||
|
|
69
|
+
obj instanceof RegExp ||
|
|
70
|
+
obj instanceof Promise ||
|
|
71
|
+
obj instanceof Error
|
|
72
|
+
)
|
|
73
|
+
}
|
|
22
74
|
|
|
23
75
|
/** Returns true if the value is a createStore proxy. */
|
|
24
76
|
export function isStore(value: unknown): boolean {
|
|
@@ -34,11 +86,41 @@ export function isStore(value: unknown): boolean {
|
|
|
34
86
|
* Returns a proxy — mutations to the proxy trigger fine-grained reactive updates.
|
|
35
87
|
*/
|
|
36
88
|
export function createStore<T extends object>(initial: T): T {
|
|
37
|
-
return wrap(initial) as T
|
|
89
|
+
return wrap(initial, false) as T
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a SHALLOW reactive store — only top-level mutations trigger updates.
|
|
94
|
+
* Nested objects are NOT auto-wrapped; reading a nested object returns the
|
|
95
|
+
* raw reference. Use when:
|
|
96
|
+
* - the nested objects are immutable (frozen API responses)
|
|
97
|
+
* - you want explicit control over which subtrees are reactive
|
|
98
|
+
* - you need to store class instances or third-party objects without
|
|
99
|
+
* paying the deep-proxy overhead
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const store = shallowReactive({ user: { name: 'Alice' }, count: 0 })
|
|
103
|
+
* effect(() => console.log(store.user)) // tracks store.user reference
|
|
104
|
+
* effect(() => console.log(store.count)) // tracks store.count
|
|
105
|
+
* store.user.name = 'Bob' // does NOT trigger any effect
|
|
106
|
+
* store.count = 5 // triggers the count effect
|
|
107
|
+
* store.user = { name: 'Bob' } // triggers the user effect
|
|
108
|
+
*/
|
|
109
|
+
export function shallowReactive<T extends object>(initial: T): T {
|
|
110
|
+
return wrap(initial, true) as T
|
|
38
111
|
}
|
|
39
112
|
|
|
40
|
-
function wrap(raw: object): object {
|
|
41
|
-
|
|
113
|
+
function wrap(raw: object, shallow: boolean): object {
|
|
114
|
+
// Built-ins with internal slots (Map, Set, Date, …) can't be proxied: their
|
|
115
|
+
// methods fail the receiver check when called on the proxy. Return raw.
|
|
116
|
+
if (isBuiltinNonProxiable(raw)) return raw
|
|
117
|
+
// Vue parity — `markRaw()` opts an object out of proxying entirely. Useful
|
|
118
|
+
// for class instances, third-party objects, or any shape the consumer
|
|
119
|
+
// wants to keep unwrapped.
|
|
120
|
+
if (isMarkedRaw(raw)) return raw
|
|
121
|
+
|
|
122
|
+
const cache = shallow ? shallowProxyCache : proxyCache
|
|
123
|
+
const cached = cache.get(raw)
|
|
42
124
|
if (cached) return cached
|
|
43
125
|
|
|
44
126
|
// Per-property signals. Lazily created on first access.
|
|
@@ -63,19 +145,29 @@ function wrap(raw: object): object {
|
|
|
63
145
|
// Array length — tracked via dedicated signal for push/pop/splice reactivity
|
|
64
146
|
if (isArray && key === 'length') return lengthSig?.()
|
|
65
147
|
|
|
66
|
-
// Non-own properties
|
|
67
|
-
//
|
|
68
|
-
//
|
|
148
|
+
// Non-own properties without a tracked signal: prototype methods
|
|
149
|
+
// (forEach, map, push, …) returned untracked so array methods work.
|
|
150
|
+
// BUT if a signal already exists for this key, the property was tracked
|
|
151
|
+
// before — most likely the property is currently absent because of a
|
|
152
|
+
// `delete` operation. Continue tracking via the existing signal so that
|
|
153
|
+
// a subsequent reassign (`state.b = 99`) re-runs effects that read the
|
|
154
|
+
// key during its absent window. Without this branch, the `delete` →
|
|
155
|
+
// notify-undefined → effect-re-runs-and-reads-`undefined`-via-this-fast-
|
|
156
|
+
// path → effect-loses-subscription chain breaks reactivity for any
|
|
157
|
+
// delete-then-reassign cycle.
|
|
69
158
|
if (!Object.hasOwn(target, key)) {
|
|
159
|
+
if (propSignals.has(key)) return propSignals.get(key)?.()
|
|
70
160
|
return (target as Record<PropertyKey, unknown>)[key]
|
|
71
161
|
}
|
|
72
162
|
|
|
73
163
|
// Track via per-property signal
|
|
74
164
|
const value = getOrCreateSignal(key)()
|
|
75
165
|
|
|
76
|
-
// Deep reactivity: wrap nested objects/arrays transparently
|
|
77
|
-
|
|
78
|
-
|
|
166
|
+
// Deep reactivity: wrap nested objects/arrays transparently. Shallow
|
|
167
|
+
// mode skips this — nested objects are returned raw so consumers can
|
|
168
|
+
// mutate them outside the proxy without triggering effects.
|
|
169
|
+
if (!shallow && value !== null && typeof value === 'object') {
|
|
170
|
+
return wrap(value as object, false)
|
|
79
171
|
}
|
|
80
172
|
|
|
81
173
|
return value
|
|
@@ -113,9 +205,17 @@ function wrap(raw: object): object {
|
|
|
113
205
|
|
|
114
206
|
deleteProperty(target, key) {
|
|
115
207
|
delete (target as Record<PropertyKey, unknown>)[key]
|
|
208
|
+
// Notify subscribers that the property is now undefined, but KEEP the
|
|
209
|
+
// signal in `propSignals`. If we delete the entry, a later `set` on the
|
|
210
|
+
// same key creates a fresh signal — but every effect that previously
|
|
211
|
+
// read this key tracked the old (dropped) signal and never re-runs on
|
|
212
|
+
// the reassign. Keeping the entry preserves signal identity across
|
|
213
|
+
// delete-then-reassign cycles. The trade-off is that long-lived stores
|
|
214
|
+
// with high churn on transient keys retain those signal entries; for
|
|
215
|
+
// workloads where that's a real leak, reassign to undefined instead of
|
|
216
|
+
// delete.
|
|
116
217
|
if (typeof key !== 'symbol' && propSignals.has(key)) {
|
|
117
218
|
propSignals.get(key)?.set(undefined)
|
|
118
|
-
propSignals.delete(key)
|
|
119
219
|
}
|
|
120
220
|
if (isArray) lengthSig?.set((target as unknown[]).length)
|
|
121
221
|
return true
|
|
@@ -134,6 +234,6 @@ function wrap(raw: object): object {
|
|
|
134
234
|
},
|
|
135
235
|
})
|
|
136
236
|
|
|
137
|
-
|
|
237
|
+
cache.set(raw, proxy)
|
|
138
238
|
return proxy
|
|
139
239
|
}
|