Skip to content

Instantly share code, notes, and snippets.

@passcod
Last active December 16, 2015 04:59
Show Gist options
  • Select an option

  • Save passcod/5381385 to your computer and use it in GitHub Desktop.

Select an option

Save passcod/5381385 to your computer and use it in GitHub Desktop.
Notes on Dyno.js

Default at 3 workers. (Most processors today on both desktop and mobile are quad-core, => 1 main thread + 3 workers). Spawn them immediately.

An actor is a single function. Prototype additions are not taken into account (not straightforward to serialise). It behaves like a function, but async: arguments you give to the actor are passed through unaltered (but they must comply with the serialisation restrictions).

There's two ways to signify completion and both are bubbled up as events: return something; and this.done(something);. The something argument is optional and can be multiple in the case of the this.done method. The this.done() method can be called multiple times per actor, making it useful for map-reduce operations. Both ways can be used simultaneously.

Example

var Cartographist = new Dyno.Actor(function aCartographist(countriesToMap) {
  var self = this;
  var canDo = _(countriesToMap).filter(haveEnoughDataToMap);
  canDo.each(function asyncMappingProcess(countryData) {
    var assistant = new MapMakingAssistant();
    assistant.on("return", function(countryMap) {
      self.done(countryData.id, countryMap);
    });
    assistant.run(countryData);
  });

  // Will return with the amount of data
  // that is being processed immediately,
  // while the actual processing will be
  // running in the meantime.
  return canDo.length;
});

// Call with:
var mapMaker = new Cartographist();
mapMaker.on("return", function(dataLength) {
  giveSomeFeedbackMaybe("Making %s maps", dataLength);
});
mapMaker.on("done", function(countryId, countryMap) {
  displayThisMap(countryId, countryMap);
});
mapMaker.run(someCountries);
@jeckhart

Copy link
Copy Markdown

Where can I find dyno.js?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment