canvasengine 2.0.0-beta.6 → 2.0.0-beta.60
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/dist/DebugRenderer-DkjTAc48.js +1384 -0
- package/dist/DebugRenderer-DkjTAc48.js.map +1 -0
- package/dist/components/Button.d.ts +185 -0
- package/dist/components/Button.d.ts.map +1 -0
- package/dist/components/Canvas.d.ts +17 -0
- package/dist/components/Canvas.d.ts.map +1 -0
- package/dist/components/DOMElement.d.ts +54 -0
- package/dist/components/DOMElement.d.ts.map +1 -0
- package/dist/components/DOMSprite.d.ts +127 -0
- package/dist/components/DOMSprite.d.ts.map +1 -0
- package/dist/components/FocusContainer.d.ts +129 -0
- package/dist/components/FocusContainer.d.ts.map +1 -0
- package/dist/components/Graphic.d.ts +64 -0
- package/dist/components/Graphic.d.ts.map +1 -0
- package/dist/components/Joystick.d.ts +36 -0
- package/dist/components/Joystick.d.ts.map +1 -0
- package/dist/components/NineSliceSprite.d.ts +16 -0
- package/dist/components/NineSliceSprite.d.ts.map +1 -0
- package/dist/components/ParticleEmitter.d.ts +4 -0
- package/dist/components/ParticleEmitter.d.ts.map +1 -0
- package/dist/components/Scene.d.ts +2 -0
- package/dist/components/Scene.d.ts.map +1 -0
- package/dist/components/Text.d.ts +25 -0
- package/dist/components/Text.d.ts.map +1 -0
- package/dist/components/TilingSprite.d.ts +17 -0
- package/dist/components/TilingSprite.d.ts.map +1 -0
- package/dist/components/Video.d.ts +14 -0
- package/dist/components/Video.d.ts.map +1 -0
- package/dist/components/index.d.ts +20 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/types/DisplayObject.d.ts +118 -0
- package/dist/components/types/DisplayObject.d.ts.map +1 -0
- package/dist/components/types/MouseEvent.d.ts +4 -0
- package/dist/components/types/MouseEvent.d.ts.map +1 -0
- package/dist/components/types/Spritesheet.d.ts +248 -0
- package/dist/components/types/Spritesheet.d.ts.map +1 -0
- package/dist/components/types/index.d.ts +4 -0
- package/dist/components/types/index.d.ts.map +1 -0
- package/dist/directives/Controls.d.ts +112 -0
- package/dist/directives/Controls.d.ts.map +1 -0
- package/dist/directives/ControlsBase.d.ts +199 -0
- package/dist/directives/ControlsBase.d.ts.map +1 -0
- package/dist/directives/Drag.d.ts +69 -0
- package/dist/directives/Drag.d.ts.map +1 -0
- package/dist/directives/Flash.d.ts +116 -0
- package/dist/directives/Flash.d.ts.map +1 -0
- package/dist/directives/FocusNavigation.d.ts +52 -0
- package/dist/directives/FocusNavigation.d.ts.map +1 -0
- package/dist/directives/FogVisibility.d.ts +47 -0
- package/dist/directives/FogVisibility.d.ts.map +1 -0
- package/dist/directives/GamepadControls.d.ts +224 -0
- package/dist/directives/GamepadControls.d.ts.map +1 -0
- package/dist/directives/JoystickControls.d.ts +171 -0
- package/dist/directives/JoystickControls.d.ts.map +1 -0
- package/dist/directives/KeyboardControls.d.ts +219 -0
- package/dist/directives/KeyboardControls.d.ts.map +1 -0
- package/dist/directives/Scheduler.d.ts +35 -0
- package/dist/directives/Scheduler.d.ts.map +1 -0
- package/dist/directives/Shake.d.ts +98 -0
- package/dist/directives/Shake.d.ts.map +1 -0
- package/dist/directives/Sound.d.ts +25 -0
- package/dist/directives/Sound.d.ts.map +1 -0
- package/dist/directives/Transition.d.ts +10 -0
- package/dist/directives/Transition.d.ts.map +1 -0
- package/dist/directives/ViewportCull.d.ts +11 -0
- package/dist/directives/ViewportCull.d.ts.map +1 -0
- package/dist/directives/ViewportFollow.d.ts +18 -0
- package/dist/directives/ViewportFollow.d.ts.map +1 -0
- package/dist/directives/index.d.ts +14 -0
- package/dist/directives/index.d.ts.map +1 -0
- package/dist/dist-BOOc43Qm.js +778 -0
- package/dist/dist-BOOc43Qm.js.map +1 -0
- package/dist/engine/FocusManager.d.ts +174 -0
- package/dist/engine/FocusManager.d.ts.map +1 -0
- package/dist/engine/animation.d.ts +72 -0
- package/dist/engine/animation.d.ts.map +1 -0
- package/dist/engine/bootstrap.d.ts +48 -0
- package/dist/engine/bootstrap.d.ts.map +1 -0
- package/dist/engine/directive.d.ts +13 -0
- package/dist/engine/directive.d.ts.map +1 -0
- package/dist/engine/reactive.d.ts +134 -0
- package/dist/engine/reactive.d.ts.map +1 -0
- package/dist/engine/signal.d.ts +71 -0
- package/dist/engine/signal.d.ts.map +1 -0
- package/dist/engine/trigger.d.ts +54 -0
- package/dist/engine/trigger.d.ts.map +1 -0
- package/dist/engine/utils.d.ts +89 -0
- package/dist/engine/utils.d.ts.map +1 -0
- package/dist/hooks/addContext.d.ts +2 -0
- package/dist/hooks/addContext.d.ts.map +1 -0
- package/dist/hooks/useFocus.d.ts +60 -0
- package/dist/hooks/useFocus.d.ts.map +1 -0
- package/dist/hooks/useProps.d.ts +42 -0
- package/dist/hooks/useProps.d.ts.map +1 -0
- package/dist/hooks/useRef.d.ts +4 -0
- package/dist/hooks/useRef.d.ts.map +1 -0
- package/dist/index.d.ts +19 -1107
- package/dist/index.d.ts.map +1 -0
- package/dist/index.global.js +5 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +9768 -3135
- package/dist/index.js.map +1 -1
- package/dist/utils/Ease.d.ts +17 -0
- package/dist/utils/Ease.d.ts.map +1 -0
- package/dist/utils/GlobalAssetLoader.d.ts +141 -0
- package/dist/utils/GlobalAssetLoader.d.ts.map +1 -0
- package/dist/utils/RadialGradient.d.ts +57 -0
- package/dist/utils/RadialGradient.d.ts.map +1 -0
- package/dist/utils/functions.d.ts +2 -0
- package/dist/utils/functions.d.ts.map +1 -0
- package/dist/utils/tabindex.d.ts +16 -0
- package/dist/utils/tabindex.d.ts.map +1 -0
- package/package.json +15 -9
- package/src/components/Button.ts +399 -0
- package/src/components/Canvas.ts +62 -46
- package/src/components/Container.ts +21 -2
- package/src/components/DOMContainer.ts +379 -0
- package/src/components/DOMElement.ts +556 -0
- package/src/components/DOMSprite.ts +1040 -0
- package/src/components/DisplayObject.ts +419 -201
- package/src/components/FocusContainer.ts +368 -0
- package/src/components/Graphic.ts +227 -66
- package/src/components/Joystick.ts +363 -0
- package/src/components/Mesh.ts +222 -0
- package/src/components/NineSliceSprite.ts +4 -1
- package/src/components/ParticleEmitter.ts +12 -8
- package/src/components/Sprite.ts +418 -52
- package/src/components/Text.ts +125 -18
- package/src/components/Viewport.ts +122 -63
- package/src/components/index.ts +9 -2
- package/src/components/types/DisplayObject.ts +53 -5
- package/src/components/types/Spritesheet.ts +0 -118
- package/src/directives/Controls.ts +254 -0
- package/src/directives/ControlsBase.ts +267 -0
- package/src/directives/Drag.ts +357 -52
- package/src/directives/Flash.ts +419 -0
- package/src/directives/FocusNavigation.ts +113 -0
- package/src/directives/FogVisibility.ts +273 -0
- package/src/directives/GamepadControls.ts +537 -0
- package/src/directives/JoystickControls.ts +396 -0
- package/src/directives/KeyboardControls.ts +85 -430
- package/src/directives/Scheduler.ts +12 -4
- package/src/directives/Shake.ts +298 -0
- package/src/directives/Sound.ts +94 -31
- package/src/directives/ViewportFollow.ts +40 -9
- package/src/directives/index.ts +13 -6
- package/src/engine/FocusManager.ts +510 -0
- package/src/engine/animation.ts +175 -21
- package/src/engine/bootstrap.ts +93 -3
- package/src/engine/directive.ts +4 -4
- package/src/engine/reactive.ts +979 -176
- package/src/engine/signal.ts +113 -25
- package/src/engine/trigger.ts +34 -7
- package/src/engine/utils.ts +19 -3
- package/src/hooks/useFocus.ts +91 -0
- package/src/hooks/useProps.ts +1 -1
- package/src/index.ts +8 -2
- package/src/types/pixi-cull.d.ts +7 -0
- package/src/utils/GlobalAssetLoader.ts +257 -0
- package/src/utils/functions.ts +7 -0
- package/src/utils/tabindex.ts +70 -0
- package/testing/index.ts +35 -4
- package/tsconfig.json +18 -0
- package/vite.config.ts +39 -0
package/src/engine/reactive.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Signal, WritableArraySignal, WritableObjectSignal, isSignal } from "@signe/reactive";
|
|
1
|
+
import { ArrayChange, ObjectChange, Signal, WritableArraySignal, WritableObjectSignal, isComputed, isSignal, signal, computed } from "@signe/reactive";
|
|
2
|
+
import { isAnimatedSignal, AnimatedSignal } from "./animation";
|
|
2
3
|
import {
|
|
3
4
|
Observable,
|
|
4
5
|
Subject,
|
|
@@ -7,7 +8,15 @@ import {
|
|
|
7
8
|
from,
|
|
8
9
|
map,
|
|
9
10
|
of,
|
|
11
|
+
share,
|
|
12
|
+
shareReplay,
|
|
10
13
|
switchMap,
|
|
14
|
+
debounceTime,
|
|
15
|
+
distinctUntilChanged,
|
|
16
|
+
bufferTime,
|
|
17
|
+
filter,
|
|
18
|
+
throttleTime,
|
|
19
|
+
combineLatest,
|
|
11
20
|
} from "rxjs";
|
|
12
21
|
import { ComponentInstance } from "../components/DisplayObject";
|
|
13
22
|
import { Directive, applyDirective } from "./directive";
|
|
@@ -17,18 +26,6 @@ export interface Props {
|
|
|
17
26
|
[key: string]: any;
|
|
18
27
|
}
|
|
19
28
|
|
|
20
|
-
export type ArrayChange<T> = {
|
|
21
|
-
type: "add" | "remove" | "update" | "init" | "reset";
|
|
22
|
-
index?: number;
|
|
23
|
-
items: T[];
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
type ElementObservable<T> = Observable<
|
|
27
|
-
ArrayChange<T> & {
|
|
28
|
-
value: Element | Element[];
|
|
29
|
-
}
|
|
30
|
-
>;
|
|
31
|
-
|
|
32
29
|
type NestedSignalObjects = {
|
|
33
30
|
[Key in string]: NestedSignalObjects | Signal<any>;
|
|
34
31
|
};
|
|
@@ -51,12 +48,16 @@ export interface Element<T = ComponentInstance> {
|
|
|
51
48
|
};
|
|
52
49
|
destroy: () => void;
|
|
53
50
|
allElements: Subject<void>;
|
|
51
|
+
isFrozen: boolean;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
type
|
|
54
|
+
type FlowResult = {
|
|
57
55
|
elements: Element[];
|
|
58
56
|
prev?: Element;
|
|
59
|
-
|
|
57
|
+
fullElements?: Element[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type FlowObservable = Observable<FlowResult>;
|
|
60
61
|
|
|
61
62
|
const components: { [key: string]: any } = {};
|
|
62
63
|
|
|
@@ -80,10 +81,219 @@ export const isPrimitive = (value) => {
|
|
|
80
81
|
);
|
|
81
82
|
};
|
|
82
83
|
|
|
84
|
+
const DOM_ROUTING_MAP: Record<string, string> = {
|
|
85
|
+
Sprite: "DOMSprite",
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const DOM_ALLOWED_TAGS = new Set(["DOMContainer", "DOMElement", "DOMSprite"]);
|
|
89
|
+
const DOM_UNSUPPORTED_TAGS = new Set([
|
|
90
|
+
"Canvas",
|
|
91
|
+
"Container",
|
|
92
|
+
"Graphics",
|
|
93
|
+
"Rect",
|
|
94
|
+
"Circle",
|
|
95
|
+
"Ellipse",
|
|
96
|
+
"Triangle",
|
|
97
|
+
"Svg",
|
|
98
|
+
"Mesh",
|
|
99
|
+
"Scene",
|
|
100
|
+
"ParticlesEmitter",
|
|
101
|
+
"Sprite",
|
|
102
|
+
"Video",
|
|
103
|
+
"Text",
|
|
104
|
+
"TilingSprite",
|
|
105
|
+
"Viewport",
|
|
106
|
+
"NineSliceSprite",
|
|
107
|
+
"Button",
|
|
108
|
+
"Joystick",
|
|
109
|
+
"FocusContainer",
|
|
110
|
+
]);
|
|
111
|
+
|
|
112
|
+
const hasDomAncestor = (element: Element | null): boolean => {
|
|
113
|
+
let current = element;
|
|
114
|
+
while (current) {
|
|
115
|
+
if (current.tag === "DOMContainer" || current.tag === "DOMElement") {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
current = current.parent;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const cleanupElementForRouting = (element: Element) => {
|
|
124
|
+
element.propSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
125
|
+
element.effectSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
126
|
+
element.effectUnmounts?.forEach((fn) => fn?.());
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const routeDomComponent = (parent: Element, child: Element): Element => {
|
|
130
|
+
if (!hasDomAncestor(parent)) {
|
|
131
|
+
return child;
|
|
132
|
+
}
|
|
133
|
+
if (DOM_ALLOWED_TAGS.has(child.tag)) {
|
|
134
|
+
return child;
|
|
135
|
+
}
|
|
136
|
+
const routedTag = DOM_ROUTING_MAP[child.tag];
|
|
137
|
+
if (routedTag) {
|
|
138
|
+
cleanupElementForRouting(child);
|
|
139
|
+
const routedProps = child.propObservables ?? child.props;
|
|
140
|
+
return createComponent(routedTag, routedProps);
|
|
141
|
+
}
|
|
142
|
+
if (DOM_UNSUPPORTED_TAGS.has(child.tag)) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Component ${child.tag} is not implemented for DOMContainer context yet. Only Sprite is supported.`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return child;
|
|
148
|
+
};
|
|
149
|
+
|
|
83
150
|
export function registerComponent(name, component) {
|
|
84
151
|
components[name] = component;
|
|
85
152
|
}
|
|
86
153
|
|
|
154
|
+
// Track if components have been registered to avoid duplicate imports
|
|
155
|
+
let componentsRegistered = false;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Registers all default CanvasEngine components.
|
|
159
|
+
*
|
|
160
|
+
* This function imports and registers all core components that are available by default.
|
|
161
|
+
* It's called automatically by bootstrapCanvas() if no custom component configuration is provided.
|
|
162
|
+
*
|
|
163
|
+
* Components register themselves when their modules are imported, so this function ensures
|
|
164
|
+
* all component modules are loaded. Since components call registerComponent() at module load time,
|
|
165
|
+
* importing them will automatically register them synchronously.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // Register all default components manually
|
|
170
|
+
* registerAllComponents();
|
|
171
|
+
*
|
|
172
|
+
* // Now you can use any component
|
|
173
|
+
* const sprite = createComponent('Sprite', { image: 'hero.png' });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export function registerAllComponents() {
|
|
177
|
+
if (componentsRegistered) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Components are registered when their modules are imported
|
|
182
|
+
// Since bootstrap.ts imports all components, they should already be registered
|
|
183
|
+
// when bootstrapCanvas() is called. This function just marks that registration
|
|
184
|
+
// has been attempted. If components aren't registered yet, they will be when
|
|
185
|
+
// bootstrap.ts imports them (which happens before bootstrapCanvas() is called).
|
|
186
|
+
componentsRegistered = true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Checks if all dependencies are ready (not undefined).
|
|
191
|
+
* Handles signals synchronously and promises asynchronously.
|
|
192
|
+
* For reactive signals, sets up subscriptions to mount when all become ready.
|
|
193
|
+
*
|
|
194
|
+
* @param deps - Array of signals, promises, or direct values
|
|
195
|
+
* @returns Promise<boolean> - true if all dependencies are ready
|
|
196
|
+
*/
|
|
197
|
+
export async function checkDependencies(
|
|
198
|
+
deps: any[]
|
|
199
|
+
): Promise<boolean> {
|
|
200
|
+
const values = await Promise.all(
|
|
201
|
+
deps.map(async (dep) => {
|
|
202
|
+
if (isSignal(dep)) {
|
|
203
|
+
return dep(); // Read current signal value
|
|
204
|
+
} else if (isPromise(dep)) {
|
|
205
|
+
return await dep; // Await promise resolution
|
|
206
|
+
}
|
|
207
|
+
return dep; // Direct value
|
|
208
|
+
})
|
|
209
|
+
);
|
|
210
|
+
return values.every((v) => v !== undefined);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function waitForDependencies(deps: any[]): Promise<void> {
|
|
214
|
+
return new Promise(async (resolve) => {
|
|
215
|
+
const ready = await checkDependencies(deps);
|
|
216
|
+
if (ready) {
|
|
217
|
+
resolve();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const signalDeps = deps.filter((dep) => isSignal(dep));
|
|
222
|
+
if (signalDeps.length === 0) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const signalObservables = signalDeps.map((sig) => sig.observable);
|
|
227
|
+
const subscription = combineLatest(signalObservables).subscribe(async () => {
|
|
228
|
+
const allReady = await checkDependencies(deps);
|
|
229
|
+
if (allReady) {
|
|
230
|
+
subscription.unsubscribe();
|
|
231
|
+
resolve();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Checks if an element is currently frozen.
|
|
239
|
+
* An element is frozen when the `freeze` prop is set to `true` (either as a boolean or Signal<boolean>),
|
|
240
|
+
* or when any of its parent elements are frozen (recursive freeze propagation).
|
|
241
|
+
*
|
|
242
|
+
* @param element - The element to check
|
|
243
|
+
* @returns `true` if the element is frozen, `false` otherwise
|
|
244
|
+
*/
|
|
245
|
+
export function isElementFrozen(element: Element): boolean {
|
|
246
|
+
if (!element) return false;
|
|
247
|
+
|
|
248
|
+
// Check if this element itself is frozen
|
|
249
|
+
const freezeProp = element.propObservables?.freeze ?? element.props?.freeze;
|
|
250
|
+
|
|
251
|
+
if (freezeProp !== undefined && freezeProp !== null) {
|
|
252
|
+
// Handle Signal<boolean>
|
|
253
|
+
if (isSignal(freezeProp)) {
|
|
254
|
+
if (freezeProp() === true) {
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
} else if (freezeProp === true) {
|
|
258
|
+
// Handle direct boolean
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Check if any parent is frozen (recursive check)
|
|
264
|
+
if (element.parent) {
|
|
265
|
+
return isElementFrozen(element.parent);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Pauses or resumes all animatedSignals in an element based on freeze state.
|
|
273
|
+
*
|
|
274
|
+
* @param element - The element containing animatedSignals
|
|
275
|
+
* @param shouldPause - Whether to pause (true) or resume (false) animations
|
|
276
|
+
*/
|
|
277
|
+
function handleAnimatedSignalsFreeze(element: Element, shouldPause: boolean) {
|
|
278
|
+
if (!element.propObservables) return;
|
|
279
|
+
|
|
280
|
+
const processValue = (value: any) => {
|
|
281
|
+
if (isSignal(value) && isAnimatedSignal(value as any)) {
|
|
282
|
+
const animatedSig = value as unknown as AnimatedSignal<any>;
|
|
283
|
+
if (shouldPause) {
|
|
284
|
+
animatedSig.pause();
|
|
285
|
+
} else {
|
|
286
|
+
animatedSig.resume();
|
|
287
|
+
}
|
|
288
|
+
} else if (isObject(value) && !isElement(value)) {
|
|
289
|
+
// Recursively process nested objects
|
|
290
|
+
Object.values(value).forEach(processValue);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
Object.values(element.propObservables).forEach(processValue);
|
|
295
|
+
}
|
|
296
|
+
|
|
87
297
|
function destroyElement(element: Element | Element[]) {
|
|
88
298
|
if (Array.isArray(element)) {
|
|
89
299
|
element.forEach((e) => destroyElement(e));
|
|
@@ -92,13 +302,34 @@ function destroyElement(element: Element | Element[]) {
|
|
|
92
302
|
if (!element) {
|
|
93
303
|
return;
|
|
94
304
|
}
|
|
95
|
-
element.
|
|
96
|
-
|
|
305
|
+
if (element.props?.children) {
|
|
306
|
+
for (let child of element.props.children) {
|
|
307
|
+
destroyElement(child)
|
|
308
|
+
}
|
|
309
|
+
}
|
|
97
310
|
for (let name in element.directives) {
|
|
98
|
-
element.directives[name].onDestroy?.();
|
|
311
|
+
element.directives[name].onDestroy?.(element);
|
|
312
|
+
}
|
|
313
|
+
if (element.componentInstance && element.componentInstance.onDestroy) {
|
|
314
|
+
element.componentInstance.onDestroy(element.parent as any, () => {
|
|
315
|
+
element.propSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
316
|
+
element.effectSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
317
|
+
element.effectUnmounts?.forEach((fn) => {
|
|
318
|
+
if (isPromise(fn)) {
|
|
319
|
+
(fn as unknown as Promise<any>).then((retFn) => {
|
|
320
|
+
retFn?.();
|
|
321
|
+
});
|
|
322
|
+
} else {
|
|
323
|
+
fn?.();
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
} else {
|
|
328
|
+
// If componentInstance is undefined or doesn't have onDestroy, still clean up subscriptions
|
|
329
|
+
element.propSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
330
|
+
element.effectSubscriptions?.forEach((sub) => sub.unsubscribe());
|
|
331
|
+
element.effectUnmounts?.forEach((fn) => fn?.());
|
|
99
332
|
}
|
|
100
|
-
element.componentInstance.onDestroy?.(element.parent as any);
|
|
101
|
-
element.effectUnmounts.forEach((fn) => fn?.());
|
|
102
333
|
}
|
|
103
334
|
|
|
104
335
|
/**
|
|
@@ -131,6 +362,7 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
131
362
|
destroyElement(this);
|
|
132
363
|
},
|
|
133
364
|
allElements: new Subject(),
|
|
365
|
+
isFrozen: false,
|
|
134
366
|
};
|
|
135
367
|
|
|
136
368
|
// Iterate over each property in the props object
|
|
@@ -149,27 +381,78 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
149
381
|
const _value = value as Signal<any>;
|
|
150
382
|
if ("dependencies" in _value && _value.dependencies.size == 0) {
|
|
151
383
|
_set(path, key, _value());
|
|
384
|
+
// Handle freeze prop initialization
|
|
385
|
+
if (key === "freeze") {
|
|
386
|
+
element.isFrozen = _value() === true;
|
|
387
|
+
}
|
|
152
388
|
return;
|
|
153
389
|
}
|
|
390
|
+
|
|
391
|
+
// Handle freeze prop as signal
|
|
392
|
+
if (key === "freeze") {
|
|
393
|
+
element.isFrozen = _value() === true;
|
|
394
|
+
|
|
395
|
+
// Pause/resume animatedSignals based on initial freeze state
|
|
396
|
+
handleAnimatedSignalsFreeze(element, element.isFrozen);
|
|
397
|
+
|
|
398
|
+
element.propSubscriptions.push(
|
|
399
|
+
_value.observable.subscribe((freezeValue) => {
|
|
400
|
+
const wasFrozen = element.isFrozen;
|
|
401
|
+
element.isFrozen = freezeValue === true;
|
|
402
|
+
|
|
403
|
+
// Handle animatedSignal pause/resume when freeze state changes
|
|
404
|
+
if (wasFrozen !== element.isFrozen) {
|
|
405
|
+
handleAnimatedSignalsFreeze(element, element.isFrozen);
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
154
412
|
element.propSubscriptions.push(
|
|
155
413
|
_value.observable.subscribe((value) => {
|
|
414
|
+
// Block updates if element is frozen
|
|
415
|
+
if (isElementFrozen(element)) {
|
|
416
|
+
// Pause animatedSignal if it's an animated signal
|
|
417
|
+
if (isAnimatedSignal(_value as any)) {
|
|
418
|
+
(_value as unknown as AnimatedSignal<any>).pause();
|
|
419
|
+
}
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Resume animatedSignal if it was paused
|
|
424
|
+
if (isAnimatedSignal(_value as any)) {
|
|
425
|
+
(_value as unknown as AnimatedSignal<any>).resume();
|
|
426
|
+
}
|
|
427
|
+
|
|
156
428
|
_set(path, key, value);
|
|
157
429
|
if (element.directives[key]) {
|
|
158
|
-
element.directives[key].onUpdate?.(value);
|
|
430
|
+
element.directives[key].onUpdate?.(value, element);
|
|
159
431
|
}
|
|
160
432
|
if (key == "tick") {
|
|
433
|
+
// Block tick updates if element is frozen
|
|
434
|
+
if (isElementFrozen(element)) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
161
437
|
return
|
|
162
438
|
}
|
|
163
439
|
instance.onUpdate?.(
|
|
164
440
|
path == ""
|
|
165
441
|
? {
|
|
166
|
-
|
|
167
|
-
|
|
442
|
+
[key]: value,
|
|
443
|
+
}
|
|
168
444
|
: set({}, path + "." + key, value)
|
|
169
445
|
);
|
|
170
446
|
})
|
|
171
447
|
);
|
|
172
448
|
} else {
|
|
449
|
+
// Handle freeze prop as direct boolean
|
|
450
|
+
if (key === "freeze") {
|
|
451
|
+
element.isFrozen = value === true;
|
|
452
|
+
|
|
453
|
+
// Pause/resume animatedSignals based on freeze state
|
|
454
|
+
handleAnimatedSignalsFreeze(element, element.isFrozen);
|
|
455
|
+
}
|
|
173
456
|
if (isObject(value) && key != "context" && !isElement(value)) {
|
|
174
457
|
recursiveProps(value, (path ? path + "." : "") + key);
|
|
175
458
|
} else {
|
|
@@ -199,9 +482,69 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
199
482
|
}
|
|
200
483
|
}
|
|
201
484
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
485
|
+
/**
|
|
486
|
+
* Checks if all dependencies are ready (not undefined).
|
|
487
|
+
* Handles signals synchronously and promises asynchronously.
|
|
488
|
+
* For reactive signals, sets up subscriptions to mount when all become ready.
|
|
489
|
+
*
|
|
490
|
+
* @param deps - Array of signals, promises, or direct values
|
|
491
|
+
* @returns Promise<boolean> - true if all dependencies are ready
|
|
492
|
+
*/
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Sets up subscriptions to reactive signal dependencies.
|
|
497
|
+
* When all signals become defined, mounts the component.
|
|
498
|
+
*/
|
|
499
|
+
/**
|
|
500
|
+
* Sets up subscriptions to reactive signal dependencies.
|
|
501
|
+
* When all signals become defined, mounts the component.
|
|
502
|
+
*/
|
|
503
|
+
function setupDependencySubscriptions(
|
|
504
|
+
parent: Element,
|
|
505
|
+
element: Element,
|
|
506
|
+
deps: any[],
|
|
507
|
+
index?: number
|
|
508
|
+
) {
|
|
509
|
+
const signalDeps = deps.filter((dep) => isSignal(dep));
|
|
510
|
+
const promiseDeps = deps.filter((dep) => isPromise(dep));
|
|
511
|
+
|
|
512
|
+
if (signalDeps.length === 0) {
|
|
513
|
+
// No reactive signals, nothing to subscribe to
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// Create observables from signals
|
|
518
|
+
const signalObservables = signalDeps.map((sig) => sig.observable);
|
|
519
|
+
|
|
520
|
+
// Combine all signal observables
|
|
521
|
+
const subscription = combineLatest(signalObservables).subscribe(
|
|
522
|
+
async () => {
|
|
523
|
+
// Check if all dependencies are now ready
|
|
524
|
+
const allReady = await checkDependencies(deps);
|
|
525
|
+
if (allReady) {
|
|
526
|
+
// Unsubscribe - we only need to mount once
|
|
527
|
+
subscription.unsubscribe();
|
|
528
|
+
// Remove from subscriptions
|
|
529
|
+
const idx = element.propSubscriptions.indexOf(subscription);
|
|
530
|
+
if (idx > -1) {
|
|
531
|
+
element.propSubscriptions.splice(idx, 1);
|
|
532
|
+
}
|
|
533
|
+
// Now mount the component
|
|
534
|
+
performMount(parent, element, index);
|
|
535
|
+
propagateContext(element);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
// Store subscription for cleanup
|
|
541
|
+
element.propSubscriptions.push(subscription);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Performs the actual mounting of the component.
|
|
546
|
+
*/
|
|
547
|
+
function performMount(parent: Element, element: Element, index?: number) {
|
|
205
548
|
element.componentInstance.onMount?.(element, index);
|
|
206
549
|
for (let name in element.directives) {
|
|
207
550
|
element.directives[name].onMount?.(element);
|
|
@@ -209,6 +552,34 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
209
552
|
element.effectMounts.forEach((fn: any) => {
|
|
210
553
|
element.effectUnmounts.push(fn(element));
|
|
211
554
|
});
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
async function onMount(parent: Element, element: Element, index?: number) {
|
|
558
|
+
let actualParent = parent;
|
|
559
|
+
while (actualParent?.tag === 'fragment') {
|
|
560
|
+
actualParent = actualParent.parent;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
element.props.context = actualParent.props.context;
|
|
564
|
+
element.parent = actualParent;
|
|
565
|
+
|
|
566
|
+
// Inherit freeze state from parent if element doesn't have its own freeze prop
|
|
567
|
+
if (!element.propObservables?.freeze && !element.props?.freeze && isElementFrozen(actualParent)) {
|
|
568
|
+
element.isFrozen = true;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// Check dependencies before mounting
|
|
572
|
+
if (element.props.dependencies && Array.isArray(element.props.dependencies)) {
|
|
573
|
+
const deps = element.props.dependencies;
|
|
574
|
+
const ready = await checkDependencies(deps);
|
|
575
|
+
if (!ready) {
|
|
576
|
+
// Set up subscriptions for reactive signals to trigger mount later
|
|
577
|
+
setupDependencySubscriptions(actualParent, element, deps, index);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
performMount(actualParent, element, index);
|
|
212
583
|
};
|
|
213
584
|
|
|
214
585
|
async function propagateContext(element) {
|
|
@@ -219,71 +590,246 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
219
590
|
}
|
|
220
591
|
else {
|
|
221
592
|
await new Promise((resolve) => {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
593
|
+
let lastElement = null
|
|
594
|
+
element.propSubscriptions.push(element.propObservables.attach.observable.subscribe(async (args) => {
|
|
595
|
+
const value = args?.value ?? args
|
|
596
|
+
if (!value) {
|
|
597
|
+
throw new Error(`attach in ${element.tag} is undefined or null, add a component`)
|
|
598
|
+
}
|
|
599
|
+
if (lastElement) {
|
|
600
|
+
destroyElement(lastElement)
|
|
601
|
+
}
|
|
602
|
+
lastElement = value
|
|
603
|
+
await createElement(element, value)
|
|
604
|
+
resolve(undefined)
|
|
605
|
+
}))
|
|
235
606
|
})
|
|
236
607
|
}
|
|
237
608
|
}
|
|
238
609
|
if (!element.props.children) {
|
|
239
610
|
return;
|
|
240
611
|
}
|
|
241
|
-
for (let
|
|
612
|
+
for (let i = 0; i < element.props.children.length; i++) {
|
|
613
|
+
const child = element.props.children[i];
|
|
242
614
|
if (!child) continue;
|
|
243
|
-
await createElement(element, child)
|
|
615
|
+
await createElement(element, child, i)
|
|
244
616
|
}
|
|
245
617
|
};
|
|
246
618
|
|
|
247
|
-
|
|
619
|
+
/**
|
|
620
|
+
* Creates and mounts a child element to a parent element.
|
|
621
|
+
* Handles different types of children: Elements, Promises resolving to Elements, and Observables.
|
|
622
|
+
*
|
|
623
|
+
* @description This function is designed to handle reactive child components that can be:
|
|
624
|
+
* - Direct Element instances
|
|
625
|
+
* - Promises that resolve to Elements (for async components)
|
|
626
|
+
* - Observables that emit Elements, arrays of Elements, or FlowObservable results
|
|
627
|
+
* - Nested observables within arrays or FlowObservable results (handled recursively)
|
|
628
|
+
*
|
|
629
|
+
* For Observables, it subscribes to the stream and automatically mounts/unmounts elements
|
|
630
|
+
* as they are emitted. The function handles nested observables recursively, ensuring that
|
|
631
|
+
* observables within arrays or FlowObservable results are also properly subscribed to.
|
|
632
|
+
* All subscriptions are stored in the parent's effectSubscriptions for automatic cleanup.
|
|
633
|
+
*
|
|
634
|
+
* @param {Element} parent - The parent element to mount the child to
|
|
635
|
+
* @param {Element | Observable<any> | Promise<Element>} child - The child to create and mount
|
|
636
|
+
*
|
|
637
|
+
* @example
|
|
638
|
+
* ```typescript
|
|
639
|
+
* // Direct element
|
|
640
|
+
* await createElement(parent, childElement);
|
|
641
|
+
*
|
|
642
|
+
* // Observable of elements (from cond, loop, etc.)
|
|
643
|
+
* await createElement(parent, cond(signal(visible), () => h(Container)));
|
|
644
|
+
*
|
|
645
|
+
* // Observable that emits arrays containing other observables
|
|
646
|
+
* await createElement(parent, observableOfObservables);
|
|
647
|
+
*
|
|
648
|
+
* // Promise resolving to element
|
|
649
|
+
* await createElement(parent, import('./MyComponent').then(mod => h(mod.default)));
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
async function createElement(parent: Element, child: Element | Observable<any> | Promise<Element>, childOrder?: number) {
|
|
248
653
|
if (isPromise(child)) {
|
|
249
654
|
child = await child;
|
|
250
655
|
}
|
|
656
|
+
|
|
657
|
+
const childGroups = ((parent as any).__childGroups ??= []);
|
|
658
|
+
const resolvedOrder =
|
|
659
|
+
childOrder ??
|
|
660
|
+
(parent.props.children ? parent.props.children.indexOf(child as any) : -1);
|
|
661
|
+
const childGroup = {
|
|
662
|
+
order: resolvedOrder >= 0 ? resolvedOrder : childGroups.length,
|
|
663
|
+
mounted: new Map<any, Element>(),
|
|
664
|
+
};
|
|
665
|
+
childGroups.push(childGroup);
|
|
666
|
+
|
|
667
|
+
const getMountedIndex = (element?: Element): number | undefined => {
|
|
668
|
+
const children = (parent.componentInstance as any)?.children;
|
|
669
|
+
if (!element || !children) return;
|
|
670
|
+
const index = children.indexOf(element.componentInstance);
|
|
671
|
+
return index >= 0 ? index : undefined;
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
const getNextGroupIndex = (): number | undefined => {
|
|
675
|
+
const nextGroups = childGroups
|
|
676
|
+
.filter((group) => group !== childGroup && group.order > childGroup.order)
|
|
677
|
+
.sort((a, b) => a.order - b.order);
|
|
678
|
+
|
|
679
|
+
for (const group of nextGroups) {
|
|
680
|
+
for (const mounted of group.mounted.values()) {
|
|
681
|
+
const index = getMountedIndex(mounted);
|
|
682
|
+
if (index !== undefined) return index;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
const getInsertIndex = (
|
|
688
|
+
sourceIndex: number,
|
|
689
|
+
orderedSources: any[]
|
|
690
|
+
): number | undefined => {
|
|
691
|
+
for (let i = sourceIndex + 1; i < orderedSources.length; i++) {
|
|
692
|
+
const index = getMountedIndex(childGroup.mounted.get(orderedSources[i]));
|
|
693
|
+
if (index !== undefined) return index;
|
|
694
|
+
}
|
|
695
|
+
return getNextGroupIndex();
|
|
696
|
+
};
|
|
697
|
+
|
|
251
698
|
if (child instanceof Observable) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
699
|
+
const mountedFlowElements = childGroup.mounted;
|
|
700
|
+
|
|
701
|
+
const createFragmentOwner = (): Element => ({
|
|
702
|
+
tag: 'fragment',
|
|
703
|
+
props: { children: [] },
|
|
704
|
+
componentInstance: {} as any,
|
|
705
|
+
propSubscriptions: [],
|
|
706
|
+
effectSubscriptions: [],
|
|
707
|
+
effectMounts: [],
|
|
708
|
+
effectUnmounts: [],
|
|
709
|
+
propObservables: {},
|
|
710
|
+
parent,
|
|
711
|
+
directives: {},
|
|
712
|
+
destroy() { destroyElement(this) },
|
|
713
|
+
allElements: new Subject(),
|
|
714
|
+
isFrozen: false
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
const mountFlowElement = (
|
|
718
|
+
element: Element,
|
|
719
|
+
sourceIndex: number,
|
|
720
|
+
orderedSources: any[]
|
|
721
|
+
) => {
|
|
722
|
+
if (mountedFlowElements.has(element)) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
const routed = routeDomComponent(parent, element);
|
|
727
|
+
mountedFlowElements.set(element, routed);
|
|
728
|
+
onMount(parent, routed, getInsertIndex(sourceIndex, orderedSources));
|
|
729
|
+
propagateContext(routed);
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
const syncFlowElements = (nextElements: Set<any>) => {
|
|
733
|
+
mountedFlowElements.forEach((mounted, source) => {
|
|
734
|
+
if (nextElements.has(source)) {
|
|
268
735
|
return;
|
|
269
736
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
737
|
+
mountedFlowElements.delete(source);
|
|
738
|
+
if (mounted !== source) {
|
|
739
|
+
destroyElement(mounted);
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
const processFlowComponent = (
|
|
745
|
+
component: any,
|
|
746
|
+
nextElements: Set<any>,
|
|
747
|
+
index: number,
|
|
748
|
+
orderedSources: any[]
|
|
749
|
+
) => {
|
|
750
|
+
if (component instanceof Observable) {
|
|
751
|
+
nextElements.add(component);
|
|
752
|
+
if (!mountedFlowElements.has(component)) {
|
|
753
|
+
const owner = createFragmentOwner();
|
|
754
|
+
mountedFlowElements.set(component, owner);
|
|
755
|
+
void createElement(owner, component);
|
|
756
|
+
}
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
if (Array.isArray(component)) {
|
|
760
|
+
component.forEach((comp) =>
|
|
761
|
+
processFlowComponent(comp, nextElements, index, orderedSources)
|
|
762
|
+
);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
if (!isElement(component)) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
nextElements.add(component);
|
|
770
|
+
mountFlowElement(component, index, orderedSources);
|
|
771
|
+
};
|
|
772
|
+
|
|
773
|
+
// Subscribe to the observable and handle the emitted values
|
|
774
|
+
const subscription = child.subscribe(
|
|
775
|
+
(value: any) => {
|
|
776
|
+
// Handle different types of observable emissions
|
|
777
|
+
if (value && typeof value === 'object' && 'elements' in value) {
|
|
778
|
+
// Handle FlowObservable result (from loop, cond, etc.)
|
|
779
|
+
const {
|
|
780
|
+
elements: comp,
|
|
781
|
+
prev,
|
|
782
|
+
}: {
|
|
783
|
+
elements: Element[];
|
|
784
|
+
prev?: Element;
|
|
785
|
+
} = value;
|
|
786
|
+
|
|
787
|
+
const components = comp.filter((c) => c !== null);
|
|
788
|
+
const nextElements = new Set<any>();
|
|
789
|
+
if (prev) {
|
|
790
|
+
components.forEach((c) => {
|
|
791
|
+
const index = parent.props.children.indexOf(prev.props.key);
|
|
792
|
+
processFlowComponent(c, nextElements, index + 1, components);
|
|
278
793
|
});
|
|
794
|
+
syncFlowElements(nextElements);
|
|
795
|
+
return;
|
|
279
796
|
}
|
|
280
|
-
|
|
281
|
-
|
|
797
|
+
components.forEach((component, index) => {
|
|
798
|
+
processFlowComponent(component, nextElements, index, components);
|
|
799
|
+
});
|
|
800
|
+
syncFlowElements(nextElements);
|
|
801
|
+
} else if (isElement(value)) {
|
|
802
|
+
// Handle direct Element emission
|
|
803
|
+
const routed = routeDomComponent(parent, value);
|
|
804
|
+
childGroup.mounted.set(value, routed);
|
|
805
|
+
onMount(parent, routed, getInsertIndex(0, [value]));
|
|
806
|
+
propagateContext(routed);
|
|
807
|
+
} else if (Array.isArray(value)) {
|
|
808
|
+
// Handle array of elements (which can also be observables)
|
|
809
|
+
const nextElements = new Set<any>();
|
|
810
|
+
value.forEach((element, index) => {
|
|
811
|
+
processFlowComponent(element, nextElements, index, value);
|
|
812
|
+
});
|
|
813
|
+
syncFlowElements(nextElements);
|
|
814
|
+
}
|
|
815
|
+
elementsListen.next(undefined);
|
|
282
816
|
}
|
|
283
817
|
);
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
818
|
+
|
|
819
|
+
subscription.add(() => {
|
|
820
|
+
mountedFlowElements.forEach((mounted) => {
|
|
821
|
+
destroyElement(mounted);
|
|
822
|
+
});
|
|
823
|
+
mountedFlowElements.clear();
|
|
824
|
+
});
|
|
825
|
+
|
|
826
|
+
// Store subscription for cleanup
|
|
827
|
+
parent.effectSubscriptions.push(subscription);
|
|
828
|
+
} else if (isElement(child)) {
|
|
829
|
+
const routed = routeDomComponent(parent, child);
|
|
830
|
+
childGroup.mounted.set(child, routed);
|
|
831
|
+
onMount(parent, routed, getInsertIndex(0, [child]));
|
|
832
|
+
await propagateContext(routed);
|
|
287
833
|
}
|
|
288
834
|
}
|
|
289
835
|
|
|
@@ -292,148 +838,405 @@ export function createComponent(tag: string, props?: Props): Element {
|
|
|
292
838
|
}
|
|
293
839
|
|
|
294
840
|
/**
|
|
295
|
-
* Observes a BehaviorSubject containing an array of items and dynamically creates child elements for each item.
|
|
841
|
+
* Observes a BehaviorSubject containing an array or object of items and dynamically creates child elements for each item.
|
|
296
842
|
*
|
|
297
|
-
* @param {
|
|
843
|
+
* @param {WritableArraySignal<T> | WritableObjectSignal<T>} itemsSubject - A signal that emits an array or object of items.
|
|
298
844
|
* @param {Function} createElementFn - A function that takes an item and returns an element representation.
|
|
299
845
|
* @returns {Observable} An observable that emits the list of created child elements.
|
|
300
846
|
*/
|
|
301
|
-
export function loop<T
|
|
302
|
-
itemsSubject:
|
|
303
|
-
createElementFn: (item:
|
|
847
|
+
export function loop<T>(
|
|
848
|
+
itemsSubject: any,
|
|
849
|
+
createElementFn: (item: T, index: number | string) => Element | null
|
|
304
850
|
): FlowObservable {
|
|
305
|
-
let elements: Element[] = [];
|
|
306
851
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
};
|
|
852
|
+
if (isComputed(itemsSubject) && itemsSubject.dependencies.size == 0) {
|
|
853
|
+
itemsSubject = signal(itemsSubject());
|
|
854
|
+
}
|
|
855
|
+
else if (!isSignal(itemsSubject)) {
|
|
856
|
+
itemsSubject = signal(itemsSubject);
|
|
857
|
+
}
|
|
314
858
|
|
|
315
859
|
return defer(() => {
|
|
316
|
-
let
|
|
317
|
-
let
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
860
|
+
let elements: Element[] = [];
|
|
861
|
+
let elementMap = new Map<string | number, Element>();
|
|
862
|
+
let isFirstSubscription = true;
|
|
863
|
+
|
|
864
|
+
const ensureElement = (itemResult: any): Element | null => {
|
|
865
|
+
if (!itemResult) return null;
|
|
866
|
+
if (isElement(itemResult)) return itemResult;
|
|
867
|
+
return {
|
|
868
|
+
tag: 'fragment',
|
|
869
|
+
props: { children: Array.isArray(itemResult) ? itemResult : [itemResult] },
|
|
870
|
+
componentInstance: {} as any,
|
|
871
|
+
propSubscriptions: [],
|
|
872
|
+
effectSubscriptions: [],
|
|
873
|
+
effectMounts: [],
|
|
874
|
+
effectUnmounts: [],
|
|
875
|
+
propObservables: {},
|
|
876
|
+
parent: null,
|
|
877
|
+
directives: {},
|
|
878
|
+
destroy() { destroyElement(this) },
|
|
879
|
+
allElements: new Subject(),
|
|
880
|
+
isFrozen: false
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
const isArraySignal = (signal: any): signal is WritableArraySignal<T[]> =>
|
|
885
|
+
Array.isArray(signal());
|
|
886
|
+
|
|
887
|
+
return new Observable<FlowResult>(subscriber => {
|
|
888
|
+
const subscription = isArraySignal(itemsSubject)
|
|
889
|
+
? itemsSubject.observable.subscribe(change => {
|
|
890
|
+
if (isFirstSubscription) {
|
|
891
|
+
isFirstSubscription = false;
|
|
892
|
+
elements.forEach(el => el.destroy());
|
|
893
|
+
elements = [];
|
|
894
|
+
elementMap.clear();
|
|
895
|
+
|
|
896
|
+
const items = itemsSubject();
|
|
897
|
+
if (items) {
|
|
898
|
+
items.forEach((item, index) => {
|
|
899
|
+
const element = ensureElement(createElementFn(item, index));
|
|
900
|
+
if (element) {
|
|
901
|
+
elements.push(element);
|
|
902
|
+
elementMap.set(index, element);
|
|
903
|
+
}
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
subscriber.next({
|
|
907
|
+
elements: [...elements]
|
|
908
|
+
});
|
|
909
|
+
return;
|
|
327
910
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
911
|
+
|
|
912
|
+
// Handle computed signals that emit array values directly (not ArrayChange objects)
|
|
913
|
+
// When a computed emits, `change` is the array itself, not an object with `type`
|
|
914
|
+
const isDirectArrayChange = Array.isArray(change) || (change && typeof change === 'object' && !('type' in change));
|
|
915
|
+
|
|
916
|
+
if (change.type === 'init' || change.type === 'reset' || isDirectArrayChange) {
|
|
917
|
+
elements.forEach(el => destroyElement(el));
|
|
918
|
+
elements = [];
|
|
919
|
+
elementMap.clear();
|
|
920
|
+
|
|
921
|
+
const items = itemsSubject();
|
|
922
|
+
if (items) {
|
|
923
|
+
items.forEach((item, index) => {
|
|
924
|
+
const element = ensureElement(createElementFn(item, index));
|
|
925
|
+
if (element) {
|
|
926
|
+
elements.push(element);
|
|
927
|
+
elementMap.set(index, element);
|
|
928
|
+
}
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
} else if (change.type === 'add' && change.index !== undefined) {
|
|
932
|
+
const newElements = change.items.map((item, i) => {
|
|
933
|
+
const element = ensureElement(createElementFn(item as T, change.index! + i));
|
|
934
|
+
if (element) {
|
|
935
|
+
elementMap.set(change.index! + i, element);
|
|
936
|
+
}
|
|
937
|
+
return element;
|
|
938
|
+
}).filter((el): el is Element => el !== null);
|
|
939
|
+
|
|
940
|
+
elements.splice(change.index, 0, ...newElements);
|
|
941
|
+
} else if (change.type === 'remove' && change.index !== undefined) {
|
|
942
|
+
const removed = elements.splice(change.index, 1);
|
|
943
|
+
removed.forEach(el => {
|
|
944
|
+
destroyElement(el)
|
|
945
|
+
elementMap.delete(change.index!);
|
|
339
946
|
});
|
|
947
|
+
} else if (change.type === 'update' && change.index !== undefined && change.items.length === 1) {
|
|
948
|
+
const index = change.index;
|
|
949
|
+
const newItem = change.items[0];
|
|
950
|
+
|
|
951
|
+
// Check if the previous item at this index was effectively undefined or non-existent
|
|
952
|
+
if (index >= elements.length || elements[index] === undefined || !elementMap.has(index)) {
|
|
953
|
+
// Treat as add operation
|
|
954
|
+
const newElement = ensureElement(createElementFn(newItem as T, index));
|
|
955
|
+
if (newElement) {
|
|
956
|
+
elements.splice(index, 0, newElement); // Insert at the correct index
|
|
957
|
+
elementMap.set(index, newElement);
|
|
958
|
+
// Adjust indices in elementMap for subsequent elements might be needed if map relied on exact indices
|
|
959
|
+
// This simple implementation assumes keys are stable or createElementFn handles context correctly
|
|
960
|
+
} else {
|
|
961
|
+
console.warn(`Element creation returned null for index ${index} during add-like update.`);
|
|
962
|
+
}
|
|
963
|
+
} else {
|
|
964
|
+
// Treat as a standard update operation
|
|
965
|
+
const oldElement = elements[index];
|
|
966
|
+
destroyElement(oldElement)
|
|
967
|
+
const newElement = ensureElement(createElementFn(newItem as T, index));
|
|
968
|
+
if (newElement) {
|
|
969
|
+
elements[index] = newElement;
|
|
970
|
+
elementMap.set(index, newElement);
|
|
971
|
+
} else {
|
|
972
|
+
// Handle case where new element creation returns null
|
|
973
|
+
elements.splice(index, 1);
|
|
974
|
+
elementMap.delete(index);
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
subscriber.next({
|
|
980
|
+
elements: [...elements] // Create a new array to ensure change detection
|
|
981
|
+
});
|
|
982
|
+
})
|
|
983
|
+
: (itemsSubject as WritableObjectSignal<T>).observable.subscribe(change => {
|
|
984
|
+
const key = change.key as string | number
|
|
985
|
+
if (isFirstSubscription) {
|
|
986
|
+
isFirstSubscription = false;
|
|
987
|
+
elements.forEach(el => destroyElement(el));
|
|
340
988
|
elements = [];
|
|
989
|
+
elementMap.clear();
|
|
990
|
+
|
|
991
|
+
const items = (itemsSubject as WritableObjectSignal<T>)();
|
|
992
|
+
if (items) {
|
|
993
|
+
Object.entries(items).forEach(([key, value]) => {
|
|
994
|
+
const element = ensureElement(createElementFn(value, key));
|
|
995
|
+
if (element) {
|
|
996
|
+
elements.push(element);
|
|
997
|
+
elementMap.set(key, element);
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
subscriber.next({
|
|
1002
|
+
elements: [...elements]
|
|
1003
|
+
});
|
|
1004
|
+
return;
|
|
341
1005
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
elements
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
1006
|
+
|
|
1007
|
+
if (change.type === 'init' || change.type === 'reset') {
|
|
1008
|
+
elements.forEach(el => destroyElement(el));
|
|
1009
|
+
elements = [];
|
|
1010
|
+
elementMap.clear();
|
|
1011
|
+
|
|
1012
|
+
const items = (itemsSubject as WritableObjectSignal<T>)();
|
|
1013
|
+
if (items) {
|
|
1014
|
+
Object.entries(items).forEach(([key, value]) => {
|
|
1015
|
+
const element = ensureElement(createElementFn(value, key));
|
|
1016
|
+
if (element) {
|
|
1017
|
+
elements.push(element);
|
|
1018
|
+
elementMap.set(key, element);
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
} else if (change.type === 'add' && change.key && change.value !== undefined) {
|
|
1023
|
+
const element = ensureElement(createElementFn(change.value as T, key));
|
|
1024
|
+
if (element) {
|
|
1025
|
+
elements.push(element);
|
|
1026
|
+
elementMap.set(key, element);
|
|
1027
|
+
}
|
|
1028
|
+
} else if (change.type === 'remove' && change.key) {
|
|
1029
|
+
const index = elements.findIndex(el => elementMap.get(key) === el);
|
|
1030
|
+
if (index !== -1) {
|
|
1031
|
+
const [removed] = elements.splice(index, 1);
|
|
1032
|
+
destroyElement(removed)
|
|
1033
|
+
elementMap.delete(key);
|
|
1034
|
+
}
|
|
1035
|
+
} else if (change.type === 'update' && change.key && change.value !== undefined) {
|
|
1036
|
+
const index = elements.findIndex(el => elementMap.get(key) === el);
|
|
1037
|
+
if (index !== -1) {
|
|
1038
|
+
const oldElement = elements[index];
|
|
1039
|
+
destroyElement(oldElement)
|
|
1040
|
+
const newElement = ensureElement(createElementFn(change.value as T, key));
|
|
1041
|
+
if (newElement) {
|
|
1042
|
+
elements[index] = newElement;
|
|
1043
|
+
elementMap.set(key, newElement);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
subscriber.next({
|
|
1049
|
+
elements: [...elements] // Create a new array to ensure change detection
|
|
1050
|
+
});
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
return () => {
|
|
1054
|
+
subscription.unsubscribe();
|
|
1055
|
+
elements.forEach(el => destroyElement(el));
|
|
1056
|
+
};
|
|
1057
|
+
});
|
|
1058
|
+
}).pipe(shareReplay({ bufferSize: 1, refCount: true }));
|
|
370
1059
|
}
|
|
371
1060
|
|
|
372
1061
|
/**
|
|
373
|
-
* Conditionally creates and destroys elements based on
|
|
1062
|
+
* Conditionally creates and destroys elements based on condition signals with support for else if and else.
|
|
374
1063
|
*
|
|
375
|
-
* @
|
|
1064
|
+
* @description This function creates conditional rendering with support for multiple conditions (if/else if/else pattern).
|
|
1065
|
+
* It evaluates conditions in order and renders the first matching condition's element.
|
|
1066
|
+
* The function maintains full reactivity with signals and ensures proper cleanup of elements.
|
|
1067
|
+
*
|
|
1068
|
+
* @param {Signal<boolean> | boolean | (() => boolean)} condition - A signal, boolean, or function that determines whether to create an element.
|
|
376
1069
|
* @param {Function} createElementFn - A function that returns an element or a promise that resolves to an element.
|
|
377
|
-
* @
|
|
1070
|
+
* @param {...Array} additionalConditions - Additional conditions for else if and else cases.
|
|
1071
|
+
* Can be:
|
|
1072
|
+
* - A function for else case: `() => Element | Promise<Element>`
|
|
1073
|
+
* - An array for else if case: `[Signal<boolean> | boolean | (() => boolean), () => Element | Promise<Element>]`
|
|
1074
|
+
* @returns {Observable} An observable that emits the created element based on the matching condition.
|
|
1075
|
+
*
|
|
1076
|
+
* @example
|
|
1077
|
+
* ```typescript
|
|
1078
|
+
* // Simple if/else
|
|
1079
|
+
* cond(
|
|
1080
|
+
* signal(isVisible),
|
|
1081
|
+
* () => h(Container),
|
|
1082
|
+
* () => h(Text, { text: 'Hidden' }) // else
|
|
1083
|
+
* );
|
|
1084
|
+
*
|
|
1085
|
+
* // Multiple else if + else
|
|
1086
|
+
* cond(
|
|
1087
|
+
* signal(status === 'loading'),
|
|
1088
|
+
* () => h(LoadingSpinner),
|
|
1089
|
+
* [signal(status === 'error'), () => h(ErrorMessage)], // else if
|
|
1090
|
+
* [signal(status === 'success'), () => h(SuccessMessage)], // else if
|
|
1091
|
+
* () => h(DefaultMessage) // else
|
|
1092
|
+
* );
|
|
1093
|
+
* ```
|
|
378
1094
|
*/
|
|
379
1095
|
export function cond(
|
|
380
|
-
condition: Signal<boolean> | boolean,
|
|
381
|
-
createElementFn: () => Element | Promise<Element
|
|
1096
|
+
condition: Signal<boolean> | boolean | (() => boolean),
|
|
1097
|
+
createElementFn: () => Element | Promise<Element>,
|
|
1098
|
+
...additionalConditions: Array<
|
|
1099
|
+
| (() => Element | Promise<Element>) // else final
|
|
1100
|
+
| [Signal<boolean> | boolean | (() => boolean), () => Element | Promise<Element>] // else if
|
|
1101
|
+
>
|
|
382
1102
|
): FlowObservable {
|
|
383
|
-
let
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
1103
|
+
let currentElement: Element | null = null;
|
|
1104
|
+
let currentConditionIndex = -1;
|
|
1105
|
+
|
|
1106
|
+
// Parse additional conditions
|
|
1107
|
+
const elseIfConditions: Array<{
|
|
1108
|
+
condition: Signal<boolean>;
|
|
1109
|
+
elementFn: () => Element | Promise<Element>;
|
|
1110
|
+
}> = [];
|
|
1111
|
+
let elseElementFn: (() => Element | Promise<Element>) | null = null;
|
|
1112
|
+
|
|
1113
|
+
// Convert function conditions to computed signals
|
|
1114
|
+
const convertConditionToSignal = (cond: Signal<boolean> | boolean | (() => boolean)): Signal<boolean> => {
|
|
1115
|
+
if (isSignal(cond)) {
|
|
1116
|
+
return cond as Signal<boolean>;
|
|
1117
|
+
} else if (typeof cond === 'function') {
|
|
1118
|
+
return computed(cond as () => boolean);
|
|
1119
|
+
} else {
|
|
1120
|
+
return signal(cond as boolean);
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
|
|
1124
|
+
// Process additional conditions
|
|
1125
|
+
for (const param of additionalConditions) {
|
|
1126
|
+
if (Array.isArray(param)) {
|
|
1127
|
+
// else if case: [condition, elementFn]
|
|
1128
|
+
elseIfConditions.push({
|
|
1129
|
+
condition: convertConditionToSignal(param[0]),
|
|
1130
|
+
elementFn: param[1],
|
|
1131
|
+
});
|
|
1132
|
+
} else if (typeof param === 'function') {
|
|
1133
|
+
// else case: elementFn (should be the last one)
|
|
1134
|
+
elseElementFn = param;
|
|
1135
|
+
break; // Stop processing after else
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
// Collect all conditions with their element functions
|
|
1140
|
+
const allConditions = [
|
|
1141
|
+
{ condition: convertConditionToSignal(condition), elementFn: createElementFn },
|
|
1142
|
+
...elseIfConditions,
|
|
1143
|
+
];
|
|
1144
|
+
|
|
1145
|
+
// All conditions are now signals, so we always use the reactive path
|
|
1146
|
+
return new Observable<{ elements: Element[], type?: "init" | "remove" }>(subscriber => {
|
|
1147
|
+
const subscriptions: Subscription[] = [];
|
|
1148
|
+
|
|
1149
|
+
const evaluateConditions = () => {
|
|
1150
|
+
// Find the first matching condition
|
|
1151
|
+
let matchingIndex = -1;
|
|
1152
|
+
for (let i = 0; i < allConditions.length; i++) {
|
|
1153
|
+
const condition = allConditions[i].condition;
|
|
1154
|
+
const conditionValue = condition();
|
|
1155
|
+
|
|
1156
|
+
if (conditionValue) {
|
|
1157
|
+
matchingIndex = i;
|
|
1158
|
+
break;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
// If no condition matches and we have an else, use else
|
|
1163
|
+
const shouldUseElse = matchingIndex === -1 && elseElementFn;
|
|
1164
|
+
const newConditionIndex = shouldUseElse ? -2 : matchingIndex; // -2 for else, -1 for nothing
|
|
1165
|
+
|
|
1166
|
+
// Only update if the condition changed
|
|
1167
|
+
if (newConditionIndex !== currentConditionIndex) {
|
|
1168
|
+
// Destroy current element if it exists
|
|
1169
|
+
if (currentElement) {
|
|
1170
|
+
destroyElement(currentElement);
|
|
1171
|
+
currentElement = null;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
currentConditionIndex = newConditionIndex;
|
|
1175
|
+
|
|
1176
|
+
if (shouldUseElse) {
|
|
1177
|
+
// Render else element
|
|
1178
|
+
let _el = elseElementFn!();
|
|
391
1179
|
if (isPromise(_el)) {
|
|
392
1180
|
from(_el as Promise<Element>).subscribe(el => {
|
|
393
|
-
|
|
1181
|
+
currentElement = el;
|
|
394
1182
|
subscriber.next({
|
|
395
1183
|
type: "init",
|
|
396
1184
|
elements: [el],
|
|
397
1185
|
});
|
|
398
1186
|
});
|
|
399
1187
|
} else {
|
|
400
|
-
|
|
1188
|
+
currentElement = _el as Element;
|
|
401
1189
|
subscriber.next({
|
|
402
1190
|
type: "init",
|
|
403
|
-
elements: [
|
|
1191
|
+
elements: [currentElement],
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
} else if (matchingIndex >= 0) {
|
|
1195
|
+
// Render matching condition element
|
|
1196
|
+
let _el = allConditions[matchingIndex].elementFn();
|
|
1197
|
+
if (isPromise(_el)) {
|
|
1198
|
+
from(_el as Promise<Element>).subscribe(el => {
|
|
1199
|
+
currentElement = el;
|
|
1200
|
+
subscriber.next({
|
|
1201
|
+
type: "init",
|
|
1202
|
+
elements: [el],
|
|
1203
|
+
});
|
|
1204
|
+
});
|
|
1205
|
+
} else {
|
|
1206
|
+
currentElement = _el as Element;
|
|
1207
|
+
subscriber.next({
|
|
1208
|
+
type: "init",
|
|
1209
|
+
elements: [currentElement],
|
|
404
1210
|
});
|
|
405
1211
|
}
|
|
406
|
-
} else if (element) {
|
|
407
|
-
destroyElement(element);
|
|
408
|
-
subscriber.next({
|
|
409
|
-
elements: [],
|
|
410
|
-
});
|
|
411
1212
|
} else {
|
|
1213
|
+
// No matching condition and no else
|
|
412
1214
|
subscriber.next({
|
|
413
1215
|
elements: [],
|
|
414
1216
|
});
|
|
415
1217
|
}
|
|
416
|
-
});
|
|
417
|
-
});
|
|
418
|
-
} else {
|
|
419
|
-
// Handle boolean case
|
|
420
|
-
if (condition) {
|
|
421
|
-
let _el = createElementFn();
|
|
422
|
-
if (isPromise(_el)) {
|
|
423
|
-
return from(_el as Promise<Element>).pipe(
|
|
424
|
-
map((el) => ({
|
|
425
|
-
type: "init",
|
|
426
|
-
elements: [el],
|
|
427
|
-
}))
|
|
428
|
-
);
|
|
429
1218
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
1219
|
+
};
|
|
1220
|
+
|
|
1221
|
+
// Subscribe to all signal conditions
|
|
1222
|
+
allConditions.forEach(({ condition }) => {
|
|
1223
|
+
const signalCondition = condition as WritableObjectSignal<boolean>;
|
|
1224
|
+
subscriptions.push(
|
|
1225
|
+
signalCondition.observable.subscribe(() => {
|
|
1226
|
+
evaluateConditions();
|
|
1227
|
+
})
|
|
1228
|
+
);
|
|
437
1229
|
});
|
|
438
|
-
|
|
1230
|
+
|
|
1231
|
+
// Initial evaluation
|
|
1232
|
+
evaluateConditions();
|
|
1233
|
+
|
|
1234
|
+
// Return cleanup function
|
|
1235
|
+
return () => {
|
|
1236
|
+
subscriptions.forEach(sub => sub.unsubscribe());
|
|
1237
|
+
if (currentElement) {
|
|
1238
|
+
destroyElement(currentElement);
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
}).pipe(share());
|
|
439
1242
|
}
|