mutts 1.0.9 → 1.0.10

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.
Files changed (204) hide show
  1. package/README.md +58 -33
  2. package/dist/browser.cjs +1371 -1206
  3. package/dist/browser.cjs.map +1 -1
  4. package/dist/browser.d.ts +2 -1392
  5. package/dist/browser.dev.cjs +110 -0
  6. package/dist/browser.dev.cjs.map +1 -0
  7. package/dist/browser.dev.d.ts +2 -0
  8. package/dist/browser.dev.esm.js +5 -0
  9. package/dist/browser.dev.esm.js.map +1 -0
  10. package/dist/browser.esm.js +6 -87
  11. package/dist/browser.esm.js.map +1 -1
  12. package/dist/chunks/{async-browser-lvzLOCgk.cjs → async-browser-BU_IfxYD.cjs} +4 -85
  13. package/dist/chunks/async-browser-BU_IfxYD.cjs.map +1 -0
  14. package/dist/chunks/{async-node-C3DeIb0y.cjs → async-node-3PrbVAbB.cjs} +3 -1
  15. package/dist/chunks/async-node-3PrbVAbB.cjs.map +1 -0
  16. package/dist/chunks/{index-2vea86wD.esm.js → index-CaaQQlPJ.esm.js} +1116 -953
  17. package/dist/chunks/index-CaaQQlPJ.esm.js.map +1 -0
  18. package/dist/chunks/node-nKJBk8iJ.esm.js +96 -0
  19. package/dist/chunks/node-nKJBk8iJ.esm.js.map +1 -0
  20. package/dist/chunks/{index-VTO-b2vR.cjs → proxy-Dtg-bJ3T.cjs} +2285 -2983
  21. package/dist/chunks/proxy-Dtg-bJ3T.cjs.map +1 -0
  22. package/dist/chunks/{index-CtA2AWl3.esm.js → proxy-r7lARftl.esm.js} +2273 -2948
  23. package/dist/chunks/proxy-r7lARftl.esm.js.map +1 -0
  24. package/dist/debug.cjs +973 -28
  25. package/dist/debug.cjs.map +1 -1
  26. package/dist/debug.d.ts +36 -37
  27. package/dist/debug.esm.js +949 -1
  28. package/dist/debug.esm.js.map +1 -1
  29. package/dist/devtools/manifest.json +1 -1
  30. package/dist/devtools/panel.html +1 -1
  31. package/dist/devtools/panel.js +107 -94
  32. package/dist/devtools/panel.js.map +1 -1
  33. package/dist/index.d.ts +1447 -1
  34. package/dist/mutts.umd.js +7167 -1
  35. package/dist/mutts.umd.js.map +1 -1
  36. package/dist/mutts.umd.min.js +1 -1
  37. package/dist/mutts.umd.min.js.map +1 -1
  38. package/dist/node.cjs +62 -60
  39. package/dist/node.cjs.map +1 -1
  40. package/dist/node.d.ts +2 -2
  41. package/dist/node.dev.cjs +110 -0
  42. package/dist/node.dev.cjs.map +1 -0
  43. package/dist/node.dev.d.ts +2 -0
  44. package/dist/node.dev.esm.js +6 -0
  45. package/dist/node.dev.esm.js.map +1 -0
  46. package/dist/node.esm.js +4 -97
  47. package/dist/node.esm.js.map +1 -1
  48. package/dist/{types-DaHFfhlN.d.ts → types-W5vD6m2n.d.ts} +99 -44
  49. package/docs/ai/api-reference.md +11 -18
  50. package/docs/ai/manual.md +308 -230
  51. package/docs/reactive/advanced.md +312 -2
  52. package/docs/reactive/attend.md +1 -2
  53. package/docs/reactive/collections.md +21 -39
  54. package/docs/reactive/core.md +7 -37
  55. package/docs/reactive/debugging.md +117 -10
  56. package/docs/reactive/error-handling.md +10 -10
  57. package/docs/reactive/resource.md +125 -0
  58. package/docs/reactive/scan.md +32 -1
  59. package/docs/reactive.md +3 -2
  60. package/docs/utils.md +70 -0
  61. package/package.json +59 -30
  62. package/dist/chunks/async-browser-lvzLOCgk.cjs.map +0 -1
  63. package/dist/chunks/async-node-C3DeIb0y.cjs.map +0 -1
  64. package/dist/chunks/index-2vea86wD.esm.js.map +0 -1
  65. package/dist/chunks/index-CtA2AWl3.esm.js.map +0 -1
  66. package/dist/chunks/index-VTO-b2vR.cjs.map +0 -1
  67. package/dist/debug/debug.d.ts +0 -122
  68. package/dist/debug/debug.d.ts.map +0 -1
  69. package/dist/debug/index.d.ts +0 -4
  70. package/dist/debug/index.d.ts.map +0 -1
  71. package/dist/debug/lineage-panel.d.ts +0 -5
  72. package/dist/debug/lineage-panel.d.ts.map +0 -1
  73. package/dist/debug/lineage.d.ts +0 -79
  74. package/dist/debug/lineage.d.ts.map +0 -1
  75. package/dist/src/async/browser.d.ts +0 -2
  76. package/dist/src/async/browser.d.ts.map +0 -1
  77. package/dist/src/async/index.d.ts +0 -19
  78. package/dist/src/async/index.d.ts.map +0 -1
  79. package/dist/src/async/node.d.ts +0 -2
  80. package/dist/src/async/node.d.ts.map +0 -1
  81. package/dist/src/decorator.d.ts +0 -106
  82. package/dist/src/decorator.d.ts.map +0 -1
  83. package/dist/src/destroyable.d.ts +0 -87
  84. package/dist/src/destroyable.d.ts.map +0 -1
  85. package/dist/src/entry-browser.d.ts +0 -3
  86. package/dist/src/entry-browser.d.ts.map +0 -1
  87. package/dist/src/entry-node.d.ts +0 -3
  88. package/dist/src/entry-node.d.ts.map +0 -1
  89. package/dist/src/eventful.d.ts +0 -20
  90. package/dist/src/eventful.d.ts.map +0 -1
  91. package/dist/src/flavored.d.ts +0 -33
  92. package/dist/src/flavored.d.ts.map +0 -1
  93. package/dist/src/index.d.ts +0 -14
  94. package/dist/src/index.d.ts.map +0 -1
  95. package/dist/src/indexable.d.ts +0 -243
  96. package/dist/src/indexable.d.ts.map +0 -1
  97. package/dist/src/introspection.d.ts +0 -27
  98. package/dist/src/introspection.d.ts.map +0 -1
  99. package/dist/src/iterableWeak.d.ts +0 -53
  100. package/dist/src/iterableWeak.d.ts.map +0 -1
  101. package/dist/src/mixins.d.ts +0 -25
  102. package/dist/src/mixins.d.ts.map +0 -1
  103. package/dist/src/promiseChain.d.ts +0 -20
  104. package/dist/src/promiseChain.d.ts.map +0 -1
  105. package/dist/src/reactive/array.d.ts +0 -48
  106. package/dist/src/reactive/array.d.ts.map +0 -1
  107. package/dist/src/reactive/buffer.d.ts +0 -120
  108. package/dist/src/reactive/buffer.d.ts.map +0 -1
  109. package/dist/src/reactive/change.d.ts +0 -29
  110. package/dist/src/reactive/change.d.ts.map +0 -1
  111. package/dist/src/reactive/deep-touch.d.ts +0 -28
  112. package/dist/src/reactive/deep-touch.d.ts.map +0 -1
  113. package/dist/src/reactive/deep-watch-state.d.ts +0 -25
  114. package/dist/src/reactive/deep-watch-state.d.ts.map +0 -1
  115. package/dist/src/reactive/deep-watch.d.ts +0 -20
  116. package/dist/src/reactive/deep-watch.d.ts.map +0 -1
  117. package/dist/src/reactive/describe.d.ts +0 -12
  118. package/dist/src/reactive/describe.d.ts.map +0 -1
  119. package/dist/src/reactive/effect-context.d.ts +0 -34
  120. package/dist/src/reactive/effect-context.d.ts.map +0 -1
  121. package/dist/src/reactive/effects.d.ts +0 -164
  122. package/dist/src/reactive/effects.d.ts.map +0 -1
  123. package/dist/src/reactive/index.d.ts +0 -19
  124. package/dist/src/reactive/index.d.ts.map +0 -1
  125. package/dist/src/reactive/map.d.ts +0 -28
  126. package/dist/src/reactive/map.d.ts.map +0 -1
  127. package/dist/src/reactive/memoize.d.ts +0 -28
  128. package/dist/src/reactive/memoize.d.ts.map +0 -1
  129. package/dist/src/reactive/non-reactive-state.d.ts +0 -9
  130. package/dist/src/reactive/non-reactive-state.d.ts.map +0 -1
  131. package/dist/src/reactive/non-reactive.d.ts +0 -11
  132. package/dist/src/reactive/non-reactive.d.ts.map +0 -1
  133. package/dist/src/reactive/project.d.ts +0 -40
  134. package/dist/src/reactive/project.d.ts.map +0 -1
  135. package/dist/src/reactive/proxy-state.d.ts +0 -8
  136. package/dist/src/reactive/proxy-state.d.ts.map +0 -1
  137. package/dist/src/reactive/proxy.d.ts +0 -23
  138. package/dist/src/reactive/proxy.d.ts.map +0 -1
  139. package/dist/src/reactive/record.d.ts +0 -115
  140. package/dist/src/reactive/record.d.ts.map +0 -1
  141. package/dist/src/reactive/register.d.ts +0 -125
  142. package/dist/src/reactive/register.d.ts.map +0 -1
  143. package/dist/src/reactive/registry.d.ts +0 -21
  144. package/dist/src/reactive/registry.d.ts.map +0 -1
  145. package/dist/src/reactive/set.d.ts +0 -26
  146. package/dist/src/reactive/set.d.ts.map +0 -1
  147. package/dist/src/reactive/tracking.d.ts +0 -7
  148. package/dist/src/reactive/tracking.d.ts.map +0 -1
  149. package/dist/src/reactive/types.d.ts +0 -424
  150. package/dist/src/reactive/types.d.ts.map +0 -1
  151. package/dist/src/reactive/watch.d.ts +0 -48
  152. package/dist/src/reactive/watch.d.ts.map +0 -1
  153. package/dist/src/std-decorators.d.ts +0 -45
  154. package/dist/src/std-decorators.d.ts.map +0 -1
  155. package/dist/src/utils.d.ts +0 -49
  156. package/dist/src/utils.d.ts.map +0 -1
  157. package/dist/src/zone.d.ts +0 -40
  158. package/dist/src/zone.d.ts.map +0 -1
  159. package/docs/reactive/describe.md +0 -85
  160. package/docs/reactive/project.md +0 -93
  161. package/src/async/browser.ts +0 -323
  162. package/src/async/index.ts +0 -27
  163. package/src/async/node.ts +0 -92
  164. package/src/decorator.ts +0 -272
  165. package/src/destroyable.ts +0 -199
  166. package/src/entry-browser.ts +0 -5
  167. package/src/entry-node.ts +0 -5
  168. package/src/eventful.ts +0 -110
  169. package/src/flavored.ts +0 -106
  170. package/src/index.d.ts +0 -12
  171. package/src/index.ts +0 -64
  172. package/src/indexable.ts +0 -526
  173. package/src/introspection.ts +0 -59
  174. package/src/iterableWeak.ts +0 -233
  175. package/src/mixins.ts +0 -123
  176. package/src/promiseChain.ts +0 -110
  177. package/src/reactive/array.ts +0 -500
  178. package/src/reactive/buffer.ts +0 -328
  179. package/src/reactive/change.ts +0 -131
  180. package/src/reactive/deep-touch.ts +0 -273
  181. package/src/reactive/deep-watch-state.ts +0 -82
  182. package/src/reactive/deep-watch.ts +0 -171
  183. package/src/reactive/describe.ts +0 -39
  184. package/src/reactive/effect-context.ts +0 -83
  185. package/src/reactive/effects.ts +0 -1434
  186. package/src/reactive/index.ts +0 -72
  187. package/src/reactive/map.ts +0 -142
  188. package/src/reactive/memoize.ts +0 -186
  189. package/src/reactive/non-reactive-state.ts +0 -49
  190. package/src/reactive/non-reactive.ts +0 -43
  191. package/src/reactive/project.md +0 -107
  192. package/src/reactive/project.ts +0 -430
  193. package/src/reactive/proxy-state.ts +0 -27
  194. package/src/reactive/proxy.ts +0 -282
  195. package/src/reactive/record.ts +0 -181
  196. package/src/reactive/register.ts +0 -538
  197. package/src/reactive/registry.ts +0 -72
  198. package/src/reactive/set.ts +0 -117
  199. package/src/reactive/tracking.ts +0 -41
  200. package/src/reactive/types.ts +0 -520
  201. package/src/reactive/watch.ts +0 -180
  202. package/src/std-decorators.ts +0 -256
  203. package/src/utils.ts +0 -300
  204. package/src/zone.ts +0 -142
