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

usync

v0.0.18

Published

A lightweight library used for serial task control

Downloads

95

Readme

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.