Created
May 22, 2010 22:06
-
-
Save tmpvar/410402 to your computer and use it in GitHub Desktop.
performer backup..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * | |
| Performer | |
| */ | |
| (function (exports) { | |
| // TODO: async require | |
| var jsemitter = require("jsemitter/lib/jsemitter").jsemitter; | |
| exports.nodeResolvers = { | |
| regular : function(nodeName, meta, performer) { | |
| try { | |
| return require("./nodes/" + nodeName).create(meta, performer); | |
| } catch (e) { | |
| return false; | |
| } | |
| } | |
| }; | |
| exports.performer = function() { | |
| var private = false; | |
| var emitter = new jsemitter(); | |
| var resolvePort = function(port) { | |
| var segments = port.split("/"); | |
| var len=segments.length,idx=0, current=private, found = true; | |
| for (idx; idx<len; idx++) { | |
| if (current[segments[idx]]) { | |
| current = current[segments[idx]]; | |
| } | |
| } | |
| return found && current !== private ? current : false; | |
| }; | |
| var resolveNode = function(nodeName, meta, performerInstance) { | |
| var node = false; | |
| for (var i in exports.nodeResolvers) | |
| { | |
| if (exports.nodeResolvers.hasOwnProperty(i)) { | |
| node = exports.nodeResolvers[i](nodeName, meta, performerInstance); | |
| if (node) { | |
| return node; | |
| } | |
| } | |
| } | |
| return false; | |
| }; | |
| var performer = { | |
| // trigger an output port | |
| // triggering only the port with is the same thing as closing the stream. | |
| trigger : function(port /*, ... */) { | |
| emitter.emit.apply(emitter, arguments); | |
| }, | |
| // load a performance | |
| load : function(performance) { | |
| // Setup the private scope with the incomming performance | |
| private = performance; | |
| // Instantiate nodes | |
| var node, current, idx=0, total = private.nodes.length; | |
| for (idx; idx<total; idx++) { | |
| current = private.nodes[idx]; | |
| current.meta = current.meta || {}; | |
| current.meta.id = idx; | |
| node = resolveNode(current.name, current.meta, performer); | |
| if (!node) { | |
| throw new Error("node could not be resolved - " + current.name); | |
| } | |
| private.nodes[idx] = node; | |
| } | |
| // Setup emitter bindings | |
| var targets, i, len; | |
| for (var output in private.pipes) | |
| { | |
| if (private.pipes.hasOwnProperty(output)) { | |
| targets = private.pipes[output]; | |
| // Resolve the target ports | |
| // handle a single target port | |
| if (typeof(targets) === "string") { | |
| var port = resolvePort(targets); | |
| emitter.addListener(output, port); | |
| // handle an array of target ports | |
| } else { | |
| for (var i=0; i<targets.length; i++) { | |
| var port = resolvePort(targets[i]); | |
| emitter.addListener(output, port); | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| // Run a performance | |
| perform : function(performance) { | |
| // load all the nodes if not loaded | |
| if (private === false) { | |
| performer.load(performance); | |
| } | |
| // Trigger init on the performance | |
| performer.trigger("ports/input/init"); | |
| } | |
| }; | |
| return performer; | |
| } | |
| })(typeof exports !== "undefined" ? exports : this.NAMESPACE = {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment