@bromscandium/runtime 1.0.0 → 1.0.2

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/src/lifecycle.ts CHANGED
@@ -1,133 +1,133 @@
1
- /**
2
- * Component lifecycle hook system.
3
- * @module
4
- */
5
-
6
- import { ComponentInstance } from './vnode.js';
7
-
8
- let currentInstance: ComponentInstance | null = null;
9
-
10
- /**
11
- * Sets the currently rendering component instance.
12
- * Used internally by the renderer.
13
- *
14
- * @param instance - The component instance or null
15
- */
16
- export function setCurrentInstance(instance: ComponentInstance | null): void {
17
- currentInstance = instance;
18
- }
19
-
20
- /**
21
- * Returns the currently rendering component instance.
22
- * Used internally by hooks to access component state.
23
- *
24
- * @returns The current component instance or null if outside a component
25
- */
26
- export function getCurrentInstance(): ComponentInstance | null {
27
- return currentInstance;
28
- }
29
-
30
- function registerLifecycleHook(
31
- type: 'mounted' | 'unmounted' | 'updated',
32
- fn: () => void
33
- ): void {
34
- if (!currentInstance) {
35
- if (import.meta.env?.DEV) {
36
- console.warn(
37
- `${type} hook called outside of component setup. ` +
38
- `Lifecycle hooks can only be used inside a component function.`
39
- );
40
- }
41
- return;
42
- }
43
-
44
- currentInstance[type].push(fn);
45
- }
46
-
47
- /**
48
- * Registers a callback to be invoked after the component is mounted to the DOM.
49
- * The callback runs after the initial render and DOM insertion.
50
- *
51
- * @param fn - The callback function to invoke on mount
52
- *
53
- * @example
54
- * ```ts
55
- * function MyComponent() {
56
- * onMounted(() => {
57
- * console.log('Component mounted!');
58
- * // DOM is available here
59
- * });
60
- *
61
- * return <div>Hello</div>;
62
- * }
63
- * ```
64
- */
65
- export function onMounted(fn: () => void): void {
66
- registerLifecycleHook('mounted', fn);
67
- }
68
-
69
- /**
70
- * Registers a callback to be invoked before the component is unmounted.
71
- * Use this to clean up side effects like timers, subscriptions, or event listeners.
72
- *
73
- * @param fn - The callback function to invoke on unmount
74
- *
75
- * @example
76
- * ```ts
77
- * function MyComponent() {
78
- * const timer = setInterval(() => {}, 1000);
79
- *
80
- * onUnmounted(() => {
81
- * clearInterval(timer);
82
- * });
83
- *
84
- * return <div>Hello</div>;
85
- * }
86
- * ```
87
- */
88
- export function onUnmounted(fn: () => void): void {
89
- registerLifecycleHook('unmounted', fn);
90
- }
91
-
92
- /**
93
- * Registers a callback to be invoked after the component updates.
94
- * The callback runs after each re-render caused by reactive state changes.
95
- *
96
- * @param fn - The callback function to invoke after updates
97
- *
98
- * @example
99
- * ```ts
100
- * function MyComponent() {
101
- * const count = ref(0);
102
- *
103
- * onUpdated(() => {
104
- * console.log('Component updated, count is now:', count.value);
105
- * });
106
- *
107
- * return <button onClick={() => count.value++}>{count.value}</button>;
108
- * }
109
- * ```
110
- */
111
- export function onUpdated(fn: () => void): void {
112
- registerLifecycleHook('updated', fn);
113
- }
114
-
115
- /**
116
- * Invokes an array of lifecycle hook callbacks with error handling.
117
- * Used internally by the renderer.
118
- *
119
- * @param hooks - Array of callback functions to invoke
120
- * @param errorContext - Description of the hook type for error messages
121
- */
122
- export function invokeLifecycleHooks(
123
- hooks: Array<() => void>,
124
- errorContext?: string
125
- ): void {
126
- hooks.forEach(hook => {
127
- try {
128
- hook();
129
- } catch (error) {
130
- console.error(`Error in ${errorContext || 'lifecycle hook'}:`, error);
131
- }
132
- });
133
- }
1
+ /**
2
+ * Component lifecycle hook system.
3
+ * @module
4
+ */
5
+
6
+ import { ComponentInstance } from './vnode.js';
7
+
8
+ let currentInstance: ComponentInstance | null = null;
9
+
10
+ /**
11
+ * Sets the currently rendering component instance.
12
+ * Used internally by the renderer.
13
+ *
14
+ * @param instance - The component instance or null
15
+ */
16
+ export function setCurrentInstance(instance: ComponentInstance | null): void {
17
+ currentInstance = instance;
18
+ }
19
+
20
+ /**
21
+ * Returns the currently rendering component instance.
22
+ * Used internally by hooks to access component state.
23
+ *
24
+ * @returns The current component instance or null if outside a component
25
+ */
26
+ export function getCurrentInstance(): ComponentInstance | null {
27
+ return currentInstance;
28
+ }
29
+
30
+ function registerLifecycleHook(
31
+ type: 'mounted' | 'unmounted' | 'updated',
32
+ fn: () => void
33
+ ): void {
34
+ if (!currentInstance) {
35
+ if (import.meta.env?.DEV) {
36
+ console.warn(
37
+ `${type} hook called outside of component setup. ` +
38
+ `Lifecycle hooks can only be used inside a component function.`
39
+ );
40
+ }
41
+ return;
42
+ }
43
+
44
+ currentInstance[type].push(fn);
45
+ }
46
+
47
+ /**
48
+ * Registers a callback to be invoked after the component is mounted to the DOM.
49
+ * The callback runs after the initial render and DOM insertion.
50
+ *
51
+ * @param fn - The callback function to invoke on mount
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * function MyComponent() {
56
+ * onMounted(() => {
57
+ * console.log('Component mounted!');
58
+ * // DOM is available here
59
+ * });
60
+ *
61
+ * return <div>Hello</div>;
62
+ * }
63
+ * ```
64
+ */
65
+ export function onMounted(fn: () => void): void {
66
+ registerLifecycleHook('mounted', fn);
67
+ }
68
+
69
+ /**
70
+ * Registers a callback to be invoked before the component is unmounted.
71
+ * Use this to clean up side effects like timers, subscriptions, or event listeners.
72
+ *
73
+ * @param fn - The callback function to invoke on unmount
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * function MyComponent() {
78
+ * const timer = setInterval(() => {}, 1000);
79
+ *
80
+ * onUnmounted(() => {
81
+ * clearInterval(timer);
82
+ * });
83
+ *
84
+ * return <div>Hello</div>;
85
+ * }
86
+ * ```
87
+ */
88
+ export function onUnmounted(fn: () => void): void {
89
+ registerLifecycleHook('unmounted', fn);
90
+ }
91
+
92
+ /**
93
+ * Registers a callback to be invoked after the component updates.
94
+ * The callback runs after each re-render caused by reactive state changes.
95
+ *
96
+ * @param fn - The callback function to invoke after updates
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * function MyComponent() {
101
+ * const count = ref(0);
102
+ *
103
+ * onUpdated(() => {
104
+ * console.log('Component updated, count is now:', count.value);
105
+ * });
106
+ *
107
+ * return <button onClick={() => count.value++}>{count.value}</button>;
108
+ * }
109
+ * ```
110
+ */
111
+ export function onUpdated(fn: () => void): void {
112
+ registerLifecycleHook('updated', fn);
113
+ }
114
+
115
+ /**
116
+ * Invokes an array of lifecycle hook callbacks with error handling.
117
+ * Used internally by the renderer.
118
+ *
119
+ * @param hooks - Array of callback functions to invoke
120
+ * @param errorContext - Description of the hook type for error messages
121
+ */
122
+ export function invokeLifecycleHooks(
123
+ hooks: Array<() => void>,
124
+ errorContext?: string
125
+ ): void {
126
+ hooks.forEach(hook => {
127
+ try {
128
+ hook();
129
+ } catch (error) {
130
+ console.error(`Error in ${errorContext || 'lifecycle hook'}:`, error);
131
+ }
132
+ });
133
+ }