@blac/core 2.0.0-rc-1 → 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 +10 -101
- package/dist/index.es.js +217 -316
- package/dist/index.es.js.map +1 -1
- package/package.json +1 -1
- package/src/Blac.ts +41 -108
- package/src/BlacObserver.ts +17 -12
- package/src/BlocBase.ts +14 -33
- package/src/index.ts +2 -3
- package/src/types.ts +1 -1
- 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,33 +214,32 @@ 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
|
-
|
|
274
|
-
this.bloc = t;
|
|
233
|
+
s(this, "bloc");
|
|
234
|
+
s(this, "_observers", /* @__PURE__ */ new Set());
|
|
235
|
+
this.bloc = e;
|
|
275
236
|
}
|
|
276
237
|
/**
|
|
277
238
|
* Gets the number of active observers
|
|
278
239
|
* @returns The number of observers currently subscribed
|
|
279
240
|
*/
|
|
280
241
|
get size() {
|
|
281
|
-
return this._observers.
|
|
242
|
+
return this._observers.size;
|
|
282
243
|
}
|
|
283
244
|
/**
|
|
284
245
|
* Gets the set of all observers
|
|
@@ -292,17 +253,17 @@ class D {
|
|
|
292
253
|
* @param observer - The observer to subscribe
|
|
293
254
|
* @returns A function that can be called to unsubscribe the observer
|
|
294
255
|
*/
|
|
295
|
-
subscribe(
|
|
296
|
-
return
|
|
297
|
-
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);
|
|
298
259
|
};
|
|
299
260
|
}
|
|
300
261
|
/**
|
|
301
262
|
* Unsubscribes an observer from state changes
|
|
302
263
|
* @param observer - The observer to unsubscribe
|
|
303
264
|
*/
|
|
304
|
-
unsubscribe(
|
|
305
|
-
this._observers
|
|
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,103 +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
|
-
|
|
338
|
-
|
|
339
|
-
}), this._observers = [];
|
|
296
|
+
clear() {
|
|
297
|
+
this._observers.forEach((e) => {
|
|
298
|
+
this.unsubscribe(e);
|
|
299
|
+
}), this._observers.clear();
|
|
340
300
|
}
|
|
341
301
|
}
|
|
342
|
-
class
|
|
302
|
+
class g {
|
|
343
303
|
/**
|
|
344
304
|
* Creates a new BlocBase instance with the given initial state.
|
|
345
305
|
* Sets up the observer, registers with the Blac manager, and initializes addons.
|
|
346
306
|
*
|
|
347
307
|
* @param initialState The initial state value for this Bloc
|
|
348
308
|
*/
|
|
349
|
-
constructor(
|
|
309
|
+
constructor(e) {
|
|
310
|
+
s(this, "uid", crypto.randomUUID());
|
|
350
311
|
/**
|
|
351
312
|
* Defines how dependencies are selected from the state for efficient updates.
|
|
352
313
|
* When provided, observers will only be notified when selected dependencies change.
|
|
353
314
|
*/
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* @internal
|
|
357
|
-
* Optional array of addons to extend the functionality of this Bloc.
|
|
358
|
-
*/
|
|
359
|
-
n(this, "_addons");
|
|
315
|
+
s(this, "defaultDependencySelector");
|
|
360
316
|
/**
|
|
361
317
|
* @internal
|
|
362
318
|
* Indicates if this specific Bloc instance is isolated from others of the same type.
|
|
363
319
|
*/
|
|
364
|
-
|
|
320
|
+
s(this, "_isolated", !1);
|
|
365
321
|
/**
|
|
366
322
|
* @internal
|
|
367
323
|
* Observable responsible for managing state listeners and notifying consumers.
|
|
368
324
|
*/
|
|
369
|
-
|
|
325
|
+
s(this, "_observer");
|
|
370
326
|
/**
|
|
371
327
|
* @internal
|
|
372
328
|
* Reference to the global Blac manager instance.
|
|
373
329
|
*/
|
|
374
|
-
|
|
330
|
+
s(this, "_blac", c.getInstance());
|
|
375
331
|
/**
|
|
376
332
|
* The unique identifier for this Bloc instance.
|
|
377
333
|
* Defaults to the class name, but can be customized.
|
|
378
334
|
*/
|
|
379
|
-
|
|
335
|
+
s(this, "_id");
|
|
380
336
|
/**
|
|
381
337
|
* @internal
|
|
382
338
|
* Reference string used internally for tracking and debugging.
|
|
383
339
|
*/
|
|
384
|
-
|
|
340
|
+
s(this, "_instanceRef");
|
|
385
341
|
/**
|
|
386
342
|
* @internal
|
|
387
343
|
* Indicates if this specific Bloc instance should be kept alive when no consumers are present.
|
|
388
344
|
*/
|
|
389
|
-
|
|
345
|
+
s(this, "_keepAlive", !1);
|
|
390
346
|
/**
|
|
391
347
|
* @readonly
|
|
392
348
|
* Timestamp when this Bloc instance was created, useful for debugging and performance tracking.
|
|
393
349
|
*/
|
|
394
|
-
|
|
350
|
+
s(this, "_createdAt", Date.now());
|
|
395
351
|
/**
|
|
396
352
|
* @internal
|
|
397
353
|
* The current state of the Bloc.
|
|
398
354
|
*/
|
|
399
|
-
|
|
355
|
+
s(this, "_state");
|
|
400
356
|
/**
|
|
401
357
|
* @internal
|
|
402
358
|
* The previous state of the Bloc, maintained for comparison and history.
|
|
403
359
|
*/
|
|
404
|
-
|
|
360
|
+
s(this, "_oldState");
|
|
405
361
|
/**
|
|
406
362
|
* Props passed during Bloc instance creation.
|
|
407
363
|
* Can be used to configure or parameterize the Bloc's behavior.
|
|
408
364
|
*/
|
|
409
|
-
|
|
365
|
+
s(this, "props", null);
|
|
410
366
|
/**
|
|
411
367
|
* @internal
|
|
412
368
|
* Updates the Bloc instance's ID to a new value.
|
|
@@ -414,20 +370,20 @@ class E {
|
|
|
414
370
|
*
|
|
415
371
|
* @param id The new ID to assign to this Bloc instance
|
|
416
372
|
*/
|
|
417
|
-
|
|
418
|
-
const
|
|
419
|
-
!
|
|
373
|
+
s(this, "_updateId", (e) => {
|
|
374
|
+
const t = this._id;
|
|
375
|
+
!e || e === t || (this._id = e);
|
|
420
376
|
});
|
|
421
377
|
/**
|
|
422
378
|
* @internal
|
|
423
379
|
* Optional function to be called when the Bloc is disposed.
|
|
424
380
|
*/
|
|
425
|
-
|
|
381
|
+
s(this, "onDispose");
|
|
426
382
|
/**
|
|
427
383
|
* @internal
|
|
428
384
|
* Set of consumer IDs currently listening to this Bloc's state changes.
|
|
429
385
|
*/
|
|
430
|
-
|
|
386
|
+
s(this, "_consumers", /* @__PURE__ */ new Set());
|
|
431
387
|
/**
|
|
432
388
|
* @internal
|
|
433
389
|
* Registers a new consumer to this Bloc instance.
|
|
@@ -435,8 +391,8 @@ class E {
|
|
|
435
391
|
*
|
|
436
392
|
* @param consumerId The unique ID of the consumer being added
|
|
437
393
|
*/
|
|
438
|
-
|
|
439
|
-
this._consumers.
|
|
394
|
+
s(this, "_addConsumer", (e) => {
|
|
395
|
+
this._consumers.has(e) || (this._consumers.add(e), c.log("BlocBase._addConsumer", this, e));
|
|
440
396
|
});
|
|
441
397
|
/**
|
|
442
398
|
* @internal
|
|
@@ -445,22 +401,10 @@ class E {
|
|
|
445
401
|
*
|
|
446
402
|
* @param consumerId The unique ID of the consumer being removed
|
|
447
403
|
*/
|
|
448
|
-
|
|
449
|
-
this.
|
|
450
|
-
});
|
|
451
|
-
/**
|
|
452
|
-
* @internal
|
|
453
|
-
* Initializes all registered addons for this Bloc instance.
|
|
454
|
-
* Calls the onInit lifecycle method on each addon if defined.
|
|
455
|
-
*/
|
|
456
|
-
n(this, "_connectAddons", () => {
|
|
457
|
-
const { _addons: t } = this;
|
|
458
|
-
t && t.forEach((e) => {
|
|
459
|
-
var s;
|
|
460
|
-
(s = e.onInit) == null || s.call(e, this);
|
|
461
|
-
});
|
|
404
|
+
s(this, "_removeConsumer", (e) => {
|
|
405
|
+
this._consumers.has(e) && (this._consumers.delete(e), c.log("BlocBase._removeConsumer", this, e));
|
|
462
406
|
});
|
|
463
|
-
|
|
407
|
+
s(this, "lastUpdate", Date.now());
|
|
464
408
|
/**
|
|
465
409
|
* @internal
|
|
466
410
|
* Updates the state and notifies all observers of the change.
|
|
@@ -469,12 +413,12 @@ class E {
|
|
|
469
413
|
* @param oldState The previous state for comparison
|
|
470
414
|
* @param action Optional metadata about what caused the state change
|
|
471
415
|
*/
|
|
472
|
-
|
|
473
|
-
this._state =
|
|
416
|
+
s(this, "_pushState", (e, t, n) => {
|
|
417
|
+
this._state = e, this._observer.notify(e, t, n), this.lastUpdate = Date.now();
|
|
474
418
|
});
|
|
475
|
-
this._state =
|
|
476
|
-
const
|
|
477
|
-
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;
|
|
478
422
|
}
|
|
479
423
|
get isIsolated() {
|
|
480
424
|
return this._isolated;
|
|
@@ -502,8 +446,8 @@ class E {
|
|
|
502
446
|
* Notifies the Blac manager and clears all observers.
|
|
503
447
|
*/
|
|
504
448
|
_dispose() {
|
|
505
|
-
var
|
|
506
|
-
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);
|
|
507
451
|
}
|
|
508
452
|
}
|
|
509
453
|
/**
|
|
@@ -511,93 +455,47 @@ class E {
|
|
|
511
455
|
* Use this when state should not be shared between components.
|
|
512
456
|
* @default false
|
|
513
457
|
*/
|
|
514
|
-
|
|
458
|
+
s(g, "isolated", !1), /**
|
|
515
459
|
* When true, the Bloc instance persists even when there are no active consumers.
|
|
516
460
|
* Useful for maintaining state between component unmount/remount cycles.
|
|
517
461
|
* @default false
|
|
518
462
|
*/
|
|
519
|
-
|
|
520
|
-
class
|
|
521
|
-
/**
|
|
522
|
-
* Updates the current state and notifies all observers of the change.
|
|
523
|
-
* If the new state is identical to the current state (using Object.is),
|
|
524
|
-
* no update will occur.
|
|
525
|
-
* @param state - The new state to set
|
|
526
|
-
*/
|
|
527
|
-
emit(t) {
|
|
528
|
-
if (Object.is(t, this.state))
|
|
529
|
-
return;
|
|
530
|
-
const e = this.state, s = t;
|
|
531
|
-
this._pushState(s, e);
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* Partially updates the current state by merging it with the provided state patch.
|
|
535
|
-
* This method is only applicable when the state is an object type.
|
|
536
|
-
*
|
|
537
|
-
* @param statePatch - A partial state object containing only the properties to update
|
|
538
|
-
* @param ignoreChangeCheck - If true, skips checking if the state has actually changed
|
|
539
|
-
* @throws {TypeError} If the state is not an object type
|
|
540
|
-
*/
|
|
541
|
-
patch(t, e = !1) {
|
|
542
|
-
if (typeof this.state != "object" || this.state === null) {
|
|
543
|
-
h.warn(
|
|
544
|
-
"Cubit.patch: was called on a cubit where the state is not an object. This is a no-op."
|
|
545
|
-
);
|
|
546
|
-
return;
|
|
547
|
-
}
|
|
548
|
-
let s = !1;
|
|
549
|
-
if (e)
|
|
550
|
-
s = !0;
|
|
551
|
-
else
|
|
552
|
-
for (const i in t)
|
|
553
|
-
if (Object.prototype.hasOwnProperty.call(t, i)) {
|
|
554
|
-
const o = this.state[i];
|
|
555
|
-
if (!Object.is(t[i], o)) {
|
|
556
|
-
s = !0;
|
|
557
|
-
break;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
s && this.emit({
|
|
561
|
-
...this.state,
|
|
562
|
-
...t
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
class w extends E {
|
|
463
|
+
s(g, "keepAlive", !1);
|
|
464
|
+
class b extends g {
|
|
567
465
|
constructor() {
|
|
568
466
|
super(...arguments);
|
|
569
467
|
// Stores handlers: Map<EventConstructor (subtype of A), HandlerFunction>
|
|
570
468
|
// The handler's event parameter will be correctly typed to the specific EventConstructor
|
|
571
469
|
// by the 'on' method's signature.
|
|
572
|
-
|
|
470
|
+
s(this, "eventHandlers", /* @__PURE__ */ new Map());
|
|
573
471
|
/**
|
|
574
472
|
* Dispatches an action/event to the Bloc.
|
|
575
473
|
* If a handler is registered for this specific event type (via 'on'), it will be invoked.
|
|
576
474
|
* Asynchronous handlers are awaited.
|
|
577
475
|
* @param action The action/event instance to be processed.
|
|
578
476
|
*/
|
|
579
|
-
|
|
580
|
-
const
|
|
477
|
+
s(this, "add", async (t) => {
|
|
478
|
+
const n = t.constructor, i = this.eventHandlers.get(n);
|
|
581
479
|
if (i) {
|
|
582
|
-
const a = (
|
|
583
|
-
const
|
|
584
|
-
this._pushState(
|
|
480
|
+
const a = (r) => {
|
|
481
|
+
const l = this.state;
|
|
482
|
+
this._pushState(r, l, t);
|
|
585
483
|
};
|
|
586
484
|
try {
|
|
587
|
-
await i(
|
|
588
|
-
} catch (
|
|
589
|
-
|
|
590
|
-
`[Bloc ${this._name}:${String(this._id)}] Error in event handler for '${
|
|
591
|
-
|
|
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,
|
|
592
490
|
"Action:",
|
|
593
|
-
|
|
491
|
+
t
|
|
594
492
|
);
|
|
595
493
|
}
|
|
596
494
|
} else {
|
|
597
|
-
const a =
|
|
598
|
-
|
|
495
|
+
const a = t.constructor.name || "UnnamedConstructor";
|
|
496
|
+
c.warn(
|
|
599
497
|
`[Bloc ${this._name}:${String(this._id)}] No handler registered for action type: '${a}'. Action was:`,
|
|
600
|
-
|
|
498
|
+
t
|
|
601
499
|
);
|
|
602
500
|
}
|
|
603
501
|
});
|
|
@@ -609,63 +507,66 @@ class w extends E {
|
|
|
609
507
|
* @param handler A function that processes the event and can emit new states.
|
|
610
508
|
* The 'event' parameter in the handler will be typed to the specific eventConstructor.
|
|
611
509
|
*/
|
|
612
|
-
on(
|
|
613
|
-
this.eventHandlers.has(
|
|
614
|
-
`[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.`
|
|
615
513
|
), this.eventHandlers.set(
|
|
616
|
-
|
|
617
|
-
|
|
514
|
+
t,
|
|
515
|
+
n
|
|
618
516
|
);
|
|
619
517
|
}
|
|
620
518
|
}
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
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);
|
|
629
531
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
return typeof f.v < "u" ? f.v : s;
|
|
645
|
-
} catch (u) {
|
|
646
|
-
return (l = c.onError) == null || l.call(c, u), s;
|
|
647
|
-
}
|
|
648
|
-
}, g = (d) => {
|
|
649
|
-
const l = e ?? d._id, u = o(l);
|
|
650
|
-
d._pushState(u, null);
|
|
651
|
-
};
|
|
652
|
-
let p = "";
|
|
653
|
-
return {
|
|
654
|
-
name: "Persist",
|
|
655
|
-
onInit: g,
|
|
656
|
-
onEmit: ({ newState: d, cubit: l }) => {
|
|
657
|
-
const u = e ?? l._id, f = JSON.stringify({ v: d });
|
|
658
|
-
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;
|
|
659
546
|
}
|
|
660
|
-
|
|
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
|
+
}
|
|
661
564
|
}
|
|
662
565
|
export {
|
|
663
|
-
|
|
664
|
-
_ as
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
M as Cubit,
|
|
669
|
-
$ as Persist
|
|
566
|
+
c as Blac,
|
|
567
|
+
_ as BlacObservable,
|
|
568
|
+
b as Bloc,
|
|
569
|
+
g as BlocBase,
|
|
570
|
+
m as Cubit
|
|
670
571
|
};
|
|
671
572
|
//# sourceMappingURL=index.es.js.map
|