energy-visualization-sankey 1.0.26 → 1.1.0
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 +91 -0
- package/babel.config.cjs +18 -21
- package/dist/.tsbuildinfo +1 -0
- package/dist/sankey.esm.js +285 -123
- package/dist/sankey.esm.js.map +1 -1
- package/dist/sankey.umd.js +285 -123
- package/dist/sankey.umd.js.map +1 -1
- package/dist/types/core/Sankey.d.ts +60 -46
- package/dist/types/core/Sankey.d.ts.map +1 -1
- package/dist/types/core/types/public-events.d.ts +159 -0
- package/dist/types/core/types/public-events.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/AnimationService.d.ts.map +1 -1
- package/dist/types/services/InteractionService.d.ts.map +1 -1
- package/dist/types/services/data/DataValidationService.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +11 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/jest.config.cjs +22 -7
- package/package.json +3 -2
- package/src/core/Sankey.ts +282 -21
- package/src/core/types/public-events.ts +189 -0
- package/src/index.ts +21 -0
- package/src/services/AnimationService.ts +34 -19
- package/src/services/InteractionService.ts +49 -31
- package/src/services/data/DataValidationService.ts +8 -5
- package/src/types/index.ts +16 -0
- package/tests/event-bus.test.ts +137 -0
- package/tests/sankey-events.test.ts +289 -0
- package/tests/setup.js +13 -48
package/dist/sankey.umd.js
CHANGED
|
@@ -5020,15 +5020,16 @@
|
|
|
5020
5020
|
// Emit validation error event for error handling - Error Recovery Architecture
|
|
5021
5021
|
// Enables centralized error handling and user notification systems
|
|
5022
5022
|
// Marks validation errors as non-recoverable (require data fix)
|
|
5023
|
+
const validationErrorData = {
|
|
5024
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
5025
|
+
context: 'data_validation', // Error categorization for debugging
|
|
5026
|
+
recoverable: false // Validation errors require data correction
|
|
5027
|
+
};
|
|
5023
5028
|
this.eventBus.emit({
|
|
5024
5029
|
type: 'system.error',
|
|
5025
5030
|
timestamp: Date.now(),
|
|
5026
5031
|
source: 'DataValidationService',
|
|
5027
|
-
data:
|
|
5028
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
5029
|
-
context: 'data_validation', // Error categorization for debugging
|
|
5030
|
-
recoverable: false // Validation errors require data correction
|
|
5031
|
-
}
|
|
5032
|
+
data: validationErrorData
|
|
5032
5033
|
});
|
|
5033
5034
|
// Re-throw error to maintain validation contract - Fail-Fast Pattern
|
|
5034
5035
|
// Prevents invalid data from propagating to calculation services
|
|
@@ -6678,52 +6679,6 @@
|
|
|
6678
6679
|
}
|
|
6679
6680
|
}
|
|
6680
6681
|
|
|
6681
|
-
/**
|
|
6682
|
-
* Main Sankey Visualization Class - Event-Driven Architecture Orchestrator
|
|
6683
|
-
*
|
|
6684
|
-
* ARCHITECTURE PATTERN: Service Orchestration with Event-Driven Communication
|
|
6685
|
-
*
|
|
6686
|
-
* This class implements the Orchestrator pattern from Clean Architecture,
|
|
6687
|
-
* coordinating between focused, single-responsibility services through
|
|
6688
|
-
* a type-safe event bus. It acts as the application boundary, managing
|
|
6689
|
-
* the complete lifecycle from initialization to destruction.
|
|
6690
|
-
*
|
|
6691
|
-
* SERVICE COMPOSITION ARCHITECTURE:
|
|
6692
|
-
* 1. Infrastructure Layer: ConfigurationService (mathematical constants)
|
|
6693
|
-
* 2. Data Layer: DataService, ValidationService, TransformService
|
|
6694
|
-
* 3. Calculation Layer: SummaryService, GraphService, PositionService
|
|
6695
|
-
* 4. Presentation Layer: RenderingService, AnimationService
|
|
6696
|
-
* 5. Interaction Layer: InteractionService, TooltipService
|
|
6697
|
-
*
|
|
6698
|
-
* DEPENDENCY INJECTION PATTERN:
|
|
6699
|
-
* - Constructor Injection: Services receive dependencies via constructor
|
|
6700
|
-
* - Service Locator: Services are registered in central container
|
|
6701
|
-
* - Event Bus Mediation: Services communicate without direct coupling
|
|
6702
|
-
* - Lifecycle Management: Services created in dependency order
|
|
6703
|
-
*
|
|
6704
|
-
* EVENT-DRIVEN COMMUNICATION BENEFITS:
|
|
6705
|
-
* - Loose Coupling: Services don't hold references to each other
|
|
6706
|
-
* - Testability: Services can be mocked and tested in isolation
|
|
6707
|
-
* - Maintainability: Changes to one service don't affect others
|
|
6708
|
-
* - Performance: Async event dispatch prevents blocking operations
|
|
6709
|
-
*
|
|
6710
|
-
* PUBLIC API COMPATIBILITY:
|
|
6711
|
-
* Maintains complete API compatibility with previous versions while
|
|
6712
|
-
* providing enhanced functionality including configurable animation
|
|
6713
|
-
* looping, improved error handling, and comprehensive performance monitoring.
|
|
6714
|
-
*
|
|
6715
|
-
* Usage Example:
|
|
6716
|
-
* ```typescript
|
|
6717
|
-
* const sankey = new SankeyVisualization('container', {
|
|
6718
|
-
* data: energyData,
|
|
6719
|
-
* includeControls: true,
|
|
6720
|
-
* loopAnimation: false
|
|
6721
|
-
* });
|
|
6722
|
-
*
|
|
6723
|
-
* // Chain-able API maintained for compatibility
|
|
6724
|
-
* sankey.play().setSpeed(100).setYear(2020);
|
|
6725
|
-
* ```
|
|
6726
|
-
*/
|
|
6727
6682
|
class Sankey {
|
|
6728
6683
|
constructor(containerId, options) {
|
|
6729
6684
|
// SERVICE DEPENDENCY CONTAINER: Holds all service instances after creation
|
|
@@ -6738,6 +6693,10 @@
|
|
|
6738
6693
|
// Critical for memory leak prevention in long-running applications
|
|
6739
6694
|
// Each subscription must be explicitly unsubscribed during destroy()
|
|
6740
6695
|
this.subscriptions = [];
|
|
6696
|
+
// PUBLIC SUBSCRIPTION REGISTRY: consumer-facing on()/once() registrations.
|
|
6697
|
+
// Kept separate from `subscriptions` (internal service wiring) so that off()
|
|
6698
|
+
// and destroy() can reason about the two independently.
|
|
6699
|
+
this.publicListeners = [];
|
|
6741
6700
|
this.logger = new Logger(options);
|
|
6742
6701
|
// Initialize core system components
|
|
6743
6702
|
// 1. Validate inputs using comprehensive validation logic
|
|
@@ -6749,9 +6708,23 @@
|
|
|
6749
6708
|
this.wasteHeatVisible = this.options.showWasteHeat !== false;
|
|
6750
6709
|
// 4. Create core event bus
|
|
6751
6710
|
this.eventBus = new EventBus(this.logger);
|
|
6752
|
-
// 5.
|
|
6711
|
+
// 5. Arm the readiness latch before any asynchronous work starts.
|
|
6712
|
+
// The no-op catch is REQUIRED: without it, a failed initialization
|
|
6713
|
+
// produces a fresh unhandled rejection for every caller that never
|
|
6714
|
+
// uses whenReady(), which would be a behaviour change.
|
|
6715
|
+
this.readyPromise = new Promise((resolve, reject) => {
|
|
6716
|
+
this.resolveReady = resolve;
|
|
6717
|
+
this.rejectReady = reject;
|
|
6718
|
+
});
|
|
6719
|
+
this.readyPromise.catch(() => {
|
|
6720
|
+
});
|
|
6721
|
+
// 6. Setup system event listeners
|
|
6753
6722
|
this.setupSystemEventListeners();
|
|
6754
|
-
//
|
|
6723
|
+
// 7. Register handlers supplied through the `events` option. MUST happen
|
|
6724
|
+
// before initialize(): the event bus drops events that have no
|
|
6725
|
+
// subscriber at emit time and there is no replay.
|
|
6726
|
+
this.registerConfiguredEventHandlers();
|
|
6727
|
+
// 8. Initialize services and visualization
|
|
6755
6728
|
this.initialize();
|
|
6756
6729
|
}
|
|
6757
6730
|
/**
|
|
@@ -6826,31 +6799,54 @@
|
|
|
6826
6799
|
// Must be created first as other services depend on configuration constants
|
|
6827
6800
|
this.logger.log('SankeyVisualization: Creating infrastructure services...');
|
|
6828
6801
|
await this.createConfigurationService();
|
|
6829
|
-
//
|
|
6802
|
+
// CANCELLATION CHECK: destroy() empties the service container, so a
|
|
6803
|
+
// destroy() issued while initialization is still in flight would
|
|
6804
|
+
// otherwise leave the remaining layers reading undefined services and
|
|
6805
|
+
// throwing asynchronously, where nothing can catch it. Each await is
|
|
6806
|
+
// a suspension point at which that can happen.
|
|
6807
|
+
if (this.destroyed) {
|
|
6808
|
+
return;
|
|
6809
|
+
}
|
|
6810
|
+
// LAYER 2: DATA PROCESSING SERVICES
|
|
6830
6811
|
// Handle input validation, data access, and transformation pipelines
|
|
6831
6812
|
// Depend on configuration service for validation rules and constants
|
|
6832
6813
|
this.logger.log('SankeyVisualization: Creating data processing services...');
|
|
6833
6814
|
await this.createDataServices();
|
|
6815
|
+
if (this.destroyed) {
|
|
6816
|
+
return;
|
|
6817
|
+
}
|
|
6834
6818
|
// LAYER 3: MATHEMATICAL CALCULATION SERVICES
|
|
6835
|
-
// Perform complex energy flow calculations with performance optimizations
|
|
6819
|
+
// Perform complex energy flow calculations with performance optimizations
|
|
6836
6820
|
// Depend on data services for input and configuration for mathematical constants
|
|
6837
6821
|
this.logger.log('SankeyVisualization: Creating calculation services...');
|
|
6838
6822
|
await this.createCalculationServices();
|
|
6823
|
+
if (this.destroyed) {
|
|
6824
|
+
return;
|
|
6825
|
+
}
|
|
6839
6826
|
// LAYER 4: VISUAL RENDERING SERVICES
|
|
6840
6827
|
// Generate SVG elements and manage visual output
|
|
6841
6828
|
// Depend on calculation services for positioning data and configuration for styling
|
|
6842
6829
|
this.logger.log('SankeyVisualization: Creating rendering services...');
|
|
6843
6830
|
await this.createRenderingServices();
|
|
6831
|
+
if (this.destroyed) {
|
|
6832
|
+
return;
|
|
6833
|
+
}
|
|
6844
6834
|
// LAYER 5: ANIMATION CONTROL SERVICES
|
|
6845
6835
|
// Manage timeline navigation and smooth transitions between years
|
|
6846
6836
|
// Depend on rendering services for visual updates and calculation services for data
|
|
6847
6837
|
this.logger.log('SankeyVisualization: Creating animation services...');
|
|
6848
6838
|
await this.createAnimationService();
|
|
6839
|
+
if (this.destroyed) {
|
|
6840
|
+
return;
|
|
6841
|
+
}
|
|
6849
6842
|
// LAYER 6: USER INTERACTION SERVICES
|
|
6850
6843
|
// Handle mouse, keyboard, and touch events with accessibility support
|
|
6851
6844
|
// Depend on animation services for playback control and rendering for visual feedback
|
|
6852
6845
|
this.logger.log('SankeyVisualization: Creating interaction services...');
|
|
6853
6846
|
await this.createInteractionServices();
|
|
6847
|
+
if (this.destroyed) {
|
|
6848
|
+
return;
|
|
6849
|
+
}
|
|
6854
6850
|
// DOM INITIALIZATION: Create visualization structure in browser
|
|
6855
6851
|
// Must happen after all services are created as services may reference DOM elements
|
|
6856
6852
|
this.logger.log('SankeyVisualization: Initializing DOM structure...');
|
|
@@ -6859,24 +6855,32 @@
|
|
|
6859
6855
|
// Triggers the complete data → calculation → render pipeline for the first time
|
|
6860
6856
|
this.logger.log('SankeyVisualization: Performing initial render...');
|
|
6861
6857
|
await this.performInitialRender();
|
|
6858
|
+
if (this.destroyed) {
|
|
6859
|
+
return;
|
|
6860
|
+
}
|
|
6862
6861
|
const totalInitializationTime = performance.now() - initializationStartTime;
|
|
6863
|
-
|
|
6864
|
-
|
|
6865
|
-
|
|
6862
|
+
const readyData = {
|
|
6863
|
+
totalInitTime: totalInitializationTime,
|
|
6864
|
+
dataPointCount: this.options.data.length,
|
|
6865
|
+
yearRange: [
|
|
6866
|
+
Math.min(...this.options.data.map(d => d.year)),
|
|
6867
|
+
Math.max(...this.options.data.map(d => d.year))
|
|
6868
|
+
]
|
|
6869
|
+
};
|
|
6870
|
+
const readyEvent = {
|
|
6866
6871
|
type: 'system.ready',
|
|
6867
6872
|
timestamp: Date.now(),
|
|
6868
6873
|
source: 'SankeyVisualization',
|
|
6869
|
-
data:
|
|
6870
|
-
|
|
6871
|
-
|
|
6872
|
-
|
|
6873
|
-
|
|
6874
|
-
Math.max(...this.options.data.map(d => d.year))
|
|
6875
|
-
]
|
|
6876
|
-
}
|
|
6877
|
-
});
|
|
6874
|
+
data: readyData
|
|
6875
|
+
};
|
|
6876
|
+
// LIFECYCLE EVENT: Signal system fully ready for use
|
|
6877
|
+
// Public API methods are safe to call after this event
|
|
6878
|
+
this.eventBus.emit(readyEvent);
|
|
6878
6879
|
// SYSTEM STATE: Mark initialization complete
|
|
6879
6880
|
this.initialized = true;
|
|
6881
|
+
// Latch readiness so callers that subscribe after this point can
|
|
6882
|
+
// still observe it through whenReady().
|
|
6883
|
+
this.resolveReady(readyEvent);
|
|
6880
6884
|
// PERFORMANCE LOGGING: Track initialization time for regression detection
|
|
6881
6885
|
this.logger.log(`SankeyVisualization: Complete initialization in ${totalInitializationTime.toFixed(2)}ms`);
|
|
6882
6886
|
// INITIALIZATION BENCHMARK: Performance regression detection system
|
|
@@ -7064,12 +7068,12 @@
|
|
|
7064
7068
|
* Creates the DOM structure needed for controls, timeline, and chart display
|
|
7065
7069
|
*/
|
|
7066
7070
|
injectHTML() {
|
|
7067
|
-
var _a
|
|
7071
|
+
var _a;
|
|
7068
7072
|
const country = this.options.country;
|
|
7069
7073
|
const firstYear = this.services.dataService.firstYear;
|
|
7070
7074
|
const lastYear = this.services.dataService.lastYear;
|
|
7071
7075
|
let html = `
|
|
7072
|
-
<div class="evs-sankey evs-title-container"
|
|
7076
|
+
<div class="evs-sankey evs-title-container">
|
|
7073
7077
|
<div class="evs-header-content">
|
|
7074
7078
|
<div class="evs-header-logo">
|
|
7075
7079
|
<a href="https://www.rdcep.org/" target="_blank">
|
|
@@ -7105,7 +7109,7 @@
|
|
|
7105
7109
|
</div>
|
|
7106
7110
|
</div>
|
|
7107
7111
|
</div>
|
|
7108
|
-
<div class="evs-us-energy-sankey-wrapper" style="width: ${(
|
|
7112
|
+
<div class="evs-us-energy-sankey-wrapper" style="width: ${(_a = this.services['configurationService']) === null || _a === void 0 ? void 0 : _a.WIDTH}px;">
|
|
7109
7113
|
<div class="evs-sankey" style="line-height: 0;"></div>
|
|
7110
7114
|
`;
|
|
7111
7115
|
if (this.options.includeTimeline) {
|
|
@@ -7289,22 +7293,97 @@
|
|
|
7289
7293
|
});
|
|
7290
7294
|
this.subscriptions.push(errorSubscription, yearChangeSubscription);
|
|
7291
7295
|
}
|
|
7296
|
+
/**
|
|
7297
|
+
* Register handlers supplied through the `events` constructor option.
|
|
7298
|
+
* Runs before initialize() so that no lifecycle event is dropped.
|
|
7299
|
+
*/
|
|
7300
|
+
registerConfiguredEventHandlers() {
|
|
7301
|
+
const configured = this.options.events;
|
|
7302
|
+
if (!configured) {
|
|
7303
|
+
return;
|
|
7304
|
+
}
|
|
7305
|
+
Object.keys(configured).forEach(type => {
|
|
7306
|
+
const handler = configured[type];
|
|
7307
|
+
if (typeof handler === 'function') {
|
|
7308
|
+
this.addListener(type, handler, handler);
|
|
7309
|
+
}
|
|
7310
|
+
});
|
|
7311
|
+
}
|
|
7312
|
+
/**
|
|
7313
|
+
* Shared registration path for on(), once() and the `events` option.
|
|
7314
|
+
*
|
|
7315
|
+
* @param identity what off() matches on - the handler the caller passed
|
|
7316
|
+
* @param invoke what actually runs, which differs from identity for once()
|
|
7317
|
+
*/
|
|
7318
|
+
addListener(type, identity, invoke) {
|
|
7319
|
+
if (typeof identity !== 'function' || typeof invoke !== 'function') {
|
|
7320
|
+
throw new SankeyError(`SankeyVisualization: event handler for '${type}' must be a function`);
|
|
7321
|
+
}
|
|
7322
|
+
if (this.destroyed) {
|
|
7323
|
+
console.warn(`SankeyVisualization: cannot subscribe to '${type}' - instance has been destroyed`);
|
|
7324
|
+
return () => {
|
|
7325
|
+
};
|
|
7326
|
+
}
|
|
7327
|
+
// HANDLER IDENTITY ISOLATION - do not remove.
|
|
7328
|
+
// EventBus stores handlers in a Set keyed by function identity. Passing
|
|
7329
|
+
// the same function object twice collapses into a single Set entry, and
|
|
7330
|
+
// unsubscribing either subscription would then silently remove BOTH.
|
|
7331
|
+
// Allocating a fresh closure per registration keeps every registration
|
|
7332
|
+
// independently removable. Covered by tests/event-bus.test.ts.
|
|
7333
|
+
const wrapped = (event) => invoke(event);
|
|
7334
|
+
const record = {
|
|
7335
|
+
type,
|
|
7336
|
+
identity,
|
|
7337
|
+
subscription: this.eventBus.subscribe(type, wrapped)
|
|
7338
|
+
};
|
|
7339
|
+
this.publicListeners.push(record);
|
|
7340
|
+
let alreadyRemoved = false;
|
|
7341
|
+
return () => {
|
|
7342
|
+
if (alreadyRemoved) {
|
|
7343
|
+
return;
|
|
7344
|
+
}
|
|
7345
|
+
alreadyRemoved = true;
|
|
7346
|
+
this.removeListener(record);
|
|
7347
|
+
};
|
|
7348
|
+
}
|
|
7349
|
+
/**
|
|
7350
|
+
* Remove a single public subscription and its bus registration
|
|
7351
|
+
*/
|
|
7352
|
+
removeListener(record) {
|
|
7353
|
+
const index = this.publicListeners.indexOf(record);
|
|
7354
|
+
if (index === -1) {
|
|
7355
|
+
return;
|
|
7356
|
+
}
|
|
7357
|
+
this.publicListeners.splice(index, 1);
|
|
7358
|
+
// After destroy() the bus has already been cleared; unsubscribing again
|
|
7359
|
+
// would only log a spurious "non-existent subscription" warning.
|
|
7360
|
+
if (!this.destroyed) {
|
|
7361
|
+
this.eventBus.unsubscribe(record.subscription);
|
|
7362
|
+
}
|
|
7363
|
+
}
|
|
7292
7364
|
/**
|
|
7293
7365
|
* Handle initialization errors
|
|
7294
7366
|
*/
|
|
7295
7367
|
handleInitializationError(error) {
|
|
7296
|
-
|
|
7368
|
+
const normalizedError = error instanceof Error ? error : new Error(String(error));
|
|
7369
|
+
console.error('SankeyVisualization: Initialization failed:', normalizedError);
|
|
7297
7370
|
this.eventBus.emit({
|
|
7298
7371
|
type: 'system.error',
|
|
7299
7372
|
timestamp: Date.now(),
|
|
7300
7373
|
source: 'SankeyVisualization',
|
|
7301
7374
|
data: {
|
|
7302
|
-
error:
|
|
7375
|
+
error: normalizedError,
|
|
7303
7376
|
context: 'initialization',
|
|
7304
7377
|
recoverable: false
|
|
7305
7378
|
},
|
|
7306
7379
|
});
|
|
7307
|
-
//
|
|
7380
|
+
// Settle whenReady() with the real cause BEFORE destroy(), which would
|
|
7381
|
+
// otherwise reject first with a generic "destroyed" error and lose it.
|
|
7382
|
+
this.rejectReady(normalizedError);
|
|
7383
|
+
// Clean up any partially initialized state.
|
|
7384
|
+
// NOTE: the emit above is already in flight and still delivers. emit()
|
|
7385
|
+
// captures its handler Set synchronously and clear() empties the Map
|
|
7386
|
+
// rather than the captured Sets, so 'system.error' handlers still run.
|
|
7308
7387
|
this.destroy();
|
|
7309
7388
|
throw error;
|
|
7310
7389
|
}
|
|
@@ -7408,6 +7487,69 @@
|
|
|
7408
7487
|
isInitialized() {
|
|
7409
7488
|
return this.initialized;
|
|
7410
7489
|
}
|
|
7490
|
+
// ==================== PUBLIC EVENT API ====================
|
|
7491
|
+
/**
|
|
7492
|
+
* Subscribe to a visualization event
|
|
7493
|
+
*
|
|
7494
|
+
* Handlers run asynchronously, one microtask after the event is produced,
|
|
7495
|
+
* and are error-isolated: a throwing handler cannot break the visualization
|
|
7496
|
+
* or prevent other handlers from running.
|
|
7497
|
+
*
|
|
7498
|
+
* LIFECYCLE TIMING: `system.ready` and `system.error` are produced during the
|
|
7499
|
+
* asynchronous initialization the constructor starts. Subscribing in the same
|
|
7500
|
+
* synchronous tick as `new Sankey(...)` is soon enough; subscribing later (for
|
|
7501
|
+
* example after `await nextTick()`) is not. For those, use the `events`
|
|
7502
|
+
* constructor option or {@link whenReady}.
|
|
7503
|
+
*
|
|
7504
|
+
* Registering the same function twice registers it twice and it is called
|
|
7505
|
+
* twice; each call returns its own unsubscribe.
|
|
7506
|
+
*
|
|
7507
|
+
* @returns an unsubscribe function, safe to call more than once
|
|
7508
|
+
*/
|
|
7509
|
+
on(type, handler) {
|
|
7510
|
+
const listener = handler;
|
|
7511
|
+
return this.addListener(type, listener, listener);
|
|
7512
|
+
}
|
|
7513
|
+
/**
|
|
7514
|
+
* Subscribe until the first delivery, then unsubscribe automatically
|
|
7515
|
+
*
|
|
7516
|
+
* Cancellable either with the returned function or with `off(type, handler)`.
|
|
7517
|
+
*/
|
|
7518
|
+
once(type, handler) {
|
|
7519
|
+
let unsubscribe = () => {
|
|
7520
|
+
};
|
|
7521
|
+
const invoke = (event) => {
|
|
7522
|
+
// Remove BEFORE invoking so a throwing handler cannot fire twice.
|
|
7523
|
+
unsubscribe();
|
|
7524
|
+
return handler(event);
|
|
7525
|
+
};
|
|
7526
|
+
// `handler` is recorded as the identity so that off(type, handler)
|
|
7527
|
+
// cancels a pending once() even though the bus holds `invoke`.
|
|
7528
|
+
unsubscribe = this.addListener(type, handler, invoke);
|
|
7529
|
+
return unsubscribe;
|
|
7530
|
+
}
|
|
7531
|
+
/**
|
|
7532
|
+
* Remove every registration of `handler` for `type`, including a pending once()
|
|
7533
|
+
*
|
|
7534
|
+
* Removes all matching registrations rather than one, so teardown code
|
|
7535
|
+
* reliably stops delivery. No-op if the handler was never registered.
|
|
7536
|
+
*/
|
|
7537
|
+
off(type, handler) {
|
|
7538
|
+
// Snapshot first: removeListener() mutates this.publicListeners.
|
|
7539
|
+
this.publicListeners
|
|
7540
|
+
.filter(record => record.type === type && record.identity === handler)
|
|
7541
|
+
.forEach(record => this.removeListener(record));
|
|
7542
|
+
return this;
|
|
7543
|
+
}
|
|
7544
|
+
/**
|
|
7545
|
+
* Resolve when initialization completes, or reject if it failed
|
|
7546
|
+
*
|
|
7547
|
+
* Unlike `on('system.ready', ...)` this is a latch: it resolves even when
|
|
7548
|
+
* called long after the event was emitted. Safe to call any number of times.
|
|
7549
|
+
*/
|
|
7550
|
+
whenReady() {
|
|
7551
|
+
return this.readyPromise;
|
|
7552
|
+
}
|
|
7411
7553
|
/**
|
|
7412
7554
|
* Get array of available years in dataset
|
|
7413
7555
|
* @returns Readonly array of years available for visualization
|
|
@@ -7465,6 +7607,9 @@
|
|
|
7465
7607
|
// Clean up event subscriptions
|
|
7466
7608
|
this.subscriptions.forEach(sub => this.eventBus.unsubscribe(sub));
|
|
7467
7609
|
this.subscriptions = [];
|
|
7610
|
+
// Release consumer closures. eventBus.clear() below drops the underlying
|
|
7611
|
+
// handlers; this drops our bookkeeping references to them.
|
|
7612
|
+
this.publicListeners = [];
|
|
7468
7613
|
// Clean up event bus
|
|
7469
7614
|
this.eventBus.clear();
|
|
7470
7615
|
// Clean up DOM
|
|
@@ -7485,6 +7630,11 @@
|
|
|
7485
7630
|
this.services = {};
|
|
7486
7631
|
this.destroyed = true;
|
|
7487
7632
|
this.initialized = false;
|
|
7633
|
+
// Settle whenReady() so callers awaiting an instance that never came up
|
|
7634
|
+
// are not left hanging. No-op once the promise has already settled, so
|
|
7635
|
+
// this is harmless on the happy path. The constructor's no-op catch
|
|
7636
|
+
// prevents an unhandled rejection here.
|
|
7637
|
+
this.rejectReady(new SankeyError('SankeyVisualization: destroyed before initialization completed'));
|
|
7488
7638
|
// Resource cleanup completed successfully
|
|
7489
7639
|
}
|
|
7490
7640
|
// ==================== VALIDATION METHODS ====================
|
|
@@ -7952,16 +8102,19 @@
|
|
|
7952
8102
|
this.updateYearDisplay(year);
|
|
7953
8103
|
// EVENT SYSTEM INTEGRATION: Notify other services of year change
|
|
7954
8104
|
// Enables coordinated updates across the entire visualization system
|
|
8105
|
+
// Annotated against the public payload type so that any drift between
|
|
8106
|
+
// what is emitted and what SankeyEventMap advertises fails type-check.
|
|
8107
|
+
const yearChangedData = {
|
|
8108
|
+
year, // New target year
|
|
8109
|
+
previousYear, // Previous year for transition context
|
|
8110
|
+
yearIndex, // Array index for efficient data access
|
|
8111
|
+
isAnimating: this.state.isAnimating // Animation state for context
|
|
8112
|
+
};
|
|
7955
8113
|
this.eventBus.emit({
|
|
7956
8114
|
type: 'year.changed',
|
|
7957
8115
|
timestamp: Date.now(),
|
|
7958
8116
|
source: 'AnimationControlService',
|
|
7959
|
-
data:
|
|
7960
|
-
year, // New target year
|
|
7961
|
-
previousYear, // Previous year for transition context
|
|
7962
|
-
yearIndex, // Array index for efficient data access
|
|
7963
|
-
isAnimating: this.state.isAnimating // Animation state for context
|
|
7964
|
-
},
|
|
8117
|
+
data: yearChangedData,
|
|
7965
8118
|
});
|
|
7966
8119
|
}
|
|
7967
8120
|
/**
|
|
@@ -8231,15 +8384,16 @@
|
|
|
8231
8384
|
}, this.state.speed); // Configurable timing (50-1000ms typically)
|
|
8232
8385
|
// EVENT SYSTEM INTEGRATION: Broadcast animation start to other services
|
|
8233
8386
|
// Enables coordinated behavior across visualization components during playback
|
|
8387
|
+
const startedData = {
|
|
8388
|
+
isPlaying: true,
|
|
8389
|
+
currentYear: this.getCurrentYear(), // Starting year for context
|
|
8390
|
+
speed: this.state.speed // Playback speed for coordination
|
|
8391
|
+
};
|
|
8234
8392
|
this.eventBus.emit({
|
|
8235
8393
|
type: 'animation.started',
|
|
8236
8394
|
timestamp: Date.now(),
|
|
8237
8395
|
source: 'AnimationControlService',
|
|
8238
|
-
data:
|
|
8239
|
-
isPlaying: true,
|
|
8240
|
-
currentYear: this.getCurrentYear(), // Starting year for context
|
|
8241
|
-
speed: this.state.speed // Playback speed for coordination
|
|
8242
|
-
},
|
|
8396
|
+
data: startedData,
|
|
8243
8397
|
});
|
|
8244
8398
|
}
|
|
8245
8399
|
/**
|
|
@@ -8281,15 +8435,16 @@
|
|
|
8281
8435
|
}
|
|
8282
8436
|
// EVENT SYSTEM INTEGRATION: Broadcast animation stop to other services
|
|
8283
8437
|
// Enables coordinated pause behavior across visualization components
|
|
8438
|
+
const stoppedData = {
|
|
8439
|
+
isPlaying: false,
|
|
8440
|
+
currentYear: this.getCurrentYear(), // Current position preserved
|
|
8441
|
+
speed: this.state.speed // Speed settings maintained
|
|
8442
|
+
};
|
|
8284
8443
|
this.eventBus.emit({
|
|
8285
8444
|
type: 'animation.stopped',
|
|
8286
8445
|
timestamp: Date.now(),
|
|
8287
8446
|
source: 'AnimationControlService',
|
|
8288
|
-
data:
|
|
8289
|
-
isPlaying: false,
|
|
8290
|
-
currentYear: this.getCurrentYear(), // Current position preserved
|
|
8291
|
-
speed: this.state.speed // Speed settings maintained
|
|
8292
|
-
}
|
|
8447
|
+
data: stoppedData
|
|
8293
8448
|
});
|
|
8294
8449
|
}
|
|
8295
8450
|
/**
|
|
@@ -8324,13 +8479,14 @@
|
|
|
8324
8479
|
}, 100);
|
|
8325
8480
|
}
|
|
8326
8481
|
// Emit speed changed event
|
|
8482
|
+
const speedChangedData = {
|
|
8483
|
+
speed
|
|
8484
|
+
};
|
|
8327
8485
|
this.eventBus.emit({
|
|
8328
8486
|
type: 'speed.changed',
|
|
8329
8487
|
timestamp: Date.now(),
|
|
8330
8488
|
source: 'AnimationControlService',
|
|
8331
|
-
data:
|
|
8332
|
-
speed
|
|
8333
|
-
}
|
|
8489
|
+
data: speedChangedData
|
|
8334
8490
|
});
|
|
8335
8491
|
// Animation speed updated for timeline playback
|
|
8336
8492
|
}
|
|
@@ -8735,16 +8891,17 @@
|
|
|
8735
8891
|
// Emit keyboard navigation event - Event-Driven Architecture Integration
|
|
8736
8892
|
// Enables other services to react to keyboard interactions
|
|
8737
8893
|
// Provides rich event context (key + modifiers) for advanced interactions
|
|
8894
|
+
const keypressData = {
|
|
8895
|
+
key, // Primary key pressed (normalized to lowercase)
|
|
8896
|
+
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
8897
|
+
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
8898
|
+
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
8899
|
+
};
|
|
8738
8900
|
this.eventBus.emit({
|
|
8739
8901
|
type: 'interaction.keypress',
|
|
8740
8902
|
timestamp: Date.now(),
|
|
8741
8903
|
source: 'InteractionService',
|
|
8742
|
-
data:
|
|
8743
|
-
key, // Primary key pressed (normalized to lowercase)
|
|
8744
|
-
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
8745
|
-
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
8746
|
-
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
8747
|
-
}
|
|
8904
|
+
data: keypressData
|
|
8748
8905
|
});
|
|
8749
8906
|
// Call custom handler if provided - Extensibility Pattern
|
|
8750
8907
|
// Allows external code to extend keyboard navigation behavior
|
|
@@ -8792,14 +8949,15 @@
|
|
|
8792
8949
|
// Emit slider interaction event - Event-Driven Architecture
|
|
8793
8950
|
// Enables other services to react to timeline position changes
|
|
8794
8951
|
// Provides structured event data for logging and analytics
|
|
8952
|
+
const sliderData = {
|
|
8953
|
+
year, // Selected year value (numeric)
|
|
8954
|
+
value: year // Duplicate for backwards compatibility
|
|
8955
|
+
};
|
|
8795
8956
|
this.eventBus.emit({
|
|
8796
8957
|
type: 'interaction.slider',
|
|
8797
8958
|
timestamp: Date.now(),
|
|
8798
8959
|
source: 'InteractionService',
|
|
8799
|
-
data:
|
|
8800
|
-
year, // Selected year value (numeric)
|
|
8801
|
-
value: year // Duplicate for backwards compatibility
|
|
8802
|
-
}
|
|
8960
|
+
data: sliderData
|
|
8803
8961
|
});
|
|
8804
8962
|
// Call custom handler if provided - Extensibility Pattern
|
|
8805
8963
|
// Supports application-specific slider interaction behavior
|
|
@@ -8855,14 +9013,15 @@
|
|
|
8855
9013
|
// Emit button interaction event - Event-Driven Architecture
|
|
8856
9014
|
// Enables other services to react to animation state changes
|
|
8857
9015
|
// Note: Action reflects the RESULT of the button press, not the current state
|
|
9016
|
+
const playButtonData = {
|
|
9017
|
+
buttonId: 'evs-play-button',
|
|
9018
|
+
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
9019
|
+
};
|
|
8858
9020
|
this.eventBus.emit({
|
|
8859
9021
|
type: 'interaction.button',
|
|
8860
9022
|
timestamp: Date.now(),
|
|
8861
9023
|
source: 'InteractionService',
|
|
8862
|
-
data:
|
|
8863
|
-
buttonId: 'evs-play-button',
|
|
8864
|
-
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
8865
|
-
}
|
|
9024
|
+
data: playButtonData
|
|
8866
9025
|
});
|
|
8867
9026
|
};
|
|
8868
9027
|
this.addEventListener(playButton, 'click', playButtonHandler);
|
|
@@ -8879,15 +9038,16 @@
|
|
|
8879
9038
|
this.animationControlService.setSpeed(speed);
|
|
8880
9039
|
// Emit speed change event - Event-Driven Architecture
|
|
8881
9040
|
// Enables UI updates (active button state, speed indicator)
|
|
9041
|
+
const speedButtonData = {
|
|
9042
|
+
buttonId: target.id,
|
|
9043
|
+
action: 'speed-change',
|
|
9044
|
+
speed // New animation speed in milliseconds
|
|
9045
|
+
};
|
|
8882
9046
|
this.eventBus.emit({
|
|
8883
9047
|
type: 'interaction.button',
|
|
8884
9048
|
timestamp: Date.now(),
|
|
8885
9049
|
source: 'InteractionService',
|
|
8886
|
-
data:
|
|
8887
|
-
buttonId: target.id,
|
|
8888
|
-
action: 'speed-change',
|
|
8889
|
-
speed // New animation speed in milliseconds
|
|
8890
|
-
}
|
|
9050
|
+
data: speedButtonData
|
|
8891
9051
|
});
|
|
8892
9052
|
};
|
|
8893
9053
|
this.addEventListener(button, 'click', speedButtonHandler);
|
|
@@ -8998,16 +9158,17 @@
|
|
|
8998
9158
|
});
|
|
8999
9159
|
// Emit hover event - Event-Driven Architecture
|
|
9000
9160
|
// Provides rich context for tooltip display, analytics, and custom behavior
|
|
9161
|
+
const hoverData = {
|
|
9162
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9163
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9164
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9165
|
+
mousePosition: { x: event.clientX, y: event.clientY } // Coordinates for tooltip positioning
|
|
9166
|
+
};
|
|
9001
9167
|
this.eventBus.emit({
|
|
9002
9168
|
type: 'interaction.hover',
|
|
9003
9169
|
timestamp: Date.now(),
|
|
9004
9170
|
source: 'InteractionService',
|
|
9005
|
-
data:
|
|
9006
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9007
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9008
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9009
|
-
mousePosition: { x: event.clientX, y: event.clientY } // Coordinates for tooltip positioning
|
|
9010
|
-
}
|
|
9171
|
+
data: hoverData
|
|
9011
9172
|
});
|
|
9012
9173
|
// Call custom handler if provided - Extensibility Pattern
|
|
9013
9174
|
// Supports application-specific hover behavior
|
|
@@ -9049,16 +9210,17 @@
|
|
|
9049
9210
|
// element.classList.add('selected');
|
|
9050
9211
|
// Emit click event - Event-Driven Architecture
|
|
9051
9212
|
// Provides comprehensive click context for analytics, tooltips, and custom behavior
|
|
9213
|
+
const clickData = {
|
|
9214
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9215
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9216
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9217
|
+
mousePosition: { x: event.clientX, y: event.clientY } // Click coordinates for UI positioning
|
|
9218
|
+
};
|
|
9052
9219
|
this.eventBus.emit({
|
|
9053
9220
|
type: 'interaction.click',
|
|
9054
9221
|
timestamp: Date.now(),
|
|
9055
9222
|
source: 'InteractionService',
|
|
9056
|
-
data:
|
|
9057
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9058
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9059
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9060
|
-
mousePosition: { x: event.clientX, y: event.clientY } // Click coordinates for UI positioning
|
|
9061
|
-
}
|
|
9223
|
+
data: clickData
|
|
9062
9224
|
});
|
|
9063
9225
|
// Call custom handler if provided - Extensibility Pattern
|
|
9064
9226
|
// Supports application-specific click behavior
|