GithubHelp home page GithubHelp logo

ready-callback's Introduction

ready-callback

NPM version CI Test coverage npm download

Launch server after all async task ready


Install

$ npm install ready-callback

Usage

Note: ready-callback is using class, so you should use node>=2

var koa = require('koa');
var ready = require('ready-callback')();
var app = koa();
ready.mixin(app);

// register a service
var done = app.readyCallback('service');
serviceLaunch(done);

// callback will be fired after all service launched
app.ready(function() {
  app.listen();
});

Error Handle

If task is called with error, error event will be emit, ready will never be called.

// register a service that will emit error
var done = app.readyCallback('service');
serviceLaunch(function(err) {
  done(err);
});

// listen error event
app.on('error', function(err) {
  // catch error
});

Weak Dependency

If you set a task weak dependency, task will be done without emit error.

var done = app.readyCallback('service', {isWeakDep: true});
serviceLaunch(function(err) {
  done(err);
});

// will be ready
app.ready(function() {
  app.listen();
});

app.on('error', function(err) {
  // never be called
});

You can also set for all ready-callback

var ready = require('ready-callback')({isWeakDep: true});

Ready Status

You can get status every callback end.

app.on('ready_stat', function(data) {
  console.log(data.id); // id of the ended task
  console.log(data.remain); // tasks waiting to be ended
});

Timeout

You can set timeout when a task run a long time.

var ready = require('ready-callback')({timeout: 1000});
ready.mixin(app);
app.on('ready_timeout', function(id) {
  // this will be called after 1s that `service` task don't complete
});

var done = app.readyCallback('service');
serviceLaunch(function() {
  // run a long time
  done();
});

You can also set timeout for every task

ready.mixin(app);
app.on('ready_timeout', function(id) {
  // this will be called after 1s that `service` task don't complete
});

var done = app.readyCallback('service1', {timeout: 1000});
serviceLaunch(done);

lazyStart

You can set a ready-callback object to lazyStart. It will not check ready status immediately, and should start manualy to check ready status.

var ready = require('ready-callback')({ lazyStart: true });
yield sleep(1);
// ready obj is not ready
ready.start();
yield sleep(1);
// ready obj is ready now

LISENCE

Copyright (c) 2015 popomore. Licensed under the MIT license.

Contributors


popomore


fengmk2


semantic-release-bot


greenkeeperio-bot


killagu


liuhanqu

This project follows the git-contributor spec, auto updated at Wed Oct 11 2023 10:27:33 GMT+0800.

ready-callback's People

Contributors

fengmk2 avatar greenkeeperio-bot avatar killagu avatar liuhanqu avatar popomore avatar semantic-release-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ready-callback's Issues

isWeakDep 与 timeout 语义

ready-callback 被广泛应用于控制 app 和 agent 启动。

有时候我们会添加一些弱依赖

const done = app.readyCallback('init xx', { isWeakDep: true, timeout: 3000 });
xx.ready(done);

我们写这段代码的时候,设想的是弱依赖的启动控制项不会影响初始化。

但实际上,如果 xx 启动不成功,在出错或者超时的时候必须 done(err),app 才会继续启动。

isWeakDep 目前的语义是或略 done 传过来的 err,继续去 ready,但是前提是 done 还是被调用一次。

这个地方很多 plugin 都踩坑了,也就是说我们以为的弱依赖都变成了强依赖(因为没有保证一定 done)。

@popomore 是否要改进一下 ready-callback,变成 timeout 之后就继续 ready 并不再等 done 被调用.

docs typo

Remove -g flag in the npm install command of the Installation section.

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.