Skip to content

Adapter turning synchronous functions into asynchronous Tart actors.

License

Notifications You must be signed in to change notification settings

tristanls/tart-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tart-adapter

Stability: 1 - Experimental

NPM version

Adapter turning synchronous functions into asynchronous Tiny Actor Run-Time in JavaScript actors.

Contributors

@dalnefre, @tristanls

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 test

Documentation

Public API

adapter(obj, fn)

  • obj: Object (Default: {}) Object to bind this to when invoking fn.
  • fn: Function Function to invoke on obj.
  • Return: Behavior function (message) {} An actor behavior that will call fn with message.arguments and return result as a message to message.ok. If an exception is thrown, it will be sent to message.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.

Sources

About

Adapter turning synchronous functions into asynchronous Tart actors.

Resources

License

Stars

Watchers

Forks

Packages

No packages published