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

disposable-cls

v1.0.17

Published

Provides disposable continuation local storage for Node.js.

Downloads

29

Readme

Continuation Local Storage (CLS)

Continuation Local Storage (CLS) works like thread local storage in most threaded programming languages, but is instead based on chains of asynchronous callbacks rather than threads. This package allows you to set and get values that are scoped to the lifetime of these asynchronous continuations without having to pass the values via parameters or use closures, and also provides support for 'disposable' semantics at the end of the asynchronous callback chain. This is a pattern that will be familiar to Microsoft .NET developers who may have used IDisposable implementations in conjunction with using scopes.

This package was heaviliy inspired by https://github.com/othiym23/node-continuation-local-storage

var using = require("disposable-cls").using;
var getCurrentObject = require("disposable-cls").getCurrentObject;

using([new HttpRequest()], function() {
	...
	var currentRequest = getCurrentObject(HttpRequest);
	...
});

The using() function establishes scope for the entire duration of the supplied function, regardless of whether that function executes synchronously, or makes further asynchronous invocations. Furthermore, if the supplied context objects define a dispose() function then it will be invoked when all synchronous and asynchronous functions complete inside the supplied function.

Exported Functions

using

using(contextItems: Object[], asyncFunction: Function)

Creates scope that will be preserved for the lifetime of the supplied asynchronous invocations.

contextItems: An array of objects that will be scoped. asyncFunction: The root asynchronous function for which scope is to be preserved.

getCurrentObject

getCurrentObject(objectType: Function): Object

Retrieves an object from the current scope.

objectType: A constructor function representing the type of object that should be returned.

Returns an object from the current scope that is of the supplied type; or undefined if no object of that type could be found in the current scope.

bindEventEmitter

bindEventEmitter(emitter: EventEmitter)

Binds the specified EventEmitter to the currently active context stack so that listeners added within that scope may access the context items.

emitter: The EventEmitter to bind.

Nested scopes

Use of the using() function can be nested to create scope hierarchies, and code can continue to access context objects that have been established at any level in those scope hierarchies:

using([new HttpRequest()], function() {
	...
	using([new DataTransaction()], function() {
		// Can still access the outer scope context objects
		var currentRequest = getCurrentObject(HttpRequest);
		
		var currentTransaction = getCurrentObject(DataTransaction);
	});
	...
});

Example Ambient Context Implementation (TypeScript)

The following code demonstrates a typical class definition for implementing ambient context that will be preserved across all continuations encountered inside a using() block.

export class MyContext {
	private _state: any;
	
	constructor(state: any) {
		this._state = state;			
	}
	
	public get state(): any {
		return this._state;
	}
	
	public static get current(): MyContext {
		return <MyContext>getCurrentObject(MyContext);
	} 	
}


using([new MyContext({ data: "Some Data" })], () => {
	let context = MyContext.current;
	
	if (context) {
		doSomethingWith(context.state);
	}
);

EventEmitters

Callbacks registered as listeners for EventEmitters are not processed in the same way that asynchronous functions are. As such, EventEmitter instances must be explicitly bound within the currently active scope using the bindEventEmitter() function if they are to require access to it.

For example:

let emitter = new EventEmitter();

using([new MyContext({ data: "Some Data" })], () => {
	emitter.on("my-event", () => {
		let currentState = MyContext.current.state;
		
		// MyContext.current will be undefined
	});
);

should be written to use the bindEventEmitter() function as follows:

let emitter = new EventEmitter();

using([new MyContext({ data: "Some Data" })], () => {
	bindEventEmitter(emitter);
	
	emitter.on("my-event", () => {
		let currentState = MyContext.current.state;
		
		// MyContext.current will now be the currently in scope MyContext object
	});
);

It should be noted that any context objects that are bound to EventEmitter's will not be disposed of when all asynchronous functions complete since the events emitted may occur beyond the scope of the using() block. As a result, EventEmitters inside using() blocks should be used sparingly and with caution.