@estjs/template 0.0.15-beta.13 → 0.0.15-beta.14

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.
@@ -37,6 +37,108 @@ var __async = (__this, __arguments, generator) => {
37
37
  step((generator = generator.apply(__this, __arguments)).next());
38
38
  });
39
39
  };
40
+ var LIFECYCLE = {
41
+ mount: "mount",
42
+ destroy: "destroy",
43
+ update: "update"
44
+ };
45
+ function registerScopedHook(scope, listKey, hook) {
46
+ let hookList = scope[listKey];
47
+ if (!hookList) {
48
+ hookList = scope[listKey] = [];
49
+ }
50
+ if (!hookList.includes(hook)) {
51
+ hookList.push(hook);
52
+ }
53
+ }
54
+ function executeHooks(hooks, scopeId2, phase) {
55
+ const len = hooks.length;
56
+ if (len === 0) return;
57
+ let pending;
58
+ for (let i = 0; i < len; i++) {
59
+ try {
60
+ const result = hooks[i]();
61
+ if (isPromise(result)) {
62
+ const safePromise = result.catch((error_) => {
63
+ if (true) {
64
+ error(`Scope(${scopeId2}): Async ${phase} hook rejected:`, error_);
65
+ }
66
+ });
67
+ (pending != null ? pending : pending = []).push(safePromise);
68
+ }
69
+ } catch (error_) {
70
+ {
71
+ error(`Scope(${scopeId2}): Error in ${phase} hook:`, error_);
72
+ }
73
+ }
74
+ }
75
+ if (!pending) return;
76
+ return Promise.all(pending).then(() => void 0);
77
+ }
78
+ function onMount(hook) {
79
+ const scope = getActiveScope();
80
+ if (!scope) {
81
+ error("onMount() must be called within a scope");
82
+ return;
83
+ }
84
+ if (scope.isMounted) {
85
+ try {
86
+ const result = hook();
87
+ if (isPromise(result)) {
88
+ result.catch((error_) => {
89
+ if (true) error(`Scope(${scope.id}): Async ${LIFECYCLE.mount} hook rejected:`, error_);
90
+ });
91
+ }
92
+ } catch (error_) {
93
+ error(`Scope(${scope.id}): Error in ${LIFECYCLE.mount} hook:`, error_);
94
+ }
95
+ return;
96
+ }
97
+ registerScopedHook(scope, "onMount", hook);
98
+ }
99
+ function onUpdate(hook) {
100
+ const scope = getActiveScope();
101
+ if (!scope) {
102
+ error("onUpdate() must be called within a scope");
103
+ return;
104
+ }
105
+ registerScopedHook(scope, "onUpdate", hook);
106
+ }
107
+ function onDestroy(hook) {
108
+ const scope = getActiveScope();
109
+ if (!scope) {
110
+ error("onDestroy() must be called within a scope");
111
+ return;
112
+ }
113
+ registerScopedHook(scope, "onDestroy", hook);
114
+ }
115
+ function triggerMountHooks(scope) {
116
+ var _a2;
117
+ if (scope.isDestroyed || !((_a2 = scope.onMount) == null ? void 0 : _a2.length)) {
118
+ scope.isMounted = true;
119
+ return;
120
+ }
121
+ const mountHooks = scope.onMount;
122
+ const result = runWithScope(scope, () => executeHooks(mountHooks, scope.id, LIFECYCLE.mount));
123
+ mountHooks.length = 0;
124
+ scope.isMounted = true;
125
+ return result;
126
+ }
127
+ function triggerUpdateHooks(scope) {
128
+ var _a2;
129
+ if (scope.isDestroyed || !((_a2 = scope.onUpdate) == null ? void 0 : _a2.length)) return;
130
+ const updateHooks = scope.onUpdate;
131
+ const result = runWithScope(scope, () => executeHooks(updateHooks, scope.id, "update"));
132
+ updateHooks.length = 0;
133
+ return result;
134
+ }
135
+ function triggerDestroyHooks(scope) {
136
+ var _a2;
137
+ if (scope.isDestroyed || !((_a2 = scope.onDestroy) == null ? void 0 : _a2.length)) return;
138
+ return runWithScope(scope, () => executeHooks(scope.onDestroy, scope.id, "destroy"));
139
+ }
140
+
141
+ // src/scope.ts
40
142
  var activeScope = null;
