Coder Social home page Coder Social logo

tart-adapter's Introduction

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

tart-adapter's People

Contributors

tristanls avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.