Stability: 1 - Experimental
Adapter turning synchronous functions into asynchronous Tiny Actor Run-Time in JavaScript actors.
Tart adapter turns synchronous functions into asynchronous Tiny Actor Run-Time in JavaScript actors.
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]});
npm test
Public API
obj
: Object (Default: {}) Object to bindthis
to when invokingfn
.fn
: Function Function to invoke onobj
.- Return: Behavior
function (message) {}
An actor behavior that will callfn
withmessage.arguments
and 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.