@blac/core 2.0.12 → 2.0.13

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.
Files changed (180) hide show
  1. package/dist/Cubit-BBKF3PIg.js +248 -0
  2. package/dist/Cubit-BBKF3PIg.js.map +1 -0
  3. package/dist/Cubit-BOxMvpfc.js +250 -0
  4. package/dist/Cubit-BOxMvpfc.js.map +1 -0
  5. package/dist/Cubit-BogJXkLm.js +243 -0
  6. package/dist/Cubit-BogJXkLm.js.map +1 -0
  7. package/dist/Cubit-COfHT1Bm.cjs +280 -0
  8. package/dist/Cubit-COfHT1Bm.cjs.map +1 -0
  9. package/dist/Cubit-CXxh9iol.js +260 -0
  10. package/dist/Cubit-CXxh9iol.js.map +1 -0
  11. package/dist/Cubit-CYOWfzQt.cjs +274 -0
  12. package/dist/Cubit-CYOWfzQt.cjs.map +1 -0
  13. package/dist/Cubit-CcKgMEBO.js +257 -0
  14. package/dist/Cubit-CcKgMEBO.js.map +1 -0
  15. package/dist/Cubit-CfT6Y1jl.js +298 -0
  16. package/dist/Cubit-CfT6Y1jl.js.map +1 -0
  17. package/dist/Cubit-DKEY9WYq.cjs +260 -0
  18. package/dist/Cubit-DKEY9WYq.cjs.map +1 -0
  19. package/dist/Cubit-DrqG3SP3.cjs +321 -0
  20. package/dist/Cubit-DrqG3SP3.cjs.map +1 -0
  21. package/dist/Cubit-DsDkCo42.cjs +271 -0
  22. package/dist/Cubit-DsDkCo42.cjs.map +1 -0
  23. package/dist/Cubit-DwRw1E9D.cjs +267 -0
  24. package/dist/Cubit-DwRw1E9D.cjs.map +1 -0
  25. package/dist/Cubit-NRhvdDQ6.js +257 -0
  26. package/dist/Cubit-NRhvdDQ6.js.map +1 -0
  27. package/dist/Cubit-jz922NKf.cjs +277 -0
  28. package/dist/Cubit-jz922NKf.cjs.map +1 -0
  29. package/dist/StateContainerRegistry-BAcxouCu.js +655 -0
  30. package/dist/StateContainerRegistry-BAcxouCu.js.map +1 -0
  31. package/dist/StateContainerRegistry-BK57H92E.js +676 -0
  32. package/dist/StateContainerRegistry-BK57H92E.js.map +1 -0
  33. package/dist/StateContainerRegistry-BglEhqdA.js +646 -0
  34. package/dist/StateContainerRegistry-BglEhqdA.js.map +1 -0
  35. package/dist/StateContainerRegistry-BpO5t-0i.js +620 -0
  36. package/dist/StateContainerRegistry-BpO5t-0i.js.map +1 -0
  37. package/dist/StateContainerRegistry-CDH9mnQS.cjs +661 -0
  38. package/dist/StateContainerRegistry-CDH9mnQS.cjs.map +1 -0
  39. package/dist/StateContainerRegistry-CTOXUf75.cjs +687 -0
  40. package/dist/StateContainerRegistry-CTOXUf75.cjs.map +1 -0
  41. package/dist/StateContainerRegistry-CX2CBOmv.cjs +735 -0
  42. package/dist/StateContainerRegistry-CX2CBOmv.cjs.map +1 -0
  43. package/dist/StateContainerRegistry-Ci0ajc5g.js +652 -0
  44. package/dist/StateContainerRegistry-Ci0ajc5g.js.map +1 -0
  45. package/dist/StateContainerRegistry-D1lVJFGL.cjs +693 -0
  46. package/dist/StateContainerRegistry-D1lVJFGL.cjs.map +1 -0
  47. package/dist/StateContainerRegistry-Upsrlg9G.cjs +696 -0
  48. package/dist/StateContainerRegistry-Upsrlg9G.cjs.map +1 -0
  49. package/dist/config-B3NIwDQv.cjs +32 -0
  50. package/dist/config-B3NIwDQv.cjs.map +1 -0
  51. package/dist/config-BKGHop0V.js +16 -0
  52. package/dist/config-BKGHop0V.js.map +1 -0
  53. package/dist/config-BR0Xy-Tn.cjs +32 -0
  54. package/dist/config-BR0Xy-Tn.cjs.map +1 -0
  55. package/dist/config-CQH9fTY-.cjs +32 -0
  56. package/dist/config-CQH9fTY-.cjs.map +1 -0
  57. package/dist/config-Cwj8-C1J.js +16 -0
  58. package/dist/config-Cwj8-C1J.js.map +1 -0
  59. package/dist/config-DEcslxan.cjs +32 -0
  60. package/dist/config-DEcslxan.cjs.map +1 -0
  61. package/dist/config-De9oQ6Ki.js +16 -0
  62. package/dist/config-De9oQ6Ki.js.map +1 -0
  63. package/dist/config-H6FAqbW4.cjs +32 -0
  64. package/dist/config-H6FAqbW4.cjs.map +1 -0
  65. package/dist/config-Yp645dGK.js +16 -0
  66. package/dist/config-Yp645dGK.js.map +1 -0
  67. package/dist/config-ispAzOZV.js +16 -0
  68. package/dist/config-ispAzOZV.js.map +1 -0
  69. package/dist/constants.d.cts +5 -28
  70. package/dist/constants.d.ts +5 -28
  71. package/dist/core/Cubit.d.ts +0 -1
  72. package/dist/core/StateContainer.d.ts +4 -7
  73. package/dist/core/StateContainerRegistry.d.ts +18 -3
  74. package/dist/core/symbols.d.ts +0 -1
  75. package/dist/debug.cjs +3 -3
  76. package/dist/debug.js +3 -3
  77. package/dist/ensure-C3eIRs6e.cjs +14 -0
  78. package/dist/ensure-C3eIRs6e.cjs.map +1 -0
  79. package/dist/ensure-CN6unBOe.cjs +14 -0
  80. package/dist/ensure-CN6unBOe.cjs.map +1 -0
  81. package/dist/ensure-CWKzeno7.js +9 -0
  82. package/dist/ensure-CWKzeno7.js.map +1 -0
  83. package/dist/ensure-DGEtPU9A.cjs +14 -0
  84. package/dist/ensure-DGEtPU9A.cjs.map +1 -0
  85. package/dist/ensure-DME-_T0j.cjs +14 -0
  86. package/dist/ensure-DME-_T0j.cjs.map +1 -0
  87. package/dist/ensure-DWd0GCYB.js +9 -0
  88. package/dist/ensure-DWd0GCYB.js.map +1 -0
  89. package/dist/ensure-Dr7I78Ku.cjs +14 -0
  90. package/dist/ensure-Dr7I78Ku.cjs.map +1 -0
  91. package/dist/ensure-SLVdGd5b.js +9 -0
  92. package/dist/ensure-SLVdGd5b.js.map +1 -0
  93. package/dist/ensure-l2-H7S5W.js +9 -0
  94. package/dist/ensure-l2-H7S5W.js.map +1 -0
  95. package/dist/ensure-nCxg7MCN.js +9 -0
  96. package/dist/ensure-nCxg7MCN.js.map +1 -0
  97. package/dist/index.cjs +9 -7
  98. package/dist/index.d.cts +2 -1
  99. package/dist/index.d.ts +2 -1
  100. package/dist/index.js +7 -7
  101. package/dist/management-B46QwA9Y.js +32 -0
  102. package/dist/management-B46QwA9Y.js.map +1 -0
  103. package/dist/management-BCKTEZ8Q.js +32 -0
  104. package/dist/management-BCKTEZ8Q.js.map +1 -0
  105. package/dist/management-BnedF5ZD.cjs +79 -0
  106. package/dist/management-BnedF5ZD.cjs.map +1 -0
  107. package/dist/management-CK-GL3wY.js +32 -0
  108. package/dist/management-CK-GL3wY.js.map +1 -0
  109. package/dist/management-DL0Uzsoz.js +32 -0
  110. package/dist/management-DL0Uzsoz.js.map +1 -0
  111. package/dist/management-DMZ8qpwr.cjs +79 -0
  112. package/dist/management-DMZ8qpwr.cjs.map +1 -0
  113. package/dist/management-DYLNyj9p.cjs +79 -0
  114. package/dist/management-DYLNyj9p.cjs.map +1 -0
  115. package/dist/management-DjBU1gtb.cjs +79 -0
  116. package/dist/management-DjBU1gtb.cjs.map +1 -0
  117. package/dist/management-DwCozsY8.js +32 -0
  118. package/dist/management-DwCozsY8.js.map +1 -0
  119. package/dist/management-ry_KteFK.cjs +79 -0
  120. package/dist/management-ry_KteFK.cjs.map +1 -0
  121. package/dist/plugin/BlacPlugin.d.ts +1 -105
  122. package/dist/plugin/PluginManager.d.ts +2 -0
  123. package/dist/plugins.cjs +1 -1
  124. package/dist/plugins.js +1 -1
  125. package/dist/resolve-dependencies-B2QggQAi.js +646 -0
  126. package/dist/resolve-dependencies-B2QggQAi.js.map +1 -0
  127. package/dist/resolve-dependencies-B6JDXvpr.js +654 -0
  128. package/dist/resolve-dependencies-B6JDXvpr.js.map +1 -0
  129. package/dist/resolve-dependencies-BNeayhbQ.cjs +779 -0
  130. package/dist/resolve-dependencies-BNeayhbQ.cjs.map +1 -0
  131. package/dist/resolve-dependencies-BWEMpuDy.js +646 -0
  132. package/dist/resolve-dependencies-BWEMpuDy.js.map +1 -0
  133. package/dist/resolve-dependencies-By26rT_i.js +654 -0
  134. package/dist/resolve-dependencies-By26rT_i.js.map +1 -0
  135. package/dist/resolve-dependencies-C9Kf7AzY.cjs +774 -0
  136. package/dist/resolve-dependencies-C9Kf7AzY.cjs.map +1 -0
  137. package/dist/resolve-dependencies-ClSsP0DJ.cjs +771 -0
  138. package/dist/resolve-dependencies-ClSsP0DJ.cjs.map +1 -0
  139. package/dist/resolve-dependencies-DMlqYJR2.js +649 -0
  140. package/dist/resolve-dependencies-DMlqYJR2.js.map +1 -0
  141. package/dist/resolve-dependencies-Dd8T6TLt.cjs +774 -0
  142. package/dist/resolve-dependencies-Dd8T6TLt.cjs.map +1 -0
  143. package/dist/resolve-dependencies-Dfdp_DZd.cjs +779 -0
  144. package/dist/resolve-dependencies-Dfdp_DZd.cjs.map +1 -0
  145. package/dist/resolve-dependencies-KSpTt4jy.cjs +771 -0
  146. package/dist/resolve-dependencies-KSpTt4jy.cjs.map +1 -0
  147. package/dist/resolve-dependencies-Nvjp4CwS.js +649 -0
  148. package/dist/resolve-dependencies-Nvjp4CwS.js.map +1 -0
  149. package/dist/testing.cjs +4 -4
  150. package/dist/testing.js +4 -4
  151. package/dist/tracked-BTbFkFeM.js +192 -0
  152. package/dist/tracked-BTbFkFeM.js.map +1 -0
  153. package/dist/tracked-BhQtzUhZ.js +192 -0
  154. package/dist/tracked-BhQtzUhZ.js.map +1 -0
  155. package/dist/tracked-C52awNV9.js +177 -0
  156. package/dist/tracked-C52awNV9.js.map +1 -0
  157. package/dist/tracked-CGzduF2m.js +192 -0
  158. package/dist/tracked-CGzduF2m.js.map +1 -0
  159. package/dist/tracked-CK_-ixtX.cjs +206 -0
  160. package/dist/tracked-CK_-ixtX.cjs.map +1 -0
  161. package/dist/tracked-CLlvURln.js +192 -0
  162. package/dist/tracked-CLlvURln.js.map +1 -0
  163. package/dist/tracked-CgHWmuGC.cjs +221 -0
  164. package/dist/tracked-CgHWmuGC.cjs.map +1 -0
  165. package/dist/tracked-D3Xjm1H1.cjs +221 -0
  166. package/dist/tracked-D3Xjm1H1.cjs.map +1 -0
  167. package/dist/tracked-DN3h3Rp1.js +177 -0
  168. package/dist/tracked-DN3h3Rp1.js.map +1 -0
  169. package/dist/tracked-DUkp4zve.cjs +206 -0
  170. package/dist/tracked-DUkp4zve.cjs.map +1 -0
  171. package/dist/tracked-O2lv-kJV.cjs +221 -0
  172. package/dist/tracked-O2lv-kJV.cjs.map +1 -0
  173. package/dist/tracked-njU-_pVa.cjs +221 -0
  174. package/dist/tracked-njU-_pVa.cjs.map +1 -0
  175. package/dist/tracking.cjs +1 -1
  176. package/dist/tracking.js +1 -1
  177. package/dist/utils/static-props.d.ts +9 -0
  178. package/dist/watch-entry.cjs +1 -1
  179. package/dist/watch-entry.js +1 -1
  180. package/package.json +1 -1
