superfly-timeline
Version:
Resolver for defining objects with temporal boolean logic relationships on a timeline
89 lines • 3.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResolverHandler = void 0;
const ResolvedTimelineHandler_1 = require("./ResolvedTimelineHandler");
const performance_1 = require("./lib/performance");
const CacheHandler_1 = require("./CacheHandler");
const TimelineValidator_1 = require("./TimelineValidator");
const Error_1 = require("./lib/Error");
/**
* Note: A Resolver instance is short-lived and used to resolve a timeline.
* Intended usage:
* 1. const resolver = new Resolver(options)
* 2. resolver.run(timeline)
*/
class ResolverHandler {
constructor(options) {
this.options = options;
this.hasRun = false;
const toc = (0, performance_1.tic)('new Resolver');
this.resolvedTimeline = new ResolvedTimelineHandler_1.ResolvedTimelineHandler(this.options);
this.validator = new TimelineValidator_1.TimelineValidator();
if (this.options.traceResolving) {
this.resolvedTimeline.addResolveTrace(`init`);
}
toc();
}
/**
* Resolves a timeline, i.e. resolves the references between objects
* This method can only be run once per Resolver instance.
*/
resolveTimeline(timeline) {
try {
const toc = (0, performance_1.tic)('resolveTimeline');
/* istanbul ignore if */
if (this.hasRun) {
if (this.options.traceResolving)
this.resolvedTimeline.addResolveTrace(`Error: has already run`);
throw new Error(`Resolver.resolveTimeline can only run once per instance!
Usage:
const resolver = new Resolver(options);
resolver.run(timeline);`);
}
this.hasRun = true;
if (this.options.traceResolving) {
this.resolvedTimeline.addResolveTrace(`resolveTimeline start`);
this.resolvedTimeline.addResolveTrace(`timeline object count ${timeline.length}`);
}
// Step 0: Validate the timeline:
if (!this.options.skipValidation) {
this.validator.validateTimeline(timeline, false);
}
// Step 1: Populate ResolvedTimeline with the timeline:
for (const obj of timeline) {
this.resolvedTimeline.addTimelineObject(obj);
}
if (this.options.traceResolving) {
this.resolvedTimeline.addResolveTrace(`objects: ${this.resolvedTimeline.objectsMap.size}`);
}
// Step 2: Use cache:
let cacheHandler;
if (this.options.cache) {
if (this.options.traceResolving)
this.resolvedTimeline.addResolveTrace(`using cache`);
cacheHandler = this.resolvedTimeline.initializeCache(this.options.cache);
cacheHandler.determineChangedObjects();
}
// Step 3: Go through and resolve all objects:
this.resolvedTimeline.resolveAllTimelineObjs();
// Step 5: persist cache
if (cacheHandler) {
cacheHandler.persistData();
}
if (this.options.traceResolving)
this.resolvedTimeline.addResolveTrace(`resolveTimeline done!`);
const resolvedTimeline = this.resolvedTimeline.getResolvedTimeline();
toc();
return resolvedTimeline;
}
catch (e) {
if (this.options.cache) {
// Reset cache, since it might be corrupt.
CacheHandler_1.CacheHandler.resetCache(this.options.cache);
}
throw new Error_1.ResolveError(e, this.resolvedTimeline.getResolvedTimeline());
}
}
}
exports.ResolverHandler = ResolverHandler;
//# sourceMappingURL=ResolverHandler.js.map