@tschk/moonshine-vue 0.4.4 → 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 +279 -52
- package/package.json +13 -5
- package/src/app.ts +482 -16
- package/src/context.ts +231 -13
- package/src/error-context.ts +53 -3
- package/src/events.ts +30 -2
- package/src/expose.ts +123 -0
- package/src/lifecycle.ts +145 -12
- package/src/map.ts +196 -0
- package/src/model-equality.ts +43 -0
- package/src/model-ssr.ts +39 -0
- package/src/model.ts +222 -0
- package/src/reactive.ts +2419 -116
- package/src/router.ts +1562 -0
- package/src/select-ssr.ts +24 -0
- package/src/select.ts +159 -0
- package/src/server-renderer.ts +257 -0
- package/src/set.ts +145 -0
- package/src/slots.ts +26 -0
- package/src/store.ts +1042 -0
package/src/context.ts
CHANGED
|
@@ -1,24 +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 } 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");
|
|
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
|
+
|
|
13
121
|
type Provides = {
|
|
14
122
|
frame: ComponentContext;
|
|
15
123
|
parent?: Provides;
|
|
16
|
-
values: Map<
|
|
124
|
+
values: Map<PropertyKey, unknown>;
|
|
17
125
|
};
|
|
18
126
|
function layer(frame: ComponentContext): Provides | undefined {
|
|
19
127
|
return frame.contexts.get(providesKey) as Provides | undefined;
|
|
20
128
|
}
|
|
21
|
-
export function provide<T>(
|
|
129
|
+
export function provide<T>(
|
|
130
|
+
key: InjectionKey<T> | string | number,
|
|
131
|
+
value: T,
|
|
132
|
+
): void {
|
|
22
133
|
const frame = getComponentContext();
|
|
23
134
|
let current = layer(frame);
|
|
24
135
|
if (current?.frame !== frame) {
|
|
@@ -43,17 +154,124 @@ export function inject<T>(
|
|
|
43
154
|
defaultValue?: T | (() => T),
|
|
44
155
|
treatDefaultAsFactory = false,
|
|
45
156
|
): T | undefined {
|
|
46
|
-
const frame =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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);
|
|
51
171
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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);
|
|
179
|
+
}
|
|
180
|
+
getComponentContext();
|
|
181
|
+
return undefined;
|
|
55
182
|
}
|
|
56
183
|
export function hasInjectionContext(): boolean {
|
|
57
184
|
const frame = peekComponentContext();
|
|
58
|
-
return
|
|
185
|
+
return (
|
|
186
|
+
(!!frame?.active && !frame.owner.disposed) || Boolean(activeAppContext)
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
import { getDefaultSlotFactory } from "./slots";
|
|
190
|
+
|
|
191
|
+
export function useId(): string {
|
|
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;
|
|
201
|
+
while (root.parent) root = root.parent;
|
|
202
|
+
let state = idCounters.get(root);
|
|
203
|
+
if (!state) {
|
|
204
|
+
state = { next: 0 };
|
|
205
|
+
idCounters.set(root, state);
|
|
206
|
+
}
|
|
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;
|
|
59
277
|
}
|
package/src/error-context.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/** Private callback policy; no Vue app/public-instance ABI. */
|
|
2
2
|
import {
|
|
3
3
|
peekComponentContext,
|
|
4
|
+
setContext,
|
|
5
|
+
withComponentConsumer,
|
|
4
6
|
type ComponentContext,
|
|
5
7
|
} from "@tschk/moonshine-dom/component";
|
|
6
|
-
import { getOwner, untrack } from "@tschk/moonshine-dom/reactive";
|
|
8
|
+
import { getOwner, runWithOwner, untrack } from "@tschk/moonshine-dom/reactive";
|
|
7
9
|
export type WatchErrorPhase =
|
|
8
10
|
"watch getter" | "watch callback" | "watch effect" | "watch cleanup";
|
|
9
11
|
export type ComponentErrorInfo = {
|
|
@@ -14,18 +16,66 @@ export type ComponentErrorHandler = (
|
|
|
14
16
|
error: unknown,
|
|
15
17
|
info: ComponentErrorInfo,
|
|
16
18
|
) => void;
|
|
19
|
+
export type ErrorCapturedHook<TError = unknown> = (
|
|
20
|
+
err: TError,
|
|
21
|
+
instance: ComponentContext | undefined,
|
|
22
|
+
info: WatchErrorPhase,
|
|
23
|
+
) => boolean | void;
|
|
17
24
|
export type ErrorDispatcher = (error: unknown, phase: WatchErrorPhase) => void;
|
|
18
25
|
export const componentErrorKey = /* @__PURE__ */ Symbol(
|
|
19
26
|
"moonshine.vue.callback-error",
|
|
20
27
|
);
|
|
28
|
+
const errorCapturesKey = /* @__PURE__ */ Symbol("moonshine.vue.error-captures");
|
|
29
|
+
type ErrorCaptureLayer = {
|
|
30
|
+
frame: ComponentContext;
|
|
31
|
+
parent?: ErrorCaptureLayer;
|
|
32
|
+
hooks: Array<ErrorCapturedHook<any>>;
|
|
33
|
+
};
|
|
34
|
+
export function registerErrorCaptured(
|
|
35
|
+
frame: ComponentContext,
|
|
36
|
+
hook: ErrorCapturedHook<any>,
|
|
37
|
+
): void {
|
|
38
|
+
let current = frame.contexts.get(errorCapturesKey) as
|
|
39
|
+
ErrorCaptureLayer | undefined;
|
|
40
|
+
if (current?.frame !== frame) {
|
|
41
|
+
current = { frame, parent: current, hooks: [] };
|
|
42
|
+
setContext(errorCapturesKey, current);
|
|
43
|
+
}
|
|
44
|
+
current.hooks.push(hook);
|
|
45
|
+
}
|
|
21
46
|
export function captureWatchErrorHandler(): ErrorDispatcher | undefined {
|
|
22
47
|
const handler = getOwner()?.context?.get(componentErrorKey) as
|
|
23
48
|
ComponentErrorHandler | undefined;
|
|
24
|
-
if (!handler) return;
|
|
25
49
|
const component = peekComponentContext();
|
|
50
|
+
let captures = component?.contexts.get(errorCapturesKey) as
|
|
51
|
+
ErrorCaptureLayer | undefined;
|
|
52
|
+
if (captures && captures.frame === component) captures = captures.parent;
|
|
53
|
+
if (!handler && !captures) return;
|
|
26
54
|
return (error, phase) => {
|
|
55
|
+
let currentError: unknown = error;
|
|
56
|
+
for (let layer = captures; layer; layer = layer.parent) {
|
|
57
|
+
if (layer.frame.owner.disposed) continue;
|
|
58
|
+
for (const hook of layer.hooks.slice()) {
|
|
59
|
+
if (layer.frame.owner.disposed) break;
|
|
60
|
+
let stopPropagation: boolean | void;
|
|
61
|
+
try {
|
|
62
|
+
stopPropagation = runWithOwner(layer.frame.owner, () =>
|
|
63
|
+
untrack(() =>
|
|
64
|
+
withComponentConsumer(layer.frame, () =>
|
|
65
|
+
hook(currentError, component, phase),
|
|
66
|
+
),
|
|
67
|
+
),
|
|
68
|
+
);
|
|
69
|
+
} catch (secondary) {
|
|
70
|
+
currentError = secondary;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (stopPropagation === false) return;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!handler) throw currentError;
|
|
27
77
|
try {
|
|
28
|
-
untrack(() => handler(
|
|
78
|
+
untrack(() => handler(currentError, { component, phase }));
|
|
29
79
|
} catch (secondary) {
|
|
30
80
|
console.error(secondary);
|
|
31
81
|
}
|
package/src/events.ts
CHANGED
|
@@ -23,9 +23,20 @@ export type Emit<E extends EventMap<E>> = <K extends keyof E & string>(
|
|
|
23
23
|
) => void;
|
|
24
24
|
const camel = (name: string) =>
|
|
25
25
|
name.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase());
|
|
26
|
+
const modifiersOf = (source: object, model: string) => {
|
|
27
|
+
const read = (name: string) =>
|
|
28
|
+
Reflect.get(source, name + "Modifiers", source);
|
|
29
|
+
return model === "modelValue" || model === "model-value"
|
|
30
|
+
? Reflect.get(source, "modelModifiers", source) ||
|
|
31
|
+
read("modelValue") ||
|
|
32
|
+
read("model-value")
|
|
33
|
+
: read(model) ||
|
|
34
|
+
read(camel(model)) ||
|
|
35
|
+
read(model.replace(/\B([A-Z])/g, "-$1").toLowerCase());
|
|
36
|
+
};
|
|
26
37
|
const key = (name: string) =>
|
|
27
38
|
name ? "on" + name[0]!.toUpperCase() + name.slice(1) : "";
|
|
28
|
-
/** Compiler-private declared component events; no DOM dispatch
|
|
39
|
+
/** Compiler-private declared component events; no DOM dispatch or Vue app error API. `update:*` emits honor live trim/number modifier flags. */
|
|
29
40
|
export function createEmitter<E extends EventMap<E>>(source: object): Emit<E> {
|
|
30
41
|
const frame = getComponentContext();
|
|
31
42
|
const route = frame.owner.context?.get(errorHandlerKey) as
|
|
@@ -34,9 +45,26 @@ export function createEmitter<E extends EventMap<E>>(source: object): Emit<E> {
|
|
|
34
45
|
const report = (error: unknown) => {
|
|
35
46
|
if (!route?.(error)) throw error;
|
|
36
47
|
};
|
|
37
|
-
return ((event: string, ...
|
|
48
|
+
return ((event: string, ...rawArgs: readonly unknown[]) => {
|
|
38
49
|
if (frame.mode === "dom" && frame.owner.disposed) return;
|
|
39
50
|
return untrack(() => {
|
|
51
|
+
let args = rawArgs;
|
|
52
|
+
if (event.startsWith("update:")) {
|
|
53
|
+
const modifiers = modifiersOf(source, event.slice(7)) as
|
|
54
|
+
{ trim?: unknown; number?: unknown } | undefined;
|
|
55
|
+
if (modifiers) {
|
|
56
|
+
if (modifiers.trim)
|
|
57
|
+
args = rawArgs.map((value) =>
|
|
58
|
+
typeof value === "string" ? value.trim() : value,
|
|
59
|
+
);
|
|
60
|
+
// Number conversion uses raw arguments even when trim also runs.
|
|
61
|
+
if (modifiers.number)
|
|
62
|
+
args = rawArgs.map((value) => {
|
|
63
|
+
const parsed = parseFloat(value as string);
|
|
64
|
+
return Number.isNaN(parsed) ? value : parsed;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
40
68
|
let name = key(event),
|
|
41
69
|
listener = Reflect.get(source, name, source);
|
|
42
70
|
if (!listener) {
|
package/src/expose.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bindOwnedReference,
|
|
3
|
+
enqueueComponentReference,
|
|
4
|
+
type ComponentReference,
|
|
5
|
+
} from "@tschk/moonshine-dom/component-instance";
|
|
6
|
+
import { getOwner } from "@tschk/moonshine-dom/reactive";
|
|
7
|
+
import {
|
|
8
|
+
getTemplateRefBinding,
|
|
9
|
+
isRef,
|
|
10
|
+
unref,
|
|
11
|
+
useTemplateRef,
|
|
12
|
+
type Ref,
|
|
13
|
+
type TemplateRef,
|
|
14
|
+
} from "./reactive";
|
|
15
|
+
|
|
16
|
+
export { useTemplateRef, type TemplateRef };
|
|
17
|
+
|
|
18
|
+
export type ShallowUnwrap<T> = T extends () => infer R
|
|
19
|
+
? { [K in keyof R]: R[K] extends Readonly<Ref<infer V>> ? V : R[K] }
|
|
20
|
+
: { [K in keyof T]: T[K] extends Readonly<Ref<infer V>> ? V : T[K] };
|
|
21
|
+
|
|
22
|
+
export type TemplateRefBinding<T> =
|
|
23
|
+
| {
|
|
24
|
+
get: () => T | null | undefined;
|
|
25
|
+
set: (value: T | null) => void;
|
|
26
|
+
}
|
|
27
|
+
| Readonly<Ref<T | null>>;
|
|
28
|
+
|
|
29
|
+
function resolveRefBinding<T>(binding: TemplateRefBinding<T>): {
|
|
30
|
+
get: () => T | null | undefined;
|
|
31
|
+
set: (value: T | null) => void;
|
|
32
|
+
} {
|
|
33
|
+
if (isRef(binding)) {
|
|
34
|
+
const internal = getTemplateRefBinding(binding);
|
|
35
|
+
if (internal) return internal;
|
|
36
|
+
return {
|
|
37
|
+
get: () => binding.value,
|
|
38
|
+
set: (value: T | null) => {
|
|
39
|
+
(binding as Ref<T | null>).value = value;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
const accessor = binding as {
|
|
44
|
+
get?: () => T | null | undefined;
|
|
45
|
+
set?: (value: T | null) => void;
|
|
46
|
+
};
|
|
47
|
+
if (
|
|
48
|
+
!accessor ||
|
|
49
|
+
typeof accessor.get !== "function" ||
|
|
50
|
+
typeof accessor.set !== "function"
|
|
51
|
+
)
|
|
52
|
+
throw new TypeError("Template ref binding requires get and set functions");
|
|
53
|
+
return { get: accessor.get, set: accessor.set };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Wrap a plain defineExpose object so own enumerable keys unwrap and write through refs. */
|
|
57
|
+
export function exposeRefs<T extends object>(source: T): ShallowUnwrap<T> {
|
|
58
|
+
if (
|
|
59
|
+
source === null ||
|
|
60
|
+
typeof source !== "object" ||
|
|
61
|
+
(Object.getPrototypeOf(source) !== Object.prototype &&
|
|
62
|
+
Object.getPrototypeOf(source) !== null)
|
|
63
|
+
)
|
|
64
|
+
throw new TypeError(
|
|
65
|
+
"Component exports must be a plain object of named exports",
|
|
66
|
+
);
|
|
67
|
+
const exposed = Object.create(null) as ShallowUnwrap<T>;
|
|
68
|
+
for (const key of Reflect.ownKeys(source)) {
|
|
69
|
+
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
70
|
+
Object.defineProperty(exposed, key, {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
configurable: true,
|
|
73
|
+
get() {
|
|
74
|
+
return unref(Reflect.get(source, key));
|
|
75
|
+
},
|
|
76
|
+
set(next: unknown) {
|
|
77
|
+
const previous = Reflect.get(source, key);
|
|
78
|
+
if (isRef(previous) && !isRef(next)) {
|
|
79
|
+
previous.value = next;
|
|
80
|
+
} else if (!Reflect.set(source, key, next)) {
|
|
81
|
+
throw new TypeError(
|
|
82
|
+
`Cannot assign to read only property '${String(key)}'`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return exposed;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Bind an intrinsic element reference into the shared component reference flush wave. */
|
|
92
|
+
export function bindRef<T extends Element>(
|
|
93
|
+
element: T,
|
|
94
|
+
binding: TemplateRefBinding<T>,
|
|
95
|
+
): void {
|
|
96
|
+
const owner = getOwner();
|
|
97
|
+
if (!owner || owner.disposed) return;
|
|
98
|
+
const resolved = resolveRefBinding(binding);
|
|
99
|
+
enqueueComponentReference(
|
|
100
|
+
bindOwnedReference(
|
|
101
|
+
owner,
|
|
102
|
+
element,
|
|
103
|
+
{
|
|
104
|
+
cleanup: "commit",
|
|
105
|
+
get: resolved.get,
|
|
106
|
+
set: resolved.set,
|
|
107
|
+
},
|
|
108
|
+
enqueueComponentReference,
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Create a commit-cleaned component reference binding for a setup ref or useTemplateRef. */
|
|
114
|
+
export function bindComponentRef<T extends object>(
|
|
115
|
+
binding: TemplateRefBinding<T>,
|
|
116
|
+
): ComponentReference<T> {
|
|
117
|
+
const resolved = resolveRefBinding(binding);
|
|
118
|
+
return {
|
|
119
|
+
cleanup: "commit",
|
|
120
|
+
get: resolved.get,
|
|
121
|
+
set: resolved.set,
|
|
122
|
+
};
|
|
123
|
+
}
|