@blac/core 2.0.0-rc-2 → 2.0.0-rc-3
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/README.md +53 -45
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +7 -98
- package/dist/index.es.js +215 -313
- package/dist/index.es.js.map +1 -1
- package/package.json +1 -1
- package/src/Blac.ts +40 -107
- package/src/BlacObserver.ts +12 -6
- package/src/BlocBase.ts +13 -32
- package/src/index.ts +2 -3
- package/src/BlacObservable.ts +0 -85
- package/src/BlacPlugin.ts +0 -13
- package/src/addons/BlacAddon.ts +0 -17
- package/src/addons/Persist.ts +0 -105
- package/src/addons/index.ts +0 -1
package/dist/index.es.js
CHANGED
|
@@ -1,111 +1,71 @@
|
|
|
1
1
|
var B = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
const r = class r {
|
|
2
|
+
var I = (d, e, t) => e in d ? B(d, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : d[e] = t;
|
|
3
|
+
var s = (d, e, t) => I(d, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
const o = class o {
|
|
6
5
|
/**
|
|
7
6
|
* Creates a new Blac instance.
|
|
8
7
|
* @param options - Configuration options including singleton control
|
|
9
8
|
*/
|
|
10
|
-
constructor(
|
|
9
|
+
constructor(e = {}) {
|
|
11
10
|
/** Timestamp when the instance was created */
|
|
12
|
-
|
|
11
|
+
s(this, "createdAt", Date.now());
|
|
13
12
|
/** Map storing all registered bloc instances by their class name and ID */
|
|
14
|
-
|
|
13
|
+
s(this, "blocInstanceMap", /* @__PURE__ */ new Map());
|
|
15
14
|
/** Map storing isolated bloc instances grouped by their constructor */
|
|
16
|
-
|
|
17
|
-
n(this, "pluginList", []);
|
|
15
|
+
s(this, "isolatedBlocMap", /* @__PURE__ */ new Map());
|
|
18
16
|
/** Flag to control whether changes should be posted to document */
|
|
19
|
-
|
|
17
|
+
s(this, "postChangesToDocument", !1);
|
|
20
18
|
/**
|
|
21
19
|
* Logs messages to console when logging is enabled
|
|
22
20
|
* @param args - Arguments to log
|
|
23
21
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
s(this, "log", (...e) => {
|
|
23
|
+
o.enableLog && console.warn(`☢️ [Blac ${this.createdAt.toString()}]`, ...e);
|
|
26
24
|
});
|
|
27
25
|
/**
|
|
28
26
|
* Logs a warning message
|
|
29
27
|
* @param message - Warning message
|
|
30
28
|
* @param args - Additional arguments
|
|
31
29
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
s(this, "warn", (e, ...t) => {
|
|
31
|
+
o.enableLog && console.warn(`🚨 [Blac ${String(o.instance.createdAt)}]`, e, ...t);
|
|
34
32
|
});
|
|
35
33
|
/**
|
|
36
34
|
* Logs an error message
|
|
37
35
|
* @param message - Error message
|
|
38
36
|
* @param args - Additional arguments
|
|
39
37
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
s(this, "error", (e, ...t) => {
|
|
39
|
+
o.enableLog && console.error(`🚨 [Blac ${String(o.instance.createdAt)}]`, e, ...t);
|
|
42
40
|
});
|
|
43
41
|
/**
|
|
44
42
|
* Resets the Blac instance to a new one, disposing non-keepAlive blocs
|
|
45
43
|
* from the old instance.
|
|
46
44
|
*/
|
|
47
|
-
|
|
45
|
+
s(this, "resetInstance", () => {
|
|
48
46
|
this.log("Reset Blac instance");
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}),
|
|
53
|
-
|
|
47
|
+
const e = new Map(this.blocInstanceMap), t = new Map(this.isolatedBlocMap);
|
|
48
|
+
e.forEach((n) => {
|
|
49
|
+
n._dispose();
|
|
50
|
+
}), t.forEach((n) => {
|
|
51
|
+
n.forEach((i) => {
|
|
54
52
|
i._dispose();
|
|
55
53
|
});
|
|
56
|
-
}), this.blocInstanceMap.clear(), this.isolatedBlocMap.clear(),
|
|
54
|
+
}), this.blocInstanceMap.clear(), this.isolatedBlocMap.clear(), o.instance = new o({
|
|
57
55
|
__unsafe_ignore_singleton: !0
|
|
58
56
|
});
|
|
59
57
|
});
|
|
60
|
-
/**
|
|
61
|
-
* Adds a plugin to the Blac instance
|
|
62
|
-
* @param plugin - The plugin to add
|
|
63
|
-
*/
|
|
64
|
-
n(this, "addPlugin", (t) => {
|
|
65
|
-
this.pluginList.findIndex((s) => s.name === t.name) === -1 && (this.log("Add plugin", t.name), this.pluginList.push(t));
|
|
66
|
-
});
|
|
67
|
-
/**
|
|
68
|
-
* Dispatches a lifecycle event to all registered plugins
|
|
69
|
-
* @param event - The lifecycle event to dispatch
|
|
70
|
-
* @param bloc - The bloc instance involved in the event
|
|
71
|
-
* @param params - Additional parameters for the event
|
|
72
|
-
*/
|
|
73
|
-
n(this, "dispatchEventToPlugins", (t, e, s) => {
|
|
74
|
-
this.pluginList.forEach((i) => {
|
|
75
|
-
i.onEvent(t, e, s);
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
/**
|
|
79
|
-
* Dispatches a lifecycle event and handles related cleanup actions.
|
|
80
|
-
* This method is responsible for:
|
|
81
|
-
* - Logging the event
|
|
82
|
-
* - Handling bloc disposal when needed
|
|
83
|
-
* - Managing bloc consumer cleanup
|
|
84
|
-
* - Forwarding the event to plugins
|
|
85
|
-
*
|
|
86
|
-
* @param event - The lifecycle event to dispatch
|
|
87
|
-
* @param bloc - The bloc instance involved in the event
|
|
88
|
-
* @param params - Additional parameters for the event
|
|
89
|
-
*/
|
|
90
|
-
n(this, "dispatchEvent", (t, e, s) => {
|
|
91
|
-
switch (this.log(t, e, s), t) {
|
|
92
|
-
case "BLOC_DISPOSED":
|
|
93
|
-
this.disposeBloc(e);
|
|
94
|
-
break;
|
|
95
|
-
case "BLOC_CONSUMER_REMOVED":
|
|
96
|
-
case "LISTENER_REMOVED":
|
|
97
|
-
this.log(`[${e._name}:${String(e._id)}] Listener/Consumer removed. Listeners: ${String(e._observer.size)}, Consumers: ${String(e._consumers.size)}, KeepAlive: ${String(e._keepAlive)}`), e._observer.size === 0 && e._consumers.size === 0 && !e._keepAlive && (this.log(`[${e._name}:${String(e._id)}] No listeners or consumers left and not keepAlive. Disposing.`), e._dispose());
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
this.dispatchEventToPlugins(t, e, s);
|
|
101
|
-
});
|
|
102
58
|
/**
|
|
103
59
|
* Disposes of a bloc instance by removing it from the appropriate registry
|
|
104
60
|
* @param bloc - The bloc instance to dispose
|
|
105
61
|
*/
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
this.log(`[${
|
|
62
|
+
s(this, "disposeBloc", (e) => {
|
|
63
|
+
const t = e.constructor, n = this.createBlocInstanceMapKey(e._name, e._id);
|
|
64
|
+
this.log(`[${e._name}:${String(e._id)}] disposeBloc called. Isolated: ${String(t.isolated)}`), t.isolated ? (this.unregisterIsolatedBlocInstance(e), this.blocInstanceMap.delete(n)) : this.unregisterBlocInstance(e), this.log("dispatched bloc", e);
|
|
65
|
+
});
|
|
66
|
+
s(this, "activateBloc", (e) => {
|
|
67
|
+
const t = e.constructor;
|
|
68
|
+
(e.isIsolated ? this.findIsolatedBlocInstance(t, e._id) : this.findRegisteredBlocInstance(t, e._id)) || (this.log(`[${e._name}:${String(e._id)}] activateBloc called. Isolated: ${String(e.isIsolated)}`), e.isIsolated ? this.registerIsolatedBlocInstance(e) : this.registerBlocInstance(e));
|
|
109
69
|
});
|
|
110
70
|
/**
|
|
111
71
|
* Gets or creates a bloc instance based on the provided class and options.
|
|
@@ -118,23 +78,27 @@ const r = class r {
|
|
|
118
78
|
* - instanceRef: Optional reference string for the instance
|
|
119
79
|
* @returns The bloc instance
|
|
120
80
|
*/
|
|
121
|
-
|
|
122
|
-
const { id:
|
|
123
|
-
if (
|
|
124
|
-
const
|
|
125
|
-
if (
|
|
126
|
-
return this.log(`[${
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
81
|
+
s(this, "getBloc", (e, t = {}) => {
|
|
82
|
+
const { id: n } = t, i = e, a = n ?? e.name;
|
|
83
|
+
if (i.isolated) {
|
|
84
|
+
const l = this.findIsolatedBlocInstance(e, a);
|
|
85
|
+
if (l)
|
|
86
|
+
return this.log(`[${e.name}:${String(a)}] (getBloc) Found existing isolated instance.`, t), l;
|
|
87
|
+
if (t.throwIfNotFound)
|
|
88
|
+
throw new Error(`Isolated bloc ${e.name} not found`);
|
|
89
|
+
} else {
|
|
90
|
+
const l = this.findRegisteredBlocInstance(e, a);
|
|
91
|
+
if (l)
|
|
92
|
+
return this.log(`[${e.name}:${String(a)}] (getBloc) Found existing registered instance.`, t), l;
|
|
93
|
+
if (t.throwIfNotFound)
|
|
94
|
+
throw new Error(`Registered bloc ${e.name} not found`);
|
|
132
95
|
}
|
|
133
|
-
|
|
134
|
-
|
|
96
|
+
const r = this.createNewBlocInstance(
|
|
97
|
+
e,
|
|
135
98
|
a,
|
|
136
|
-
|
|
99
|
+
t
|
|
137
100
|
);
|
|
101
|
+
return this.log(`[${e.name}:${String(a)}] (getBloc) No existing instance found. Creating new one.`, t, r), r;
|
|
138
102
|
});
|
|
139
103
|
/**
|
|
140
104
|
* Gets a bloc instance or throws an error if it doesn't exist
|
|
@@ -144,11 +108,11 @@ const r = class r {
|
|
|
144
108
|
* - props: Properties to pass to the bloc constructor
|
|
145
109
|
* - instanceRef: Optional reference string for the instance
|
|
146
110
|
*/
|
|
147
|
-
|
|
148
|
-
const
|
|
111
|
+
s(this, "getBlocOrThrow", (e, t = {}) => {
|
|
112
|
+
const n = e.isolated, i = t.id || e.name, a = n ? this.findIsolatedBlocInstance(e, i) : this.findRegisteredBlocInstance(e, i);
|
|
149
113
|
if (a)
|
|
150
114
|
return a;
|
|
151
|
-
throw new Error(`Bloc ${
|
|
115
|
+
throw new Error(`Bloc ${e.name} not found`);
|
|
152
116
|
});
|
|
153
117
|
/**
|
|
154
118
|
* Gets all instances of a specific bloc class
|
|
@@ -157,27 +121,27 @@ const r = class r {
|
|
|
157
121
|
* - searchIsolated: Whether to search in isolated blocs (defaults to bloc's isolated property)
|
|
158
122
|
* @returns Array of matching bloc instances
|
|
159
123
|
*/
|
|
160
|
-
|
|
161
|
-
const
|
|
124
|
+
s(this, "getAllBlocs", (e, t = {}) => {
|
|
125
|
+
const n = [];
|
|
162
126
|
if (this.blocInstanceMap.forEach((i) => {
|
|
163
|
-
i.constructor ===
|
|
164
|
-
}),
|
|
165
|
-
const i = this.isolatedBlocMap.get(
|
|
166
|
-
i &&
|
|
127
|
+
i.constructor === e && n.push(i);
|
|
128
|
+
}), t.searchIsolated !== !1) {
|
|
129
|
+
const i = this.isolatedBlocMap.get(e);
|
|
130
|
+
i && n.push(...i.map((a) => a));
|
|
167
131
|
}
|
|
168
|
-
return
|
|
132
|
+
return n;
|
|
169
133
|
});
|
|
170
|
-
const { __unsafe_ignore_singleton:
|
|
171
|
-
if (!
|
|
172
|
-
return
|
|
173
|
-
|
|
134
|
+
const { __unsafe_ignore_singleton: t = !1 } = e;
|
|
135
|
+
if (!t)
|
|
136
|
+
return o.instance;
|
|
137
|
+
o.instance = this;
|
|
174
138
|
}
|
|
175
139
|
/**
|
|
176
140
|
* Gets the singleton instance of Blac
|
|
177
141
|
* @returns The Blac instance
|
|
178
142
|
*/
|
|
179
143
|
static getInstance() {
|
|
180
|
-
return
|
|
144
|
+
return o.instance;
|
|
181
145
|
}
|
|
182
146
|
/**
|
|
183
147
|
* Creates a unique key for a bloc instance in the map based on the bloc class name and instance ID
|
|
@@ -185,24 +149,24 @@ const r = class r {
|
|
|
185
149
|
* @param id - The instance ID
|
|
186
150
|
* @returns A unique key string in the format "className:id"
|
|
187
151
|
*/
|
|
188
|
-
createBlocInstanceMapKey(
|
|
189
|
-
return `${
|
|
152
|
+
createBlocInstanceMapKey(e, t) {
|
|
153
|
+
return `${e}:${String(t)}`;
|
|
190
154
|
}
|
|
191
155
|
/**
|
|
192
156
|
* Unregister a bloc instance from the main registry
|
|
193
157
|
* @param bloc - The bloc instance to unregister
|
|
194
158
|
*/
|
|
195
|
-
unregisterBlocInstance(
|
|
196
|
-
const
|
|
197
|
-
this.blocInstanceMap.delete(
|
|
159
|
+
unregisterBlocInstance(e) {
|
|
160
|
+
const t = this.createBlocInstanceMapKey(e._name, e._id);
|
|
161
|
+
this.blocInstanceMap.delete(t);
|
|
198
162
|
}
|
|
199
163
|
/**
|
|
200
164
|
* Registers a bloc instance in the main registry
|
|
201
165
|
* @param bloc - The bloc instance to register
|
|
202
166
|
*/
|
|
203
|
-
registerBlocInstance(
|
|
204
|
-
const
|
|
205
|
-
this.blocInstanceMap.set(
|
|
167
|
+
registerBlocInstance(e) {
|
|
168
|
+
const t = this.createBlocInstanceMapKey(e._name, e._id);
|
|
169
|
+
this.blocInstanceMap.set(t, e);
|
|
206
170
|
}
|
|
207
171
|
/**
|
|
208
172
|
* Finds a registered bloc instance by its class and ID
|
|
@@ -210,39 +174,37 @@ const r = class r {
|
|
|
210
174
|
* @param id - The instance ID
|
|
211
175
|
* @returns The found bloc instance or undefined if not found
|
|
212
176
|
*/
|
|
213
|
-
findRegisteredBlocInstance(
|
|
214
|
-
if (
|
|
215
|
-
const i = this.createBlocInstanceMapKey(
|
|
216
|
-
return
|
|
177
|
+
findRegisteredBlocInstance(e, t) {
|
|
178
|
+
if (e.isolated) return;
|
|
179
|
+
const i = this.createBlocInstanceMapKey(e.name, t);
|
|
180
|
+
return this.blocInstanceMap.get(i);
|
|
217
181
|
}
|
|
218
182
|
/**
|
|
219
183
|
* Registers an isolated bloc instance in the isolated registry
|
|
220
184
|
* @param bloc - The isolated bloc instance to register
|
|
221
185
|
*/
|
|
222
|
-
registerIsolatedBlocInstance(
|
|
223
|
-
const
|
|
224
|
-
|
|
186
|
+
registerIsolatedBlocInstance(e) {
|
|
187
|
+
const t = e.constructor, n = this.isolatedBlocMap.get(t);
|
|
188
|
+
n ? n.push(e) : this.isolatedBlocMap.set(t, [e]);
|
|
225
189
|
}
|
|
226
190
|
/**
|
|
227
191
|
* Unregister an isolated bloc instance from the isolated registry
|
|
228
192
|
* @param bloc - The isolated bloc instance to unregister
|
|
229
193
|
*/
|
|
230
|
-
unregisterIsolatedBlocInstance(
|
|
231
|
-
const
|
|
232
|
-
if (
|
|
233
|
-
const i =
|
|
234
|
-
i !== -1 &&
|
|
194
|
+
unregisterIsolatedBlocInstance(e) {
|
|
195
|
+
const t = e.constructor, n = this.isolatedBlocMap.get(t);
|
|
196
|
+
if (n) {
|
|
197
|
+
const i = n.findIndex((a) => a._id === e._id);
|
|
198
|
+
i !== -1 && n.splice(i, 1), n.length === 0 && this.isolatedBlocMap.delete(t);
|
|
235
199
|
}
|
|
236
200
|
}
|
|
237
201
|
/**
|
|
238
202
|
* Finds an isolated bloc instance by its class and ID
|
|
239
203
|
*/
|
|
240
|
-
findIsolatedBlocInstance(
|
|
241
|
-
if (!
|
|
242
|
-
const i = this.isolatedBlocMap.get(
|
|
243
|
-
|
|
244
|
-
const a = i.find((o) => o._id === e);
|
|
245
|
-
return a && this.log(`[${t.name}:${String(e)}] Found isolated instance. Returning.`), a;
|
|
204
|
+
findIsolatedBlocInstance(e, t) {
|
|
205
|
+
if (!e.isolated) return;
|
|
206
|
+
const i = this.isolatedBlocMap.get(e);
|
|
207
|
+
return i ? i.find((r) => r._id === t) : void 0;
|
|
246
208
|
}
|
|
247
209
|
/**
|
|
248
210
|
* Creates a new bloc instance and registers it in the appropriate registry
|
|
@@ -252,25 +214,25 @@ const r = class r {
|
|
|
252
214
|
* @param instanceRef - Optional reference string for the instance
|
|
253
215
|
* @returns The newly created bloc instance
|
|
254
216
|
*/
|
|
255
|
-
createNewBlocInstance(
|
|
256
|
-
const { props: i, instanceRef: a } =
|
|
257
|
-
return
|
|
217
|
+
createNewBlocInstance(e, t, n = {}) {
|
|
218
|
+
const { props: i, instanceRef: a } = n, r = new e(i);
|
|
219
|
+
return r._instanceRef = a, r.props = i || null, r._updateId(t), r.isIsolated ? (this.registerIsolatedBlocInstance(r), r) : (this.registerBlocInstance(r), r);
|
|
258
220
|
}
|
|
259
221
|
};
|
|
260
222
|
/** The singleton instance of Blac */
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
let
|
|
264
|
-
class
|
|
223
|
+
s(o, "instance", new o()), s(o, "getAllBlocs", o.instance.getAllBlocs), /** Flag to enable/disable logging */
|
|
224
|
+
s(o, "enableLog", !1), s(o, "log", o.instance.log), s(o, "warn", o.instance.warn), s(o, "error", o.instance.error), s(o, "resetInstance", o.instance.resetInstance), s(o, "activateBloc", o.instance.activateBloc), s(o, "getBloc", o.instance.getBloc), s(o, "getBlocOrThrow", o.instance.getBlocOrThrow);
|
|
225
|
+
let c = o;
|
|
226
|
+
class _ {
|
|
265
227
|
/**
|
|
266
228
|
* Creates a new BlacObservable instance
|
|
267
229
|
* @param bloc - The Bloc instance to observe
|
|
268
230
|
*/
|
|
269
|
-
constructor(
|
|
231
|
+
constructor(e) {
|
|
270
232
|
/** The Bloc instance this observable is associated with */
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
this.bloc =
|
|
233
|
+
s(this, "bloc");
|
|
234
|
+
s(this, "_observers", /* @__PURE__ */ new Set());
|
|
235
|
+
this.bloc = e;
|
|
274
236
|
}
|
|
275
237
|
/**
|
|
276
238
|
* Gets the number of active observers
|
|
@@ -291,18 +253,17 @@ class D {
|
|
|
291
253
|
* @param observer - The observer to subscribe
|
|
292
254
|
* @returns A function that can be called to unsubscribe the observer
|
|
293
255
|
*/
|
|
294
|
-
subscribe(
|
|
295
|
-
return
|
|
296
|
-
this.unsubscribe(
|
|
256
|
+
subscribe(e) {
|
|
257
|
+
return c.log("BlacObservable.subscribe: Subscribing observer.", this.bloc, e), this._observers.add(e), e.lastState || (e.lastState = e.dependencyArray ? e.dependencyArray(this.bloc.state) : []), () => {
|
|
258
|
+
c.log("BlacObservable.subscribe: Unsubscribing observer.", this.bloc, e), this.unsubscribe(e);
|
|
297
259
|
};
|
|
298
260
|
}
|
|
299
261
|
/**
|
|
300
262
|
* Unsubscribes an observer from state changes
|
|
301
263
|
* @param observer - The observer to unsubscribe
|
|
302
264
|
*/
|
|
303
|
-
unsubscribe(
|
|
304
|
-
|
|
305
|
-
(e = t.dispose) == null || e.call(t), this._observers.delete(t), h.instance.dispatchEvent(_.LISTENER_REMOVED, this.bloc, { listenerId: t.id });
|
|
265
|
+
unsubscribe(e) {
|
|
266
|
+
this._observers.delete(e), this.size === 0 && (c.log("BlacObservable.unsubscribe: No observers left. Disposing bloc.", this.bloc), this.bloc._dispose());
|
|
306
267
|
}
|
|
307
268
|
/**
|
|
308
269
|
* Notifies all observers of a state change
|
|
@@ -310,102 +271,98 @@ class D {
|
|
|
310
271
|
* @param oldState - The previous state value
|
|
311
272
|
* @param action - Optional action that triggered the state change
|
|
312
273
|
*/
|
|
313
|
-
notify(
|
|
274
|
+
notify(e, t, n) {
|
|
314
275
|
this._observers.forEach((i) => {
|
|
315
276
|
let a = !1;
|
|
316
277
|
if (i.dependencyArray) {
|
|
317
|
-
const
|
|
318
|
-
for (let
|
|
319
|
-
const
|
|
320
|
-
for (let
|
|
321
|
-
if (!Object.is(
|
|
278
|
+
const r = i.lastState || [], l = i.dependencyArray(e);
|
|
279
|
+
for (let h = 0; h < l.length; h++) {
|
|
280
|
+
const f = l[h], p = r[h] || [];
|
|
281
|
+
for (let u = 0; u < f.length; u++)
|
|
282
|
+
if (!Object.is(f[u], p[u])) {
|
|
322
283
|
a = !0;
|
|
323
284
|
break;
|
|
324
285
|
}
|
|
325
286
|
}
|
|
326
|
-
i.lastState =
|
|
287
|
+
i.lastState = l;
|
|
327
288
|
} else
|
|
328
289
|
a = !0;
|
|
329
|
-
a && i.fn(
|
|
290
|
+
a && i.fn(e, t, n);
|
|
330
291
|
});
|
|
331
292
|
}
|
|
332
293
|
/**
|
|
333
|
-
*
|
|
294
|
+
* Clears the observer set
|
|
334
295
|
*/
|
|
335
|
-
|
|
336
|
-
this._observers.forEach((
|
|
337
|
-
this.unsubscribe(
|
|
296
|
+
clear() {
|
|
297
|
+
this._observers.forEach((e) => {
|
|
298
|
+
this.unsubscribe(e);
|
|
338
299
|
}), this._observers.clear();
|
|
339
300
|
}
|
|
340
301
|
}
|
|
341
|
-
class
|
|
302
|
+
class g {
|
|
342
303
|
/**
|
|
343
304
|
* Creates a new BlocBase instance with the given initial state.
|
|
344
305
|
* Sets up the observer, registers with the Blac manager, and initializes addons.
|
|
345
306
|
*
|
|
346
307
|
* @param initialState The initial state value for this Bloc
|
|
347
308
|
*/
|
|
348
|
-
constructor(
|
|
309
|
+
constructor(e) {
|
|
310
|
+
s(this, "uid", crypto.randomUUID());
|
|
349
311
|
/**
|
|
350
312
|
* Defines how dependencies are selected from the state for efficient updates.
|
|
351
313
|
* When provided, observers will only be notified when selected dependencies change.
|
|
352
314
|
*/
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* @internal
|
|
356
|
-
* Optional array of addons to extend the functionality of this Bloc.
|
|
357
|
-
*/
|
|
358
|
-
n(this, "_addons");
|
|
315
|
+
s(this, "defaultDependencySelector");
|
|
359
316
|
/**
|
|
360
317
|
* @internal
|
|
361
318
|
* Indicates if this specific Bloc instance is isolated from others of the same type.
|
|
362
319
|
*/
|
|
363
|
-
|
|
320
|
+
s(this, "_isolated", !1);
|
|
364
321
|
/**
|
|
365
322
|
* @internal
|
|
366
323
|
* Observable responsible for managing state listeners and notifying consumers.
|
|
367
324
|
*/
|
|
368
|
-
|
|
325
|
+
s(this, "_observer");
|
|
369
326
|
/**
|
|
370
327
|
* @internal
|
|
371
328
|
* Reference to the global Blac manager instance.
|
|
372
329
|
*/
|
|
373
|
-
|
|
330
|
+
s(this, "_blac", c.getInstance());
|
|
374
331
|
/**
|
|
375
332
|
* The unique identifier for this Bloc instance.
|
|
376
333
|
* Defaults to the class name, but can be customized.
|
|
377
334
|
*/
|
|
378
|
-
|
|
335
|
+
s(this, "_id");
|
|
379
336
|
/**
|
|
380
337
|
* @internal
|
|
381
338
|
* Reference string used internally for tracking and debugging.
|
|
382
339
|
*/
|
|
383
|
-
|
|
340
|
+
s(this, "_instanceRef");
|
|
384
341
|
/**
|
|
385
342
|
* @internal
|
|
386
343
|
* Indicates if this specific Bloc instance should be kept alive when no consumers are present.
|
|
387
344
|
*/
|
|
388
|
-
|
|
345
|
+
s(this, "_keepAlive", !1);
|
|
389
346
|
/**
|
|
390
347
|
* @readonly
|
|
391
348
|
* Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
|
|
392
349
|
*/
|
|
393
|
-
|
|
350
|
+
s(this, "_createdAt", Date.now());
|
|
394
351
|
/**
|
|
395
352
|
* @internal
|
|
396
353
|
* The current state of the Bloc.
|
|
397
354
|
*/
|
|
398
|
-
|
|
355
|
+
s(this, "_state");
|
|
399
356
|
/**
|
|
400
357
|
* @internal
|
|
401
358
|
* The previous state of the Bloc, maintained for comparison and history.
|
|
402
359
|
*/
|
|
403
|
-
|
|
360
|
+
s(this, "_oldState");
|
|
404
361
|
/**
|
|
405
362
|
* Props passed during Bloc instance creation.
|
|
406
363
|
* Can be used to configure or parameterize the Bloc's behavior.
|
|
407
364
|
*/
|
|
408
|
-
|
|
365
|
+
s(this, "props", null);
|
|
409
366
|
/**
|
|
410
367
|
* @internal
|
|
411
368
|
* Updates the Bloc instance's ID to a new value.
|
|
@@ -413,20 +370,20 @@ class E {
|
|
|
413
370
|
*
|
|
414
371
|
* @param id The new ID to assign to this Bloc instance
|
|
415
372
|
*/
|
|
416
|
-
|
|
417
|
-
const
|
|
418
|
-
!
|
|
373
|
+
s(this, "_updateId", (e) => {
|
|
374
|
+
const t = this._id;
|
|
375
|
+
!e || e === t || (this._id = e);
|
|
419
376
|
});
|
|
420
377
|
/**
|
|
421
378
|
* @internal
|
|
422
379
|
* Optional function to be called when the Bloc is disposed.
|
|
423
380
|
*/
|
|
424
|
-
|
|
381
|
+
s(this, "onDispose");
|
|
425
382
|
/**
|
|
426
383
|
* @internal
|
|
427
384
|
* Set of consumer IDs currently listening to this Bloc's state changes.
|
|
428
385
|
*/
|
|
429
|
-
|
|
386
|
+
s(this, "_consumers", /* @__PURE__ */ new Set());
|
|
430
387
|
/**
|
|
431
388
|
* @internal
|
|
432
389
|
* Registers a new consumer to this Bloc instance.
|
|
@@ -434,8 +391,8 @@ class E {
|
|
|
434
391
|
*
|
|
435
392
|
* @param consumerId The unique ID of the consumer being added
|
|
436
393
|
*/
|
|
437
|
-
|
|
438
|
-
this._consumers.
|
|
394
|
+
s(this, "_addConsumer", (e) => {
|
|
395
|
+
this._consumers.has(e) || (this._consumers.add(e), c.log("BlocBase._addConsumer", this, e));
|
|
439
396
|
});
|
|
440
397
|
/**
|
|
441
398
|
* @internal
|
|
@@ -444,22 +401,10 @@ class E {
|
|
|
444
401
|
*
|
|
445
402
|
* @param consumerId The unique ID of the consumer being removed
|
|
446
403
|
*/
|
|
447
|
-
|
|
448
|
-
this.
|
|
404
|
+
s(this, "_removeConsumer", (e) => {
|
|
405
|
+
this._consumers.has(e) && (this._consumers.delete(e), c.log("BlocBase._removeConsumer", this, e));
|
|
449
406
|
});
|
|
450
|
-
|
|
451
|
-
* @internal
|
|
452
|
-
* Initializes all registered addons for this Bloc instance.
|
|
453
|
-
* Calls the onInit lifecycle method on each addon if defined.
|
|
454
|
-
*/
|
|
455
|
-
n(this, "_connectAddons", () => {
|
|
456
|
-
const { _addons: t } = this;
|
|
457
|
-
t && t.forEach((e) => {
|
|
458
|
-
var s;
|
|
459
|
-
(s = e.onInit) == null || s.call(e, this);
|
|
460
|
-
});
|
|
461
|
-
});
|
|
462
|
-
n(this, "lastUpdate", Date.now());
|
|
407
|
+
s(this, "lastUpdate", Date.now());
|
|
463
408
|
/**
|
|
464
409
|
* @internal
|
|
465
410
|
* Updates the state and notifies all observers of the change.
|
|
@@ -468,12 +413,12 @@ class E {
|
|
|
468
413
|
* @param oldState The previous state for comparison
|
|
469
414
|
* @param action Optional metadata about what caused the state change
|
|
470
415
|
*/
|
|
471
|
-
|
|
472
|
-
this._state =
|
|
416
|
+
s(this, "_pushState", (e, t, n) => {
|
|
417
|
+
this._state = e, this._observer.notify(e, t, n), this.lastUpdate = Date.now();
|
|
473
418
|
});
|
|
474
|
-
this._state =
|
|
475
|
-
const
|
|
476
|
-
this._keepAlive =
|
|
419
|
+
this._state = e, this._observer = new _(this), c.log("Bloc Created", this), this._id = this.constructor.name;
|
|
420
|
+
const t = this.constructor;
|
|
421
|
+
this._keepAlive = t.keepAlive, this._isolated = t.isolated;
|
|
477
422
|
}
|
|
478
423
|
get isIsolated() {
|
|
479
424
|
return this._isolated;
|
|
@@ -501,8 +446,8 @@ class E {
|
|
|
501
446
|
* Notifies the Blac manager and clears all observers.
|
|
502
447
|
*/
|
|
503
448
|
_dispose() {
|
|
504
|
-
var
|
|
505
|
-
this._observer.
|
|
449
|
+
var e;
|
|
450
|
+
this._observer.clear(), (e = this.onDispose) == null || e.call(this), c.log("BlocBase._dispose", this), c.instance.disposeBloc(this);
|
|
506
451
|
}
|
|
507
452
|
}
|
|
508
453
|
/**
|
|
@@ -510,93 +455,47 @@ class E {
|
|
|
510
455
|
* Use this when state should not be shared between components.
|
|
511
456
|
* @default false
|
|
512
457
|
*/
|
|
513
|
-
|
|
458
|
+
s(g, "isolated", !1), /**
|
|
514
459
|
* When true, the Bloc instance persists even when there are no active consumers.
|
|
515
460
|
* Useful for maintaining state between component unmount/remount cycles.
|
|
516
461
|
* @default false
|
|
517
462
|
*/
|
|
518
|
-
|
|
519
|
-
class
|
|
520
|
-
/**
|
|
521
|
-
* Updates the current state and notifies all observers of the change.
|
|
522
|
-
* If the new state is identical to the current state (using Object.is),
|
|
523
|
-
* no update will occur.
|
|
524
|
-
* @param state - The new state to set
|
|
525
|
-
*/
|
|
526
|
-
emit(t) {
|
|
527
|
-
if (Object.is(t, this.state))
|
|
528
|
-
return;
|
|
529
|
-
const e = this.state, s = t;
|
|
530
|
-
this._pushState(s, e);
|
|
531
|
-
}
|
|
532
|
-
/**
|
|
533
|
-
* Partially updates the current state by merging it with the provided state patch.
|
|
534
|
-
* This method is only applicable when the state is an object type.
|
|
535
|
-
*
|
|
536
|
-
* @param statePatch - A partial state object containing only the properties to update
|
|
537
|
-
* @param ignoreChangeCheck - If true, skips checking if the state has actually changed
|
|
538
|
-
* @throws {TypeError} If the state is not an object type
|
|
539
|
-
*/
|
|
540
|
-
patch(t, e = !1) {
|
|
541
|
-
if (typeof this.state != "object" || this.state === null) {
|
|
542
|
-
h.warn(
|
|
543
|
-
"Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
|
|
544
|
-
);
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
let s = !1;
|
|
548
|
-
if (e)
|
|
549
|
-
s = !0;
|
|
550
|
-
else
|
|
551
|
-
for (const i in t)
|
|
552
|
-
if (Object.prototype.hasOwnProperty.call(t, i)) {
|
|
553
|
-
const o = this.state[i];
|
|
554
|
-
if (!Object.is(t[i], o)) {
|
|
555
|
-
s = !0;
|
|
556
|
-
break;
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
s && this.emit({
|
|
560
|
-
...this.state,
|
|
561
|
-
...t
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
class w extends E {
|
|
463
|
+
s(g, "keepAlive", !1);
|
|
464
|
+
class b extends g {
|
|
566
465
|
constructor() {
|
|
567
466
|
super(...arguments);
|
|
568
467
|
// Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
|
|
569
468
|
// The handler's event parameter will be correctly typed to the specific EventConstructor
|
|
570
469
|
// by the 'on' method's signature.
|
|
571
|
-
|
|
470
|
+
s(this, "eventHandlers", /* @__PURE__ */ new Map());
|
|
572
471
|
/**
|
|
573
472
|
* Dispatches an action/event to the Bloc.
|
|
574
473
|
* If a handler is registered for this specific event type (via 'on'), it will be invoked.
|
|
575
474
|
* Asynchronous handlers are awaited.
|
|
576
475
|
* @param action The action/event instance to be processed.
|
|
577
476
|
*/
|
|
578
|
-
|
|
579
|
-
const
|
|
477
|
+
s(this, "add", async (t) => {
|
|
478
|
+
const n = t.constructor, i = this.eventHandlers.get(n);
|
|
580
479
|
if (i) {
|
|
581
|
-
const a = (
|
|
582
|
-
const
|
|
583
|
-
this._pushState(
|
|
480
|
+
const a = (r) => {
|
|
481
|
+
const l = this.state;
|
|
482
|
+
this._pushState(r, l, t);
|
|
584
483
|
};
|
|
585
484
|
try {
|
|
586
|
-
await i(
|
|
587
|
-
} catch (
|
|
588
|
-
|
|
589
|
-
`[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${
|
|
590
|
-
|
|
485
|
+
await i(t, a);
|
|
486
|
+
} catch (r) {
|
|
487
|
+
c.error(
|
|
488
|
+
`[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${n.name}':`,
|
|
489
|
+
r,
|
|
591
490
|
"Action:",
|
|
592
|
-
|
|
491
|
+
t
|
|
593
492
|
);
|
|
594
493
|
}
|
|
595
494
|
} else {
|
|
596
|
-
const a =
|
|
597
|
-
|
|
495
|
+
const a = t.constructor.name || "UnnamedConstructor";
|
|
496
|
+
c.warn(
|
|
598
497
|
`[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${a}'. Action was:`,
|
|
599
|
-
|
|
498
|
+
t
|
|
600
499
|
);
|
|
601
500
|
}
|
|
602
501
|
});
|
|
@@ -608,63 +507,66 @@ class w extends E {
|
|
|
608
507
|
* @param handler A function that processes the event and can emit new states.
|
|
609
508
|
* The 'event' parameter in the handler will be typed to the specific eventConstructor.
|
|
610
509
|
*/
|
|
611
|
-
on(
|
|
612
|
-
this.eventHandlers.has(
|
|
613
|
-
`[Bloc ${this._name}:${String(this._id)}] Handler for event '${
|
|
510
|
+
on(t, n) {
|
|
511
|
+
this.eventHandlers.has(t) && c.warn(
|
|
512
|
+
`[Bloc ${this._name}:${String(this._id)}] Handler for event '${t.name}' already registered. It will be overwritten.`
|
|
614
513
|
), this.eventHandlers.set(
|
|
615
|
-
|
|
616
|
-
|
|
514
|
+
t,
|
|
515
|
+
n
|
|
617
516
|
);
|
|
618
517
|
}
|
|
619
518
|
}
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
519
|
+
class m extends g {
|
|
520
|
+
/**
|
|
521
|
+
* Updates the current state and notifies all observers of the change.
|
|
522
|
+
* If the new state is identical to the current state (using Object.is),
|
|
523
|
+
* no update will occur.
|
|
524
|
+
* @param state - The new state to set
|
|
525
|
+
*/
|
|
526
|
+
emit(e) {
|
|
527
|
+
if (Object.is(e, this.state))
|
|
528
|
+
return;
|
|
529
|
+
const t = this.state, n = e;
|
|
530
|
+
this._pushState(n, t);
|
|
628
531
|
}
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
return typeof f.v < "u" ? f.v : s;
|
|
644
|
-
} catch (u) {
|
|
645
|
-
return (d = c.onError) == null || d.call(c, u), s;
|
|
646
|
-
}
|
|
647
|
-
}, g = (l) => {
|
|
648
|
-
const d = e ?? l._id, u = o(d);
|
|
649
|
-
l._pushState(u, null);
|
|
650
|
-
};
|
|
651
|
-
let p = "";
|
|
652
|
-
return {
|
|
653
|
-
name: "Persist",
|
|
654
|
-
onInit: g,
|
|
655
|
-
onEmit: ({ newState: l, cubit: d }) => {
|
|
656
|
-
const u = e ?? d._id, f = JSON.stringify({ v: l });
|
|
657
|
-
f !== p && (I(i).setItem(a(u), f), p = f);
|
|
532
|
+
/**
|
|
533
|
+
* Partially updates the current state by merging it with the provided state patch.
|
|
534
|
+
* This method is only applicable when the state is an object type.
|
|
535
|
+
*
|
|
536
|
+
* @param statePatch - A partial state object containing only the properties to update
|
|
537
|
+
* @param ignoreChangeCheck - If true, skips checking if the state has actually changed
|
|
538
|
+
* @throws {TypeError} If the state is not an object type
|
|
539
|
+
*/
|
|
540
|
+
patch(e, t = !1) {
|
|
541
|
+
if (typeof this.state != "object" || this.state === null) {
|
|
542
|
+
c.warn(
|
|
543
|
+
"Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
|
|
544
|
+
);
|
|
545
|
+
return;
|
|
658
546
|
}
|
|
659
|
-
|
|
547
|
+
let n = !1;
|
|
548
|
+
if (t)
|
|
549
|
+
n = !0;
|
|
550
|
+
else
|
|
551
|
+
for (const i in e)
|
|
552
|
+
if (Object.prototype.hasOwnProperty.call(e, i)) {
|
|
553
|
+
const r = this.state[i];
|
|
554
|
+
if (!Object.is(e[i], r)) {
|
|
555
|
+
n = !0;
|
|
556
|
+
break;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
n && this.emit({
|
|
560
|
+
...this.state,
|
|
561
|
+
...e
|
|
562
|
+
});
|
|
563
|
+
}
|
|
660
564
|
}
|
|
661
565
|
export {
|
|
662
|
-
|
|
663
|
-
_ as
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
M as Cubit,
|
|
668
|
-
$ as Persist
|
|
566
|
+
c as Blac,
|
|
567
|
+
_ as BlacObservable,
|
|
568
|
+
b as Bloc,
|
|
569
|
+
g as BlocBase,
|
|
570
|
+
m as Cubit
|
|
669
571
|
};
|
|
670
572
|
//# sourceMappingURL=index.es.js.map
|