UNPKG

1.65 kBJavaScriptView Raw
1var Pipeline = module.exports = function (controller) {
2 this.steps = [];
3 this.controller = controller;
4}
5
6Pipeline.prototype.addStep = function (step) {
7 this.steps.push(step);
8}
9
10Pipeline.prototype.run = function (frame) {
11 var stepsLength = this.steps.length;
12 for (var i = 0; i != stepsLength; i++) {
13 if (!frame) break;
14 frame = this.steps[i](frame);
15 }
16 return frame;
17}
18
19Pipeline.prototype.removeStep = function(step){
20 var index = this.steps.indexOf(step);
21 if (index === -1) throw "Step not found in pipeline";
22 this.steps.splice(index, 1);
23}
24
25/*
26 * Wraps a plugin callback method in method which can be run inside the pipeline.
27 * This wrapper method loops the callback over objects within the frame as is appropriate,
28 * calling the callback for each in turn.
29 *
30 * @method createStepFunction
31 * @memberOf Leap.Controller.prototype
32 * @param {Controller} The controller on which the callback is called.
33 * @param {String} type What frame object the callback is run for and receives.
34 * Can be one of 'frame', 'finger', 'hand', 'pointable', 'tool'
35 * @param {function} callback The method which will be run inside the pipeline loop. Receives one argument, such as a hand.
36 * @private
37 */
38Pipeline.prototype.addWrappedStep = function (type, callback) {
39 var controller = this.controller,
40 step = function (frame) {
41 var dependencies, i, len;
42 dependencies = (type == 'frame') ? [frame] : (frame[type + 's'] || []);
43
44 for (i = 0, len = dependencies.length; i < len; i++) {
45 callback.call(controller, dependencies[i]);
46 }
47
48 return frame;
49 };
50
51 this.addStep(step);
52 return step;
53};
\No newline at end of file