diff --git a/src/engine.js b/src/engine/engine.js similarity index 98% rename from src/engine.js rename to src/engine/engine.js index 89bf7bf1..ab5c6de5 100644 --- a/src/engine.js +++ b/src/engine/engine.js @@ -1,5 +1,5 @@ -import { Component } from './component'; -import { Utils } from './utils'; +import { Component } from '../component'; +import { Utils } from '../utils'; var State = { AVALIABLE:0, PROCESSED: 1, ABORT: 2}; diff --git a/src/engine/task.js b/src/engine/task.js new file mode 100644 index 00000000..8ffae04e --- /dev/null +++ b/src/engine/task.js @@ -0,0 +1,62 @@ +export class Task { + + constructor(inputs, action) { + this.inputs = inputs; + this.action = action; + this.next = []; + this.outputData = null; + this.closed = []; + + this.getOptions().forEach(input => { + input.forEach(con => { + con.task.next.push({index: con.index, task: this}); + }) + }); + } + + getOptions() { + return this.inputs.filter(input => input[0] && input[0].task) + } + + getOutputs() { + return this.inputs.filter(input => input[0] && input[0].get); + } + + reset() { + this.outputData = null; + } + + run(data) { + var inputs = this.getOutputs().map(input => { + return input.map(con => { + if (con) { + con.run(); + return con.get(); + } + }) + }); + + if (!this.outputData) { + this.outputData = this.action(inputs, data); + + this.next.filter(f => !this.closed.includes(f.index)).forEach(f => f.task.run()); + } + } + + option(index) { + var task = this; + + return {task, index} + } + + output(index) { + var task = this; + + return { + run: task.run.bind(task), + get() { + return task.outputData[index]; + } + } + } +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index e51ef61e..f568ad7b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,14 @@ import { Component } from './component'; import { ContextMenu } from './contextmenu'; import { Control } from './control'; -import { Engine } from './engine'; +import { Engine } from './engine/engine'; import { Group } from './group'; import { Input } from './input'; import { Node } from './node'; import { NodeEditor } from './editor'; import { Output } from './output'; import { Socket } from './socket'; +import { Task } from './engine/task'; export { Component, @@ -19,5 +20,6 @@ export { Input, Node, Output, - Socket + Socket, + Task } \ No newline at end of file