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

context-block

v1.0.20

Published

Contextualized promises by name and symbol

Downloads

35

Readme

#context-block Contextualized promise blocks by name and symbol. Supports async, promises, generators, and manual reject/resolve.


###What/Why? context-block is intended to assist in managing promises and asynchronous behaviors.

Imagine you want to update a UI based on the result of a service request - or multiple service requests without blocking user input. Maybe you are writing server logic and you want to ensure that you avoid sending multiple of database lookups simultaneously.

Context-block provides a developer friendly syntax that will ensure multiple of the same asynchronous activities are not occurring.


###Install

npm install --save-dev context-block

API Reference

  • Block
  • Context
  • ReverseContext
  • Blocks are segments of code associated with a name. Blocks can either be Forward or Reverse. Forward blocks ALWAYS execute the latest code segment. Reverse blocks only execute the latest code segment if one is not already scheduled to run.

    Blocks return Contexts or ReverseContexts (which extend Promise) and associate the behavior of an asynchronous action with a name. A name can be either string or symbol, allowing asynchronous behaviors to be managed at various hierarchies (component vs global levels). Blocks also support a tagged literal format to help differentiate Blocks from other code (example)

    There are three forward and reverse block behavior functions defined.

    • block.dismiss(name[,fn,delay]) Returns a Context for name and executes fn. If another block of the same name is created, the returned Context will be dismissed (rejected).
    • block.stop(name[,fn,delay])Returns a Context for name and executes fn. If another block of the same name is created, the returned Context will NOT be resolved or rejected.
    • block.join(name[,fn,delay]) Returns a Context for name and executes fn. If another block of the same name is created, the returned Context will resolve/reject with the result of the new block.
    • block.reverse.dismiss(name[,fn,delay])Returns a ReverseContext for name and only executes fn if another block of the same name is not already scheduled to activate. If another block already exists, the returned ReverseContext will be automatically dismissed.
    • block.reverse.stop(name[,fn,delay])Returns a ReverseContext for name and only executes fn if another block of the same name is not already scheduled to activate. If another block already exists, the returned ReverseContext will not resolve or reject
    • block.reverse.join(name[,fn,delay])Returns a ReverseContext for name and only executes fn if another block of the same name is not already scheduled to activate. If another block already exists, the returned ReverseContext will resolve or reject with the result of the existing block. Additionally, if a block of the same name is created, the returned ReverseContext will resolve/reject with the result of the new block

    Blocks accept the following arguments:


  • Contexts are the returned value from a Block and are not intended to be constructed directly. Contexts extend from promises and integrate Promise functionality such as Promise.all, Promise.race (example) and much more!

    The premise behind Contexts are that multiple contexts can potentially exist for a given Block name. However, each context decides how to act in the event multiple Contexts of the same name are created. Context actions include:

    • dismiss Rejects if another Context of the same name is created
    • stop Does not resolve or reject if another Context of the same name is created
    • join Resolves or rejects with the latest result

  • ReverseContexts take Contexts a step further by deciding the action if a Block already exists.

    ReverseContexts work under the premise that priority lies with an earlier Context, which naturally changes the meaning of dismiss, stop, and join:


###Examples

const block = require('context-block');

//tagged literal block with unspecified dismiss,stop,join defaults to dismiss.
block `test` (({reject,resolve})=>{
	//never called
}).then(()=>{
	//never called
}).catch((v)=>{
	console.error(v); // "dismissed"
});

//tagged literal block with specified method
//context will be dismissed as another context will activate with the same name
block.dismiss `test` (()=>{
	return new Promise((resolve,reject)=>{
		resolve('hello world');
	});
}).catch ((v)=>{
	console.error(v); //v is "dismissed"
}); 

//untagged equivalents for reference
//another context of the same is defined again, so nothing nothing will get called
block.stop('test',({reject,resolve})=> {
	//never called
}).catch((v)=>{
    	//never called
});

//join context will result in "world" because the last context resolves "world"
block('test').join(({reject,resolve})=>{
	resolve('hello');
}).then((v)=>{
    	console.error(v); //v is "world"
});

block.join `test` (({reject,resolve})=>{
	resolve('world');
}).then((v)=>{
    	console.error(v); //v is "world"
});

OUTPUT:
dismissed
dismissed
world
world
block.dismiss `test` (new Promise((resolve,reject)=>{
	resolve('hi');
})).then((v)=>{
	console.error(v); //v is "hi"
});


OUTPUT:
hi
//note the es6 destructure syntax
//if you specify resolve, reject arguments, you must resolve the context using them!!!
block `test` (({resolve,reject})=>{
	resolve(1);
}).then((v)=>{
	console.error(v); //v is 1
});

OUTPUT:
1
block `test` (()=>{
	return new Promise((resolve,reject)=>{
		resolve(2);
	});
}).then((v)=>{
	console.error(v); //v is 2
});

OUTPUT:
1
async function getSomething () {
	return new Promise((resolve,reject)=>{
		resolve('banana');
	});
}
block `test` (async ()=>{
	return await getSomething('for my monkey');
}).then((v)=>{
	console.error(v); //v is "banana"
});

OUTPUT:
banana
block `test` (function * () {
	yield new Promise((resolve,reject)=>{
		setTimeout(()=>resolve(1),20)
	});
	yield 2;
	yield new Promise((resolve,reject)=>{
		resolve(3);
	});
	return 4
}).then((v)=>{
	console.error(v); //v is [1,2,3,4]
});

OUTPUT:
1,2,3,4
Promise.all([
	block `test1` (({resolve,reject})=>{resolve(1);}),
	block `test2` (({resolve,reject})=>{resolve(2);})
]).then((v)=>{
	console.log(v);
});


OUTPUT:
1,2

------------

Promise.race([
	block `test1` (({resolve,reject})=>{resolve(1);}),
	block `test2` (({resolve,reject})=>{setTimeout(()=>resolve(2));})
]).then((v)=>{
	console.log(v);
});


OUTPUT:
1