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