/
vanilla.ts
274 lines (261 loc) 路 8.14 KB
/
vanilla.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { getUntracked, markToTrack } from 'proxy-compare'
const VERSION = __DEV__ ? Symbol('VERSION') : Symbol()
const LISTENERS = __DEV__ ? Symbol('LISTENERS') : Symbol()
const SNAPSHOT = __DEV__ ? Symbol('SNAPSHOT') : Symbol()
const HANDLER = __DEV__ ? Symbol('HANDLER') : Symbol()
const PROMISE_RESULT = __DEV__ ? Symbol('PROMISE_RESULT') : Symbol()
const PROMISE_ERROR = __DEV__ ? Symbol('PROMISE_ERROR') : Symbol()
/**
* This not a public API.
* It can be changed without notice.
*/
export interface INTERNAL_AsRef {
$$valtioRef: true
}
const refSet = new WeakSet()
export function ref<T extends object>(o: T): T & INTERNAL_AsRef {
refSet.add(o)
return o as T & INTERNAL_AsRef
}
const isObject = (x: unknown): x is object =>
typeof x === 'object' && x !== null
const canProxy = (x: unknown) =>
isObject(x) &&
!refSet.has(x) &&
(Array.isArray(x) || !(Symbol.iterator in x)) &&
!(x instanceof WeakMap) &&
!(x instanceof WeakSet) &&
!(x instanceof Error) &&
!(x instanceof Number) &&
!(x instanceof Date) &&
!(x instanceof String) &&
!(x instanceof RegExp) &&
!(x instanceof ArrayBuffer)
type ProxyObject = object
const proxyCache = new WeakMap<object, ProxyObject>()
type Path = (string | symbol)[]
type Op =
| [op: 'set', path: Path, value: unknown, prevValue: unknown]
| [op: 'delete', path: Path, prevValue: unknown]
| [op: 'resolve', path: Path, value: unknown]
| [op: 'reject', path: Path, error: unknown]
type Listener = (op: Op, nextVersion: number) => void
let globalVersion = 1
const snapshotCache = new WeakMap<
object,
[version: number, snapshot: unknown]
>()
export function proxy<T extends object>(initialObject: T = {} as T): T {
if (!isObject(initialObject)) {
throw new Error('object required')
}
const found = proxyCache.get(initialObject) as T | undefined
if (found) {
return found
}
let version = globalVersion
const listeners = new Set<Listener>()
const notifyUpdate = (op: Op, nextVersion = ++globalVersion) => {
if (version !== nextVersion) {
version = nextVersion
listeners.forEach((listener) => listener(op, nextVersion))
}
}
const propListeners = new Map<string | symbol, Listener>()
const getPropListener = (prop: string | symbol) => {
let propListener = propListeners.get(prop)
if (!propListener) {
propListener = (op, nextVersion) => {
const newOp: Op = [...op]
newOp[1] = [prop, ...(newOp[1] as Path)]
notifyUpdate(newOp, nextVersion)
}
propListeners.set(prop, propListener)
}
return propListener
}
const popPropListener = (prop: string | symbol) => {
const propListener = propListeners.get(prop)
propListeners.delete(prop)
return propListener
}
const createSnapshot = (target: T, receiver: any) => {
const cache = snapshotCache.get(receiver)
if (cache?.[0] === version) {
return cache[1]
}
const snapshot: any = Array.isArray(target)
? []
: Object.create(Object.getPrototypeOf(target))
markToTrack(snapshot, true) // mark to track
snapshotCache.set(receiver, [version, snapshot])
Reflect.ownKeys(target).forEach((key) => {
const value = Reflect.get(target, key, receiver)
if (refSet.has(value)) {
markToTrack(value, false) // mark not to track
snapshot[key] = value
} else if (value instanceof Promise) {
if (PROMISE_RESULT in value) {
snapshot[key] = (value as any)[PROMISE_RESULT]
} else {
const errorOrPromise = (value as any)[PROMISE_ERROR] || value
Object.defineProperty(snapshot, key, {
get() {
if (PROMISE_RESULT in value) {
return (value as any)[PROMISE_RESULT]
}
throw errorOrPromise
},
})
}
} else if (value?.[LISTENERS]) {
snapshot[key] = value[SNAPSHOT]
} else {
snapshot[key] = value
}
})
Object.freeze(snapshot)
return snapshot
}
const baseObject = Array.isArray(initialObject)
? []
: Object.create(Object.getPrototypeOf(initialObject))
const handler = {
get(target: T, prop: string | symbol, receiver: any) {
if (prop === VERSION) {
return version
}
if (prop === LISTENERS) {
return listeners
}
if (prop === SNAPSHOT) {
return createSnapshot(target, receiver)
}
if (prop === HANDLER) {
return handler
}
return Reflect.get(target, prop, receiver)
},
deleteProperty(target: T, prop: string | symbol) {
const prevValue = Reflect.get(target, prop)
const childListeners = prevValue?.[LISTENERS]
if (childListeners) {
childListeners.delete(popPropListener(prop))
}
const deleted = Reflect.deleteProperty(target, prop)
if (deleted) {
notifyUpdate(['delete', [prop], prevValue])
}
return deleted
},
is: Object.is,
canProxy,
set(target: T, prop: string | symbol, value: any, receiver: any) {
const hasPrevValue = Reflect.has(target, prop)
const prevValue = Reflect.get(target, prop, receiver)
if (hasPrevValue && this.is(prevValue, value)) {
return true
}
const childListeners = prevValue?.[LISTENERS]
if (childListeners) {
childListeners.delete(popPropListener(prop))
}
if (isObject(value)) {
value = getUntracked(value) || value
}
let nextValue: any
if (Object.getOwnPropertyDescriptor(target, prop)?.set) {
nextValue = value
} else if (value instanceof Promise) {
nextValue = value
.then((v) => {
nextValue[PROMISE_RESULT] = v
notifyUpdate(['resolve', [prop], v])
return v
})
.catch((e) => {
nextValue[PROMISE_ERROR] = e
notifyUpdate(['reject', [prop], e])
})
} else if (value?.[LISTENERS]) {
nextValue = value
nextValue[LISTENERS].add(getPropListener(prop))
} else if (this.canProxy(value)) {
nextValue = proxy(value)
nextValue[LISTENERS].add(getPropListener(prop))
} else {
nextValue = value
}
Reflect.set(target, prop, nextValue, receiver)
notifyUpdate(['set', [prop], value, prevValue])
return true
},
}
const proxyObject = new Proxy(baseObject, handler)
proxyCache.set(initialObject, proxyObject)
Reflect.ownKeys(initialObject).forEach((key) => {
const desc = Object.getOwnPropertyDescriptor(
initialObject,
key
) as PropertyDescriptor
if (desc.get || desc.set) {
Object.defineProperty(baseObject, key, desc)
} else {
proxyObject[key] = initialObject[key as keyof T]
}
})
return proxyObject
}
export function getVersion(proxyObject: unknown): number | undefined {
return isObject(proxyObject) ? (proxyObject as any)[VERSION] : undefined
}
export function subscribe<T extends object>(
proxyObject: T,
callback: (ops: Op[]) => void,
notifyInSync?: boolean
) {
if (__DEV__ && !(proxyObject as any)?.[LISTENERS]) {
console.warn('Please use proxy object')
}
let promise: Promise<void> | undefined
const ops: Op[] = []
const listener: Listener = (op) => {
ops.push(op)
if (notifyInSync) {
callback(ops.splice(0))
return
}
if (!promise) {
promise = Promise.resolve().then(() => {
promise = undefined
callback(ops.splice(0))
})
}
}
;(proxyObject as any)[LISTENERS].add(listener)
return () => {
;(proxyObject as any)[LISTENERS].delete(listener)
}
}
type AnyFunction = (...args: any[]) => any
type Snapshot<T> = T extends AnyFunction
? T
: T extends INTERNAL_AsRef
? T
: T extends Promise<infer V>
? Snapshot<V>
: {
readonly [K in keyof T]: Snapshot<T[K]>
}
export function snapshot<T extends object>(proxyObject: T): Snapshot<T> {
if (__DEV__ && !(proxyObject as any)?.[SNAPSHOT]) {
console.warn('Please use proxy object')
}
return (proxyObject as any)[SNAPSHOT]
}
export function getHandler<T extends object>(proxyObject: T) {
if (__DEV__ && !(proxyObject as any)?.[HANDLER]) {
console.warn('Please use proxy object')
}
return (proxyObject as any)[HANDLER]
}