@comunica/core
Advanced tools
Comparing version 1.8.0 to 1.9.0
export * from './lib/Bus'; | ||
export * from './lib/BusIndexed'; | ||
export * from './lib/ActionObserver'; | ||
@@ -3,0 +4,0 @@ export * from './lib/Actor'; |
@@ -10,2 +10,3 @@ "use strict"; | ||
__export(require("./lib/Bus")); | ||
__export(require("./lib/BusIndexed")); | ||
__export(require("./lib/ActionObserver")); | ||
@@ -12,0 +13,0 @@ __export(require("./lib/Actor")); |
@@ -23,2 +23,3 @@ import { Map } from "immutable"; | ||
readonly bus: Bus<Actor<I, T, O>, I, T, O>; | ||
readonly beforeActors: Actor<I, T, O>[]; | ||
/** | ||
@@ -95,2 +96,3 @@ * All enumerable properties from the `args` object are inherited to this actor. | ||
bus: Bus<Actor<I, T, O>, I, T, O>; | ||
beforeActors?: Actor<I, T, O>[]; | ||
} | ||
@@ -97,0 +99,0 @@ /** |
@@ -34,4 +34,8 @@ "use strict"; | ||
constructor(args) { | ||
this.beforeActors = []; | ||
require('lodash.assign')(this, args); | ||
this.bus.subscribe(this); | ||
if (this.beforeActors.length) { | ||
this.bus.addDependencies(this, this.beforeActors); | ||
} | ||
} | ||
@@ -38,0 +42,0 @@ /** |
@@ -22,2 +22,3 @@ import { ActionObserver } from "./ActionObserver"; | ||
protected readonly observers: ActionObserver<I, O>[]; | ||
protected readonly dependencyLinks: Map<A, A[]>; | ||
/** | ||
@@ -87,2 +88,15 @@ * All enumerable properties from the `args` object are inherited to this bus. | ||
onRun(actor: Actor<I, T, O>, action: I, output: Promise<O>): void; | ||
/** | ||
* Indicate that the given actor has the given actor dependencies. | ||
* | ||
* This will ensure that the given actor will be present in the bus *before* the given dependencies. | ||
* | ||
* @param {A} dependent A dependent actor that will be placed before the given actors. | ||
* @param {A[]} dependencies Actor dependencies that will be placed after the given actor. | ||
*/ | ||
addDependencies(dependent: A, dependencies: A[]): void; | ||
/** | ||
* Reorder the bus based on all present dependencies. | ||
*/ | ||
reorderForDependencies(): void; | ||
} | ||
@@ -89,0 +103,0 @@ export interface IBusArgs { |
@@ -29,2 +29,3 @@ "use strict"; | ||
this.observers = []; | ||
this.dependencyLinks = new Map(); // Mapping from dependency (after) to dependents (before) | ||
require('lodash.assign')(this, args); | ||
@@ -42,2 +43,3 @@ } | ||
this.actors.push(actor); | ||
this.reorderForDependencies(); | ||
} | ||
@@ -115,4 +117,65 @@ /** | ||
} | ||
/** | ||
* Indicate that the given actor has the given actor dependencies. | ||
* | ||
* This will ensure that the given actor will be present in the bus *before* the given dependencies. | ||
* | ||
* @param {A} dependent A dependent actor that will be placed before the given actors. | ||
* @param {A[]} dependencies Actor dependencies that will be placed after the given actor. | ||
*/ | ||
addDependencies(dependent, dependencies) { | ||
for (const dependency of dependencies) { | ||
let existingDependencies = this.dependencyLinks.get(dependency); | ||
if (!existingDependencies) { | ||
existingDependencies = []; | ||
this.dependencyLinks.set(dependency, existingDependencies); | ||
} | ||
existingDependencies.push(dependent); | ||
} | ||
this.reorderForDependencies(); | ||
} | ||
/** | ||
* Reorder the bus based on all present dependencies. | ||
*/ | ||
reorderForDependencies() { | ||
if (this.dependencyLinks.size > 0) { | ||
const actorsAfter = []; | ||
// Temporarily remove all actors that have dependencies | ||
for (const actorAfter of this.dependencyLinks.keys()) { | ||
const dependentPos = this.actors.indexOf(actorAfter); | ||
if (dependentPos >= 0) { | ||
this.actors.splice(dependentPos, 1); | ||
actorsAfter.push(actorAfter); | ||
} | ||
} | ||
// Iteratively append actors based on the first dependency link | ||
// that has all of its dependencies available in the array | ||
while (actorsAfter.length > 0) { | ||
// Find the first actor that has all of its dependencies available. | ||
let activeActorAfterId = -1; | ||
for (let i = 0; i < actorsAfter.length; i++) { | ||
let validLink = true; | ||
for (const dependency of this.dependencyLinks.get(actorsAfter[i])) { | ||
if (this.actors.indexOf(dependency) < 0 && actorsAfter.indexOf(dependency) >= 0) { | ||
validLink = false; | ||
break; | ||
} | ||
} | ||
if (validLink) { | ||
activeActorAfterId = i; | ||
break; | ||
} | ||
} | ||
// If none of the pending links are possible, there must be a cyclic dependency | ||
if (activeActorAfterId < 0) { | ||
throw new Error('Cyclic dependency links detected in bus ' + this.name); | ||
} | ||
// The dependent may not be available (yet), so we don't add it to the array (yet). | ||
const activeActorAfter = actorsAfter.splice(activeActorAfterId, 1)[0]; | ||
this.actors.push(activeActorAfter); | ||
} | ||
} | ||
} | ||
} | ||
exports.Bus = Bus; | ||
//# sourceMappingURL=Bus.js.map |
{ | ||
"name": "@comunica/core", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "Lightweight, semantic and modular actor framework", | ||
@@ -68,3 +68,3 @@ "lsd:module": "https://linkedsoftwaredependencies.org/bundles/npm/@comunica/core", | ||
}, | ||
"gitHead": "7e65817dad65cf10c032f7548b37cedf3055eea1" | ||
"gitHead": "2e0cba815fd65d5660f15c96ffc734f9099d8b6a" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49481
25
1022