@blac/core 2.0.0-rc.15 → 2.0.0-rc.16
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/dist/index.cjs +128 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +161 -42
- package/dist/index.d.ts +161 -42
- package/dist/index.js +125 -26
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1238,33 +1238,70 @@ var Cubit = class extends StateContainer {
|
|
|
1238
1238
|
//#endregion
|
|
1239
1239
|
//#region src/core/Vertex.ts
|
|
1240
1240
|
/**
|
|
1241
|
-
* Event-driven state container
|
|
1242
|
-
*
|
|
1241
|
+
* Event-driven state container using discriminated union events.
|
|
1242
|
+
* Extends StateContainer with event-based state transitions and
|
|
1243
|
+
* compile-time exhaustive checking for event handlers.
|
|
1243
1244
|
*
|
|
1244
|
-
* @template S - State type
|
|
1245
|
-
* @template E - Event type (
|
|
1245
|
+
* @template S - State type (must be an object)
|
|
1246
|
+
* @template E - Event union type (discriminated by 'type' property)
|
|
1246
1247
|
* @template P - Props type (optional)
|
|
1248
|
+
*
|
|
1249
|
+
* @example
|
|
1250
|
+
* ```typescript
|
|
1251
|
+
* type CounterEvent =
|
|
1252
|
+
* | { type: 'increment'; amount: number }
|
|
1253
|
+
* | { type: 'decrement' }
|
|
1254
|
+
* | { type: 'reset' };
|
|
1255
|
+
*
|
|
1256
|
+
* class CounterVertex extends Vertex<{ count: number }, CounterEvent> {
|
|
1257
|
+
* constructor() {
|
|
1258
|
+
* super({ count: 0 });
|
|
1259
|
+
* this.createHandlers({
|
|
1260
|
+
* increment: (event, emit) => {
|
|
1261
|
+
* emit({ count: this.state.count + event.amount });
|
|
1262
|
+
* },
|
|
1263
|
+
* decrement: (_, emit) => {
|
|
1264
|
+
* emit({ count: this.state.count - 1 });
|
|
1265
|
+
* },
|
|
1266
|
+
* reset: (_, emit) => {
|
|
1267
|
+
* emit({ count: 0 });
|
|
1268
|
+
* },
|
|
1269
|
+
* });
|
|
1270
|
+
* }
|
|
1271
|
+
*
|
|
1272
|
+
* increment = (amount = 1) => this.add({ type: 'increment', amount });
|
|
1273
|
+
* }
|
|
1274
|
+
* ```
|
|
1247
1275
|
*/
|
|
1248
1276
|
var Vertex = class extends StateContainer {
|
|
1249
1277
|
constructor(initialState) {
|
|
1250
1278
|
super(initialState);
|
|
1251
|
-
this.
|
|
1279
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
1252
1280
|
this.isProcessing = false;
|
|
1253
1281
|
this.eventQueue = [];
|
|
1254
|
-
this.on = (EventClass, handler) => {
|
|
1255
|
-
const className = EventClass.name;
|
|
1256
|
-
const registrations = this.eventHandlers.get(className) || [];
|
|
1257
|
-
registrations.push({
|
|
1258
|
-
eventClass: EventClass,
|
|
1259
|
-
handler
|
|
1260
|
-
});
|
|
1261
|
-
this.eventHandlers.set(className, registrations);
|
|
1262
|
-
};
|
|
1263
1282
|
this.add = (event) => {
|
|
1264
|
-
|
|
1265
|
-
|
|
1283
|
+
const enrichedEvent = this.enrichEvent(event);
|
|
1284
|
+
StateContainer._registry.emit("eventAdded", this, enrichedEvent);
|
|
1285
|
+
this.processEvent(enrichedEvent);
|
|
1266
1286
|
};
|
|
1267
1287
|
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Register all event handlers with exhaustive type checking.
|
|
1290
|
+
* TypeScript will error if any event type from the union is missing.
|
|
1291
|
+
*
|
|
1292
|
+
* @param handlers - Map of event type to handler function
|
|
1293
|
+
*/
|
|
1294
|
+
createHandlers(handlers) {
|
|
1295
|
+
for (const [eventType, handler] of Object.entries(handlers)) this.handlers.set(eventType, handler);
|
|
1296
|
+
}
|
|
1297
|
+
/** @internal */
|
|
1298
|
+
enrichEvent(event) {
|
|
1299
|
+
return {
|
|
1300
|
+
...event,
|
|
1301
|
+
timestamp: Date.now()
|
|
1302
|
+
};
|
|
1303
|
+
}
|
|
1304
|
+
/** @internal */
|
|
1268
1305
|
processEvent(event) {
|
|
1269
1306
|
if (this.isProcessing) {
|
|
1270
1307
|
this.eventQueue.push(event);
|
|
@@ -1281,26 +1318,27 @@ var Vertex = class extends StateContainer {
|
|
|
1281
1318
|
this.isProcessing = false;
|
|
1282
1319
|
}
|
|
1283
1320
|
}
|
|
1321
|
+
/** @internal */
|
|
1284
1322
|
handleEvent(event) {
|
|
1285
|
-
const
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
if (this.debug) console.warn(`No handler registered for event: ${className}`);
|
|
1323
|
+
const handler = this.handlers.get(event.type);
|
|
1324
|
+
if (!handler) {
|
|
1325
|
+
if (this.debug) console.warn(`No handler registered for event type: ${event.type}`);
|
|
1289
1326
|
return;
|
|
1290
1327
|
}
|
|
1291
1328
|
const emitFn = (state) => {
|
|
1292
1329
|
this.emit(state);
|
|
1293
1330
|
};
|
|
1294
|
-
|
|
1295
|
-
|
|
1331
|
+
try {
|
|
1332
|
+
handler(event, emitFn);
|
|
1296
1333
|
} catch (error$1) {
|
|
1297
|
-
if (this.debug) console.error(`Error handling event ${
|
|
1334
|
+
if (this.debug) console.error(`Error handling event ${event.type}:`, error$1);
|
|
1298
1335
|
this.onEventError(event, error$1);
|
|
1299
1336
|
}
|
|
1300
1337
|
}
|
|
1301
1338
|
/**
|
|
1302
|
-
*
|
|
1303
|
-
* Override
|
|
1339
|
+
* Called when an error occurs during event processing.
|
|
1340
|
+
* Override to implement custom error handling.
|
|
1341
|
+
*
|
|
1304
1342
|
* @param _event - The event that caused the error
|
|
1305
1343
|
* @param _error - The error that occurred
|
|
1306
1344
|
*/
|
|
@@ -2038,6 +2076,67 @@ function configureLogger(opts) {
|
|
|
2038
2076
|
defaultLogger = createLogger(currentConfig);
|
|
2039
2077
|
}
|
|
2040
2078
|
|
|
2079
|
+
//#endregion
|
|
2080
|
+
//#region src/config.ts
|
|
2081
|
+
const defaultConfig = {
|
|
2082
|
+
devMode: process.env.NODE_ENV !== "production",
|
|
2083
|
+
logger: {
|
|
2084
|
+
enabled: false,
|
|
2085
|
+
level: LogLevel.INFO
|
|
2086
|
+
}
|
|
2087
|
+
};
|
|
2088
|
+
let globalConfig = { ...defaultConfig };
|
|
2089
|
+
/**
|
|
2090
|
+
* Configure global defaults for @blac/core.
|
|
2091
|
+
*
|
|
2092
|
+
* @example
|
|
2093
|
+
* ```ts
|
|
2094
|
+
* import { configureBlac, LogLevel } from '@blac/core';
|
|
2095
|
+
*
|
|
2096
|
+
* configureBlac({
|
|
2097
|
+
* devMode: true,
|
|
2098
|
+
* logger: {
|
|
2099
|
+
* enabled: true,
|
|
2100
|
+
* level: LogLevel.DEBUG
|
|
2101
|
+
* }
|
|
2102
|
+
* });
|
|
2103
|
+
* ```
|
|
2104
|
+
*
|
|
2105
|
+
* @param config - Partial configuration to merge with defaults
|
|
2106
|
+
*/
|
|
2107
|
+
function configureBlac(config) {
|
|
2108
|
+
globalConfig = {
|
|
2109
|
+
...globalConfig,
|
|
2110
|
+
...config,
|
|
2111
|
+
logger: {
|
|
2112
|
+
...globalConfig.logger,
|
|
2113
|
+
...config.logger
|
|
2114
|
+
}
|
|
2115
|
+
};
|
|
2116
|
+
if (config.logger) configureLogger(config.logger);
|
|
2117
|
+
}
|
|
2118
|
+
/**
|
|
2119
|
+
* Get the current global configuration.
|
|
2120
|
+
* @internal
|
|
2121
|
+
*/
|
|
2122
|
+
function getBlacConfig() {
|
|
2123
|
+
return globalConfig;
|
|
2124
|
+
}
|
|
2125
|
+
/**
|
|
2126
|
+
* Check if development mode is enabled.
|
|
2127
|
+
*/
|
|
2128
|
+
function isDevMode() {
|
|
2129
|
+
return globalConfig.devMode;
|
|
2130
|
+
}
|
|
2131
|
+
/**
|
|
2132
|
+
* Reset configuration to defaults (useful for testing).
|
|
2133
|
+
* @internal
|
|
2134
|
+
*/
|
|
2135
|
+
function resetBlacConfig() {
|
|
2136
|
+
globalConfig = { ...defaultConfig };
|
|
2137
|
+
configureLogger(defaultConfig.logger);
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2041
2140
|
//#endregion
|
|
2042
2141
|
//#region src/decorators/blac.ts
|
|
2043
2142
|
/**
|
|
@@ -2089,6 +2188,7 @@ exports.captureTrackedPaths = captureTrackedPaths;
|
|
|
2089
2188
|
exports.clearActiveTracker = clearActiveTracker;
|
|
2090
2189
|
exports.clearExternalDependencies = clearExternalDependencies;
|
|
2091
2190
|
exports.commitTrackedGetters = commitTrackedGetters;
|
|
2191
|
+
exports.configureBlac = configureBlac;
|
|
2092
2192
|
exports.configureLogger = configureLogger;
|
|
2093
2193
|
exports.createArrayProxy = createArrayProxy;
|
|
2094
2194
|
exports.createAutoTrackSnapshot = createAutoTrackSnapshot;
|
|
@@ -2114,6 +2214,7 @@ exports.generateId = generateId;
|
|
|
2114
2214
|
exports.generateIsolatedKey = generateIsolatedKey;
|
|
2115
2215
|
exports.generateSimpleId = generateSimpleId;
|
|
2116
2216
|
exports.getActiveTracker = getActiveTracker;
|
|
2217
|
+
exports.getBlacConfig = getBlacConfig;
|
|
2117
2218
|
exports.getDescriptor = getDescriptor;
|
|
2118
2219
|
exports.getGetterExecutionContext = getGetterExecutionContext;
|
|
2119
2220
|
exports.getPluginManager = getPluginManager;
|
|
@@ -2129,6 +2230,7 @@ exports.initAutoTrackState = initAutoTrackState;
|
|
|
2129
2230
|
exports.initManualDepsState = initManualDepsState;
|
|
2130
2231
|
exports.initNoTrackState = initNoTrackState;
|
|
2131
2232
|
exports.invalidateRenderCache = invalidateRenderCache;
|
|
2233
|
+
exports.isDevMode = isDevMode;
|
|
2132
2234
|
exports.isExcludedFromDevTools = isExcludedFromDevTools;
|
|
2133
2235
|
exports.isGetter = isGetter;
|
|
2134
2236
|
exports.isIsolatedClass = isIsolatedClass;
|
|
@@ -2136,6 +2238,7 @@ exports.isIsolatedKey = isIsolatedKey;
|
|
|
2136
2238
|
exports.isKeepAliveClass = isKeepAliveClass;
|
|
2137
2239
|
exports.isProxyable = isProxyable;
|
|
2138
2240
|
exports.parsePath = parsePath;
|
|
2241
|
+
exports.resetBlacConfig = resetBlacConfig;
|
|
2139
2242
|
exports.resetGetterTracker = resetGetterTracker;
|
|
2140
2243
|
exports.setActiveTracker = setActiveTracker;
|
|
2141
2244
|
exports.shallowEqual = shallowEqual;
|