@pyreon/vue-compat 0.1.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/LICENSE +21 -0
- package/README.md +86 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/index.js +309 -0
- package/lib/index.js.map +1 -0
- package/lib/types/index.d.ts +309 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +189 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +49 -0
- package/src/index.ts +481 -0
- package/src/tests/setup.ts +3 -0
- package/src/tests/vue-compat.test.ts +698 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { Fragment, createContext, h as pyreonH, onMount, onUnmount, onUpdate, popContext, pushContext, useContext } from "@pyreon/core";
|
|
2
|
+
import { batch, computed as computed$1, createStore, effect, nextTick as nextTick$1, signal } from "@pyreon/reactivity";
|
|
3
|
+
import { mount } from "@pyreon/runtime-dom";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const V_IS_REF = Symbol("__v_isRef");
|
|
7
|
+
const V_IS_READONLY = Symbol("__v_isReadonly");
|
|
8
|
+
const V_RAW = Symbol("__v_raw");
|
|
9
|
+
/**
|
|
10
|
+
* Creates a reactive ref wrapping the given value.
|
|
11
|
+
* Access via `.value` — reads track, writes trigger.
|
|
12
|
+
*
|
|
13
|
+
* Difference from Vue: backed by a Pyreon signal. No `__v_isShallow` distinction
|
|
14
|
+
* at runtime since Pyreon signals are always shallow (deep reactivity is via stores).
|
|
15
|
+
*/
|
|
16
|
+
function ref(value) {
|
|
17
|
+
const s = signal(value);
|
|
18
|
+
return {
|
|
19
|
+
[V_IS_REF]: true,
|
|
20
|
+
get value() {
|
|
21
|
+
return s();
|
|
22
|
+
},
|
|
23
|
+
set value(newValue) {
|
|
24
|
+
s.set(newValue);
|
|
25
|
+
},
|
|
26
|
+
_signal: s
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates a shallow ref — same as `ref()` in Pyreon since signals are inherently shallow.
|
|
31
|
+
*
|
|
32
|
+
* Difference from Vue: identical to `ref()` — Pyreon signals don't perform deep conversion.
|
|
33
|
+
*/
|
|
34
|
+
function shallowRef(value) {
|
|
35
|
+
return ref(value);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Force trigger a ref's subscribers, even if the value hasn't changed.
|
|
39
|
+
*/
|
|
40
|
+
function triggerRef(r) {
|
|
41
|
+
const internal = r;
|
|
42
|
+
if (internal._signal) {
|
|
43
|
+
const current = internal._signal.peek();
|
|
44
|
+
internal._signal.set(void 0);
|
|
45
|
+
internal._signal.set(current);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Returns `true` if the value is a ref (created by `ref()` or `computed()`).
|
|
50
|
+
*/
|
|
51
|
+
function isRef(val) {
|
|
52
|
+
return val !== null && typeof val === "object" && val[V_IS_REF] === true;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Unwraps a ref: if it has `.value`, return `.value`; otherwise return as-is.
|
|
56
|
+
*/
|
|
57
|
+
function unref(r) {
|
|
58
|
+
return isRef(r) ? r.value : r;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Creates a computed ref. Supports both readonly and writable forms:
|
|
62
|
+
* - `computed(() => value)` — readonly
|
|
63
|
+
* - `computed({ get: () => value, set: (v) => ... })` — writable
|
|
64
|
+
*
|
|
65
|
+
* Backed by Pyreon's `computed()`, wrapped in a `.value` accessor.
|
|
66
|
+
*/
|
|
67
|
+
function computed(fnOrOptions) {
|
|
68
|
+
const getter = typeof fnOrOptions === "function" ? fnOrOptions : fnOrOptions.get;
|
|
69
|
+
const setter = typeof fnOrOptions === "object" ? fnOrOptions.set : void 0;
|
|
70
|
+
const c = computed$1(getter);
|
|
71
|
+
return {
|
|
72
|
+
[V_IS_REF]: true,
|
|
73
|
+
get value() {
|
|
74
|
+
return c();
|
|
75
|
+
},
|
|
76
|
+
set value(v) {
|
|
77
|
+
if (!setter) throw new Error("Cannot set value of a computed ref — computed refs are readonly");
|
|
78
|
+
setter(v);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Creates a deeply reactive proxy from a plain object.
|
|
84
|
+
* Backed by Pyreon's `createStore()`.
|
|
85
|
+
*
|
|
86
|
+
* Difference from Vue: uses Pyreon's fine-grained per-property signals.
|
|
87
|
+
* Direct mutation triggers only affected signals.
|
|
88
|
+
*/
|
|
89
|
+
function reactive(obj) {
|
|
90
|
+
const proxy = createStore(obj);
|
|
91
|
+
rawMap.set(proxy, obj);
|
|
92
|
+
return proxy;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a shallow reactive proxy.
|
|
96
|
+
* In Pyreon, `createStore` is already per-property (not deeply recursive for primitives),
|
|
97
|
+
* but nested objects will be wrapped. For truly shallow behavior, use individual refs.
|
|
98
|
+
*
|
|
99
|
+
* Difference from Vue: backed by `createStore()` — same as `reactive()` in practice.
|
|
100
|
+
*/
|
|
101
|
+
function shallowReactive(obj) {
|
|
102
|
+
return reactive(obj);
|
|
103
|
+
}
|
|
104
|
+
const rawMap = /* @__PURE__ */ new WeakMap();
|
|
105
|
+
/**
|
|
106
|
+
* Returns a readonly proxy that throws on mutation attempts.
|
|
107
|
+
*
|
|
108
|
+
* Difference from Vue: uses a simple Proxy with a set trap that throws,
|
|
109
|
+
* rather than Vue's full readonly reactive system.
|
|
110
|
+
*/
|
|
111
|
+
function readonly(obj) {
|
|
112
|
+
return new Proxy(obj, {
|
|
113
|
+
get(target, key) {
|
|
114
|
+
if (key === V_IS_READONLY) return true;
|
|
115
|
+
if (key === V_RAW) return target;
|
|
116
|
+
return Reflect.get(target, key);
|
|
117
|
+
},
|
|
118
|
+
set(_target, key) {
|
|
119
|
+
if (key === V_IS_READONLY || key === V_RAW) return true;
|
|
120
|
+
throw new Error(`Cannot set property "${String(key)}" on a readonly object`);
|
|
121
|
+
},
|
|
122
|
+
deleteProperty(_target, key) {
|
|
123
|
+
throw new Error(`Cannot delete property "${String(key)}" from a readonly object`);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns the raw (unwrapped) object behind a reactive or readonly proxy.
|
|
129
|
+
*
|
|
130
|
+
* Difference from Vue: only works for objects created via `reactive()` or `readonly()`.
|
|
131
|
+
*/
|
|
132
|
+
function toRaw(proxy) {
|
|
133
|
+
const readonlyRaw = proxy[V_RAW];
|
|
134
|
+
if (readonlyRaw) return readonlyRaw;
|
|
135
|
+
return rawMap.get(proxy) ?? proxy;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Creates a ref linked to a property of a reactive object.
|
|
139
|
+
* Reading/writing the ref's `.value` reads/writes the original property.
|
|
140
|
+
*/
|
|
141
|
+
function toRef(obj, key) {
|
|
142
|
+
return {
|
|
143
|
+
[V_IS_REF]: true,
|
|
144
|
+
get value() {
|
|
145
|
+
return obj[key];
|
|
146
|
+
},
|
|
147
|
+
set value(newValue) {
|
|
148
|
+
obj[key] = newValue;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Converts all properties of a reactive object into individual refs.
|
|
154
|
+
* Each ref is linked to the original property (not a copy).
|
|
155
|
+
*/
|
|
156
|
+
function toRefs(obj) {
|
|
157
|
+
const result = {};
|
|
158
|
+
for (const key of Object.keys(obj)) result[key] = toRef(obj, key);
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Watches a reactive source and calls `cb` when it changes.
|
|
163
|
+
* Tracks old and new values.
|
|
164
|
+
*
|
|
165
|
+
* Difference from Vue: `deep` option is ignored — Pyreon tracks dependencies automatically.
|
|
166
|
+
* Returns a stop function to dispose the watcher.
|
|
167
|
+
*/
|
|
168
|
+
function watch(source, cb, options) {
|
|
169
|
+
const getter = isRef(source) ? () => source.value : source;
|
|
170
|
+
let oldValue;
|
|
171
|
+
let initialized = false;
|
|
172
|
+
if (options?.immediate) {
|
|
173
|
+
oldValue = void 0;
|
|
174
|
+
const current = getter();
|
|
175
|
+
cb(current, oldValue);
|
|
176
|
+
oldValue = current;
|
|
177
|
+
initialized = true;
|
|
178
|
+
}
|
|
179
|
+
const e = effect(() => {
|
|
180
|
+
const newValue = getter();
|
|
181
|
+
if (initialized) cb(newValue, oldValue);
|
|
182
|
+
oldValue = newValue;
|
|
183
|
+
initialized = true;
|
|
184
|
+
});
|
|
185
|
+
return () => e.dispose();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Runs the given function reactively — re-executes whenever its tracked
|
|
189
|
+
* dependencies change.
|
|
190
|
+
*
|
|
191
|
+
* Difference from Vue: identical to Pyreon's `effect()`.
|
|
192
|
+
* Returns a stop function.
|
|
193
|
+
*/
|
|
194
|
+
function watchEffect(fn) {
|
|
195
|
+
const e = effect(fn);
|
|
196
|
+
return () => e.dispose();
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Registers a callback to run after the component is mounted.
|
|
200
|
+
*
|
|
201
|
+
* Difference from Vue: maps directly to Pyreon's `onMount()`.
|
|
202
|
+
* In Pyreon there is no distinction between beforeMount and mounted.
|
|
203
|
+
*/
|
|
204
|
+
function onMounted(fn) {
|
|
205
|
+
onMount(() => {
|
|
206
|
+
fn();
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Registers a callback to run before the component is unmounted.
|
|
211
|
+
*
|
|
212
|
+
* Difference from Vue: maps to Pyreon's `onUnmount()`.
|
|
213
|
+
* In Pyreon there is no distinction between beforeUnmount and unmounted.
|
|
214
|
+
*/
|
|
215
|
+
function onUnmounted(fn) {
|
|
216
|
+
onUnmount(fn);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Registers a callback to run after a reactive update.
|
|
220
|
+
*
|
|
221
|
+
* Difference from Vue: maps to Pyreon's `onUpdate()`.
|
|
222
|
+
*/
|
|
223
|
+
function onUpdated(fn) {
|
|
224
|
+
onUpdate(fn);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Registers a callback to run before mount.
|
|
228
|
+
* In Pyreon there is no pre-mount phase — maps to `onMount()`.
|
|
229
|
+
*/
|
|
230
|
+
function onBeforeMount(fn) {
|
|
231
|
+
onMount(() => {
|
|
232
|
+
fn();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Registers a callback to run before unmount.
|
|
237
|
+
* In Pyreon there is no pre-unmount phase — maps to `onUnmount()`.
|
|
238
|
+
*/
|
|
239
|
+
function onBeforeUnmount(fn) {
|
|
240
|
+
onUnmount(fn);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Returns a Promise that resolves after all pending reactive updates have flushed.
|
|
244
|
+
*
|
|
245
|
+
* Difference from Vue: identical to Pyreon's `nextTick()`.
|
|
246
|
+
*/
|
|
247
|
+
function nextTick() {
|
|
248
|
+
return nextTick$1();
|
|
249
|
+
}
|
|
250
|
+
const _contextRegistry = /* @__PURE__ */ new Map();
|
|
251
|
+
function getOrCreateContext(key, defaultValue) {
|
|
252
|
+
if (!_contextRegistry.has(key)) _contextRegistry.set(key, createContext(defaultValue));
|
|
253
|
+
return _contextRegistry.get(key);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Provides a value to all descendant components.
|
|
257
|
+
*
|
|
258
|
+
* Difference from Vue: backed by Pyreon's context stack (pushContext/popContext).
|
|
259
|
+
* Must be called during component setup. The value is scoped to the component
|
|
260
|
+
* tree — not globally shared.
|
|
261
|
+
*/
|
|
262
|
+
function provide(key, value) {
|
|
263
|
+
const ctx = getOrCreateContext(key);
|
|
264
|
+
pushContext(new Map([[ctx.id, value]]));
|
|
265
|
+
onUnmount(() => popContext());
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Injects a value provided by an ancestor component.
|
|
269
|
+
*
|
|
270
|
+
* Difference from Vue: backed by Pyreon's context system (useContext).
|
|
271
|
+
*/
|
|
272
|
+
function inject(key, defaultValue) {
|
|
273
|
+
const value = useContext(getOrCreateContext(key));
|
|
274
|
+
return value !== void 0 ? value : defaultValue;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Defines a component using Vue 3 Composition API style.
|
|
278
|
+
* Only supports the `setup()` function — Options API is not supported.
|
|
279
|
+
*
|
|
280
|
+
* Difference from Vue: returns a Pyreon `ComponentFn`. No template/render option —
|
|
281
|
+
* the setup function should return a render function or VNode directly.
|
|
282
|
+
*/
|
|
283
|
+
function defineComponent(options) {
|
|
284
|
+
if (typeof options === "function") return options;
|
|
285
|
+
const comp = (props) => {
|
|
286
|
+
const result = options.setup(props);
|
|
287
|
+
if (typeof result === "function") return result();
|
|
288
|
+
return result;
|
|
289
|
+
};
|
|
290
|
+
if (options.name) Object.defineProperty(comp, "name", { value: options.name });
|
|
291
|
+
return comp;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Creates a Pyreon application instance — Vue 3 `createApp()` compatible.
|
|
295
|
+
*
|
|
296
|
+
* Difference from Vue: does not support plugins, directives, or global config.
|
|
297
|
+
* The component receives `props` if provided.
|
|
298
|
+
*/
|
|
299
|
+
function createApp(component, props) {
|
|
300
|
+
return { mount(el) {
|
|
301
|
+
const container = typeof el === "string" ? document.querySelector(el) : el;
|
|
302
|
+
if (!container) throw new Error(`Cannot find mount target: ${el}`);
|
|
303
|
+
return mount(pyreonH(component, props ?? null), container);
|
|
304
|
+
} };
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
//#endregion
|
|
308
|
+
export { Fragment, batch, computed, createApp, defineComponent, pyreonH as h, inject, isRef, nextTick, onBeforeMount, onBeforeUnmount, onMounted, onUnmounted, onUpdated, provide, reactive, readonly, ref, shallowReactive, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, watch, watchEffect };
|
|
309
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["pyreonComputed","pyreonNextTick","pyreonMount"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * @pyreon/vue-compat\n *\n * Vue 3-compatible Composition API that runs on Pyreon's reactive engine.\n *\n * Allows you to write familiar Vue 3 Composition API code while getting Pyreon's\n * fine-grained reactivity and superior performance.\n *\n * DIFFERENCES FROM VUE 3:\n * - `deep` option in watch() is ignored — Pyreon tracks dependencies automatically.\n * - `shallowReactive` uses per-property signals (still shallow, but Pyreon-flavored).\n * - `readonly` returns a Proxy that throws on set (not Vue's readonly proxy).\n * - `defineComponent` only supports Composition API (setup function), not Options API.\n * - Components run ONCE (setup phase), not on every render.\n *\n * USAGE:\n * Replace `import { ref, computed, watch } from \"vue\"` with\n * `import { ref, computed, watch } from \"@pyreon/vue-compat\"`\n */\n\nimport type { ComponentFn, Props, VNodeChild } from \"@pyreon/core\"\nimport {\n createContext,\n Fragment,\n onMount,\n onUnmount,\n onUpdate,\n popContext,\n pushContext,\n h as pyreonH,\n useContext,\n} from \"@pyreon/core\"\nimport {\n createStore,\n effect,\n computed as pyreonComputed,\n nextTick as pyreonNextTick,\n type Signal,\n signal,\n} from \"@pyreon/reactivity\"\nimport { mount as pyreonMount } from \"@pyreon/runtime-dom\"\n\n// ─── Internal symbols ─────────────────────────────────────────────────────────\n\nconst V_IS_REF = Symbol(\"__v_isRef\")\nconst V_IS_READONLY = Symbol(\"__v_isReadonly\")\nconst V_RAW = Symbol(\"__v_raw\")\n\n// ─── Ref ──────────────────────────────────────────────────────────────────────\n\nexport interface Ref<T = unknown> {\n value: T\n readonly [V_IS_REF]: true\n}\n\n/**\n * Creates a reactive ref wrapping the given value.\n * Access via `.value` — reads track, writes trigger.\n *\n * Difference from Vue: backed by a Pyreon signal. No `__v_isShallow` distinction\n * at runtime since Pyreon signals are always shallow (deep reactivity is via stores).\n */\nexport function ref<T>(value: T): Ref<T> {\n const s = signal(value)\n const r = {\n [V_IS_REF]: true as const,\n get value(): T {\n return s()\n },\n set value(newValue: T) {\n s.set(newValue)\n },\n /** @internal — access underlying signal for triggerRef */\n _signal: s,\n }\n return r as Ref<T>\n}\n\n/**\n * Creates a shallow ref — same as `ref()` in Pyreon since signals are inherently shallow.\n *\n * Difference from Vue: identical to `ref()` — Pyreon signals don't perform deep conversion.\n */\nexport function shallowRef<T>(value: T): Ref<T> {\n return ref(value)\n}\n\n/**\n * Force trigger a ref's subscribers, even if the value hasn't changed.\n */\nexport function triggerRef<T>(r: Ref<T>): void {\n const internal = r as Ref<T> & { _signal: Signal<T> }\n if (internal._signal) {\n // Force notify by setting the same value with Object.is bypass\n const current = internal._signal.peek()\n internal._signal.set(undefined as T)\n internal._signal.set(current)\n }\n}\n\n/**\n * Returns `true` if the value is a ref (created by `ref()` or `computed()`).\n */\nexport function isRef(val: unknown): val is Ref {\n return (\n val !== null && typeof val === \"object\" && (val as Record<symbol, unknown>)[V_IS_REF] === true\n )\n}\n\n/**\n * Unwraps a ref: if it has `.value`, return `.value`; otherwise return as-is.\n */\nexport function unref<T>(r: T | Ref<T>): T {\n return isRef(r) ? r.value : r\n}\n\n// ─── Computed ─────────────────────────────────────────────────────────────────\n\nexport interface ComputedRef<T = unknown> extends Ref<T> {\n readonly value: T\n}\n\n/**\n * Creates a computed ref. Supports both readonly and writable forms:\n * - `computed(() => value)` — readonly\n * - `computed({ get: () => value, set: (v) => ... })` — writable\n *\n * Backed by Pyreon's `computed()`, wrapped in a `.value` accessor.\n */\nexport function computed<T>(\n fnOrOptions: (() => T) | { get: () => T; set: (value: T) => void },\n): ComputedRef<T> {\n const getter = typeof fnOrOptions === \"function\" ? fnOrOptions : fnOrOptions.get\n const setter = typeof fnOrOptions === \"object\" ? fnOrOptions.set : undefined\n const c = pyreonComputed(getter)\n const r = {\n [V_IS_REF]: true as const,\n get value(): T {\n return c()\n },\n set value(v: T) {\n if (!setter) {\n throw new Error(\"Cannot set value of a computed ref — computed refs are readonly\")\n }\n setter(v)\n },\n }\n return r as ComputedRef<T>\n}\n\n// ─── Reactive / Readonly ──────────────────────────────────────────────────────\n\n/**\n * Creates a deeply reactive proxy from a plain object.\n * Backed by Pyreon's `createStore()`.\n *\n * Difference from Vue: uses Pyreon's fine-grained per-property signals.\n * Direct mutation triggers only affected signals.\n */\nexport function reactive<T extends object>(obj: T): T {\n const proxy = createStore(obj)\n // Store raw reference for toRaw()\n rawMap.set(proxy as object, obj)\n return proxy\n}\n\n/**\n * Creates a shallow reactive proxy.\n * In Pyreon, `createStore` is already per-property (not deeply recursive for primitives),\n * but nested objects will be wrapped. For truly shallow behavior, use individual refs.\n *\n * Difference from Vue: backed by `createStore()` — same as `reactive()` in practice.\n */\nexport function shallowReactive<T extends object>(obj: T): T {\n return reactive(obj)\n}\n\n// WeakMap to track raw objects behind reactive proxies\nconst rawMap = new WeakMap<object, object>()\n\n/**\n * Returns a readonly proxy that throws on mutation attempts.\n *\n * Difference from Vue: uses a simple Proxy with a set trap that throws,\n * rather than Vue's full readonly reactive system.\n */\nexport function readonly<T extends object>(obj: T): Readonly<T> {\n const proxy = new Proxy(obj, {\n get(target, key) {\n if (key === V_IS_READONLY) return true\n if (key === V_RAW) return target\n return Reflect.get(target, key)\n },\n set(_target, key) {\n // Internal symbols used for identification are allowed\n if (key === V_IS_READONLY || key === V_RAW) return true\n throw new Error(`Cannot set property \"${String(key)}\" on a readonly object`)\n },\n deleteProperty(_target, key) {\n throw new Error(`Cannot delete property \"${String(key)}\" from a readonly object`)\n },\n })\n return proxy as Readonly<T>\n}\n\n/**\n * Returns the raw (unwrapped) object behind a reactive or readonly proxy.\n *\n * Difference from Vue: only works for objects created via `reactive()` or `readonly()`.\n */\nexport function toRaw<T extends object>(proxy: T): T {\n // Check readonly first\n const readonlyRaw = (proxy as Record<symbol, unknown>)[V_RAW]\n if (readonlyRaw) return readonlyRaw as T\n // Check reactive\n const raw = rawMap.get(proxy as object)\n return (raw as T) ?? proxy\n}\n\n// ─── toRef / toRefs ───────────────────────────────────────────────────────────\n\n/**\n * Creates a ref linked to a property of a reactive object.\n * Reading/writing the ref's `.value` reads/writes the original property.\n */\nexport function toRef<T extends object, K extends keyof T>(obj: T, key: K): Ref<T[K]> {\n const r = {\n [V_IS_REF]: true as const,\n get value(): T[K] {\n return obj[key]\n },\n set value(newValue: T[K]) {\n obj[key] = newValue\n },\n }\n return r as Ref<T[K]>\n}\n\n/**\n * Converts all properties of a reactive object into individual refs.\n * Each ref is linked to the original property (not a copy).\n */\nexport function toRefs<T extends object>(obj: T): { [K in keyof T]: Ref<T[K]> } {\n const result = {} as { [K in keyof T]: Ref<T[K]> }\n for (const key of Object.keys(obj) as (keyof T)[]) {\n result[key] = toRef(obj, key)\n }\n return result\n}\n\n// ─── Watch ────────────────────────────────────────────────────────────────────\n\nexport interface WatchOptions {\n /** Call the callback immediately with current value. Default: false */\n immediate?: boolean\n /** Ignored in Pyreon — dependencies are tracked automatically. */\n deep?: boolean\n}\n\ntype WatchSource<T> = Ref<T> | (() => T)\n\n/**\n * Watches a reactive source and calls `cb` when it changes.\n * Tracks old and new values.\n *\n * Difference from Vue: `deep` option is ignored — Pyreon tracks dependencies automatically.\n * Returns a stop function to dispose the watcher.\n */\nexport function watch<T>(\n source: WatchSource<T>,\n cb: (newValue: T, oldValue: T | undefined) => void,\n options?: WatchOptions,\n): () => void {\n const getter = isRef(source) ? () => source.value : (source as () => T)\n let oldValue: T | undefined\n let initialized = false\n\n if (options?.immediate) {\n oldValue = undefined\n const current = getter()\n cb(current, oldValue)\n oldValue = current\n initialized = true\n }\n\n const e = effect(() => {\n const newValue = getter()\n if (initialized) {\n // Only call cb if value actually changed (or on first tracked run)\n cb(newValue, oldValue)\n }\n oldValue = newValue\n initialized = true\n })\n\n return () => e.dispose()\n}\n\n/**\n * Runs the given function reactively — re-executes whenever its tracked\n * dependencies change.\n *\n * Difference from Vue: identical to Pyreon's `effect()`.\n * Returns a stop function.\n */\nexport function watchEffect(fn: () => void): () => void {\n const e = effect(fn)\n return () => e.dispose()\n}\n\n// ─── Lifecycle ────────────────────────────────────────────────────────────────\n\n/**\n * Registers a callback to run after the component is mounted.\n *\n * Difference from Vue: maps directly to Pyreon's `onMount()`.\n * In Pyreon there is no distinction between beforeMount and mounted.\n */\nexport function onMounted(fn: () => void): void {\n onMount(() => {\n fn()\n return undefined\n })\n}\n\n/**\n * Registers a callback to run before the component is unmounted.\n *\n * Difference from Vue: maps to Pyreon's `onUnmount()`.\n * In Pyreon there is no distinction between beforeUnmount and unmounted.\n */\nexport function onUnmounted(fn: () => void): void {\n onUnmount(fn)\n}\n\n/**\n * Registers a callback to run after a reactive update.\n *\n * Difference from Vue: maps to Pyreon's `onUpdate()`.\n */\nexport function onUpdated(fn: () => void): void {\n onUpdate(fn)\n}\n\n/**\n * Registers a callback to run before mount.\n * In Pyreon there is no pre-mount phase — maps to `onMount()`.\n */\nexport function onBeforeMount(fn: () => void): void {\n onMount(() => {\n fn()\n return undefined\n })\n}\n\n/**\n * Registers a callback to run before unmount.\n * In Pyreon there is no pre-unmount phase — maps to `onUnmount()`.\n */\nexport function onBeforeUnmount(fn: () => void): void {\n onUnmount(fn)\n}\n\n// ─── nextTick ─────────────────────────────────────────────────────────────────\n\n/**\n * Returns a Promise that resolves after all pending reactive updates have flushed.\n *\n * Difference from Vue: identical to Pyreon's `nextTick()`.\n */\nexport function nextTick(): Promise<void> {\n return pyreonNextTick()\n}\n\n// ─── Provide / Inject ─────────────────────────────────────────────────────────\n\n// Registry of string/symbol keys to Pyreon context objects (created lazily)\nconst _contextRegistry = new Map<string | symbol, ReturnType<typeof createContext>>()\n\nfunction getOrCreateContext<T>(key: string | symbol, defaultValue?: T) {\n if (!_contextRegistry.has(key)) {\n _contextRegistry.set(key, createContext<T>(defaultValue as T))\n }\n return _contextRegistry.get(key) as ReturnType<typeof createContext<T>>\n}\n\n/**\n * Provides a value to all descendant components.\n *\n * Difference from Vue: backed by Pyreon's context stack (pushContext/popContext).\n * Must be called during component setup. The value is scoped to the component\n * tree — not globally shared.\n */\nexport function provide<T>(key: string | symbol, value: T): void {\n const ctx = getOrCreateContext<T>(key)\n pushContext(new Map([[ctx.id, value]]))\n onUnmount(() => popContext())\n}\n\n/**\n * Injects a value provided by an ancestor component.\n *\n * Difference from Vue: backed by Pyreon's context system (useContext).\n */\nexport function inject<T>(key: string | symbol, defaultValue?: T): T | undefined {\n const ctx = getOrCreateContext<T>(key)\n const value = useContext(ctx)\n return value !== undefined ? value : defaultValue\n}\n\n// ─── defineComponent ──────────────────────────────────────────────────────────\n\ninterface ComponentOptions<P extends Props = Props> {\n /** The setup function — called once during component initialization. */\n setup: (props: P) => (() => VNodeChild) | VNodeChild\n /** Optional name for debugging. */\n name?: string\n}\n\n/**\n * Defines a component using Vue 3 Composition API style.\n * Only supports the `setup()` function — Options API is not supported.\n *\n * Difference from Vue: returns a Pyreon `ComponentFn`. No template/render option —\n * the setup function should return a render function or VNode directly.\n */\nexport function defineComponent<P extends Props = Props>(\n options: ComponentOptions<P> | ((props: P) => VNodeChild),\n): ComponentFn<P> {\n if (typeof options === \"function\") {\n return options as ComponentFn<P>\n }\n const comp = (props: P) => {\n const result = options.setup(props)\n if (typeof result === \"function\") {\n return (result as () => VNodeChild)()\n }\n return result\n }\n if (options.name) {\n Object.defineProperty(comp, \"name\", { value: options.name })\n }\n return comp as ComponentFn<P>\n}\n\n// ─── h ────────────────────────────────────────────────────────────────────────\n\n/**\n * Re-export of Pyreon's `h()` function for creating VNodes.\n */\nexport { pyreonH as h, Fragment }\n\n// ─── createApp ────────────────────────────────────────────────────────────────\n\ninterface App {\n /** Mount the application into a DOM element. Returns an unmount function. */\n mount(el: string | Element): () => void\n}\n\n/**\n * Creates a Pyreon application instance — Vue 3 `createApp()` compatible.\n *\n * Difference from Vue: does not support plugins, directives, or global config.\n * The component receives `props` if provided.\n */\nexport function createApp(component: ComponentFn, props?: Props): App {\n return {\n mount(el: string | Element): () => void {\n const container = typeof el === \"string\" ? document.querySelector(el) : el\n if (!container) {\n throw new Error(`Cannot find mount target: ${el}`)\n }\n const vnode = pyreonH(component, props ?? null)\n return pyreonMount(vnode, container)\n },\n }\n}\n\n// ─── Additional re-exports ────────────────────────────────────────────────────\n\nexport { batch } from \"@pyreon/reactivity\"\n"],"mappings":";;;;;AA4CA,MAAM,WAAW,OAAO,YAAY;AACpC,MAAM,gBAAgB,OAAO,iBAAiB;AAC9C,MAAM,QAAQ,OAAO,UAAU;;;;;;;;AAgB/B,SAAgB,IAAO,OAAkB;CACvC,MAAM,IAAI,OAAO,MAAM;AAYvB,QAXU;GACP,WAAW;EACZ,IAAI,QAAW;AACb,UAAO,GAAG;;EAEZ,IAAI,MAAM,UAAa;AACrB,KAAE,IAAI,SAAS;;EAGjB,SAAS;EACV;;;;;;;AASH,SAAgB,WAAc,OAAkB;AAC9C,QAAO,IAAI,MAAM;;;;;AAMnB,SAAgB,WAAc,GAAiB;CAC7C,MAAM,WAAW;AACjB,KAAI,SAAS,SAAS;EAEpB,MAAM,UAAU,SAAS,QAAQ,MAAM;AACvC,WAAS,QAAQ,IAAI,OAAe;AACpC,WAAS,QAAQ,IAAI,QAAQ;;;;;;AAOjC,SAAgB,MAAM,KAA0B;AAC9C,QACE,QAAQ,QAAQ,OAAO,QAAQ,YAAa,IAAgC,cAAc;;;;;AAO9F,SAAgB,MAAS,GAAkB;AACzC,QAAO,MAAM,EAAE,GAAG,EAAE,QAAQ;;;;;;;;;AAgB9B,SAAgB,SACd,aACgB;CAChB,MAAM,SAAS,OAAO,gBAAgB,aAAa,cAAc,YAAY;CAC7E,MAAM,SAAS,OAAO,gBAAgB,WAAW,YAAY,MAAM;CACnE,MAAM,IAAIA,WAAe,OAAO;AAahC,QAZU;GACP,WAAW;EACZ,IAAI,QAAW;AACb,UAAO,GAAG;;EAEZ,IAAI,MAAM,GAAM;AACd,OAAI,CAAC,OACH,OAAM,IAAI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,QAAO;CAET,MAAM,QAAQ,UAAa;EACzB,MAAM,SAAS,QAAQ,MAAM,MAAM;AACnC,MAAI,OAAO,WAAW,WACpB,QAAQ,QAA6B;AAEvC,SAAO;;AAET,KAAI,QAAQ,KACV,QAAO,eAAe,MAAM,QAAQ,EAAE,OAAO,QAAQ,MAAM,CAAC;AAE9D,QAAO;;;;;;;;AAuBT,SAAgB,UAAU,WAAwB,OAAoB;AACpE,QAAO,EACL,MAAM,IAAkC;EACtC,MAAM,YAAY,OAAO,OAAO,WAAW,SAAS,cAAc,GAAG,GAAG;AACxE,MAAI,CAAC,UACH,OAAM,IAAI,MAAM,6BAA6B,KAAK;AAGpD,SAAOC,MADO,QAAQ,WAAW,SAAS,KAAK,EACrB,UAAU;IAEvC"}
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { Fragment, createContext, h as pyreonH, onMount, onUnmount, onUpdate, popContext, pushContext, useContext } from "@pyreon/core";
|
|
2
|
+
import { batch, computed as computed$1, createStore, effect, nextTick as nextTick$1, signal } from "@pyreon/reactivity";
|
|
3
|
+
import { mount } from "@pyreon/runtime-dom";
|
|
4
|
+
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a reactive ref wrapping the given value.
|
|
9
|
+
* Access via `.value` — reads track, writes trigger.
|
|
10
|
+
*
|
|
11
|
+
* Difference from Vue: backed by a Pyreon signal. No `__v_isShallow` distinction
|
|
12
|
+
* at runtime since Pyreon signals are always shallow (deep reactivity is via stores).
|
|
13
|
+
*/
|
|
14
|
+
function ref(value) {
|
|
15
|
+
const s = signal(value);
|
|
16
|
+
return {
|
|
17
|
+
[V_IS_REF]: true,
|
|
18
|
+
get value() {
|
|
19
|
+
return s();
|
|
20
|
+
},
|
|
21
|
+
set value(newValue) {
|
|
22
|
+
s.set(newValue);
|
|
23
|
+
},
|
|
24
|
+
_signal: s
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creates a shallow ref — same as `ref()` in Pyreon since signals are inherently shallow.
|
|
29
|
+
*
|
|
30
|
+
* Difference from Vue: identical to `ref()` — Pyreon signals don't perform deep conversion.
|
|
31
|
+
*/
|
|
32
|
+
function shallowRef(value) {
|
|
33
|
+
return ref(value);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Force trigger a ref's subscribers, even if the value hasn't changed.
|
|
37
|
+
*/
|
|
38
|
+
function triggerRef(r) {
|
|
39
|
+
const internal = r;
|
|
40
|
+
if (internal._signal) {
|
|
41
|
+
const current = internal._signal.peek();
|
|
42
|
+
internal._signal.set(void 0);
|
|
43
|
+
internal._signal.set(current);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` if the value is a ref (created by `ref()` or `computed()`).
|
|
48
|
+
*/
|
|
49
|
+
function isRef(val) {
|
|
50
|
+
return val !== null && typeof val === "object" && val[V_IS_REF] === true;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Unwraps a ref: if it has `.value`, return `.value`; otherwise return as-is.
|
|
54
|
+
*/
|
|
55
|
+
function unref(r) {
|
|
56
|
+
return isRef(r) ? r.value : r;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a computed ref. Supports both readonly and writable forms:
|
|
60
|
+
* - `computed(() => value)` — readonly
|
|
61
|
+
* - `computed({ get: () => value, set: (v) => ... })` — writable
|
|
62
|
+
*
|
|
63
|
+
* Backed by Pyreon's `computed()`, wrapped in a `.value` accessor.
|
|
64
|
+
*/
|
|
65
|
+
function computed(fnOrOptions) {
|
|
66
|
+
const getter = typeof fnOrOptions === "function" ? fnOrOptions : fnOrOptions.get;
|
|
67
|
+
const setter = typeof fnOrOptions === "object" ? fnOrOptions.set : void 0;
|
|
68
|
+
const c = computed$1(getter);
|
|
69
|
+
return {
|
|
70
|
+
[V_IS_REF]: true,
|
|
71
|
+
get value() {
|
|
72
|
+
return c();
|
|
73
|
+
},
|
|
74
|
+
set value(v) {
|
|
75
|
+
if (!setter) throw new Error("Cannot set value of a computed ref — computed refs are readonly");
|
|
76
|
+
setter(v);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Creates a deeply reactive proxy from a plain object.
|
|
82
|
+
* Backed by Pyreon's `createStore()`.
|
|
83
|
+
*
|
|
84
|
+
* Difference from Vue: uses Pyreon's fine-grained per-property signals.
|
|
85
|
+
* Direct mutation triggers only affected signals.
|
|
86
|
+
*/
|
|
87
|
+
function reactive(obj) {
|
|
88
|
+
const proxy = createStore(obj);
|
|
89
|
+
rawMap.set(proxy, obj);
|
|
90
|
+
return proxy;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Creates a shallow reactive proxy.
|
|
94
|
+
* In Pyreon, `createStore` is already per-property (not deeply recursive for primitives),
|
|
95
|
+
* but nested objects will be wrapped. For truly shallow behavior, use individual refs.
|
|
96
|
+
*
|
|
97
|
+
* Difference from Vue: backed by `createStore()` — same as `reactive()` in practice.
|
|
98
|
+
*/
|
|
99
|
+
function shallowReactive(obj) {
|
|
100
|
+
return reactive(obj);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Returns a readonly proxy that throws on mutation attempts.
|
|
104
|
+
*
|
|
105
|
+
* Difference from Vue: uses a simple Proxy with a set trap that throws,
|
|
106
|
+
* rather than Vue's full readonly reactive system.
|
|
107
|
+
*/
|
|
108
|
+
function readonly(obj) {
|
|
109
|
+
return new Proxy(obj, {
|
|
110
|
+
get(target, key) {
|
|
111
|
+
if (key === V_IS_READONLY) return true;
|
|
112
|
+
if (key === V_RAW) return target;
|
|
113
|
+
return Reflect.get(target, key);
|
|
114
|
+
},
|
|
115
|
+
set(_target, key) {
|
|
116
|
+
if (key === V_IS_READONLY || key === V_RAW) return true;
|
|
117
|
+
throw new Error(`Cannot set property "${String(key)}" on a readonly object`);
|
|
118
|
+
},
|
|
119
|
+
deleteProperty(_target, key) {
|
|
120
|
+
throw new Error(`Cannot delete property "${String(key)}" from a readonly object`);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns the raw (unwrapped) object behind a reactive or readonly proxy.
|
|
126
|
+
*
|
|
127
|
+
* Difference from Vue: only works for objects created via `reactive()` or `readonly()`.
|
|
128
|
+
*/
|
|
129
|
+
function toRaw(proxy) {
|
|
130
|
+
const readonlyRaw = proxy[V_RAW];
|
|
131
|
+
if (readonlyRaw) return readonlyRaw;
|
|
132
|
+
return rawMap.get(proxy) ?? proxy;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates a ref linked to a property of a reactive object.
|
|
136
|
+
* Reading/writing the ref's `.value` reads/writes the original property.
|
|
137
|
+
*/
|
|
138
|
+
function toRef(obj, key) {
|
|
139
|
+
return {
|
|
140
|
+
[V_IS_REF]: true,
|
|
141
|
+
get value() {
|
|
142
|
+
return obj[key];
|
|
143
|
+
},
|
|
144
|
+
set value(newValue) {
|
|
145
|
+
obj[key] = newValue;
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Converts all properties of a reactive object into individual refs.
|
|
151
|
+
* Each ref is linked to the original property (not a copy).
|
|
152
|
+
*/
|
|
153
|
+
function toRefs(obj) {
|
|
154
|
+
const result = {};
|
|
155
|
+
for (const key of Object.keys(obj)) result[key] = toRef(obj, key);
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Watches a reactive source and calls `cb` when it changes.
|
|
160
|
+
* Tracks old and new values.
|
|
161
|
+
*
|
|
162
|
+
* Difference from Vue: `deep` option is ignored — Pyreon tracks dependencies automatically.
|
|
163
|
+
* Returns a stop function to dispose the watcher.
|
|
164
|
+
*/
|
|
165
|
+
function watch(source, cb, options) {
|
|
166
|
+
const getter = isRef(source) ? () => source.value : source;
|
|
167
|
+
let oldValue;
|
|
168
|
+
let initialized = false;
|
|
169
|
+
if (options?.immediate) {
|
|
170
|
+
oldValue = void 0;
|
|
171
|
+
const current = getter();
|
|
172
|
+
cb(current, oldValue);
|
|
173
|
+
oldValue = current;
|
|
174
|
+
initialized = true;
|
|
175
|
+
}
|
|
176
|
+
const e = effect(() => {
|
|
177
|
+
const newValue = getter();
|
|
178
|
+
if (initialized) cb(newValue, oldValue);
|
|
179
|
+
oldValue = newValue;
|
|
180
|
+
initialized = true;
|
|
181
|
+
});
|
|
182
|
+
return () => e.dispose();
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Runs the given function reactively — re-executes whenever its tracked
|
|
186
|
+
* dependencies change.
|
|
187
|
+
*
|
|
188
|
+
* Difference from Vue: identical to Pyreon's `effect()`.
|
|
189
|
+
* Returns a stop function.
|
|
190
|
+
*/
|
|
191
|
+
function watchEffect(fn) {
|
|
192
|
+
const e = effect(fn);
|
|
193
|
+
return () => e.dispose();
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Registers a callback to run after the component is mounted.
|
|
197
|
+
*
|
|
198
|
+
* Difference from Vue: maps directly to Pyreon's `onMount()`.
|
|
199
|
+
* In Pyreon there is no distinction between beforeMount and mounted.
|
|
200
|
+
*/
|
|
201
|
+
function onMounted(fn) {
|
|
202
|
+
onMount(() => {
|
|
203
|
+
fn();
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Registers a callback to run before the component is unmounted.
|
|
208
|
+
*
|
|
209
|
+
* Difference from Vue: maps to Pyreon's `onUnmount()`.
|
|
210
|
+
* In Pyreon there is no distinction between beforeUnmount and unmounted.
|
|
211
|
+
*/
|
|
212
|
+
function onUnmounted(fn) {
|
|
213
|
+
onUnmount(fn);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Registers a callback to run after a reactive update.
|
|
217
|
+
*
|
|
218
|
+
* Difference from Vue: maps to Pyreon's `onUpdate()`.
|
|
219
|
+
*/
|
|
220
|
+
function onUpdated(fn) {
|
|
221
|
+
onUpdate(fn);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Registers a callback to run before mount.
|
|
225
|
+
* In Pyreon there is no pre-mount phase — maps to `onMount()`.
|
|
226
|
+
*/
|
|
227
|
+
function onBeforeMount(fn) {
|
|
228
|
+
onMount(() => {
|
|
229
|
+
fn();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Registers a callback to run before unmount.
|
|
234
|
+
* In Pyreon there is no pre-unmount phase — maps to `onUnmount()`.
|
|
235
|
+
*/
|
|
236
|
+
function onBeforeUnmount(fn) {
|
|
237
|
+
onUnmount(fn);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Returns a Promise that resolves after all pending reactive updates have flushed.
|
|
241
|
+
*
|
|
242
|
+
* Difference from Vue: identical to Pyreon's `nextTick()`.
|
|
243
|
+
*/
|
|
244
|
+
function nextTick() {
|
|
245
|
+
return nextTick$1();
|
|
246
|
+
}
|
|
247
|
+
function getOrCreateContext(key, defaultValue) {
|
|
248
|
+
if (!_contextRegistry.has(key)) _contextRegistry.set(key, createContext(defaultValue));
|
|
249
|
+
return _contextRegistry.get(key);
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Provides a value to all descendant components.
|
|
253
|
+
*
|
|
254
|
+
* Difference from Vue: backed by Pyreon's context stack (pushContext/popContext).
|
|
255
|
+
* Must be called during component setup. The value is scoped to the component
|
|
256
|
+
* tree — not globally shared.
|
|
257
|
+
*/
|
|
258
|
+
function provide(key, value) {
|
|
259
|
+
const ctx = getOrCreateContext(key);
|
|
260
|
+
pushContext(new Map([[ctx.id, value]]));
|
|
261
|
+
onUnmount(() => popContext());
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Injects a value provided by an ancestor component.
|
|
265
|
+
*
|
|
266
|
+
* Difference from Vue: backed by Pyreon's context system (useContext).
|
|
267
|
+
*/
|
|
268
|
+
function inject(key, defaultValue) {
|
|
269
|
+
const value = useContext(getOrCreateContext(key));
|
|
270
|
+
return value !== void 0 ? value : defaultValue;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Defines a component using Vue 3 Composition API style.
|
|
274
|
+
* Only supports the `setup()` function — Options API is not supported.
|
|
275
|
+
*
|
|
276
|
+
* Difference from Vue: returns a Pyreon `ComponentFn`. No template/render option —
|
|
277
|
+
* the setup function should return a render function or VNode directly.
|
|
278
|
+
*/
|
|
279
|
+
function defineComponent(options) {
|
|
280
|
+
if (typeof options === "function") return options;
|
|
281
|
+
const comp = props => {
|
|
282
|
+
const result = options.setup(props);
|
|
283
|
+
if (typeof result === "function") return result();
|
|
284
|
+
return result;
|
|
285
|
+
};
|
|
286
|
+
if (options.name) Object.defineProperty(comp, "name", {
|
|
287
|
+
value: options.name
|
|
288
|
+
});
|
|
289
|
+
return comp;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Creates a Pyreon application instance — Vue 3 `createApp()` compatible.
|
|
293
|
+
*
|
|
294
|
+
* Difference from Vue: does not support plugins, directives, or global config.
|
|
295
|
+
* The component receives `props` if provided.
|
|
296
|
+
*/
|
|
297
|
+
function createApp(component, props) {
|
|
298
|
+
return {
|
|
299
|
+
mount(el) {
|
|
300
|
+
const container = typeof el === "string" ? document.querySelector(el) : el;
|
|
301
|
+
if (!container) throw new Error(`Cannot find mount target: ${el}`);
|
|
302
|
+
return mount(pyreonH(component, props ?? null), container);
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
//#endregion
|
|
308
|
+
export { Fragment, batch, computed, createApp, defineComponent, pyreonH as h, inject, isRef, nextTick, onBeforeMount, onBeforeUnmount, onMounted, onUnmounted, onUpdated, provide, reactive, readonly, ref, shallowReactive, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, watch, watchEffect };
|
|
309
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@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