npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fiber-utils

v0.10.0

Published

Various fiber utilities

Downloads

63

Readme

Fiber utilities

A package which provides utilities for node fibers.

Installation

NPM:

npm install fiber-utils

Meteor:

meteor 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:

var Fiber = require('fibers');
var Future = require('fibers/future')
var 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:

  • allowRecursive (default true): do you want to allow recursive reentry of a critical section within the same fiber
  • allowNested (default true): do you want to allow nested critical sections, which can lead to deadlocks
  • breakDeadlocks (default true): if we detect a deadlock, we can break the deadlock by making one critical section throw an exception

Examples

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