@tschk/moonshine-vue 0.4.49 → 0.4.80
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 +38 -34
- package/package.json +8 -5
- package/src/app.ts +426 -13
- package/src/context.ts +222 -16
- package/src/lifecycle.ts +80 -1
- package/src/map.ts +196 -0
- package/src/reactive.ts +1017 -28
- package/src/router.ts +1562 -0
- package/src/server-renderer.ts +257 -0
- package/src/slots.ts +26 -0
- package/src/store.ts +1042 -0
package/src/context.ts
CHANGED
|
@@ -1,25 +1,135 @@
|
|
|
1
|
-
/** Native component injection
|
|
1
|
+
/** Native component injection and application/SSR context primitives. */
|
|
2
2
|
import {
|
|
3
3
|
getComponentContext,
|
|
4
4
|
peekComponentContext,
|
|
5
5
|
setContext,
|
|
6
6
|
type ComponentContext,
|
|
7
7
|
} from "@tschk/moonshine-dom/component";
|
|
8
|
-
import { untrack, type Owner } from "@tschk/moonshine-dom/reactive";
|
|
8
|
+
import { getOwner, untrack, type Owner } from "@tschk/moonshine-dom/reactive";
|
|
9
|
+
import { isSSRRendering } from "@tschk/moonshine-dom/ssr-owner";
|
|
9
10
|
|
|
10
11
|
declare const injectionType: unique symbol;
|
|
11
12
|
export type InjectionKey<T> = symbol & { readonly [injectionType]?: T };
|
|
12
13
|
const providesKey = Symbol("moonshine.vue.provides");
|
|
14
|
+
export const appContextKey = Symbol("moonshine.vue.app-context");
|
|
13
15
|
const idCounters = /* @__PURE__ */ new WeakMap<Owner, { next: number }>();
|
|
16
|
+
const ssrContexts = /* @__PURE__ */ new WeakMap<Owner, Record<string, any>>();
|
|
17
|
+
|
|
18
|
+
export interface AppConfig {
|
|
19
|
+
errorHandler?: (
|
|
20
|
+
err: unknown,
|
|
21
|
+
instance: ComponentContext | undefined,
|
|
22
|
+
info: string,
|
|
23
|
+
) => void;
|
|
24
|
+
warnHandler?: (
|
|
25
|
+
msg: string,
|
|
26
|
+
instance: ComponentContext | undefined,
|
|
27
|
+
trace: string,
|
|
28
|
+
) => void;
|
|
29
|
+
globalProperties: Record<string, any>;
|
|
30
|
+
performance: boolean;
|
|
31
|
+
idPrefix?: string;
|
|
32
|
+
compilerOptions: Record<string, any>;
|
|
33
|
+
optionMergeStrategies: Record<string, (parent: any, child: any) => any>;
|
|
34
|
+
throwUnhandledErrorInProduction?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface AppContextState {
|
|
38
|
+
provides: Map<PropertyKey, unknown>;
|
|
39
|
+
components: Map<string, (...args: any[]) => any>;
|
|
40
|
+
directives: Map<string, any>;
|
|
41
|
+
mixins: Array<Record<string, any>>;
|
|
42
|
+
config: AppConfig;
|
|
43
|
+
idState: { next: number };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let activeAppContext: AppContextState | undefined;
|
|
47
|
+
let activeSSRContext: Record<string, any> | undefined;
|
|
48
|
+
|
|
49
|
+
export function withActiveAppContext<T>(ctx: AppContextState, fn: () => T): T {
|
|
50
|
+
const prev = activeAppContext;
|
|
51
|
+
activeAppContext = ctx;
|
|
52
|
+
try {
|
|
53
|
+
return fn();
|
|
54
|
+
} finally {
|
|
55
|
+
activeAppContext = prev;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function peekAppContext(): AppContextState | undefined {
|
|
60
|
+
if (activeAppContext) return activeAppContext;
|
|
61
|
+
const ownerCtx = getOwner()?.context?.get(appContextKey) as
|
|
62
|
+
AppContextState | undefined;
|
|
63
|
+
if (ownerCtx) return ownerCtx;
|
|
64
|
+
const frameCtx = peekComponentContext()?.owner.context?.get(appContextKey) as
|
|
65
|
+
AppContextState | undefined;
|
|
66
|
+
return frameCtx;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type SSRContext = Record<string, any>;
|
|
70
|
+
|
|
71
|
+
export function withSSRContext<T>(ctx: SSRContext | undefined, fn: () => T): T {
|
|
72
|
+
const prev = activeSSRContext;
|
|
73
|
+
activeSSRContext = ctx;
|
|
74
|
+
try {
|
|
75
|
+
return fn();
|
|
76
|
+
} finally {
|
|
77
|
+
activeSSRContext = prev;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function withSSRContextAsync<T>(
|
|
82
|
+
ctx: SSRContext | undefined,
|
|
83
|
+
fn: () => Promise<T>,
|
|
84
|
+
): Promise<T> {
|
|
85
|
+
const prev = activeSSRContext;
|
|
86
|
+
activeSSRContext = ctx;
|
|
87
|
+
try {
|
|
88
|
+
return await fn();
|
|
89
|
+
} finally {
|
|
90
|
+
activeSSRContext = prev;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function peekSSRContext<T extends SSRContext = SSRContext>():
|
|
95
|
+
T | undefined {
|
|
96
|
+
return activeSSRContext as T | undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function useSSRContext<T extends SSRContext = SSRContext>():
|
|
100
|
+
T | undefined {
|
|
101
|
+
const frame = peekComponentContext();
|
|
102
|
+
if (frame?.mode === "dom") return undefined;
|
|
103
|
+
if (activeSSRContext) return activeSSRContext as T;
|
|
104
|
+
if (frame?.mode === "ssr" || isSSRRendering()) {
|
|
105
|
+
const owner = frame?.owner ?? getOwner();
|
|
106
|
+
if (owner) {
|
|
107
|
+
let root: Owner = owner;
|
|
108
|
+
while (root.parent) root = root.parent;
|
|
109
|
+
let ctx = ssrContexts.get(root);
|
|
110
|
+
if (!ctx) {
|
|
111
|
+
ctx = {};
|
|
112
|
+
ssrContexts.set(root, ctx);
|
|
113
|
+
}
|
|
114
|
+
return ctx as T;
|
|
115
|
+
}
|
|
116
|
+
return {} as T;
|
|
117
|
+
}
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
14
121
|
type Provides = {
|
|
15
122
|
frame: ComponentContext;
|
|
16
123
|
parent?: Provides;
|
|
17
|
-
values: Map<
|
|
124
|
+
values: Map<PropertyKey, unknown>;
|
|
18
125
|
};
|
|
19
126
|
function layer(frame: ComponentContext): Provides | undefined {
|
|
20
127
|
return frame.contexts.get(providesKey) as Provides | undefined;
|
|
21
128
|
}
|
|
22
|
-
export function provide<T>(
|
|
129
|
+
export function provide<T>(
|
|
130
|
+
key: InjectionKey<T> | string | number,
|
|
131
|
+
value: T,
|
|
132
|
+
): void {
|
|
23
133
|
const frame = getComponentContext();
|
|
24
134
|
let current = layer(frame);
|
|
25
135
|
if (current?.frame !== frame) {
|
|
@@ -44,28 +154,124 @@ export function inject<T>(
|
|
|
44
154
|
defaultValue?: T | (() => T),
|
|
45
155
|
treatDefaultAsFactory = false,
|
|
46
156
|
): T | undefined {
|
|
47
|
-
const frame =
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
157
|
+
const frame = peekComponentContext();
|
|
158
|
+
if (frame?.active && !frame.owner.disposed) {
|
|
159
|
+
let current = layer(frame);
|
|
160
|
+
if (current?.frame === frame) current = current.parent;
|
|
161
|
+
for (; current; current = current.parent) {
|
|
162
|
+
if (current.values.has(key)) return current.values.get(key) as T;
|
|
163
|
+
}
|
|
164
|
+
const appCtx = peekAppContext();
|
|
165
|
+
if (appCtx?.provides.has(key)) {
|
|
166
|
+
return appCtx.provides.get(key) as T;
|
|
167
|
+
}
|
|
168
|
+
return treatDefaultAsFactory && typeof defaultValue === "function"
|
|
169
|
+
? untrack(() => (defaultValue as () => T)())
|
|
170
|
+
: (defaultValue as T | undefined);
|
|
171
|
+
}
|
|
172
|
+
if (activeAppContext) {
|
|
173
|
+
if (activeAppContext.provides.has(key)) {
|
|
174
|
+
return activeAppContext.provides.get(key) as T;
|
|
175
|
+
}
|
|
176
|
+
return treatDefaultAsFactory && typeof defaultValue === "function"
|
|
177
|
+
? untrack(() => (defaultValue as () => T)())
|
|
178
|
+
: (defaultValue as T | undefined);
|
|
52
179
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
: (defaultValue as T | undefined);
|
|
180
|
+
getComponentContext();
|
|
181
|
+
return undefined;
|
|
56
182
|
}
|
|
57
183
|
export function hasInjectionContext(): boolean {
|
|
58
184
|
const frame = peekComponentContext();
|
|
59
|
-
return
|
|
185
|
+
return (
|
|
186
|
+
(!!frame?.active && !frame.owner.disposed) || Boolean(activeAppContext)
|
|
187
|
+
);
|
|
60
188
|
}
|
|
189
|
+
import { getDefaultSlotFactory } from "./slots";
|
|
190
|
+
|
|
61
191
|
export function useId(): string {
|
|
62
|
-
const frame =
|
|
63
|
-
|
|
192
|
+
const frame = peekComponentContext();
|
|
193
|
+
if (!frame?.active && activeAppContext) {
|
|
194
|
+
const prefix = activeAppContext.config.idPrefix || "v";
|
|
195
|
+
return `${prefix}-${activeAppContext.idState.next++}`;
|
|
196
|
+
}
|
|
197
|
+
const activeFrame = getComponentContext();
|
|
198
|
+
const appCtx = peekAppContext();
|
|
199
|
+
const prefix = appCtx?.config.idPrefix || "v";
|
|
200
|
+
let root: Owner = activeFrame.owner;
|
|
64
201
|
while (root.parent) root = root.parent;
|
|
65
202
|
let state = idCounters.get(root);
|
|
66
203
|
if (!state) {
|
|
67
204
|
state = { next: 0 };
|
|
68
205
|
idCounters.set(root, state);
|
|
69
206
|
}
|
|
70
|
-
return
|
|
207
|
+
return `${prefix}-${state.next++}`;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function useAttrs(): Record<string, unknown> {
|
|
211
|
+
const frame = getComponentContext();
|
|
212
|
+
const raw =
|
|
213
|
+
frame.props !== null &&
|
|
214
|
+
typeof frame.props === "object" &&
|
|
215
|
+
!Array.isArray(frame.props)
|
|
216
|
+
? (frame.props as Record<string, unknown>)
|
|
217
|
+
: {};
|
|
218
|
+
return new Proxy(raw, {
|
|
219
|
+
get(target, prop, receiver) {
|
|
220
|
+
if (typeof prop === "symbol") return undefined;
|
|
221
|
+
if (prop === "$slots" || prop === "slots") return undefined;
|
|
222
|
+
return Reflect.get(target, prop, receiver);
|
|
223
|
+
},
|
|
224
|
+
has(target, prop) {
|
|
225
|
+
if (typeof prop === "symbol" || prop === "$slots" || prop === "slots") {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
return prop in target;
|
|
229
|
+
},
|
|
230
|
+
ownKeys(target) {
|
|
231
|
+
return Reflect.ownKeys(target).filter(
|
|
232
|
+
(key): key is string =>
|
|
233
|
+
typeof key === "string" && key !== "$slots" && key !== "slots",
|
|
234
|
+
);
|
|
235
|
+
},
|
|
236
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
237
|
+
if (typeof prop === "symbol" || prop === "$slots" || prop === "slots") {
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
240
|
+
const desc = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
241
|
+
if (desc) return { ...desc, configurable: true };
|
|
242
|
+
return undefined;
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export type Slots = Readonly<
|
|
248
|
+
Record<string, ((...args: any[]) => unknown) | undefined>
|
|
249
|
+
>;
|
|
250
|
+
|
|
251
|
+
export function useSlots<T extends Record<string, any> = Slots>(): T {
|
|
252
|
+
const frame = getComponentContext();
|
|
253
|
+
const raw =
|
|
254
|
+
frame.props !== null &&
|
|
255
|
+
typeof frame.props === "object" &&
|
|
256
|
+
!Array.isArray(frame.props)
|
|
257
|
+
? (frame.props as Record<string, unknown>)
|
|
258
|
+
: {};
|
|
259
|
+
const defaultSlot = getDefaultSlotFactory(raw, frame);
|
|
260
|
+
const explicitSlots =
|
|
261
|
+
(raw.$slots && typeof raw.$slots === "object"
|
|
262
|
+
? (raw.$slots as Record<string, (...args: any[]) => unknown>)
|
|
263
|
+
: undefined) ??
|
|
264
|
+
(raw.slots && typeof raw.slots === "object"
|
|
265
|
+
? (raw.slots as Record<string, (...args: any[]) => unknown>)
|
|
266
|
+
: undefined);
|
|
267
|
+
|
|
268
|
+
const out: Record<string, ((...args: any[]) => unknown) | undefined> = {
|
|
269
|
+
...explicitSlots,
|
|
270
|
+
};
|
|
271
|
+
if (defaultSlot && !out.default) {
|
|
272
|
+
out.default = defaultSlot;
|
|
273
|
+
} else if (typeof raw.children === "function" && !out.default) {
|
|
274
|
+
out.default = raw.children as (...args: any[]) => unknown;
|
|
275
|
+
}
|
|
276
|
+
return Object.freeze(out) as unknown as T;
|
|
71
277
|
}
|
package/src/lifecycle.ts
CHANGED
|
@@ -20,6 +20,11 @@ export type { ErrorCapturedHook } from "./error-context";
|
|
|
20
20
|
type FrameMountQueue = {
|
|
21
21
|
before: Array<() => void>;
|
|
22
22
|
mounted: Array<() => void>;
|
|
23
|
+
beforeUpdate: Array<() => void>;
|
|
24
|
+
updated: Array<() => void>;
|
|
25
|
+
activated: Array<() => void>;
|
|
26
|
+
deactivated: Array<() => void>;
|
|
27
|
+
serverPrefetch: Array<() => Promise<unknown>>;
|
|
23
28
|
};
|
|
24
29
|
const frameQueues = /* @__PURE__ */ new WeakMap<
|
|
25
30
|
ComponentContext,
|
|
@@ -35,7 +40,15 @@ function component(fn: (...args: any[]) => unknown) {
|
|
|
35
40
|
function ensureMountQueue(frame: ComponentContext): FrameMountQueue {
|
|
36
41
|
let queue = frameQueues.get(frame);
|
|
37
42
|
if (!queue) {
|
|
38
|
-
queue = {
|
|
43
|
+
queue = {
|
|
44
|
+
before: [],
|
|
45
|
+
mounted: [],
|
|
46
|
+
beforeUpdate: [],
|
|
47
|
+
updated: [],
|
|
48
|
+
activated: [],
|
|
49
|
+
deactivated: [],
|
|
50
|
+
serverPrefetch: [],
|
|
51
|
+
};
|
|
39
52
|
frameQueues.set(frame, queue);
|
|
40
53
|
const post = getRenderScheduler()?.post ?? queuePostCommit;
|
|
41
54
|
onComponentReady(() => {
|
|
@@ -84,6 +97,72 @@ export function onMounted(fn: () => void): void {
|
|
|
84
97
|
if (frame.mode === "ssr") return;
|
|
85
98
|
ensureMountQueue(frame).mounted.push(fn);
|
|
86
99
|
}
|
|
100
|
+
export function onBeforeUpdate(fn: () => void): void {
|
|
101
|
+
const frame = component(fn);
|
|
102
|
+
if (frame.mode === "ssr") return;
|
|
103
|
+
ensureMountQueue(frame).beforeUpdate.push(fn);
|
|
104
|
+
}
|
|
105
|
+
export function onUpdated(fn: () => void): void {
|
|
106
|
+
const frame = component(fn);
|
|
107
|
+
if (frame.mode === "ssr") return;
|
|
108
|
+
ensureMountQueue(frame).updated.push(fn);
|
|
109
|
+
}
|
|
110
|
+
export function onActivated(fn: () => void): void {
|
|
111
|
+
const frame = component(fn);
|
|
112
|
+
if (frame.mode === "ssr") return;
|
|
113
|
+
const queue = ensureMountQueue(frame);
|
|
114
|
+
queue.activated.push(fn);
|
|
115
|
+
queue.mounted.push(fn);
|
|
116
|
+
}
|
|
117
|
+
export function onDeactivated(fn: () => void): void {
|
|
118
|
+
const frame = component(fn);
|
|
119
|
+
if (frame.mode === "ssr") return;
|
|
120
|
+
ensureMountQueue(frame).deactivated.push(fn);
|
|
121
|
+
onBeforeUnmount(fn);
|
|
122
|
+
}
|
|
123
|
+
export interface ServerPrefetchSession {
|
|
124
|
+
phase: "collect" | "awaiting" | "replay";
|
|
125
|
+
hooks: Array<() => Promise<unknown> | unknown>;
|
|
126
|
+
cells: unknown[];
|
|
127
|
+
cursor: number;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let activeServerPrefetchSession: ServerPrefetchSession | undefined;
|
|
131
|
+
|
|
132
|
+
export function peekServerPrefetchSession(): ServerPrefetchSession | undefined {
|
|
133
|
+
return activeServerPrefetchSession;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function withServerPrefetchSession<T>(
|
|
137
|
+
session: ServerPrefetchSession,
|
|
138
|
+
fn: () => T,
|
|
139
|
+
): T {
|
|
140
|
+
const prev = activeServerPrefetchSession;
|
|
141
|
+
activeServerPrefetchSession = session;
|
|
142
|
+
try {
|
|
143
|
+
return fn();
|
|
144
|
+
} finally {
|
|
145
|
+
activeServerPrefetchSession = prev;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function onServerPrefetch(fn: () => Promise<any>): void {
|
|
150
|
+
const frame = component(fn);
|
|
151
|
+
if (frame.mode !== "ssr") return;
|
|
152
|
+
if (activeServerPrefetchSession) {
|
|
153
|
+
if (activeServerPrefetchSession.phase === "collect") {
|
|
154
|
+
activeServerPrefetchSession.hooks.push(() =>
|
|
155
|
+
Promise.resolve().then(() => untrack(() => fn())),
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!frame.owner.disposed) {
|
|
161
|
+
void Promise.resolve()
|
|
162
|
+
.then(() => untrack(() => fn()))
|
|
163
|
+
.catch(() => {});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
87
166
|
export function onBeforeUnmount(fn: () => void): void {
|
|
88
167
|
const frame = component(fn);
|
|
89
168
|
if (frame.mode === "ssr") return;
|
package/src/map.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/** Independent Map instrumentation on the native graph. */
|
|
2
|
+
import { isTracking } from "@tschk/moonshine/tracking";
|
|
3
|
+
import { batch, createSignal } from "@tschk/moonshine-dom/reactive";
|
|
4
|
+
|
|
5
|
+
type Cell = ReturnType<typeof createSignal<number>>;
|
|
6
|
+
type State = {
|
|
7
|
+
target: Map<unknown, unknown>;
|
|
8
|
+
wrap: (value: unknown) => unknown;
|
|
9
|
+
raw: (value: unknown) => unknown;
|
|
10
|
+
keys: Map<unknown, Cell>;
|
|
11
|
+
objects: WeakMap<object, Cell>;
|
|
12
|
+
iteration?: Cell;
|
|
13
|
+
cleared?: Cell;
|
|
14
|
+
};
|
|
15
|
+
const states = new WeakMap<object, State>();
|
|
16
|
+
function state(value: object): State {
|
|
17
|
+
const found = states.get(value);
|
|
18
|
+
if (!found) throw new TypeError("Map method requires a native reactive Map");
|
|
19
|
+
return found;
|
|
20
|
+
}
|
|
21
|
+
const objectKey = (key: unknown): key is object =>
|
|
22
|
+
(typeof key === "object" && key !== null) || typeof key === "function";
|
|
23
|
+
function cell(s: State, key: unknown, create = false): Cell | undefined {
|
|
24
|
+
let found = objectKey(key) ? s.objects.get(key) : s.keys.get(key);
|
|
25
|
+
if (!found && create) {
|
|
26
|
+
found = createSignal(0);
|
|
27
|
+
if (objectKey(key)) s.objects.set(key, found);
|
|
28
|
+
else s.keys.set(key, found);
|
|
29
|
+
}
|
|
30
|
+
return found;
|
|
31
|
+
}
|
|
32
|
+
function trackKey(s: State, key: unknown): void {
|
|
33
|
+
if (!isTracking()) return;
|
|
34
|
+
(s.cleared ??= createSignal(0))[0]();
|
|
35
|
+
cell(s, key, true)![0]();
|
|
36
|
+
}
|
|
37
|
+
function trackIteration(s: State): void {
|
|
38
|
+
if (isTracking()) (s.iteration ??= createSignal(0))[0]();
|
|
39
|
+
}
|
|
40
|
+
function bump(signal: Cell | undefined): void {
|
|
41
|
+
signal?.[1]((version) => version + 1);
|
|
42
|
+
}
|
|
43
|
+
function resolveTargetKey(
|
|
44
|
+
target: Map<unknown, unknown>,
|
|
45
|
+
key: unknown,
|
|
46
|
+
rawKey: unknown,
|
|
47
|
+
): { found: boolean; targetKey: unknown } {
|
|
48
|
+
if (target.has(key)) return { found: true, targetKey: key };
|
|
49
|
+
if (!Object.is(key, rawKey) && target.has(rawKey)) {
|
|
50
|
+
return { found: true, targetKey: rawKey };
|
|
51
|
+
}
|
|
52
|
+
return { found: false, targetKey: rawKey };
|
|
53
|
+
}
|
|
54
|
+
function iterator(
|
|
55
|
+
receiver: Map<unknown, unknown>,
|
|
56
|
+
mode: "keys" | "values" | "entries",
|
|
57
|
+
): MapIterator<any> {
|
|
58
|
+
const s = state(receiver);
|
|
59
|
+
trackIteration(s);
|
|
60
|
+
return (function* () {
|
|
61
|
+
for (const [key, value] of s.target) {
|
|
62
|
+
if (mode === "keys") {
|
|
63
|
+
yield s.wrap(key);
|
|
64
|
+
} else if (mode === "values") {
|
|
65
|
+
yield s.wrap(value);
|
|
66
|
+
} else {
|
|
67
|
+
yield [s.wrap(key), s.wrap(value)];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return undefined;
|
|
71
|
+
})();
|
|
72
|
+
}
|
|
73
|
+
const methods = {
|
|
74
|
+
get size() {
|
|
75
|
+
const s = state(this);
|
|
76
|
+
trackIteration(s);
|
|
77
|
+
return s.target.size;
|
|
78
|
+
},
|
|
79
|
+
has(this: Map<unknown, unknown>, key: unknown) {
|
|
80
|
+
const s = state(this);
|
|
81
|
+
const rawKey = s.raw(key);
|
|
82
|
+
trackKey(s, key);
|
|
83
|
+
if (!Object.is(key, rawKey)) trackKey(s, rawKey);
|
|
84
|
+
return s.target.has(key) || s.target.has(rawKey);
|
|
85
|
+
},
|
|
86
|
+
get(this: Map<unknown, unknown>, key: unknown) {
|
|
87
|
+
const s = state(this);
|
|
88
|
+
const rawKey = s.raw(key);
|
|
89
|
+
trackKey(s, key);
|
|
90
|
+
if (!Object.is(key, rawKey)) trackKey(s, rawKey);
|
|
91
|
+
const { found, targetKey } = resolveTargetKey(s.target, key, rawKey);
|
|
92
|
+
if (!found) return undefined;
|
|
93
|
+
return s.wrap(s.target.get(targetKey));
|
|
94
|
+
},
|
|
95
|
+
set(this: Map<unknown, unknown>, key: unknown, value: unknown) {
|
|
96
|
+
const s = state(this);
|
|
97
|
+
const rawKey = s.raw(key);
|
|
98
|
+
const rawValue = s.raw(value);
|
|
99
|
+
const { found, targetKey } = resolveTargetKey(s.target, key, rawKey);
|
|
100
|
+
if (found) {
|
|
101
|
+
const oldValue = s.target.get(targetKey);
|
|
102
|
+
s.target.set(targetKey, rawValue);
|
|
103
|
+
if (!Object.is(oldValue, rawValue)) {
|
|
104
|
+
batch(() => {
|
|
105
|
+
bump(cell(s, targetKey));
|
|
106
|
+
if (!Object.is(targetKey, rawKey)) bump(cell(s, rawKey));
|
|
107
|
+
if (!Object.is(targetKey, key)) bump(cell(s, key));
|
|
108
|
+
bump(s.iteration);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
s.target.set(rawKey, rawValue);
|
|
113
|
+
batch(() => {
|
|
114
|
+
bump(cell(s, rawKey));
|
|
115
|
+
if (!Object.is(key, rawKey)) bump(cell(s, key));
|
|
116
|
+
bump(s.iteration);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return this;
|
|
120
|
+
},
|
|
121
|
+
delete(this: Map<unknown, unknown>, key: unknown) {
|
|
122
|
+
const s = state(this);
|
|
123
|
+
const rawKey = s.raw(key);
|
|
124
|
+
const { found, targetKey } = resolveTargetKey(s.target, key, rawKey);
|
|
125
|
+
if (!found) return false;
|
|
126
|
+
const deleted = s.target.delete(targetKey);
|
|
127
|
+
if (deleted) {
|
|
128
|
+
batch(() => {
|
|
129
|
+
bump(cell(s, targetKey));
|
|
130
|
+
if (!Object.is(targetKey, rawKey)) bump(cell(s, rawKey));
|
|
131
|
+
if (!Object.is(targetKey, key)) bump(cell(s, key));
|
|
132
|
+
bump(s.iteration);
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return deleted;
|
|
136
|
+
},
|
|
137
|
+
clear(this: Map<unknown, unknown>) {
|
|
138
|
+
const s = state(this);
|
|
139
|
+
if (!s.target.size) return;
|
|
140
|
+
s.target.clear();
|
|
141
|
+
batch(() => {
|
|
142
|
+
bump(s.cleared);
|
|
143
|
+
bump(s.iteration);
|
|
144
|
+
});
|
|
145
|
+
},
|
|
146
|
+
keys(this: Map<unknown, unknown>) {
|
|
147
|
+
return iterator(this, "keys");
|
|
148
|
+
},
|
|
149
|
+
values(this: Map<unknown, unknown>) {
|
|
150
|
+
return iterator(this, "values");
|
|
151
|
+
},
|
|
152
|
+
entries(this: Map<unknown, unknown>) {
|
|
153
|
+
return iterator(this, "entries");
|
|
154
|
+
},
|
|
155
|
+
[Symbol.iterator](this: Map<unknown, unknown>) {
|
|
156
|
+
return iterator(this, "entries");
|
|
157
|
+
},
|
|
158
|
+
forEach(
|
|
159
|
+
this: Map<unknown, unknown>,
|
|
160
|
+
callback: (
|
|
161
|
+
value: unknown,
|
|
162
|
+
key: unknown,
|
|
163
|
+
map: Map<unknown, unknown>,
|
|
164
|
+
) => void,
|
|
165
|
+
thisArg?: unknown,
|
|
166
|
+
) {
|
|
167
|
+
if (typeof callback !== "function")
|
|
168
|
+
throw new TypeError("Map callback must be a function");
|
|
169
|
+
const s = state(this);
|
|
170
|
+
trackIteration(s);
|
|
171
|
+
s.target.forEach((value, key) => {
|
|
172
|
+
callback.call(thisArg, s.wrap(value), s.wrap(key), this);
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
export function createTrackedMap(
|
|
177
|
+
target: Map<unknown, unknown>,
|
|
178
|
+
wrap: State["wrap"],
|
|
179
|
+
raw: State["raw"],
|
|
180
|
+
): Map<unknown, unknown> {
|
|
181
|
+
const proxy = new Proxy(target, {
|
|
182
|
+
get(source, key, receiver) {
|
|
183
|
+
return Object.hasOwn(methods, key) && key in source
|
|
184
|
+
? Reflect.get(methods, key, receiver)
|
|
185
|
+
: Reflect.get(source, key, receiver);
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
states.set(proxy, {
|
|
189
|
+
target,
|
|
190
|
+
wrap,
|
|
191
|
+
raw,
|
|
192
|
+
keys: new Map(),
|
|
193
|
+
objects: new WeakMap(),
|
|
194
|
+
});
|
|
195
|
+
return proxy;
|
|
196
|
+
}
|