Code Monkey home page Code Monkey logo

usync's Introduction


NPM version NPM downloads Build status

usync

The core of Usync is uniform serial execution, which can be run in browser or Node.js enviroment.

In addition, it supports task segmentation and task tree design. The root task state is added by default. Because Usync is very lightweight, core API is scarce, so Usync provides a set of hook and extension methods for life cycles that allow you to easily extend Usync.

app.use([task1,task2,task3 ... ]).start()

中文请移步 README_zh_CN


Quick Start

Install

npm i usync -S
// or
yarn add usync

If you want to test Usync in the browser, you can use the CDN as follows:

First usync app

var Usync = require('Usync')
// Create a task whose name is 'Work'
var app = Usync.app('Work')

// Define three subtasks
function task1(root, next) {
    // root: The root state that continues to pass down during the life cycle of 'Work' task
    // next: Call the next task
    setTimeout(() => next(), 200)
}

function task2(root, next) {
    setTimeout(() => next(), 300)
}

function task3(root, next) {
    setTimeout(() => next(), 200)
}

// Define task queues
app.use([task1,task2,task3])

// Running task
app.start()

Features

  1. The type of subtask supports: Function / Usync / Promise / Async Function
  2. Provide Lifecycle Hook and Plugin mechanisms to facilitate extension
  3. Provide a root state (Root State) which is valid in the task life cycle by default

API


Usync.app([state])

  • state Array | Object | String
  • return value A Usync app instance

It's important to note that the state parameter is optional, and when the entire life cycle of the app needs to depend on a certain initialization state, we can inject it through state, for example:

let root = {
    // Initialize root status
} 
let app = Usync.app(root)

Next, we can use the root state at each task in the app lifecycle:

app.use(function task1(root) {
    fx(root) // Perform some operations on the root state
});

In addition, Usync initializes some values for the root state:

attribute description
root.$current Current task
root.$prev previous task
root.$next next task

Note: when the state is not set, the Usync constructor will generate a empty root state object by default


Usync.prototype.use(task)

  • task Function | Usync | Promise | Async Function | Array
  • return value this

This method is used to add subtasks to the Usync App. The use() method supports chain calls and also supports the introduction of an array.

app.use(task1).use(task2).use(task3)
// Equivalent to
app.use([task1, task2, task3])

The execution order of the above examples is: task1 => task2 => task3

The example of use() usage can be seen in the following example:

Type Example
Function Demo
Promise Demo
Async/Await Demo
Usync Demo

Note: The task needs to be a named function. Unlike the design philosophy (taskName, taskHandler) of orchestrator and its gulp , Usync requires only one taskHandler, and Usync will use the name of taskHandler as the task's name.

You can run the examples provided by this project with git clone

git clone https://github.com/toxichl/usync.git
npm i && npm run example

Note:The above log effect is not built-in at Usync ,Because Usync core doesn't have any unique API of Node or Browser. This log effect is implemented through a plug-in logger of Usync.


Life Cycle

The hook function of the life cycle provided by Usync is as follows:

Hook Parameter Description
init (UsyncApp) Before the end of an Usync app creation
beforeUse (UsyncApp, task) Before a task is going to be used in a Usync app
appStart (root) Before a Usync app starts running
appEnd (root) Before a Usync app is finished running
taskStart (root) Before a task starts running
taskEnd (root) Before a task is finished running

The properties available on a task are as follows:

Attribute Description
task.name task's name
task.$parent task's parent

About how to use these hooks, need the help of Usync.extend() or Usync.prototype.extend, please continue to look down.。


Usync.extend(object)

  • object An object consisting of one or more life cycle hook processing functions
  • return value null

extend () accept an object as parameters, the object can contain multiple attributes, the attribute name for life cycle's name, the attribute value is a handler function, About the incoming parameter of handler function, please see last section. A simple extend() example is as follows:

Usync.extend({
    taskStart(root) {
        console.log(`Starting ${root.$current.name}`)
    },
    taskEnd(root) {
        console.log(`Finished ${root.$current.name}`)
    }
})

In fact, this is the core part of implementing the plug-in [logger] (plugins/logger.js). Is it very simple?


Usync.prototype.extend(object)

  • object An object consisting of one or more life cycle hook processing functions
  • return value null

Same to the Usync.extend(), the difference of them is that the Usync.extend() will influence all the UsyncApp, But Usync.prototype.extend() is only valid for the current UsyncApp. Please choose flexibly according to different scenes.


Usync.plugin(plugin)

  • plugin Object | Function
  • return value null

The plug-in's API design is inspired by Vue

Usync uses a plug-in API design that is consistent with Vue, and the Usync plug-in should have an open method, install. The first parameter of this method is the constructor of the Usync, and the second parameter is the optional option object.

You can refer to the implementation of logger to learn how to combine a lifecycle hook and plugin API to write a plug-in for Usync.


Author

usync © toxichl, Released under the MIT License.

usync's People

Contributors

ulivz avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

usync's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet.
We recommend using:

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

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.