node-ctx

1.2.0 • Public • Published

node-ctx

A context object inspired by https://godoc.org/golang.org/x/net/context

Install

npm install node-ctx --save

Usage

var context = require('node-ctx');
var ctx = context()
            .withValues({ foo: 'bar' })
            .withTimeout(1000);
 
ctx.on('cancelled', function() {
    console.log('cancelled!');
});

API

context() Factory for creating contexts.

todo() Factory for creating an empty context when it's unclear which to use or one is not available.

Context#withValues(values:object) Creates a child context with parent values plus given values. If there are conflicting values, parent values are overridden.

Context#withDeadline(deadline:date) Creates a child context with given deadline. Once the deadline is reached, the context will cancel itself. If given deadline has already passed, the context will cancel right away. Deadline will not be set if it comes after parent deadline.

Context#withTimeout(ms:number) Convenience function for calling withDeadline by specifying the number of milliseconds from.

Context#cancel() Cancels this context and all child contexts. When an event is cancelled, it always cancels its children.

Context#on(event:string, handler:function) Used to listen for events.

Events

cancelled Emitted with context is cancelled.

Dependents (0)

Package Sidebar

Install

npm i node-ctx

Weekly Downloads

72

Version

1.2.0

License

ISC

Last publish

Collaborators

  • tleef