41
143
  var scopeId = 0;
42
144
  function getActiveScope() {
@@ -50,25 +152,19 @@ function createScope(parent = activeScope) {
50
152
  id: ++scopeId,
51
153
  parent,
52
154
  children: null,
53
- // Lazy initialized
54
155
  provides: null,
55
- // Lazy initialized
56
156
  cleanup: null,
57
- // Lazy initialized
58
157
  onMount: null,
59
- // Lazy initialized
60
158
  onUpdate: null,
61
- // Lazy initialized
62
159
  onDestroy: null,
63
- // Lazy initialized
64
160
  isMounted: false,
65
161
  isDestroyed: false
66
162
  };
67
163
  if (parent) {
68
164
  if (!parent.children) {
69
- parent.children = /* @__PURE__ */ new Set();
165
+ parent.children = [];
70
166
  }
71
- parent.children.add(scope);
167
+ parent.children.push(scope);
72
168
  }
73
169
  return scope;
74
170
  }
@@ -82,31 +178,22 @@ function runWithScope(scope, fn) {
82
178
  }
83
179
  }
84
180
  function disposeScope(scope) {
85
- var _a2, _b, _c, _d, _e;
181
+ var _a2, _b, _c;
86
182
  if (!scope || scope.isDestroyed) {
87
183
  return;
88
184
  }
185
+ const parentScope = scope.parent;
89
186
  if (scope.children) {
90
- while (scope.children.size > 0) {
91
- const child = scope.children.values().next().value;
92
- if (child) {
93
- disposeScope(child);
94
- }
187
+ for (const child of scope.children) {
188
+ disposeScope(child);
95
189
  }
190
+ scope.children.length = 0;
96
191
  }
97
- if (scope.onDestroy) {
98
- for (const hook of scope.onDestroy) {
99
- try {
100
- hook();
101
- } catch (error_) {
102
- {
103
- error(`Scope(${scope.id}): Error in destroy hook:`, error_);
104
- }
105
- }
106
- }
107
- scope.onDestroy.clear();
192
+ if ((_a2 = scope.onDestroy) == null ? void 0 : _a2.length) {
193
+ triggerDestroyHooks(scope);
194
+ scope.onDestroy.length = 0;
108
195
  }
109
- if (scope.cleanup) {
196
+ if ((_b = scope.cleanup) == null ? void 0 : _b.length) {
110
197
  for (const fn of scope.cleanup) {
111
198
  try {
112
199
  fn();
@@ -116,18 +203,22 @@ function disposeScope(scope) {
116
203
  }
117
204
  }
118
205
  }
119
- scope.cleanup.clear();
206
+ scope.cleanup.length = 0;
120
207
  }
121
- if ((_a2 = scope.parent) == null ? void 0 : _a2.children) {
122
- scope.parent.children.delete(scope);
208
+ if (parentScope == null ? void 0 : parentScope.children) {
209
+ const idx = parentScope.children.indexOf(scope);
210
+ if (idx !== -1) {
211
+ parentScope.children.splice(idx, 1);
212
+ }
123
213
  }
124
- (_b = scope.children) == null ? void 0 : _b.clear();
125
214
  (_c = scope.provides) == null ? void 0 : _c.clear();
126
- (_d = scope.onMount) == null ? void 0 : _d.clear();
127
- (_e = scope.onUpdate) == null ? void 0 : _e.clear();
128
- setActiveScope(scope.parent);
215
+ if (scope.onMount) scope.onMount.length = 0;
216
+ if (scope.onUpdate) scope.onUpdate.length = 0;
129
217
  scope.parent = null;
130
218
  scope.isDestroyed = true;
219
+ if (activeScope === scope) {
220
+ activeScope = parentScope;
221
+ }
131
222
  }
132
223
  function onCleanup(fn) {
133
224
  const scope = activeScope;
@@ -138,9 +229,9 @@ function onCleanup(fn) {
138
229
  return;
139
230
  }
140
231
  if (!scope.cleanup) {
141
- scope.cleanup = /* @__PURE__ */ new Set();
232
+ scope.cleanup = [];
142
233
  }
143
- scope.cleanup.add(fn);
234
+ scope.cleanup.push(fn);
144
235
  }
145
236
 
146
237
  // src/constants.ts
@@ -747,99 +838,6 @@ function mapNodes(template2, indexes) {
747
838
  walk(template2);
748
839
  return tree;
749
840
  }
750
- function registerMountHook(hook) {
751
- const scope = getActiveScope();
752
- if (!scope) {
753
- {
754
- error("onMount() must be called within a scope");
755
- }
756
- return;
757
- }
758
- if (scope.isMounted) {
759
- try {
760
- hook();
761
- } catch (error_) {
762
- {
763
- error(`Scope(${scope.id}): Error in mount hook:`, error_);
764
- }
765
- }
766
- return;
767
- }
768
- if (!scope.onMount) {
769
- scope.onMount = /* @__PURE__ */ new Set();
770
- }
771
- scope.onMount.add(hook);
772
- }
773
- function registerUpdateHook(hook) {
774
- const scope = getActiveScope();
775
- if (!scope) {
776
- {
777
- error("onUpdate() must be called within a scope");
778
- }
779
- return;
780
- }
781
- if (!scope.onUpdate) {
782
- scope.onUpdate = /* @__PURE__ */ new Set();
783
- }
784
- scope.onUpdate.add(hook);
785
- }
786
- function registerDestroyHook(hook) {
787
- const scope = getActiveScope();
788
- if (!scope) {
789
- {
790
- error("onDestroy() must be called within a scope");
791
- }
792
- return;
793
- }
794
- if (!scope.onDestroy) {
795
- scope.onDestroy = /* @__PURE__ */ new Set();
796
- }
797
- scope.onDestroy.add(hook);
798
- }
799
- function triggerMountHooks(scope) {
800
- if (!scope || scope.isDestroyed || scope.isMounted) {
801
- return;
802
- }
803
- scope.isMounted = true;
804
- if (scope.onMount) {
805
- runWithScope(scope, () => {
806
- for (const hook of scope.onMount) {
807
- try {
808
- hook();
809
- } catch (error_) {
810
- if (true) {
811
- error(`Scope(${scope.id}): Error in mount hook:`, error_);
812
- }
813
- }
814
- }
815
- });
816
- }
817
- }
818
- function triggerUpdateHooks(scope) {
819
- if (!scope || scope.isDestroyed) {
820
- return;
821
- }
822
- if (scope.onUpdate) {
823
- for (const hook of scope.onUpdate) {
824
- try {
825
- hook();
826
- } catch (error_) {
827
- {
828
- error(`Scope(${scope.id}): Error in update hook:`, error_);
829
- }
830
- }
831
- }
832
- }
833
- }
834
- function onMount(hook) {
835
- registerMountHook(hook);
836
- }
837
- function onDestroy(hook) {
838
- registerDestroyHook(hook);
839
- }
840
- function onUpdate(hook) {
841
- registerUpdateHook(hook);
842
- }
843
841
 
844
842
  // src/component.ts
845
843
  var _a;
@@ -1584,7 +1582,7 @@ function Portal(props) {
1584
1582
  onMount(() => {
1585
1583
  const targetElement = isString(props.target) ? document.querySelector(props.target) : props.target;
1586
1584
  if (!targetElement) {
1587
- {
1585
+ if (true) {
1588
1586
  warn(`[Portal] Target element not found: ${props.target}`);
1589
1587
  }
1590
1588
  return;