Coder Social home page Coder Social logo

chain's Introduction

@lyngs/chain

@lyngs/chain is a module that can be use to build onion-model basis chain object.

Install

Install @lyngs/chain with NPM:

npm i -s @lyngs/chain

Initialize

To create a chain, just simply call chain():

import { chain } from '@lyngs/chain';
const instance = chain();

Create with params:

const instance = chain({ caller: this }, someValue, anotherValue);

Properties

chain-instance provides some properties to be use:

Properties of chain-instance

use(injection)

To add a chain-node, use method use():

instance.use(async (context, next) => {
	context.to = 'forward'; // update context
	await next(); // push chain to the next node
});

context in chain will continualy be passed in chain-nodes, across the whole life-circle of chain.

// first-node
instance.use(async (context, next) => {
	context.to = 'forward';
	await next();
});
// last-node
instance.use(async (context, next) => {
	console.log(context.to); // 'forward'
	await next();
});

When all chain-nodes finished their jobs, stacks start to pop up.

Behavior like this, it's looking just like an onion, so that's why we call it onion-model:

// first-node
instance.use(async (context, next) => {
	console.log(context.to); // undefined
	await next();
	console.log(context.to); // 'backward'
});
// last-node
instance.use(async (context, next) => {
	await next();
	context.to = 'backward';
});

context(...params)

If you're trying to merge something to context, use method context():

instance.use(async (context, next) => {
	instance.context({
		to: 'forward'
	})
	await next();
});

start()

Use this method to start the whole chain progress.

chain-nodes you added will line up in a queue, waiting to be call:

instance
	.use(...)
	.use(...)
	.start();

cancel()

If your're trying to cancel or stop the whole chain progress, you can use this method:

instance
	.use(...)
	.use(async (context, next) => {
		if (context.catch) {
			instance.cancel();
		}
		await next();
	})
	.use(...) // if context.catch exists, then this chain-node won't be called
	.start();

Or you can simply cancel the progress by passing a param in the calling of next():

instance
	.use(...)
	.use(async (context, next) => {
		await next(!context.catch);
	})
	.use(...) // if context.catch exists, then this chain-node won't be called
	.start();

hack(injection: (rootContext) โ‡’ rootContext)

[Warn] you need to be careful with this function.

Inside of chain-instance, there are some properties hidden in.

Those are what developers normally wouldn't need to know about.

But to make sure the module have the abblity to grow, there is an API for developers to visit those hidden properties.

Method hack() can help you hack in the rootContext property of chain-instance, and then you can edit it manually:

instance.hack((root) => {
	root.queue.unshift({ ... }); // editing property 'queue' manually
	return root;
});

One thing must be clear:

editing the rootContext might break the layout of chain-instance.

And when the layout broke, chain-instance might not work as it should be.

So if you must use this function, then treat rootContext carefully.

chain's People

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.