@@ -1,430 +0,0 @@
1
- import { setEffectName } from '../../debug/debug'
2
- import { FoolProof, named } from '../utils'
3
- import { cleanedBy, getActiveEffect } from './effect-context'
4
- import { effect, untracked } from './effects'
5
- import { reactive } from './proxy'
6
- import { Register } from './register'
7
- import {
8
- cleanup,
9
- type EffectTrigger,
10
- type ProjectionContext,
11
- projectionInfo,
12
- type ScopedCallback,
13
- } from './types'
14
- /**
15
- * Maps projection effects (item effects) to their projection context
16
- */
17
- export const effectProjectionMetadata = new WeakMap<EffectTrigger, ProjectionContext>()
18
-
19
- /**
20
- * Returns the projection context of the currently running effect, if any.
21
- */
22
- export function getActiveProjection(): ProjectionContext | undefined {
23
- const active = getActiveEffect()
24
- return active ? effectProjectionMetadata.get(active) : undefined
25
- }
26
- function setActiveProjection(projection: ProjectionContext | undefined) {
27
- const active = getActiveEffect()
28
- if (active) effectProjectionMetadata.set(active, projection)
29
- }
30
-
31
- type ProjectOldValue<Target> = Target extends readonly (infer Item)[]
32
- ? Item
33
- : Target extends Map<any, infer Item>
34
- ? Item
35
- : Target extends Record<PropertyKey, infer Item>
36
- ? Item
37
- : unknown
38
-
39
- export type ProjectAccess<SourceValue, Key, SourceType, Target> = {
40
- readonly key: Key
41
- get(): SourceValue
42
- set(value: SourceValue): boolean
43
- readonly source: SourceType
44
- readonly old?: ProjectOldValue<Target>
45
- value: SourceValue
46
- }
47
-
48
- export type ProjectCallback<SourceValue, Key, Target extends object, SourceType, Result> = (
49
- access: ProjectAccess<SourceValue, Key, SourceType, Target>,
50
- target: Target
51
- ) => Result
52
-
53
- export type ProjectResult<Target extends object> = Target & { [cleanup]: ScopedCallback }
54
-
55
- function defineAccessValue<Access extends { get(): unknown; set(value: unknown): boolean }>(
56
- access: Access
57
- ) {
58
- Object.defineProperty(access, 'value', {
59
- get: access.get,
60
- set: access.set,
61
- configurable: true,
62
- enumerable: true,
63
- })
64
- }
65
-
66
- function makeCleanup<Result extends object>(
67
- target: Result,
68
- effectMap: Map<unknown, ScopedCallback>,
69
- onDispose: () => void,
70
- metadata?: any
71
- ): ProjectResult<Result> {
72
- if (metadata) {
73
- Object.defineProperty(target, projectionInfo, {
74
- value: metadata,
75
- writable: false,
76
- enumerable: false,
77
- configurable: true,
78
- })
79
- }
80
- return cleanedBy(target, () => {
81
- onDispose()
82
- for (const stop of effectMap.values()) stop?.()
83
- effectMap.clear()
84
- }) as ProjectResult<Result>
85
- }
86
-
87
- function projectArray<SourceValue, ResultValue>(
88
- source: readonly SourceValue[],
89
- apply: ProjectCallback<SourceValue, number, ResultValue[], readonly SourceValue[], ResultValue>
90
- ): ProjectResult<ResultValue[]> {
91
- source = reactive(source)
92
- const target = reactive([] as ResultValue[])
93
- const indexEffects = new Map<number, ScopedCallback>()
94
-
95
- function normalizeTargetLength(length: number) {
96
- FoolProof.set(target as unknown as object, 'length', length, target)
97
- }
98
-
99
- function disposeIndex(index: number) {
100
- const stopEffect = indexEffects.get(index)
101
- if (stopEffect) {
102
- indexEffects.delete(index)
103
- stopEffect()
104
- Reflect.deleteProperty(target as unknown as object, index)
105
- }
106
- }
107
-
108
- const parent = getActiveProjection()
109
- const depth = parent ? parent.depth + 1 : 0
110
-
111
- const cleanupLength = effect(named(`project:${source[Symbol.toStringTag]}.length`, ({ ascend }) => {
112
- const length = source.length
113
- normalizeTargetLength(length)
114
- const existing = Array.from(indexEffects.keys())
115
- for (let i = 0; i < length; i++) {
116
- if (indexEffects.has(i)) continue
117
- ascend(function projectArrayIndexAscend() {
118
- const index = i
119
- const stop = effect(named(`project:${source[Symbol.toStringTag]}[${index}]`, ({ reaction }) => {
120
- if (!reaction) setActiveProjection({ source, key: index, target, depth, parent })
121
- const previous = untracked(() => target[index])
122
- const accessBase = {
123
- key: index,
124
- source,
125
- get: () => FoolProof.get(source as any, index, source),
126
- set: (value: SourceValue) => FoolProof.set(source as any, index, value, source),
127
- old: previous,
128
- } as ProjectAccess<SourceValue, number, readonly SourceValue[], ResultValue[]>
129
- defineAccessValue(accessBase)
130
- target[index] = apply(accessBase, target)
131
- }))
132
- setEffectName(stop, `project[${depth}]:${index}`)
133
- indexEffects.set(i, stop)
134
- })
135
- }
136
- for (const index of existing) if (index >= length) disposeIndex(index)
137
- }))
138
-
139
- return makeCleanup(target, indexEffects, () => cleanupLength(), {
140
- source,
141
- target,
142
- apply,
143
- depth,
144
- parent,
145
- } as ProjectionContext)
146
- }
147
-
148
- function projectRegister<Key extends PropertyKey, SourceValue, ResultValue>(
149
- source: Register<SourceValue, Key>,
150
- apply: ProjectCallback<
151
- SourceValue,
152
- Key,
153
- Map<Key, ResultValue>,
154
- Register<SourceValue, Key>,
155
- ResultValue
156
- >
157
- ): ProjectResult<Map<Key, ResultValue>> {
158
- source = reactive(source) as Register<SourceValue, Key>
159
- const rawTarget = new Map<Key, ResultValue>()
160
- const target = reactive(rawTarget) as Map<Key, ResultValue>
161
- const keyEffects = new Map<Key, ScopedCallback>()
162
-
163
- function disposeKey(key: Key) {
164
- const stopEffect = keyEffects.get(key)
165
- if (stopEffect) {
166
- stopEffect()
167
- keyEffects.delete(key)
168
- target.delete(key)
169
- }
170
- }
171
-
172
- const parent = getActiveProjection()
173
- const depth = parent ? parent.depth + 1 : 0
174
-
175
- const cleanupKeys = effect(function projectRegisterEffect({ ascend }) {
176
- const keys = new Set<Key>()
177
- for (const key of source.mapKeys()) keys.add(key)
178
-
179
- for (const key of keys) {
180
- if (keyEffects.has(key)) continue
181
- ascend(() => {
182
- const stop = effect(function projectRegisterKeyEffect({ reaction }) {
183
- if (!reaction) setActiveProjection({ source, key, target, depth, parent })
184
- const previous = untracked(() => target.get(key))
185
- const accessBase = {
186
- key,
187
- source: source,
188
- get: () => source.get(key) as SourceValue,
189
- set: (value: SourceValue) => {
190
- source.set(key, value)
191
- return true
192
- },
193
- old: previous,
194
- } as ProjectAccess<SourceValue, Key, Register<SourceValue, Key>, Map<Key, ResultValue>>
195
- defineAccessValue(accessBase)
196
- const produced = apply(accessBase, target)
197
- target.set(key, produced)
198
- })
199
- setEffectName(stop, `project[${depth}]:${String(key)}`)
200
- keyEffects.set(key, stop)
201
- })
202
- }
203
-
204
- for (const key of Array.from(keyEffects.keys())) if (!keys.has(key)) disposeKey(key)
205
- })
206
-
207
- return makeCleanup(target, keyEffects, () => cleanupKeys(), {
208
- source: source,
209
- target,
210
- apply,
211
- depth,
212
- parent,
213
- } as ProjectionContext)
214
- }
215
-
216
- function projectRecord<Source extends Record<PropertyKey, any>, ResultValue>(
217
- source: Source,
218
- apply: ProjectCallback<
219
- Source[keyof Source],
220
- keyof Source,
221
- Record<keyof Source, ResultValue>,
222
- Source,
223
- ResultValue
224
- >
225
- ): ProjectResult<Record<keyof Source, ResultValue>> {
226
- source = reactive(source) as Source
227
- const target = reactive({} as Record<keyof Source, ResultValue>)
228
- const keyEffects = new Map<PropertyKey, ScopedCallback>()
229
-
230
- function disposeKey(key: PropertyKey) {
231
- const stopEffect = keyEffects.get(key)
232
- if (stopEffect) {
233
- stopEffect()
234
- keyEffects.delete(key)
235
- Reflect.deleteProperty(target as Record<PropertyKey, unknown>, key)
236
- }
237
- }
238
-
239
- const parent = getActiveProjection()
240
- const depth = parent ? parent.depth + 1 : 0
241
-
242
- const cleanupKeys = effect(function projectRecordEffect({ ascend }) {
243
- const keys = new Set<PropertyKey>()
244
- for (const key in source) keys.add(key)
245
- const observed = Reflect.ownKeys(source)
246
- for (const key of observed) keys.add(key)
247
-
248
- for (const key of keys) {
249
- if (keyEffects.has(key)) continue
250
- ascend(() => {
251
- const stop = effect(function projectRecordKeyEffect({ reaction }) {
252
- if (!reaction) setActiveProjection({ source, key, target, depth, parent })
253
- const sourceKey = key as keyof Source
254
- const previous = untracked(
255
- () => (target as Record<PropertyKey, ResultValue | undefined>)[key]
256
- )
257
- const accessBase = {
258
- key: sourceKey,
259
- source: source,
260
- get: () => FoolProof.get(source, sourceKey, source),
261
- set: (value: Source[typeof sourceKey]) =>
262
- FoolProof.set(source, sourceKey, value, source),
263
- old: previous,
264
- } as ProjectAccess<
265
- Source[typeof sourceKey],
266
- keyof Source,
267
- Source,
268
- Record<keyof Source, ResultValue>
269
- >
270
- defineAccessValue(accessBase)
271
- const produced = apply(accessBase, target)
272
- ;(target as any)[sourceKey] = produced
273
- })
274
- setEffectName(stop, `project[${depth}]:${String(key)}`)
275
- keyEffects.set(key, stop)
276
- })
277
- }
278
-
279
- for (const key of Array.from(keyEffects.keys())) if (!keys.has(key)) disposeKey(key)
280
- })
281
-
282
- return makeCleanup(target, keyEffects, () => cleanupKeys(), {
283
- source: source,
284
- target,
285
- apply,
286
- depth,
287
- parent,
288
- } as ProjectionContext)
289
- }
290
-
291
- function projectMap<Key, Value, ResultValue>(
292
- source: Map<Key, Value>,
293
- apply: ProjectCallback<Value, Key, Map<Key, ResultValue>, Map<Key, Value>, ResultValue>
294
- ): ProjectResult<Map<Key, ResultValue>> {
295
- source = reactive(source) as Map<Key, Value>
296
- const rawTarget = new Map<Key, ResultValue>()
297
- const target = reactive(rawTarget) as Map<Key, ResultValue>
298
- const keyEffects = new Map<Key, ScopedCallback>()
299
-
300
- function disposeKey(key: Key) {
301
- const stopEffect = keyEffects.get(key)
302
- if (stopEffect) {
303
- stopEffect()
304
- keyEffects.delete(key)
305
- target.delete(key)
306
- }
307
- }
308
-
309
- const parent = getActiveProjection()
310
- const depth = parent ? parent.depth + 1 : 0
311
-
312
- const cleanupKeys = effect(function projectMapEffect({ ascend }) {
313
- const keys = new Set<Key>()
314
- for (const key of source.keys()) keys.add(key)
315
-
316
- for (const key of keys) {
317
- if (keyEffects.has(key)) continue
318
- ascend(() => {
319
- const stop = effect(function projectMapKeyEffect({ reaction }) {
320
- if (!reaction) setActiveProjection({ source, key, target, depth, parent })
321
- const previous = untracked(() => target.get(key))
322
- const accessBase = {
323
- key,
324
- source: source,
325
- get: () => source.get(key) as Value,
326
- set: (value: Value) => {
327
- source.set(key, value)
328
- return true
329
- },
330
- old: previous,
331
- } as ProjectAccess<Value, Key, Map<Key, Value>, Map<Key, ResultValue>>
332
- defineAccessValue(accessBase)
333
- const produced = apply(accessBase, target)
334
- target.set(key, produced)
335
- })
336
- setEffectName(stop, `project[${depth}]:${String(key)}`)
337
- keyEffects.set(key, stop)
338
- })
339
- }
340
-
341
- for (const key of Array.from(keyEffects.keys())) if (!keys.has(key)) disposeKey(key)
342
- })
343
-
344
- return makeCleanup(target, keyEffects, () => cleanupKeys(), {
345
- source: source,
346
- target,
347
- apply,
348
- depth,
349
- parent,
350
- } as ProjectionContext)
351
- }
352
-
353
- type ProjectOverload = {
354
- <SourceValue, ResultValue>(
355
- source: readonly SourceValue[],
356
- apply: ProjectCallback<SourceValue, number, ResultValue[], readonly SourceValue[], ResultValue>
357
- ): ProjectResult<ResultValue[]>
358
- <Key extends PropertyKey, SourceValue, ResultValue>(
359
- source: Register<SourceValue, Key>,
360
- apply: ProjectCallback<
361
- SourceValue,
362
- Key,
363
- Map<Key, ResultValue>,
364
- Register<SourceValue, Key>,
365
- ResultValue
366
- >
367
- ): ProjectResult<Map<Key, ResultValue>>
368
- <Source extends Record<PropertyKey, any>, ResultValue>(
369
- source: Source,
370
- apply: ProjectCallback<
371
- Source[keyof Source],
372
- keyof Source,
373
- Record<keyof Source, ResultValue>,
374
- Source,
375
- ResultValue
376
- >
377
- ): ProjectResult<Record<keyof Source, ResultValue>>
378
- <Key, Value, ResultValue>(
379
- source: Map<Key, Value>,
380
- apply: ProjectCallback<Value, Key, Map<Key, ResultValue>, Map<Key, Value>, ResultValue>
381
- ): ProjectResult<Map<Key, ResultValue>>
382
- array: typeof projectArray
383
- register: typeof projectRegister
384
- record: typeof projectRecord
385
- map: typeof projectMap
386
- }
387
-
388
- function projectCore<SourceValue, ResultValue>(
389
- source: readonly SourceValue[],
390
- apply: ProjectCallback<SourceValue, number, ResultValue[], readonly SourceValue[], ResultValue>
391
- ): ProjectResult<ResultValue[]>
392
- function projectCore<Key extends PropertyKey, SourceValue, ResultValue>(
393
- source: Register<SourceValue, Key>,
394
- apply: ProjectCallback<
395
- SourceValue,
396
- Key,
397
- Map<Key, ResultValue>,
398
- Register<SourceValue, Key>,
399
- ResultValue
400
- >
401
- ): ProjectResult<Map<Key, ResultValue>>
402
- function projectCore<Source extends Record<PropertyKey, any>, ResultValue>(
403
- source: Source,
404
- apply: ProjectCallback<
405
- Source[keyof Source],
406
- keyof Source,
407
- Record<keyof Source, ResultValue>,
408
- Source,
409
- ResultValue
410
- >
411
- ): ProjectResult<Record<keyof Source, ResultValue>>
412
- function projectCore<Key, Value, ResultValue>(
413
- source: Map<Key, Value>,
414
- apply: ProjectCallback<Value, Key, Map<Key, ResultValue>, Map<Key, Value>, ResultValue>
415
- ): ProjectResult<Map<Key, ResultValue>>
416
- function projectCore(source: any, apply: any): ProjectResult<any> {
417
- if (Array.isArray(source)) return projectArray(source, apply)
418
- if (source instanceof Map) return projectMap(source, apply)
419
- if (source instanceof Register) return projectRegister(source, apply)
420
- if (source && (source.constructor === Object || source.constructor === undefined))
421
- return projectRecord(source, apply)
422
- throw new Error('Unsupported source type')
423
- }
424
-
425
- export const project: ProjectOverload = Object.assign(projectCore, {
426
- array: projectArray,
427
- register: projectRegister,
428
- record: projectRecord,
429
- map: projectMap,
430
- })
@@ -1,27 +0,0 @@
1
- export const objectToProxy = new WeakMap<object, object>()
2
- export const proxyToObject = new WeakMap<object, object>()
3
-
4
- export function storeProxyRelationship(target: object, proxy: object) {
5
- objectToProxy.set(target, proxy)
6
- proxyToObject.set(proxy, target)
7
- }
8
-
9
- export function getExistingProxy<T extends object>(target: T): T | undefined {
10
- return objectToProxy.get(target) as T | undefined
11
- }
12
-
13
- export function trackProxyObject(proxy: object, target: object) {
14
- proxyToObject.set(proxy, target)
15
- }
16
-
17
- export function unwrap<T>(obj: T): T {
18
- let current = obj
19
- while (current && typeof current === 'object' && current !== null && proxyToObject.has(current)) {
20
- current = proxyToObject.get(current) as T
21
- }
22
- return current
23
- }
24
-
25
- export function isReactive(obj: any): boolean {
26
- return proxyToObject.has(obj)
27
- }