@@ -0,0 +1,696 @@
1
+ //#region src/constants.ts
2
+ /**
3
+ * Default configuration constants for BlaC
4
+ *
5
+ * Centralized location for all magic numbers and default values.
6
+ */
7
+ /**
8
+ * Default configuration constants for BlaC
9
+ */
10
+ const BLAC_DEFAULTS = {
11
+ DEFAULT_INSTANCE_KEY: "default",
12
+ MAX_GETTER_DEPTH: 10
13
+ };
14
+ /**
15
+ * Static property names for StateContainer classes
16
+ * Used for feature flags and configuration on bloc classes
17
+ */
18
+ const BLAC_STATIC_PROPS = {
19
+ KEEP_ALIVE: "keepAlive",
20
+ EXCLUDE_FROM_DEVTOOLS: "__excludeFromDevTools"
21
+ };
22
+ /**
23
+ * Standard error message prefix
24
+ */
25
+ const BLAC_ERROR_PREFIX = "[BlaC]";
26
+ //#endregion
27
+ //#region src/plugin/PluginManager.ts
28
+ /**
29
+ * Manages plugin lifecycle for the BlaC state management system.
30
+ * Plugins receive notifications about state container lifecycle events.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const manager = createPluginManager(registry);
35
+ * manager.install(myPlugin, { environment: 'development' });
36
+ * ```
37
+ */
38
+ var PluginManager = class {
39
+ /**
40
+ * Create a new PluginManager
41
+ * @param registry - The StateContainerRegistry to monitor for lifecycle events
42
+ */
43
+ constructor(registry) {
44
+ this.plugins = /* @__PURE__ */ new Map();
45
+ this.lifecycleUnsubscribers = [];
46
+ this.registry = registry;
47
+ this.setupLifecycleHooks();
48
+ }
49
+ /**
50
+ * Install a plugin with optional configuration
51
+ * @param plugin - The plugin to install
52
+ * @param config - Optional plugin configuration
53
+ * @throws Error if plugin is already installed
54
+ */
55
+ install(plugin, config = {}) {
56
+ const effectiveConfig = {
57
+ enabled: true,
58
+ environment: "all",
59
+ ...config
60
+ };
61
+ if (!this.shouldEnablePlugin(effectiveConfig)) {
62
+ console.log(`[BlaC] Plugin "${plugin.name}" skipped (environment mismatch)`);
63
+ return;
64
+ }
65
+ if (this.plugins.has(plugin.name)) throw new Error(`Plugin "${plugin.name}" is already installed`);
66
+ const context = this.createPluginContext();
67
+ this.plugins.set(plugin.name, {
68
+ plugin,
69
+ config: effectiveConfig,
70
+ context
71
+ });
72
+ if (plugin.onInstall) try {
73
+ plugin.onInstall(context);
74
+ } catch (error) {
75
+ console.error(`[BlaC] Error installing plugin "${plugin.name}":`, error);
76
+ this.plugins.delete(plugin.name);
77
+ throw error;
78
+ }
79
+ console.log(`[BlaC] Plugin "${plugin.name}" v${plugin.version} installed`);
80
+ }
81
+ /**
82
+ * Uninstall a plugin by name
83
+ * @param pluginName - The name of the plugin to uninstall
84
+ * @throws Error if plugin is not installed
85
+ */
86
+ uninstall(pluginName) {
87
+ const installed = this.plugins.get(pluginName);
88
+ if (!installed) throw new Error(`Plugin "${pluginName}" is not installed`);
89
+ if (installed.plugin.onUninstall) try {
90
+ installed.plugin.onUninstall();
91
+ } catch (error) {
92
+ console.error(`[BlaC] Error uninstalling plugin "${pluginName}":`, error);
93
+ }
94
+ this.plugins.delete(pluginName);
95
+ console.log(`[BlaC] Plugin "${pluginName}" uninstalled`);
96
+ }
97
+ /**
98
+ * Get an installed plugin by name
99
+ * @param pluginName - The name of the plugin to retrieve
100
+ * @returns The plugin instance or undefined if not found
101
+ */
102
+ getPlugin(pluginName) {
103
+ return this.plugins.get(pluginName)?.plugin;
104
+ }
105
+ /**
106
+ * Get all installed plugins
107
+ * @returns Array of all installed plugins
108
+ */
109
+ getAllPlugins() {
110
+ return Array.from(this.plugins.values()).map((p) => p.plugin);
111
+ }
112
+ /**
113
+ * Check if a plugin is installed
114
+ * @param pluginName - The name of the plugin to check
115
+ * @returns true if the plugin is installed
116
+ */
117
+ hasPlugin(pluginName) {
118
+ return this.plugins.has(pluginName);
119
+ }
120
+ /**
121
+ * Uninstall all plugins
122
+ */
123
+ clear() {
124
+ for (const name of this.plugins.keys()) this.uninstall(name);
125
+ }
126
+ destroy() {
127
+ this.clear();
128
+ for (const unsub of this.lifecycleUnsubscribers) unsub();
129
+ this.lifecycleUnsubscribers = [];
130
+ }
131
+ /**
132
+ * Setup lifecycle hooks to notify plugins
133
+ */
134
+ setupLifecycleHooks() {
135
+ this.lifecycleUnsubscribers = [
136
+ this.registry.on("created", (instance) => {
137
+ this.notifyPlugins("onInstanceCreated", instance);
138
+ }),
139
+ this.registry.on("stateChanged", (instance, previousState, currentState) => {
140
+ this.notifyPlugins("onStateChanged", instance, previousState, currentState);
141
+ }),
142
+ this.registry.on("disposed", (instance) => {
143
+ this.notifyPlugins("onInstanceDisposed", instance);
144
+ }),
145
+ this.registry.on("refAcquired", (instance, refId) => {
146
+ this.notifyPlugins("onRefAcquired", instance, refId);
147
+ }),
148
+ this.registry.on("refReleased", (instance, refId) => {
149
+ this.notifyPlugins("onRefReleased", instance, refId);
150
+ })
151
+ ];
152
+ }
153
+ /**
154
+ * Notify all plugins of a lifecycle event
155
+ */
156
+ notifyPlugins(hookName, ...args) {
157
+ for (const { plugin, config, context } of this.plugins.values()) {
158
+ if (!config.enabled) continue;
159
+ const hook = plugin[hookName];
160
+ if (typeof hook === "function") try {
161
+ hook.apply(plugin, [...args, context]);
162
+ } catch (error) {
163
+ console.error(`[BlaC] Error in plugin "${plugin.name}" ${hookName}:`, error);
164
+ }
165
+ }
166
+ }
167
+ /**
168
+ * Create plugin context with safe API access
169
+ */
170
+ createPluginContext() {
171
+ return {
172
+ getInstanceMetadata: (instance) => {
173
+ return {
174
+ id: instance.instanceId,
175
+ className: instance.constructor.name,
176
+ isDisposed: instance.isDisposed,
177
+ name: instance.name,
178
+ createdAt: instance.createdAt,
179
+ state: instance.state,
180
+ hydrationStatus: instance.hydrationStatus,
181
+ isHydrated: instance.isHydrated,
182
+ hydrationError: instance.hydrationError,
183
+ changedWhileHydrating: instance.changedWhileHydrating
184
+ };
185
+ },
186
+ getState: (instance) => {
187
+ return instance.state;
188
+ },
189
+ getHydrationStatus: (instance) => {
190
+ return instance.hydrationStatus;
191
+ },
192
+ startHydration: (instance) => {
193
+ instance.beginHydration();
194
+ },
195
+ applyHydratedState: (instance, state) => {
196
+ return instance.applyHydratedState(state);
197
+ },
198
+ finishHydration: (instance) => {
199
+ instance.finishHydration();
200
+ },
201
+ failHydration: (instance, error) => {
202
+ instance.failHydration(error);
203
+ },
204
+ waitForHydration: (instance) => {
205
+ return instance.waitForHydration();
206
+ },
207
+ queryInstances: (typeClass) => {
208
+ return this.registry.getAll(typeClass);
209
+ },
210
+ getAllTypes: () => {
211
+ return this.registry.getTypes();
212
+ },
213
+ getStats: () => {
214
+ return this.registry.getStats();
215
+ },
216
+ getRefIds: (instanceId) => {
217
+ for (const Type of this.registry.getTypes()) {
218
+ const map = this.registry.getInstancesMap(Type);
219
+ for (const [, entry] of map) if (entry.instance.instanceId === instanceId) return Array.from(entry.refs.keys());
220
+ }
221
+ return [];
222
+ }
223
+ };
224
+ }
225
+ /**
226
+ * Check if plugin should be enabled based on environment
227
+ */
228
+ shouldEnablePlugin(config) {
229
+ if (!config.enabled) return false;
230
+ if (config.environment === "all") return true;
231
+ return this.getCurrentEnvironment() === config.environment;
232
+ }
233
+ /**
234
+ * Get current environment
235
+ */
236
+ getCurrentEnvironment() {
237
+ if (typeof process !== "undefined") {
238
+ if (process.env.NODE_ENV === "test") return "test";
239
+ if (process.env.NODE_ENV === "production") return "production";
240
+ return "development";
241
+ }
242
+ return "development";
243
+ }
244
+ };
245
+ /**
246
+ * Create a plugin manager instance
247
+ * @param registry - The StateContainerRegistry to monitor for lifecycle events
248
+ * @returns A new PluginManager instance
249
+ */
250
+ function createPluginManager(registry) {
251
+ return new PluginManager(registry);
252
+ }
253
+ //#endregion
254
+ //#region src/utils/static-props.ts
255
+ /**
256
+ * Utility functions for accessing static properties on StateContainer classes
257
+ */
258
+ /**
259
+ * Get a static property from a class constructor
260
+ * Type-safe helper that avoids (Type as any) casts
261
+ *
262
+ * @param Type - The class constructor
263
+ * @param propName - The property name to access
264
+ * @param defaultValue - Optional default value if property is undefined
265
+ * @returns The property value or default
266
+ */
267
+ function getStaticProp(Type, propName, defaultValue) {
268
+ return Type[propName] ?? defaultValue;
269
+ }
270
+ /**
271
+ * Check if a class is marked as keepAlive.
272
+ * KeepAlive classes are never auto-disposed when ref count reaches 0.
273
+ * @param Type - The class constructor to check
274
+ * @returns true if the class has `static keepAlive = true`
275
+ */
276
+ function isKeepAliveClass(Type) {
277
+ return getStaticProp(Type, BLAC_STATIC_PROPS.KEEP_ALIVE) === true;
278
+ }
279
+ //#endregion
280
+ //#region src/core/StateContainerRegistry.ts
281
+ /**
282
+ * Central registry for managing StateContainer instances.
283
+ * Handles instance lifecycle, named ref tracking, and lifecycle event emission.
284
+ *
285
+ * @example
286
+ * ```ts
287
+ * const registry = new StateContainerRegistry();
288
+ * const instance = registry.acquire(MyBloc); // ownership, must release
289
+ * const other = registry.ensure(OtherBloc); // no ownership, bloc-to-bloc
290
+ * registry.on('stateChanged', (container, prev, next) => {
291
+ * console.log('State changed:', prev, '->', next);
292
+ * });
293
+ * ```
294
+ */
295
+ var StateContainerRegistry = class {
296
+ constructor() {
297
+ this.instancesByConstructor = /* @__PURE__ */ new WeakMap();
298
+ this.types = /* @__PURE__ */ new Set();
299
+ this.registeredTypeNames = /* @__PURE__ */ new Set();
300
+ this.listeners = /* @__PURE__ */ new Map();
301
+ this._stateChangedListenerCount = 0;
302
+ this._pendingStateChanges = null;
303
+ this._autoRefIdCounter = 0;
304
+ }
305
+ get hasStateChangedListeners() {
306
+ return this._stateChangedListenerCount > 0;
307
+ }
308
+ /**
309
+ * Register a type for lifecycle event tracking
310
+ * @param constructor - The StateContainer class constructor
311
+ */
312
+ registerType(constructor) {
313
+ this.types.add(constructor);
314
+ }
315
+ /**
316
+ * Register a StateContainer class with configuration
317
+ * @param constructor - The StateContainer class constructor
318
+ * @throws Error if type is already registered
319
+ */
320
+ register(constructor) {
321
+ const className = constructor.name;
322
+ if (this.registeredTypeNames.has(className)) throw new Error(`${BLAC_ERROR_PREFIX} Type "${className}" is already registered`);
323
+ this.registeredTypeNames.add(className);
324
+ this.registerType(constructor);
325
+ }
326
+ ensureInstancesMap(Type) {
327
+ let instances = this.instancesByConstructor.get(Type);
328
+ if (!instances) {
329
+ instances = /* @__PURE__ */ new Map();
330
+ this.instancesByConstructor.set(Type, instances);
331
+ }
332
+ return instances;
333
+ }
334
+ /**
335
+ * Get the instances Map for a specific class (public API for stats/debugging)
336
+ */
337
+ getInstancesMap(Type) {
338
+ return this.instancesByConstructor.get(Type) || /* @__PURE__ */ new Map();
339
+ }
340
+ /**
341
+ * Acquire an instance with ref tracking (ownership semantics).
342
+ * Creates a new instance if one doesn't exist, or returns existing and adds a ref.
343
+ * You must call `release()` with the same refId when done.
344
+ * @param Type - The StateContainer class constructor
345
+ * @param instanceKey - Instance key (defaults to 'default')
346
+ * @param options - Acquisition options
347
+ * @param options.canCreate - Whether to create new instance if not found (default: true)
348
+ * @param options.countRef - Whether to add a reference (default: true)
349
+ * @param options.refId - Named reference ID for debugging; auto-generated if omitted
350
+ * @returns The state container instance
351
+ */
352
+ acquire(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY, options = {}) {
353
+ const { canCreate = true, countRef = true } = options;
354
+ const config = { instanceId: instanceKey };
355
+ const instances = this.ensureInstancesMap(Type);
356
+ let entry = instances.get(instanceKey);
357
+ if (entry?.instance.isDisposed) {
358
+ instances.delete(instanceKey);
359
+ entry = void 0;
360
+ }
361
+ if (entry && countRef) {
362
+ const refId = options.refId ?? `_auto_${this._autoRefIdCounter++}`;
363
+ entry.refs.set(refId, (entry.refs.get(refId) ?? 0) + 1);
364
+ this.emit("refAcquired", entry.instance, refId);
365
+ }
366
+ if (entry) return entry.instance;
367
+ if (!canCreate) throw new Error(`${BLAC_ERROR_PREFIX} ${Type.name} instance "${instanceKey}" not found and creation is disabled.`);
368
+ const instance = new Type();
369
+ instance.initConfig(config);
370
+ const initialRefs = /* @__PURE__ */ new Map();
371
+ let initialRefId;
372
+ if (countRef) {
373
+ initialRefId = options.refId ?? `_auto_${this._autoRefIdCounter++}`;
374
+ initialRefs.set(initialRefId, 1);
375
+ }
376
+ instances.set(instanceKey, {
377
+ instance,
378
+ refs: initialRefs
379
+ });
380
+ this.registerType(Type);
381
+ if (initialRefId) this.emit("refAcquired", instance, initialRefId);
382
+ return instance;
383
+ }
384
+ /**
385
+ * Borrow an existing instance without adding a ref (borrowing semantics).
386
+ * Tracks cross-bloc dependency for reactive updates.
387
+ * @param Type - The StateContainer class constructor
388
+ * @param instanceKey - Instance key (defaults to 'default')
389
+ * @returns The state container instance
390
+ * @throws Error if instance doesn't exist
391
+ */
392
+ borrow(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
393
+ return this.acquire(Type, instanceKey, {
394
+ canCreate: false,
395
+ countRef: false
396
+ });
397
+ }
398
+ /**
399
+ * Safely borrow an existing instance (borrowing semantics with error handling).
400
+ * Returns discriminated union for type-safe conditional access.
401
+ * @param Type - The StateContainer class constructor
402
+ * @param instanceKey - Instance key (defaults to 'default')
403
+ * @returns Discriminated union with either the instance or an error
404
+ */
405
+ borrowSafe(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
406
+ try {
407
+ return {
408
+ error: null,
409
+ instance: this.borrow(Type, instanceKey)
410
+ };
411
+ } catch (error) {
412
+ return {
413
+ error,
414
+ instance: null
415
+ };
416
+ }
417
+ }
418
+ /**
419
+ * Ensure an instance exists without taking ownership (for bloc-to-bloc communication).
420
+ * Gets existing instance OR creates it if it doesn't exist, without adding a ref.
421
+ * Tracks cross-bloc dependency for reactive updates.
422
+ *
423
+ * Use this in bloc-to-bloc communication when you need to ensure an instance exists
424
+ * but don't want to claim ownership (no ref added).
425
+ * @param Type - The StateContainer class constructor
426
+ * @param instanceKey - Instance key (defaults to 'default')
427
+ * @returns The state container instance
428
+ */
429
+ ensure(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
430
+ return this.acquire(Type, instanceKey, {
431
+ canCreate: true,
432
+ countRef: false
433
+ });
434
+ }
435
+ /**
436
+ * Release a reference to an instance.
437
+ * Removes the ref and disposes when refs is empty (unless keepAlive).
438
+ * Releasing an already-removed refId is a no-op (idempotent).
439
+ * @param Type - The StateContainer class constructor
440
+ * @param instanceKey - Instance key (defaults to 'default')
441
+ * @param forceDispose - Force immediate disposal regardless of refs
442
+ * @param refId - The specific ref to remove; removes one arbitrary ref if omitted
443
+ */
444
+ release(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY, forceDispose = false, refId) {
445
+ const instances = this.ensureInstancesMap(Type);
446
+ const entry = instances.get(instanceKey);
447
+ if (!entry) return;
448
+ if (forceDispose) {
449
+ if (!entry.instance.isDisposed) entry.instance.dispose();
450
+ instances.delete(instanceKey);
451
+ return;
452
+ }
453
+ let releasedRefId;
454
+ if (refId !== void 0) {
455
+ const count = entry.refs.get(refId) ?? 0;
456
+ if (count <= 1) entry.refs.delete(refId);
457
+ else entry.refs.set(refId, count - 1);
458
+ releasedRefId = refId;
459
+ } else {
460
+ const firstKey = entry.refs.keys().next().value;
461
+ if (firstKey !== void 0) {
462
+ const count = entry.refs.get(firstKey) ?? 0;
463
+ if (count <= 1) entry.refs.delete(firstKey);
464
+ else entry.refs.set(firstKey, count - 1);
465
+ releasedRefId = firstKey;
466
+ }
467
+ }
468
+ if (releasedRefId) this.emit("refReleased", entry.instance, releasedRefId);
469
+ const keepAlive = isKeepAliveClass(Type);
470
+ if (entry.refs.size === 0 && !keepAlive) {
471
+ const deps = entry.instance.dependencies;
472
+ if (!entry.instance.isDisposed) entry.instance.dispose();
473
+ instances.delete(instanceKey);
474
+ for (const [DepType, depKey] of deps) {
475
+ const depInstances = this.ensureInstancesMap(DepType);
476
+ const depEntry = depInstances.get(depKey);
477
+ if (depEntry && depEntry.refs.size === 0 && !isKeepAliveClass(DepType) && !depEntry.instance.isDisposed) {
478
+ depEntry.instance.dispose();
479
+ depInstances.delete(depKey);
480
+ }
481
+ }
482
+ }
483
+ }
484
+ /**
485
+ * Get all instances of a specific type.
486
+ * @param Type - The StateContainer class constructor
487
+ * @returns Array of all instances
488
+ */
489
+ getAll(Type) {
490
+ const instances = this.ensureInstancesMap(Type);
491
+ const result = [];
492
+ for (const entry of instances.values()) result.push(entry.instance);
493
+ return result;
494
+ }
495
+ /**
496
+ * Safely iterate over all instances of a type.
497
+ * Skips disposed instances and catches callback errors.
498
+ * @param Type - The StateContainer class constructor
499
+ * @param callback - Function to call for each instance
500
+ */
501
+ forEach(Type, callback) {
502
+ const instances = this.ensureInstancesMap(Type);
503
+ for (const entry of instances.values()) {
504
+ const instance = entry.instance;
505
+ if (!instance.isDisposed) try {
506
+ callback(instance);
507
+ } catch (error) {
508
+ console.error(`${BLAC_ERROR_PREFIX} forEach callback error for ${Type.name}:`, error);
509
+ }
510
+ }
511
+ }
512
+ /**
513
+ * Clear all instances of a specific type (disposes them).
514
+ * @param Type - The StateContainer class constructor
515
+ */
516
+ clear(Type) {
517
+ const instances = this.ensureInstancesMap(Type);
518
+ for (const entry of instances.values()) if (!entry.instance.isDisposed) entry.instance.dispose();
519
+ instances.clear();
520
+ }
521
+ /**
522
+ * Get reference count for an instance (number of active refs).
523
+ * @param Type - The StateContainer class constructor
524
+ * @param instanceKey - Instance key (defaults to 'default')
525
+ * @returns Current ref count (0 if instance doesn't exist)
526
+ */
527
+ getRefCount(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
528
+ return this.ensureInstancesMap(Type).get(instanceKey)?.refs.size ?? 0;
529
+ }
530
+ /**
531
+ * Get all active reference IDs for an instance.
532
+ * @param Type - The StateContainer class constructor
533
+ * @param instanceKey - Instance key (defaults to 'default')
534
+ * @returns Array of ref ID strings (empty if instance doesn't exist)
535
+ */
536
+ getRefIds(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
537
+ const instances = this.instancesByConstructor.get(Type);
538
+ if (!instances) return [];
539
+ const entry = instances.get(instanceKey);
540
+ return entry ? Array.from(entry.refs.keys()) : [];
541
+ }
542
+ /**
543
+ * Check if an instance exists.
544
+ * @param Type - The StateContainer class constructor
545
+ * @param instanceKey - Instance key (defaults to 'default')
546
+ * @returns true if instance exists
547
+ */
548
+ hasInstance(Type, instanceKey = BLAC_DEFAULTS.DEFAULT_INSTANCE_KEY) {
549
+ return this.ensureInstancesMap(Type).has(instanceKey);
550
+ }
551
+ /**
552
+ * Clear all instances from all types (for testing)
553
+ *
554
+ * Iterates all registered types and clears their instances.
555
+ * Also clears type tracking to reset the registry state.
556
+ */
557
+ clearAll() {
558
+ for (const Type of this.types) this.clear(Type);
559
+ this.types.clear();
560
+ this.registeredTypeNames.clear();
561
+ }
562
+ /**
563
+ * Get registry statistics for debugging.
564
+ * @returns Object with registeredTypes, totalInstances, and typeBreakdown
565
+ */
566
+ getStats() {
567
+ const typeBreakdown = {};
568
+ let totalInstances = 0;
569
+ for (const Type of this.types) {
570
+ const typeName = Type.name;
571
+ const count = this.getInstancesMap(Type).size;
572
+ typeBreakdown[typeName] = count;
573
+ totalInstances += count;
574
+ }
575
+ return {
576
+ registeredTypes: this.types.size,
577
+ totalInstances,
578
+ typeBreakdown
579
+ };
580
+ }
581
+ /**
582
+ * Get all registered types (for plugin system).
583
+ * @returns Array of all registered StateContainer class constructors
584
+ */
585
+ getTypes() {
586
+ return Array.from(this.types);
587
+ }
588
+ /**
589
+ * Subscribe to lifecycle events
590
+ * @param event - The lifecycle event to listen for
591
+ * @param listener - The listener function to call when the event occurs
592
+ * @returns Unsubscribe function
593
+ */
594
+ on(event, listener) {
595
+ if (!this.listeners.has(event)) this.listeners.set(event, /* @__PURE__ */ new Set());
596
+ const instance = this.listeners.get(event);
597
+ if (!instance) throw new Error(`${BLAC_ERROR_PREFIX} Failed to register listener for event '${event}'`);
598
+ instance.add(listener);
599
+ if (event === "stateChanged") this._stateChangedListenerCount++;
600
+ return () => {
601
+ if (this.listeners.get(event)?.delete(listener) && event === "stateChanged") this._stateChangedListenerCount--;
602
+ };
603
+ }
604
+ emit(event, ...args) {
605
+ const listeners = this.listeners.get(event);
606
+ if (!listeners || listeners.size === 0) return;
607
+ for (const listener of listeners) try {
608
+ listener(...args);
609
+ } catch (error) {
610
+ console.error(`${BLAC_ERROR_PREFIX} Listener error for '${event}':`, error);
611
+ }
612
+ }
613
+ /**
614
+ * Schedule a deferred stateChanged notification via microtask.
615
+ * Skips entirely when no stateChanged listeners are registered.
616
+ * @internal - Called by StateContainer.applyState
617
+ */
618
+ notifyStateChanged(container, previousState, newState) {
619
+ if (this._stateChangedListenerCount === 0) return;
620
+ if (!this._pendingStateChanges) {
621
+ this._pendingStateChanges = [];
622
+ queueMicrotask(() => this.flushStateChanged());
623
+ }
624
+ this._pendingStateChanges.push([
625
+ container,
626
+ previousState,
627
+ newState
628
+ ]);
629
+ }
630
+ flushStateChanged() {
631
+ const pending = this._pendingStateChanges;
632
+ this._pendingStateChanges = null;
633
+ if (!pending) return;
634
+ for (const [container, prev, next] of pending) this.emit("stateChanged", container, prev, next);
635
+ }
636
+ };
637
+ /**
638
+ * Global default registry instance
639
+ */
640
+ const globalRegistry = new StateContainerRegistry();
641
+ /**
642
+ * Global plugin manager (initialized lazily)
643
+ */
644
+ let _globalPluginManager = null;
645
+ /**
646
+ * Get the global plugin manager
647
+ */
648
+ function getPluginManager() {
649
+ if (!_globalPluginManager) _globalPluginManager = createPluginManager(globalRegistry);
650
+ return _globalPluginManager;
651
+ }
652
+ //#endregion
653
+ Object.defineProperty(exports, "BLAC_DEFAULTS", {
654
+ enumerable: true,
655
+ get: function() {
656
+ return BLAC_DEFAULTS;
657
+ }
658
+ });
659
+ Object.defineProperty(exports, "BLAC_ERROR_PREFIX", {
660
+ enumerable: true,
661
+ get: function() {
662
+ return BLAC_ERROR_PREFIX;
663
+ }
664
+ });
665
+ Object.defineProperty(exports, "BLAC_STATIC_PROPS", {
666
+ enumerable: true,
667
+ get: function() {
668
+ return BLAC_STATIC_PROPS;
669
+ }
670
+ });
671
+ Object.defineProperty(exports, "PluginManager", {
672
+ enumerable: true,
673
+ get: function() {
674
+ return PluginManager;
675
+ }
676
+ });
677
+ Object.defineProperty(exports, "StateContainerRegistry", {
678
+ enumerable: true,
679
+ get: function() {
680
+ return StateContainerRegistry;
681
+ }
682
+ });
683
+ Object.defineProperty(exports, "getPluginManager", {
684
+ enumerable: true,
685
+ get: function() {
686
+ return getPluginManager;
687
+ }
688
+ });
689
+ Object.defineProperty(exports, "globalRegistry", {
690
+ enumerable: true,
691
+ get: function() {
692
+ return globalRegistry;
693
+ }
694
+ });
695
+
696
+ //# sourceMappingURL=StateContainerRegistry-Upsrlg9G.cjs.map