peerlibrary/node-fiber-utils

Name: node-fiber-utils

Owner: PeerLibrary

Description: Various fiber utilities

Created: 2015-07-02 06:59:39.0

Updated: 2017-10-13 09:44:39.0

Pushed: 2018-01-15 09:14:09.0

Homepage:

Size: 31

Language: CoffeeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Fiber utilities

A package which provides utilities for node fibers.

Installation

NPM:

install fiber-utils

Meteor:

or add peerlibrary:fiber-utils
Initialization (NPM)

NPM package does not depend directly on fibers package because you have to make sure you have loaded fibers package only once. This is why you should install fibers package yourself, and then initialize this package using symbols imported from the fibers package:

Fiber = require('fibers');
Future = require('fibers/future')
FiberUtils = new require('fiber-utils').FiberUtils(Fiber, Future);
Initialization (Meteor)

Meteor package exports initialized FiberUtils symbol automatically. It is initialized with fibers package provided by Meteor and shared through all its code and packages.

Usage
FiberUtils.sleep(ms)

Sleep for ms milliseconds inside a current fiber.

FiberUtils.wrap(f, scope)

Returns a fiber-enabled synchronous function, which when called will pass any arguments to the original function f, and then wait for the function to finish inside a current fiber. You can optionally bind this to scope during execution of f.

FiberUtils.in(f, scope, handleErrors)

Wrap function f in a way to assure that it is run inside a fiber. If the wrapped function is called already inside a fiber, it is simply normally executed. But if it is outside of any fiber, then a new fiber is constructed and function f is executed inside it. You can optionally bind this to scope during execution of f.

If you provide handleErrors, any exception thrown from a execution of f is passed to handleErrors. If handleErrors is not provided, exception propagates on unhandled.

FiberUtils.ensure(f, scope, handleErrors)

Similar to FiberUtils.in(f, scope, handleErrors), but it also calls wrapped function immediately.

FiberUtils.synchronize(guardObject, uniqueId, f, options)

Calls function f in a way that only one fiber can call it at a time, and all other calls are queued. After one execution of f finished, the next queued call of f starts. This allows one to implement critical sections of your code in which you want to ensure only one fiber is running it, even if the fiber yields during the execution of the critical section.

guardObject serves as an object to synchronize on. You could use one object for everything, or you might want a fine-grained synchronization and for example use a current object in a method to limit synchronization to each instance of a class.

uniqueId is an ID of this critical section. Again, allows you to decide how granular you want your synchronization to be. It is namespaced to guardObject.

There are the following options available:

Examples
s Bank {
ansfer(from, to, amount) {
FiberUtils.ensure(() => {
  FiberUtils.synchronize(this, 'transfer', () => {
    from.decrease(amount)
    to.increase(amount)
  })
})
 


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.