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.esm.js
CHANGED
|
@@ -5014,15 +5014,16 @@ class DataValidationService {
|
|
|
5014
5014
|
// Emit validation error event for error handling - Error Recovery Architecture
|
|
5015
5015
|
// Enables centralized error handling and user notification systems
|
|
5016
5016
|
// Marks validation errors as non-recoverable (require data fix)
|
|
5017
|
+
const validationErrorData = {
|
|
5018
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
5019
|
+
context: 'data_validation', // Error categorization for debugging
|
|
5020
|
+
recoverable: false // Validation errors require data correction
|
|
5021
|
+
};
|
|
5017
5022
|
this.eventBus.emit({
|
|
5018
5023
|
type: 'system.error',
|
|
5019
5024
|
timestamp: Date.now(),
|
|
5020
5025
|
source: 'DataValidationService',
|
|
5021
|
-
data:
|
|
5022
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
5023
|
-
context: 'data_validation', // Error categorization for debugging
|
|
5024
|
-
recoverable: false // Validation errors require data correction
|
|
5025
|
-
}
|
|
5026
|
+
data: validationErrorData
|
|
5026
5027
|
});
|
|
5027
5028
|
// Re-throw error to maintain validation contract - Fail-Fast Pattern
|
|
5028
5029
|
// Prevents invalid data from propagating to calculation services
|
|
@@ -6672,52 +6673,6 @@ class RenderingService {
|
|
|
6672
6673
|
}
|
|
6673
6674
|
}
|
|
6674
6675
|
|
|
6675
|
-
/**
|
|
6676
|
-
* Main Sankey Visualization Class - Event-Driven Architecture Orchestrator
|
|
6677
|
-
*
|
|
6678
|
-
* ARCHITECTURE PATTERN: Service Orchestration with Event-Driven Communication
|
|
6679
|
-
*
|
|
6680
|
-
* This class implements the Orchestrator pattern from Clean Architecture,
|
|
6681
|
-
* coordinating between focused, single-responsibility services through
|
|
6682
|
-
* a type-safe event bus. It acts as the application boundary, managing
|
|
6683
|
-
* the complete lifecycle from initialization to destruction.
|
|
6684
|
-
*
|
|
6685
|
-
* SERVICE COMPOSITION ARCHITECTURE:
|
|
6686
|
-
* 1. Infrastructure Layer: ConfigurationService (mathematical constants)
|
|
6687
|
-
* 2. Data Layer: DataService, ValidationService, TransformService
|
|
6688
|
-
* 3. Calculation Layer: SummaryService, GraphService, PositionService
|
|
6689
|
-
* 4. Presentation Layer: RenderingService, AnimationService
|
|
6690
|
-
* 5. Interaction Layer: InteractionService, TooltipService
|
|
6691
|
-
*
|
|
6692
|
-
* DEPENDENCY INJECTION PATTERN:
|
|
6693
|
-
* - Constructor Injection: Services receive dependencies via constructor
|
|
6694
|
-
* - Service Locator: Services are registered in central container
|
|
6695
|
-
* - Event Bus Mediation: Services communicate without direct coupling
|
|
6696
|
-
* - Lifecycle Management: Services created in dependency order
|
|
6697
|
-
*
|
|
6698
|
-
* EVENT-DRIVEN COMMUNICATION BENEFITS:
|
|
6699
|
-
* - Loose Coupling: Services don't hold references to each other
|
|
6700
|
-
* - Testability: Services can be mocked and tested in isolation
|
|
6701
|
-
* - Maintainability: Changes to one service don't affect others
|
|
6702
|
-
* - Performance: Async event dispatch prevents blocking operations
|
|
6703
|
-
*
|
|
6704
|
-
* PUBLIC API COMPATIBILITY:
|
|
6705
|
-
* Maintains complete API compatibility with previous versions while
|
|
6706
|
-
* providing enhanced functionality including configurable animation
|
|
6707
|
-
* looping, improved error handling, and comprehensive performance monitoring.
|
|
6708
|
-
*
|
|
6709
|
-
* Usage Example:
|
|
6710
|
-
* ```typescript
|
|
6711
|
-
* const sankey = new SankeyVisualization('container', {
|
|
6712
|
-
* data: energyData,
|
|
6713
|
-
* includeControls: true,
|
|
6714
|
-
* loopAnimation: false
|
|
6715
|
-
* });
|
|
6716
|
-
*
|
|
6717
|
-
* // Chain-able API maintained for compatibility
|
|
6718
|
-
* sankey.play().setSpeed(100).setYear(2020);
|
|
6719
|
-
* ```
|
|
6720
|
-
*/
|
|
6721
6676
|
class Sankey {
|
|
6722
6677
|
constructor(containerId, options) {
|
|
6723
6678
|
// SERVICE DEPENDENCY CONTAINER: Holds all service instances after creation
|
|
@@ -6732,6 +6687,10 @@ class Sankey {
|
|
|
6732
6687
|
// Critical for memory leak prevention in long-running applications
|
|
6733
6688
|
// Each subscription must be explicitly unsubscribed during destroy()
|
|
6734
6689
|
this.subscriptions = [];
|
|
6690
|
+
// PUBLIC SUBSCRIPTION REGISTRY: consumer-facing on()/once() registrations.
|
|
6691
|
+
// Kept separate from `subscriptions` (internal service wiring) so that off()
|
|
6692
|
+
// and destroy() can reason about the two independently.
|
|
6693
|
+
this.publicListeners = [];
|
|
6735
6694
|
this.logger = new Logger(options);
|
|
6736
6695
|
// Initialize core system components
|
|
6737
6696
|
// 1. Validate inputs using comprehensive validation logic
|
|
@@ -6743,9 +6702,23 @@ class Sankey {
|
|
|
6743
6702
|
this.wasteHeatVisible = this.options.showWasteHeat !== false;
|
|
6744
6703
|
// 4. Create core event bus
|
|
6745
6704
|
this.eventBus = new EventBus(this.logger);
|
|
6746
|
-
// 5.
|
|
6705
|
+
// 5. Arm the readiness latch before any asynchronous work starts.
|
|
6706
|
+
// The no-op catch is REQUIRED: without it, a failed initialization
|
|
6707
|
+
// produces a fresh unhandled rejection for every caller that never
|
|
6708
|
+
// uses whenReady(), which would be a behaviour change.
|
|
6709
|
+
this.readyPromise = new Promise((resolve, reject) => {
|
|
6710
|
+
this.resolveReady = resolve;
|
|
6711
|
+
this.rejectReady = reject;
|
|
6712
|
+
});
|
|
6713
|
+
this.readyPromise.catch(() => {
|
|
6714
|
+
});
|
|
6715
|
+
// 6. Setup system event listeners
|
|
6747
6716
|
this.setupSystemEventListeners();
|
|
6748
|
-
//
|
|
6717
|
+
// 7. Register handlers supplied through the `events` option. MUST happen
|
|
6718
|
+
// before initialize(): the event bus drops events that have no
|
|
6719
|
+
// subscriber at emit time and there is no replay.
|
|
6720
|
+
this.registerConfiguredEventHandlers();
|
|
6721
|
+
// 8. Initialize services and visualization
|
|
6749
6722
|
this.initialize();
|
|
6750
6723
|
}
|
|
6751
6724
|
/**
|
|
@@ -6820,31 +6793,54 @@ class Sankey {
|
|
|
6820
6793
|
// Must be created first as other services depend on configuration constants
|
|
6821
6794
|
this.logger.log('SankeyVisualization: Creating infrastructure services...');
|
|
6822
6795
|
await this.createConfigurationService();
|
|
6823
|
-
//
|
|
6796
|
+
// CANCELLATION CHECK: destroy() empties the service container, so a
|
|
6797
|
+
// destroy() issued while initialization is still in flight would
|
|
6798
|
+
// otherwise leave the remaining layers reading undefined services and
|
|
6799
|
+
// throwing asynchronously, where nothing can catch it. Each await is
|
|
6800
|
+
// a suspension point at which that can happen.
|
|
6801
|
+
if (this.destroyed) {
|
|
6802
|
+
return;
|
|
6803
|
+
}
|
|
6804
|
+
// LAYER 2: DATA PROCESSING SERVICES
|
|
6824
6805
|
// Handle input validation, data access, and transformation pipelines
|
|
6825
6806
|
// Depend on configuration service for validation rules and constants
|
|
6826
6807
|
this.logger.log('SankeyVisualization: Creating data processing services...');
|
|
6827
6808
|
await this.createDataServices();
|
|
6809
|
+
if (this.destroyed) {
|
|
6810
|
+
return;
|
|
6811
|
+
}
|
|
6828
6812
|
// LAYER 3: MATHEMATICAL CALCULATION SERVICES
|
|
6829
|
-
// Perform complex energy flow calculations with performance optimizations
|
|
6813
|
+
// Perform complex energy flow calculations with performance optimizations
|
|
6830
6814
|
// Depend on data services for input and configuration for mathematical constants
|
|
6831
6815
|
this.logger.log('SankeyVisualization: Creating calculation services...');
|
|
6832
6816
|
await this.createCalculationServices();
|
|
6817
|
+
if (this.destroyed) {
|
|
6818
|
+
return;
|
|
6819
|
+
}
|
|
6833
6820
|
// LAYER 4: VISUAL RENDERING SERVICES
|
|
6834
6821
|
// Generate SVG elements and manage visual output
|
|
6835
6822
|
// Depend on calculation services for positioning data and configuration for styling
|
|
6836
6823
|
this.logger.log('SankeyVisualization: Creating rendering services...');
|
|
6837
6824
|
await this.createRenderingServices();
|
|
6825
|
+
if (this.destroyed) {
|
|
6826
|
+
return;
|
|
6827
|
+
}
|
|
6838
6828
|
// LAYER 5: ANIMATION CONTROL SERVICES
|
|
6839
6829
|
// Manage timeline navigation and smooth transitions between years
|
|
6840
6830
|
// Depend on rendering services for visual updates and calculation services for data
|
|
6841
6831
|
this.logger.log('SankeyVisualization: Creating animation services...');
|
|
6842
6832
|
await this.createAnimationService();
|
|
6833
|
+
if (this.destroyed) {
|
|
6834
|
+
return;
|
|
6835
|
+
}
|
|
6843
6836
|
// LAYER 6: USER INTERACTION SERVICES
|
|
6844
6837
|
// Handle mouse, keyboard, and touch events with accessibility support
|
|
6845
6838
|
// Depend on animation services for playback control and rendering for visual feedback
|
|
6846
6839
|
this.logger.log('SankeyVisualization: Creating interaction services...');
|
|
6847
6840
|
await this.createInteractionServices();
|
|
6841
|
+
if (this.destroyed) {
|
|
6842
|
+
return;
|
|
6843
|
+
}
|
|
6848
6844
|
// DOM INITIALIZATION: Create visualization structure in browser
|
|
6849
6845
|
// Must happen after all services are created as services may reference DOM elements
|
|
6850
6846
|
this.logger.log('SankeyVisualization: Initializing DOM structure...');
|
|
@@ -6853,24 +6849,32 @@ class Sankey {
|
|
|
6853
6849
|
// Triggers the complete data → calculation → render pipeline for the first time
|
|
6854
6850
|
this.logger.log('SankeyVisualization: Performing initial render...');
|
|
6855
6851
|
await this.performInitialRender();
|
|
6852
|
+
if (this.destroyed) {
|
|
6853
|
+
return;
|
|
6854
|
+
}
|
|
6856
6855
|
const totalInitializationTime = performance.now() - initializationStartTime;
|
|
6857
|
-
|
|
6858
|
-
|
|
6859
|
-
|
|
6856
|
+
const readyData = {
|
|
6857
|
+
totalInitTime: totalInitializationTime,
|
|
6858
|
+
dataPointCount: this.options.data.length,
|
|
6859
|
+
yearRange: [
|
|
6860
|
+
Math.min(...this.options.data.map(d => d.year)),
|
|
6861
|
+
Math.max(...this.options.data.map(d => d.year))
|
|
6862
|
+
]
|
|
6863
|
+
};
|
|
6864
|
+
const readyEvent = {
|
|
6860
6865
|
type: 'system.ready',
|
|
6861
6866
|
timestamp: Date.now(),
|
|
6862
6867
|
source: 'SankeyVisualization',
|
|
6863
|
-
data:
|
|
6864
|
-
|
|
6865
|
-
|
|
6866
|
-
|
|
6867
|
-
|
|
6868
|
-
Math.max(...this.options.data.map(d => d.year))
|
|
6869
|
-
]
|
|
6870
|
-
}
|
|
6871
|
-
});
|
|
6868
|
+
data: readyData
|
|
6869
|
+
};
|
|
6870
|
+
// LIFECYCLE EVENT: Signal system fully ready for use
|
|
6871
|
+
// Public API methods are safe to call after this event
|
|
6872
|
+
this.eventBus.emit(readyEvent);
|
|
6872
6873
|
// SYSTEM STATE: Mark initialization complete
|
|
6873
6874
|
this.initialized = true;
|
|
6875
|
+
// Latch readiness so callers that subscribe after this point can
|
|
6876
|
+
// still observe it through whenReady().
|
|
6877
|
+
this.resolveReady(readyEvent);
|
|
6874
6878
|
// PERFORMANCE LOGGING: Track initialization time for regression detection
|
|
6875
6879
|
this.logger.log(`SankeyVisualization: Complete initialization in ${totalInitializationTime.toFixed(2)}ms`);
|
|
6876
6880
|
// INITIALIZATION BENCHMARK: Performance regression detection system
|
|
@@ -7058,12 +7062,12 @@ class Sankey {
|
|
|
7058
7062
|
* Creates the DOM structure needed for controls, timeline, and chart display
|
|
7059
7063
|
*/
|
|
7060
7064
|
injectHTML() {
|
|
7061
|
-
var _a
|
|
7065
|
+
var _a;
|
|
7062
7066
|
const country = this.options.country;
|
|
7063
7067
|
const firstYear = this.services.dataService.firstYear;
|
|
7064
7068
|
const lastYear = this.services.dataService.lastYear;
|
|
7065
7069
|
let html = `
|
|
7066
|
-
<div class="evs-sankey evs-title-container"
|
|
7070
|
+
<div class="evs-sankey evs-title-container">
|
|
7067
7071
|
<div class="evs-header-content">
|
|
7068
7072
|
<div class="evs-header-logo">
|
|
7069
7073
|
<a href="https://www.rdcep.org/" target="_blank">
|
|
@@ -7099,7 +7103,7 @@ class Sankey {
|
|
|
7099
7103
|
</div>
|
|
7100
7104
|
</div>
|
|
7101
7105
|
</div>
|
|
7102
|
-
<div class="evs-us-energy-sankey-wrapper" style="width: ${(
|
|
7106
|
+
<div class="evs-us-energy-sankey-wrapper" style="width: ${(_a = this.services['configurationService']) === null || _a === void 0 ? void 0 : _a.WIDTH}px;">
|
|
7103
7107
|
<div class="evs-sankey" style="line-height: 0;"></div>
|
|
7104
7108
|
`;
|
|
7105
7109
|
if (this.options.includeTimeline) {
|
|
@@ -7283,22 +7287,97 @@ class Sankey {
|
|
|
7283
7287
|
});
|
|
7284
7288
|
this.subscriptions.push(errorSubscription, yearChangeSubscription);
|
|
7285
7289
|
}
|
|
7290
|
+
/**
|
|
7291
|
+
* Register handlers supplied through the `events` constructor option.
|
|
7292
|
+
* Runs before initialize() so that no lifecycle event is dropped.
|
|
7293
|
+
*/
|
|
7294
|
+
registerConfiguredEventHandlers() {
|
|
7295
|
+
const configured = this.options.events;
|
|
7296
|
+
if (!configured) {
|
|
7297
|
+
return;
|
|
7298
|
+
}
|
|
7299
|
+
Object.keys(configured).forEach(type => {
|
|
7300
|
+
const handler = configured[type];
|
|
7301
|
+
if (typeof handler === 'function') {
|
|
7302
|
+
this.addListener(type, handler, handler);
|
|
7303
|
+
}
|
|
7304
|
+
});
|
|
7305
|
+
}
|
|
7306
|
+
/**
|
|
7307
|
+
* Shared registration path for on(), once() and the `events` option.
|
|
7308
|
+
*
|
|
7309
|
+
* @param identity what off() matches on - the handler the caller passed
|
|
7310
|
+
* @param invoke what actually runs, which differs from identity for once()
|
|
7311
|
+
*/
|
|
7312
|
+
addListener(type, identity, invoke) {
|
|
7313
|
+
if (typeof identity !== 'function' || typeof invoke !== 'function') {
|
|
7314
|
+
throw new SankeyError(`SankeyVisualization: event handler for '${type}' must be a function`);
|
|
7315
|
+
}
|
|
7316
|
+
if (this.destroyed) {
|
|
7317
|
+
console.warn(`SankeyVisualization: cannot subscribe to '${type}' - instance has been destroyed`);
|
|
7318
|
+
return () => {
|
|
7319
|
+
};
|
|
7320
|
+
}
|
|
7321
|
+
// HANDLER IDENTITY ISOLATION - do not remove.
|
|
7322
|
+
// EventBus stores handlers in a Set keyed by function identity. Passing
|
|
7323
|
+
// the same function object twice collapses into a single Set entry, and
|
|
7324
|
+
// unsubscribing either subscription would then silently remove BOTH.
|
|
7325
|
+
// Allocating a fresh closure per registration keeps every registration
|
|
7326
|
+
// independently removable. Covered by tests/event-bus.test.ts.
|
|
7327
|
+
const wrapped = (event) => invoke(event);
|
|
7328
|
+
const record = {
|
|
7329
|
+
type,
|
|
7330
|
+
identity,
|
|
7331
|
+
subscription: this.eventBus.subscribe(type, wrapped)
|
|
7332
|
+
};
|
|
7333
|
+
this.publicListeners.push(record);
|
|
7334
|
+
let alreadyRemoved = false;
|
|
7335
|
+
return () => {
|
|
7336
|
+
if (alreadyRemoved) {
|
|
7337
|
+
return;
|
|
7338
|
+
}
|
|
7339
|
+
alreadyRemoved = true;
|
|
7340
|
+
this.removeListener(record);
|
|
7341
|
+
};
|
|
7342
|
+
}
|
|
7343
|
+
/**
|
|
7344
|
+
* Remove a single public subscription and its bus registration
|
|
7345
|
+
*/
|
|
7346
|
+
removeListener(record) {
|
|
7347
|
+
const index = this.publicListeners.indexOf(record);
|
|
7348
|
+
if (index === -1) {
|
|
7349
|
+
return;
|
|
7350
|
+
}
|
|
7351
|
+
this.publicListeners.splice(index, 1);
|
|
7352
|
+
// After destroy() the bus has already been cleared; unsubscribing again
|
|
7353
|
+
// would only log a spurious "non-existent subscription" warning.
|
|
7354
|
+
if (!this.destroyed) {
|
|
7355
|
+
this.eventBus.unsubscribe(record.subscription);
|
|
7356
|
+
}
|
|
7357
|
+
}
|
|
7286
7358
|
/**
|
|
7287
7359
|
* Handle initialization errors
|
|
7288
7360
|
*/
|
|
7289
7361
|
handleInitializationError(error) {
|
|
7290
|
-
|
|
7362
|
+
const normalizedError = error instanceof Error ? error : new Error(String(error));
|
|
7363
|
+
console.error('SankeyVisualization: Initialization failed:', normalizedError);
|
|
7291
7364
|
this.eventBus.emit({
|
|
7292
7365
|
type: 'system.error',
|
|
7293
7366
|
timestamp: Date.now(),
|
|
7294
7367
|
source: 'SankeyVisualization',
|
|
7295
7368
|
data: {
|
|
7296
|
-
error:
|
|
7369
|
+
error: normalizedError,
|
|
7297
7370
|
context: 'initialization',
|
|
7298
7371
|
recoverable: false
|
|
7299
7372
|
},
|
|
7300
7373
|
});
|
|
7301
|
-
//
|
|
7374
|
+
// Settle whenReady() with the real cause BEFORE destroy(), which would
|
|
7375
|
+
// otherwise reject first with a generic "destroyed" error and lose it.
|
|
7376
|
+
this.rejectReady(normalizedError);
|
|
7377
|
+
// Clean up any partially initialized state.
|
|
7378
|
+
// NOTE: the emit above is already in flight and still delivers. emit()
|
|
7379
|
+
// captures its handler Set synchronously and clear() empties the Map
|
|
7380
|
+
// rather than the captured Sets, so 'system.error' handlers still run.
|
|
7302
7381
|
this.destroy();
|
|
7303
7382
|
throw error;
|
|
7304
7383
|
}
|
|
@@ -7402,6 +7481,69 @@ class Sankey {
|
|
|
7402
7481
|
isInitialized() {
|
|
7403
7482
|
return this.initialized;
|
|
7404
7483
|
}
|
|
7484
|
+
// ==================== PUBLIC EVENT API ====================
|
|
7485
|
+
/**
|
|
7486
|
+
* Subscribe to a visualization event
|
|
7487
|
+
*
|
|
7488
|
+
* Handlers run asynchronously, one microtask after the event is produced,
|
|
7489
|
+
* and are error-isolated: a throwing handler cannot break the visualization
|
|
7490
|
+
* or prevent other handlers from running.
|
|
7491
|
+
*
|
|
7492
|
+
* LIFECYCLE TIMING: `system.ready` and `system.error` are produced during the
|
|
7493
|
+
* asynchronous initialization the constructor starts. Subscribing in the same
|
|
7494
|
+
* synchronous tick as `new Sankey(...)` is soon enough; subscribing later (for
|
|
7495
|
+
* example after `await nextTick()`) is not. For those, use the `events`
|
|
7496
|
+
* constructor option or {@link whenReady}.
|
|
7497
|
+
*
|
|
7498
|
+
* Registering the same function twice registers it twice and it is called
|
|
7499
|
+
* twice; each call returns its own unsubscribe.
|
|
7500
|
+
*
|
|
7501
|
+
* @returns an unsubscribe function, safe to call more than once
|
|
7502
|
+
*/
|
|
7503
|
+
on(type, handler) {
|
|
7504
|
+
const listener = handler;
|
|
7505
|
+
return this.addListener(type, listener, listener);
|
|
7506
|
+
}
|
|
7507
|
+
/**
|
|
7508
|
+
* Subscribe until the first delivery, then unsubscribe automatically
|
|
7509
|
+
*
|
|
7510
|
+
* Cancellable either with the returned function or with `off(type, handler)`.
|
|
7511
|
+
*/
|
|
7512
|
+
once(type, handler) {
|
|
7513
|
+
let unsubscribe = () => {
|
|
7514
|
+
};
|
|
7515
|
+
const invoke = (event) => {
|
|
7516
|
+
// Remove BEFORE invoking so a throwing handler cannot fire twice.
|
|
7517
|
+
unsubscribe();
|
|
7518
|
+
return handler(event);
|
|
7519
|
+
};
|
|
7520
|
+
// `handler` is recorded as the identity so that off(type, handler)
|
|
7521
|
+
// cancels a pending once() even though the bus holds `invoke`.
|
|
7522
|
+
unsubscribe = this.addListener(type, handler, invoke);
|
|
7523
|
+
return unsubscribe;
|
|
7524
|
+
}
|
|
7525
|
+
/**
|
|
7526
|
+
* Remove every registration of `handler` for `type`, including a pending once()
|
|
7527
|
+
*
|
|
7528
|
+
* Removes all matching registrations rather than one, so teardown code
|
|
7529
|
+
* reliably stops delivery. No-op if the handler was never registered.
|
|
7530
|
+
*/
|
|
7531
|
+
off(type, handler) {
|
|
7532
|
+
// Snapshot first: removeListener() mutates this.publicListeners.
|
|
7533
|
+
this.publicListeners
|
|
7534
|
+
.filter(record => record.type === type && record.identity === handler)
|
|
7535
|
+
.forEach(record => this.removeListener(record));
|
|
7536
|
+
return this;
|
|
7537
|
+
}
|
|
7538
|
+
/**
|
|
7539
|
+
* Resolve when initialization completes, or reject if it failed
|
|
7540
|
+
*
|
|
7541
|
+
* Unlike `on('system.ready', ...)` this is a latch: it resolves even when
|
|
7542
|
+
* called long after the event was emitted. Safe to call any number of times.
|
|
7543
|
+
*/
|
|
7544
|
+
whenReady() {
|
|
7545
|
+
return this.readyPromise;
|
|
7546
|
+
}
|
|
7405
7547
|
/**
|
|
7406
7548
|
* Get array of available years in dataset
|
|
7407
7549
|
* @returns Readonly array of years available for visualization
|
|
@@ -7459,6 +7601,9 @@ class Sankey {
|
|
|
7459
7601
|
// Clean up event subscriptions
|
|
7460
7602
|
this.subscriptions.forEach(sub => this.eventBus.unsubscribe(sub));
|
|
7461
7603
|
this.subscriptions = [];
|
|
7604
|
+
// Release consumer closures. eventBus.clear() below drops the underlying
|
|
7605
|
+
// handlers; this drops our bookkeeping references to them.
|
|
7606
|
+
this.publicListeners = [];
|
|
7462
7607
|
// Clean up event bus
|
|
7463
7608
|
this.eventBus.clear();
|
|
7464
7609
|
// Clean up DOM
|
|
@@ -7479,6 +7624,11 @@ class Sankey {
|
|
|
7479
7624
|
this.services = {};
|
|
7480
7625
|
this.destroyed = true;
|
|
7481
7626
|
this.initialized = false;
|
|
7627
|
+
// Settle whenReady() so callers awaiting an instance that never came up
|
|
7628
|
+
// are not left hanging. No-op once the promise has already settled, so
|
|
7629
|
+
// this is harmless on the happy path. The constructor's no-op catch
|
|
7630
|
+
// prevents an unhandled rejection here.
|
|
7631
|
+
this.rejectReady(new SankeyError('SankeyVisualization: destroyed before initialization completed'));
|
|
7482
7632
|
// Resource cleanup completed successfully
|
|
7483
7633
|
}
|
|
7484
7634
|
// ==================== VALIDATION METHODS ====================
|
|
@@ -7946,16 +8096,19 @@ class AnimationService {
|
|
|
7946
8096
|
this.updateYearDisplay(year);
|
|
7947
8097
|
// EVENT SYSTEM INTEGRATION: Notify other services of year change
|
|
7948
8098
|
// Enables coordinated updates across the entire visualization system
|
|
8099
|
+
// Annotated against the public payload type so that any drift between
|
|
8100
|
+
// what is emitted and what SankeyEventMap advertises fails type-check.
|
|
8101
|
+
const yearChangedData = {
|
|
8102
|
+
year, // New target year
|
|
8103
|
+
previousYear, // Previous year for transition context
|
|
8104
|
+
yearIndex, // Array index for efficient data access
|
|
8105
|
+
isAnimating: this.state.isAnimating // Animation state for context
|
|
8106
|
+
};
|
|
7949
8107
|
this.eventBus.emit({
|
|
7950
8108
|
type: 'year.changed',
|
|
7951
8109
|
timestamp: Date.now(),
|
|
7952
8110
|
source: 'AnimationControlService',
|
|
7953
|
-
data:
|
|
7954
|
-
year, // New target year
|
|
7955
|
-
previousYear, // Previous year for transition context
|
|
7956
|
-
yearIndex, // Array index for efficient data access
|
|
7957
|
-
isAnimating: this.state.isAnimating // Animation state for context
|
|
7958
|
-
},
|
|
8111
|
+
data: yearChangedData,
|
|
7959
8112
|
});
|
|
7960
8113
|
}
|
|
7961
8114
|
/**
|
|
@@ -8225,15 +8378,16 @@ class AnimationService {
|
|
|
8225
8378
|
}, this.state.speed); // Configurable timing (50-1000ms typically)
|
|
8226
8379
|
// EVENT SYSTEM INTEGRATION: Broadcast animation start to other services
|
|
8227
8380
|
// Enables coordinated behavior across visualization components during playback
|
|
8381
|
+
const startedData = {
|
|
8382
|
+
isPlaying: true,
|
|
8383
|
+
currentYear: this.getCurrentYear(), // Starting year for context
|
|
8384
|
+
speed: this.state.speed // Playback speed for coordination
|
|
8385
|
+
};
|
|
8228
8386
|
this.eventBus.emit({
|
|
8229
8387
|
type: 'animation.started',
|
|
8230
8388
|
timestamp: Date.now(),
|
|
8231
8389
|
source: 'AnimationControlService',
|
|
8232
|
-
data:
|
|
8233
|
-
isPlaying: true,
|
|
8234
|
-
currentYear: this.getCurrentYear(), // Starting year for context
|
|
8235
|
-
speed: this.state.speed // Playback speed for coordination
|
|
8236
|
-
},
|
|
8390
|
+
data: startedData,
|
|
8237
8391
|
});
|
|
8238
8392
|
}
|
|
8239
8393
|
/**
|
|
@@ -8275,15 +8429,16 @@ class AnimationService {
|
|
|
8275
8429
|
}
|
|
8276
8430
|
// EVENT SYSTEM INTEGRATION: Broadcast animation stop to other services
|
|
8277
8431
|
// Enables coordinated pause behavior across visualization components
|
|
8432
|
+
const stoppedData = {
|
|
8433
|
+
isPlaying: false,
|
|
8434
|
+
currentYear: this.getCurrentYear(), // Current position preserved
|
|
8435
|
+
speed: this.state.speed // Speed settings maintained
|
|
8436
|
+
};
|
|
8278
8437
|
this.eventBus.emit({
|
|
8279
8438
|
type: 'animation.stopped',
|
|
8280
8439
|
timestamp: Date.now(),
|
|
8281
8440
|
source: 'AnimationControlService',
|
|
8282
|
-
data:
|
|
8283
|
-
isPlaying: false,
|
|
8284
|
-
currentYear: this.getCurrentYear(), // Current position preserved
|
|
8285
|
-
speed: this.state.speed // Speed settings maintained
|
|
8286
|
-
}
|
|
8441
|
+
data: stoppedData
|
|
8287
8442
|
});
|
|
8288
8443
|
}
|
|
8289
8444
|
/**
|
|
@@ -8318,13 +8473,14 @@ class AnimationService {
|
|
|
8318
8473
|
}, 100);
|
|
8319
8474
|
}
|
|
8320
8475
|
// Emit speed changed event
|
|
8476
|
+
const speedChangedData = {
|
|
8477
|
+
speed
|
|
8478
|
+
};
|
|
8321
8479
|
this.eventBus.emit({
|
|
8322
8480
|
type: 'speed.changed',
|
|
8323
8481
|
timestamp: Date.now(),
|
|
8324
8482
|
source: 'AnimationControlService',
|
|
8325
|
-
data:
|
|
8326
|
-
speed
|
|
8327
|
-
}
|
|
8483
|
+
data: speedChangedData
|
|
8328
8484
|
});
|
|
8329
8485
|
// Animation speed updated for timeline playback
|
|
8330
8486
|
}
|
|
@@ -8729,16 +8885,17 @@ class InteractionService {
|
|
|
8729
8885
|
// Emit keyboard navigation event - Event-Driven Architecture Integration
|
|
8730
8886
|
// Enables other services to react to keyboard interactions
|
|
8731
8887
|
// Provides rich event context (key + modifiers) for advanced interactions
|
|
8888
|
+
const keypressData = {
|
|
8889
|
+
key, // Primary key pressed (normalized to lowercase)
|
|
8890
|
+
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
8891
|
+
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
8892
|
+
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
8893
|
+
};
|
|
8732
8894
|
this.eventBus.emit({
|
|
8733
8895
|
type: 'interaction.keypress',
|
|
8734
8896
|
timestamp: Date.now(),
|
|
8735
8897
|
source: 'InteractionService',
|
|
8736
|
-
data:
|
|
8737
|
-
key, // Primary key pressed (normalized to lowercase)
|
|
8738
|
-
ctrlKey: event.ctrlKey, // Enables Ctrl+key shortcuts
|
|
8739
|
-
shiftKey: event.shiftKey, // Enables Shift+key shortcuts
|
|
8740
|
-
altKey: event.altKey // Enables Alt+key shortcuts (power user features)
|
|
8741
|
-
}
|
|
8898
|
+
data: keypressData
|
|
8742
8899
|
});
|
|
8743
8900
|
// Call custom handler if provided - Extensibility Pattern
|
|
8744
8901
|
// Allows external code to extend keyboard navigation behavior
|
|
@@ -8786,14 +8943,15 @@ class InteractionService {
|
|
|
8786
8943
|
// Emit slider interaction event - Event-Driven Architecture
|
|
8787
8944
|
// Enables other services to react to timeline position changes
|
|
8788
8945
|
// Provides structured event data for logging and analytics
|
|
8946
|
+
const sliderData = {
|
|
8947
|
+
year, // Selected year value (numeric)
|
|
8948
|
+
value: year // Duplicate for backwards compatibility
|
|
8949
|
+
};
|
|
8789
8950
|
this.eventBus.emit({
|
|
8790
8951
|
type: 'interaction.slider',
|
|
8791
8952
|
timestamp: Date.now(),
|
|
8792
8953
|
source: 'InteractionService',
|
|
8793
|
-
data:
|
|
8794
|
-
year, // Selected year value (numeric)
|
|
8795
|
-
value: year // Duplicate for backwards compatibility
|
|
8796
|
-
}
|
|
8954
|
+
data: sliderData
|
|
8797
8955
|
});
|
|
8798
8956
|
// Call custom handler if provided - Extensibility Pattern
|
|
8799
8957
|
// Supports application-specific slider interaction behavior
|
|
@@ -8849,14 +9007,15 @@ class InteractionService {
|
|
|
8849
9007
|
// Emit button interaction event - Event-Driven Architecture
|
|
8850
9008
|
// Enables other services to react to animation state changes
|
|
8851
9009
|
// Note: Action reflects the RESULT of the button press, not the current state
|
|
9010
|
+
const playButtonData = {
|
|
9011
|
+
buttonId: 'evs-play-button',
|
|
9012
|
+
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
9013
|
+
};
|
|
8852
9014
|
this.eventBus.emit({
|
|
8853
9015
|
type: 'interaction.button',
|
|
8854
9016
|
timestamp: Date.now(),
|
|
8855
9017
|
source: 'InteractionService',
|
|
8856
|
-
data:
|
|
8857
|
-
buttonId: 'evs-play-button',
|
|
8858
|
-
action: this.animationControlService.isPlaying() ? 'play' : 'pause'
|
|
8859
|
-
}
|
|
9018
|
+
data: playButtonData
|
|
8860
9019
|
});
|
|
8861
9020
|
};
|
|
8862
9021
|
this.addEventListener(playButton, 'click', playButtonHandler);
|
|
@@ -8873,15 +9032,16 @@ class InteractionService {
|
|
|
8873
9032
|
this.animationControlService.setSpeed(speed);
|
|
8874
9033
|
// Emit speed change event - Event-Driven Architecture
|
|
8875
9034
|
// Enables UI updates (active button state, speed indicator)
|
|
9035
|
+
const speedButtonData = {
|
|
9036
|
+
buttonId: target.id,
|
|
9037
|
+
action: 'speed-change',
|
|
9038
|
+
speed // New animation speed in milliseconds
|
|
9039
|
+
};
|
|
8876
9040
|
this.eventBus.emit({
|
|
8877
9041
|
type: 'interaction.button',
|
|
8878
9042
|
timestamp: Date.now(),
|
|
8879
9043
|
source: 'InteractionService',
|
|
8880
|
-
data:
|
|
8881
|
-
buttonId: target.id,
|
|
8882
|
-
action: 'speed-change',
|
|
8883
|
-
speed // New animation speed in milliseconds
|
|
8884
|
-
}
|
|
9044
|
+
data: speedButtonData
|
|
8885
9045
|
});
|
|
8886
9046
|
};
|
|
8887
9047
|
this.addEventListener(button, 'click', speedButtonHandler);
|
|
@@ -8992,16 +9152,17 @@ class InteractionService {
|
|
|
8992
9152
|
});
|
|
8993
9153
|
// Emit hover event - Event-Driven Architecture
|
|
8994
9154
|
// Provides rich context for tooltip display, analytics, and custom behavior
|
|
9155
|
+
const hoverData = {
|
|
9156
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9157
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9158
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9159
|
+
mousePosition: { x: event.clientX, y: event.clientY } // Coordinates for tooltip positioning
|
|
9160
|
+
};
|
|
8995
9161
|
this.eventBus.emit({
|
|
8996
9162
|
type: 'interaction.hover',
|
|
8997
9163
|
timestamp: Date.now(),
|
|
8998
9164
|
source: 'InteractionService',
|
|
8999
|
-
data:
|
|
9000
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9001
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9002
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9003
|
-
mousePosition: { x: event.clientX, y: event.clientY } // Coordinates for tooltip positioning
|
|
9004
|
-
}
|
|
9165
|
+
data: hoverData
|
|
9005
9166
|
});
|
|
9006
9167
|
// Call custom handler if provided - Extensibility Pattern
|
|
9007
9168
|
// Supports application-specific hover behavior
|
|
@@ -9043,16 +9204,17 @@ class InteractionService {
|
|
|
9043
9204
|
// element.classList.add('selected');
|
|
9044
9205
|
// Emit click event - Event-Driven Architecture
|
|
9045
9206
|
// Provides comprehensive click context for analytics, tooltips, and custom behavior
|
|
9207
|
+
const clickData = {
|
|
9208
|
+
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9209
|
+
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9210
|
+
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9211
|
+
mousePosition: { x: event.clientX, y: event.clientY } // Click coordinates for UI positioning
|
|
9212
|
+
};
|
|
9046
9213
|
this.eventBus.emit({
|
|
9047
9214
|
type: 'interaction.click',
|
|
9048
9215
|
timestamp: Date.now(),
|
|
9049
9216
|
source: 'InteractionService',
|
|
9050
|
-
data:
|
|
9051
|
-
elementType: element.classList.contains('evs-flow') ? 'flow' : 'box', // Element classification
|
|
9052
|
-
fuel: element.getAttribute('data-fuel'), // Energy source identification
|
|
9053
|
-
sector: element.getAttribute('data-sector'), // Consumption sector identification
|
|
9054
|
-
mousePosition: { x: event.clientX, y: event.clientY } // Click coordinates for UI positioning
|
|
9055
|
-
}
|
|
9217
|
+
data: clickData
|
|
9056
9218
|
});
|
|
9057
9219
|
// Call custom handler if provided - Extensibility Pattern
|
|
9058
9220
|
// Supports application-specific click behavior
|