@sky.ui/reactivity 0.0.1

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.
@@ -0,0 +1,198 @@
1
+ /** Internal effect runner returned by `effect()`. */
2
+ type EffectRunner = (() => void) & {
3
+ stopped: boolean;
4
+ paused: boolean;
5
+ stop: () => void;
6
+ pause: () => void;
7
+ resume: () => void;
8
+ scheduler: (run: () => void) => void;
9
+ };
10
+ type EffectOptions = {
11
+ lazy?: boolean;
12
+ scheduler?: (run: () => void) => void;
13
+ onCleanup?: () => void;
14
+ };
15
+ type CustomRefFactory<T> = (track: () => void, trigger: () => void) => {
16
+ get: () => T;
17
+ set: (value: T) => void;
18
+ };
19
+ type EffectScopeHandle = {
20
+ readonly active: boolean;
21
+ run<T>(fn: () => T): T;
22
+ stop(): void;
23
+ };
24
+ type Ref<T> = {
25
+ value: T;
26
+ readonly __isRef?: true;
27
+ };
28
+ type ComputedOptions<T> = {
29
+ get: () => T;
30
+ set?: (v: T) => void;
31
+ };
32
+ type WatchCallback = (newVal: unknown, oldVal: unknown, onCleanup: (fn: () => void) => void) => void;
33
+ type WatchOptions = {
34
+ immediate?: boolean;
35
+ deep?: boolean;
36
+ flush?: 'pre' | 'post' | 'sync';
37
+ };
38
+ type WatchSource = unknown | (() => unknown) | (unknown | (() => unknown))[];
39
+ type SetupFn = (() => Record<string, unknown>) | Record<string, unknown>;
40
+ type ReactivityApp = {
41
+ mount(target: Element | string, initial?: Record<string, unknown>): {
42
+ ctx: Record<string, unknown>;
43
+ unmount: () => void;
44
+ };
45
+ };
46
+ /** Reactive proxy marker set on proxied objects. */
47
+ interface ReactiveProxy {
48
+ __isReactive?: boolean;
49
+ __isReadonly?: boolean;
50
+ __isRef?: boolean;
51
+ [key: string | symbol]: unknown;
52
+ }
53
+ declare const SHALLOW_REF_TARGET: unique symbol;
54
+ interface ShallowRefObject<T = unknown> {
55
+ value: T;
56
+ readonly __isRef?: true;
57
+ [SHALLOW_REF_TARGET]?: {
58
+ value: T;
59
+ };
60
+ }
61
+
62
+ declare function effect<T>(fn: () => T, opts?: EffectOptions): EffectRunner & (() => T);
63
+ /** Alias for `effect()` (Vue `watchEffect` style). */
64
+ declare const watchEffect: typeof effect;
65
+
66
+ declare function reactive<T extends object>(obj: T): T;
67
+
68
+ declare function readonly<T extends object>(obj: T): T;
69
+
70
+ declare function shallowReactive<T extends object>(obj: T): T;
71
+
72
+ declare function shallowReadonly<T extends object>(obj: T): T;
73
+
74
+ declare function toRaw<T>(proxy: T): T;
75
+ declare function markRaw<T extends object>(obj: T): T;
76
+ declare function isReactive(obj: unknown): obj is ReactiveProxy;
77
+ declare function isReadonly(obj: unknown): boolean;
78
+
79
+ /** Mutable ref wrapper `{ value }` with deep reactivity on **`.value`** when assigned plain objects/arrays. */
80
+ declare function ref<T>(v: T): Ref<T>;
81
+ /** Ref whose **`.value`** is not deeply made reactive. */
82
+ declare function shallowRef<T>(v: T): ShallowRefObject<T>;
83
+ /** Forces subscribers to re-run for a **shallow** ref when inner object is mutated without replacing `.value`. */
84
+ declare function triggerRef(r: ShallowRefObject | Ref<unknown> | null | undefined): void;
85
+ declare function customRef<T>(factory: CustomRefFactory<T>): Ref<T>;
86
+
87
+ declare function isRef(v: unknown): v is Ref<unknown> | ShallowRefObject;
88
+ declare function unref<T>(v: T | Ref<T> | ShallowRefObject<T>): T;
89
+
90
+ declare function toRef<T extends object, K extends keyof T>(object: T, key: K): Ref<T[K]>;
91
+ declare function toRefs<T extends object>(object: T): {
92
+ [K in keyof T]: Ref<T[K]>;
93
+ };
94
+
95
+ declare function computed<T>(getterOrOptions: (() => T) | ComputedOptions<T>): Ref<T>;
96
+
97
+ declare function watch(src: WatchSource, cb: WatchCallback, opts?: WatchOptions): () => void;
98
+
99
+ declare const nextTick: (fn?: () => void) => Promise<void>;
100
+
101
+ declare class EffectScope {
102
+ readonly detached: boolean;
103
+ active: boolean;
104
+ private readonly effects;
105
+ private readonly disposers;
106
+ constructor(detached?: boolean);
107
+ run<T>(fn: () => T): T;
108
+ register(runner: EffectRunner): void;
109
+ registerDispose(fn: () => void): void;
110
+ stop(): void;
111
+ }
112
+ declare function effectScope(detached?: boolean): EffectScope;
113
+ declare function getCurrentScope(): EffectScope | null;
114
+ declare function onScopeDispose(fn: () => void): void;
115
+
116
+ declare function flushSync(fn: () => void): void;
117
+
118
+ declare function mount(root: ParentNode | Element, ctx: Record<string, unknown>): Record<string, unknown>;
119
+
120
+ declare function createReactivity(setup: SetupFn): ReactivityApp;
121
+
122
+ type RenderToStringOptions = {
123
+ /** CSS selector for the mount root (default: `body`). */
124
+ root?: string;
125
+ /** Extra properties merged into context before setup exports. */
126
+ initial?: Record<string, unknown>;
127
+ /** When true, return full `<html>...</html>` instead of root outerHTML only. */
128
+ fullDocument?: boolean;
129
+ /** Remove `<!--sky-for-->` / `<!--if-chain-->` anchors from output (default: `true`). */
130
+ stripAnchors?: boolean;
131
+ };
132
+ /**
133
+ * One-shot SSR: compiles directives and `{{ }}` without reactive effects.
134
+ * Requires browser DOM or optional peer `linkedom` in Node.
135
+ */
136
+ declare function renderToString(markup: string, ctx: Record<string, unknown>, options?: RenderToStringOptions): Promise<string>;
137
+
138
+ type SSRDocument = Document & {
139
+ toString(): string;
140
+ };
141
+ /** Parse markup into a Document (browser `DOMParser` or optional `linkedom`). */
142
+ declare function parseSSRDocument(markup: string): Promise<SSRDocument>;
143
+
144
+ /** Run a template event-handler statement string against a context (for tests and tooling). */
145
+ declare function runHandlerCode(code: string, ctx: Record<string, unknown>, $event?: unknown, $el?: Node | null): Promise<unknown>;
146
+
147
+ declare global {
148
+ var __SKY_DEV__: boolean | undefined;
149
+ }
150
+ /** When `false`, dev warnings and verbose errors are suppressed (Vue-style). */
151
+ declare function getSkyDev(): boolean;
152
+ declare function setSkyDev(enabled: boolean): void;
153
+ /** Snapshot at module load; prefer `getSkyDev()` for runtime checks. */
154
+ declare const skyDevSnapshot: boolean;
155
+
156
+ /** Stable warning codes (search docs / source by code). */
157
+ declare const WarnCodes: {
158
+ readonly MODEL_ARG_ON_NATIVE: "MODEL_ARG_ON_NATIVE";
159
+ readonly MODEL_INVALID_KEY: "MODEL_INVALID_KEY";
160
+ readonly MODEL_UNKNOWN_MODIFIER: "MODEL_UNKNOWN_MODIFIER";
161
+ readonly MODEL_DUPLICATE: "MODEL_DUPLICATE";
162
+ readonly FOR_KEY_RECOMMENDED: "FOR_KEY_RECOMMENDED";
163
+ readonly FOR_INVALID_SYNTAX: "FOR_INVALID_SYNTAX";
164
+ readonly EVENT_UNKNOWN_MODIFIER: "EVENT_UNKNOWN_MODIFIER";
165
+ readonly HANDLER_RUNTIME: "HANDLER_RUNTIME";
166
+ readonly AST_EVAL: "AST_EVAL";
167
+ readonly AST_STMT: "AST_STMT";
168
+ readonly AST_BLOCKED: "AST_BLOCKED";
169
+ readonly MOUNT_TARGET_NOT_FOUND: "MOUNT_TARGET_NOT_FOUND";
170
+ readonly SSR_ROOT_NOT_FOUND: "SSR_ROOT_NOT_FOUND";
171
+ readonly SKY_HTML_UNSAFE: "SKY_HTML_UNSAFE";
172
+ };
173
+ type WarnCode = (typeof WarnCodes)[keyof typeof WarnCodes];
174
+
175
+ type WarnContext = {
176
+ /** Element the warning relates to (shown as `<tag#id>`). */
177
+ element?: Element | null;
178
+ /** Short location hint, e.g. `@click` or `sky-for`. */
179
+ label?: string;
180
+ };
181
+ type SkyWarnHandler = (code: WarnCode, message: string, context?: WarnContext) => void;
182
+ declare function setWarnHandler(handler: SkyWarnHandler | null): void;
183
+ /**
184
+ * Log a development warning (no-op when `__SKY_DEV__` is false).
185
+ */
186
+ declare function warn(code: WarnCode, args?: Record<string, string | number>, context?: WarnContext): void;
187
+ /**
188
+ * Log each distinct warning once per element/label (Vue `warnOnce` style).
189
+ */
190
+ declare function warnOnce(code: WarnCode, args?: Record<string, string | number>, context?: WarnContext): void;
191
+ /**
192
+ * Log a runtime error with Sky formatting (handlers, AST, etc.).
193
+ */
194
+ declare function logError(code: WarnCode, err: unknown, context?: WarnContext): void;
195
+ /** Clear dedupe cache (tests). */
196
+ declare function resetWarned(): void;
197
+
198
+ export { type ComputedOptions, type CustomRefFactory, type EffectOptions, type EffectRunner, EffectScope, type EffectScopeHandle, type ReactivityApp, type Ref, type RenderToStringOptions, type SSRDocument, type SetupFn, type SkyWarnHandler, type WarnCode, WarnCodes, type WarnContext, type WatchCallback, type WatchOptions, type WatchSource, skyDevSnapshot as __SKY_DEV__, computed, createReactivity, customRef, effect, effectScope, flushSync, getCurrentScope, getSkyDev, isReactive, isReadonly, isRef, logError, markRaw, mount, nextTick, onScopeDispose, parseSSRDocument, reactive, readonly, ref, renderToString, resetWarned, runHandlerCode, setSkyDev, setWarnHandler, shallowReactive, shallowReadonly, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, warn, warnOnce, watch, watchEffect };