Coder Social home page Coder Social logo

causeeffect's Introduction

Created to simplify flow management when programming in asynchronous
environments like nodejs.

	CauseEffect extends EventEmitter and allows to set cause-effect scenarios.

	It is simple yet effective.

	CauseEffect allows you to:
	* have an event fired when multiple parallel flows complete.
	* easily unite multiple parallel flows initiated from the for loop.
	* set up chains: completion of one rule may trigger another.
	* set the rules dynamically or statically.
	* have an event fired when any of the flows completes.

	To run example:
		node examples/sample.js

Installation:
	npm install causeeffect

Usage:
	To setup a rule where an event will fire if all of the causes happen:
		ce.setEvents("myevent", ["cause1", "cause2", "cause3"]);

	To setup a rule where an event will fire if some of the causes happen:
		ce.setEvents("myevent", ["cause1", "cause2", "cause3"], true);

	To setup a rule where an event will fire if it happens X times:
		ce.setEvents("myevent", 10);

	Listen to effect event as usual with EventEmitter API:
		ce.on("myevent", callback);

	To let CauseEffect know that the cause has happened call:
		ce.setState("cause1");
	or if you need a specific value use
		ce.setState("cause1", value);
	This value can be then obtained from your event callback with:
		ce.getState("cause1")

	To run example:
		node examples/sample.js

Sample:

1. Uniting multiple parallel flows initiated from the for loop:
	var ce = require('causeeffect');
	ce = new ce.CauseEffect();

	// Set up ticker cause. Event "myticker" will fire if ce.setState("myticker") was invoked 4 times.
	// Handy for figuring out when all callbacks have been called initiated from the for loop.
	ce.setEvents("myticker", 4);

	function callback() {
		ce.setState("myticker");
	}

	for(var i = 0; i< 4; i++) {
		setTimeout(callback, 100);
	}

	ce.on("myticker", function() {
	});


2. Uniting specific parallel flows
	// Set up AND cause effect. Event "alldone" will be fired if all of causes happen
	ce.setEvents("alldone", ["cause1", "cause2", "myticker"]);

	// Listen to event as usual with EventEmitter API
	ce.on("alldone", function() {
	});


	// To let CauseEffect know that the cause has happened 
	ce.setState("cause1");
	ce.setState("cause2");


causeeffect's People

Contributors

blakmatrix avatar olado avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.