0.1.1 • Published 12 years ago
tart-adapter v0.1.1
tart-adapter
Stability: 1 - Experimental
Adapter turning synchronous functions into asynchronous Tiny Actor Run-Time in JavaScript actors.
Contributors
Overview
Tart adapter turns synchronous functions into asynchronous Tiny Actor Run-Time in JavaScript actors.
Usage
To run the below example run:
npm run readme"use strict";
var adapter = require('../index.js');
var tart = require('tart');
var syncCountVal = 0;
var syncCount = function syncCount() {
return ++syncCountVal;
};
var obj = {
value : 0
};
var syncInc = function syncInc(increment) {
this.value = this.value + increment;
return this.value;
};
var asyncCountBeh = adapter(syncCount);
var asyncIncBeh = adapter(obj, syncInc);
var sponsor = tart.minimal();
var asyncCount = sponsor(asyncCountBeh);
var asyncInc = sponsor(asyncIncBeh);
var countOk = sponsor(function countOkBeh(message) {
var self = this.self;
console.log('current count', message);
setTimeout(function () {
asyncCount({ok: self, fail: fail}); // send message to async count
}, 1000);
});
var incOk = sponsor(function incOkBeh(message) {
var self = this.self;
console.log('current inc', message);
var randomInc = Math.floor(Math.random() * 4);
setTimeout(function () {
asyncInc({ok: self, fail: fail, arguments: [randomInc]}); // send message to async inc
}, Math.random() * 1000);
});
var fail = sponsor(function failBeh(message) {
console.error('failure', message);
});
asyncCount({ok: countOk, fail: fail});
asyncInc({ok: incOk, fail: fail, arguments: [1]});Tests
npm testDocumentation
Public API
adapter(obj, fn)
obj: Object (Default: {}) Object to bindthisto when invokingfn.fn: Function Function to invoke onobj.- Return: Behavior
function (message) {}An actor behavior that will callfnwithmessage.argumentsand return result as a message tomessage.ok. If an exception is thrown, it will be sent tomessage.fail.
Sets up an actor behavior that wraps invocation of fn on obj. If obj is not provided, it is set to {}.
message.arguments must be either an Array or a pseudo-array arguments object.
