ready-callback
Advanced tools
Comparing version 1.0.0 to 2.0.0
2.0.0 / 2017-02-09 | ||
================== | ||
* feat: call .ready(err) when callback pass an error (#98) | ||
* chore(package): update mocha to version 2.3.3 | ||
1.0.0 / 2015-10-20 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -6,3 +6,2 @@ 'use strict'; | ||
const ready = require('get-ready'); | ||
const extend = require('extend'); | ||
const uuid = require('uuid'); | ||
@@ -24,12 +23,14 @@ const debug = require('debug')('ready-callback'); | ||
* @param {Object} opt | ||
* - {Number} timeout - emit `ready_timeout` when it doesn't finish but reach the timeout, default: 10000ms | ||
* - {Boolean} isWeakDep - whether it's a weak dependency, default: false | ||
* - {Number} [timeout=10000] - emit `ready_timeout` when it doesn't finish but reach the timeout | ||
* - {Boolean} [isWeakDep=false] - whether it's a weak dependency | ||
*/ | ||
constructor(opt) { | ||
super(); | ||
ready.mixin(this); | ||
this.opt = opt || {}; | ||
this.isError = false; | ||
this.cache = new Map(); | ||
ready.mixin(this); | ||
setImmediate(function () { | ||
setImmediate(() => { | ||
// fire callback directly when no registered ready callback | ||
@@ -40,3 +41,3 @@ if (this.cache.size === 0) { | ||
} | ||
}.bind(this)); | ||
}); | ||
} | ||
@@ -53,4 +54,11 @@ | ||
if (!obj || this.obj) return null; | ||
// delegate API to object | ||
obj.ready = this.ready.bind(this); | ||
obj.readyCallback = this.readyCallback.bind(this); | ||
// only ready once with error | ||
this.once('error', err => obj.ready(err)); | ||
// delegate events | ||
if (obj.emit) { | ||
@@ -62,2 +70,3 @@ this.on('ready_timeout', obj.emit.bind(obj, 'ready_timeout')); | ||
this.obj = obj; | ||
return this; | ||
@@ -74,7 +83,7 @@ } | ||
readyCallback(name, opt) { | ||
const id = uuid.v1(); | ||
opt = extend({}, defaults, this.opt, opt); | ||
opt.name = name || id; | ||
const timer = setTimeout(this.emit.bind(this, 'ready_timeout', opt.name), opt.timeout); | ||
const cb = once(function(err) { | ||
opt = Object.assign({}, defaults, this.opt, opt); | ||
const cacheKey = uuid.v1(); | ||
opt.name = name || cacheKey; | ||
const timer = setTimeout(() => this.emit('ready_timeout', opt.name), opt.timeout); | ||
const cb = once(err => { | ||
clearTimeout(timer); | ||
@@ -84,7 +93,7 @@ // won't continue to fire after it's error | ||
// fire callback after all register | ||
setImmediate(this.readyDone.bind(this, id, opt, err)); | ||
}.bind(this)); | ||
debug('[%s] Register task id `%s` with %j', id, opt.name, opt); | ||
setImmediate(() => this.readyDone(cacheKey, opt, err)); | ||
}); | ||
debug('[%s] Register task id `%s` with %j', cacheKey, opt.name, opt); | ||
cb.id = opt.name; | ||
this.cache.set(id, cb); | ||
this.cache.set(cacheKey, cb); | ||
return cb; | ||
@@ -103,4 +112,7 @@ } | ||
readyDone(id, opt, err) { | ||
if (err && !opt.isWeakDep) { | ||
if (err !== undefined && !opt.isWeakDep) { | ||
this.isError = true; | ||
if (!(err instanceof Error)) { | ||
err = new Error(err); | ||
} | ||
debug('[%s] Throw error task id `%s`, error %s', id, opt.name, err); | ||
@@ -127,3 +139,5 @@ return this.emit('error', err); | ||
module.exports = Ready; | ||
// Use ready-callback with options | ||
module.exports = opt => new Ready(opt); | ||
module.exports.Ready = Ready; | ||
@@ -130,0 +144,0 @@ function getRemain(map) { |
{ | ||
"name": "ready-callback", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Launch server after all async task ready", | ||
@@ -10,3 +10,3 @@ "keywords": [ | ||
], | ||
"main": "index.js", | ||
"main": "lib/ready.js", | ||
"files": [ | ||
@@ -17,15 +17,15 @@ "index.js", | ||
"dependencies": { | ||
"debug": "~2.2.0", | ||
"extend": "~3.0.0", | ||
"once": "~1.3.2", | ||
"get-ready": "~1.0.0", | ||
"uuid": "~2.0.1" | ||
"debug": "^2.6.0", | ||
"get-ready": "^2.0.0", | ||
"once": "^1.4.0", | ||
"uuid": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"istanbul": "0", | ||
"eslint": "1", | ||
"koa": "1", | ||
"mocha": "1", | ||
"should": "6", | ||
"spy": "0" | ||
"autod": "^2.7.1", | ||
"egg-bin": "^2.0.2", | ||
"egg-ci": "^1.1.0", | ||
"eslint": "^3.15.0", | ||
"eslint-config-egg": "^3.2.0", | ||
"koa": "^1.2.4", | ||
"spy": "^1.0.0" | ||
}, | ||
@@ -40,10 +40,14 @@ "repository": { | ||
"scripts": { | ||
"autod": "autod", | ||
"lint": "eslint .", | ||
"test": "mocha -R spec -t 20000", | ||
"cov": "npm run lint && istanbul cover --report=none node_modules/mocha/bin/_mocha -- -R spec -t 20000 && istanbul report json html", | ||
"autod": "autod -e test/fixtures -f ~ -w" | ||
"test": "npm run lint -- --fix && egg-bin test", | ||
"cov": "egg-bin cov", | ||
"ci": "npm run lint && egg-bin cov" | ||
}, | ||
"engines": { | ||
"node": ">=2.0.0" | ||
"node": ">=4.0.0" | ||
}, | ||
"ci": { | ||
"version": "4, 6, 7" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8592
4
122
7
4
1
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedget-ready@2.0.1(transitive)
+ Addedis-class-hotfix@0.0.6(transitive)
+ Addedis-type-of@1.4.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedms@2.0.0(transitive)
+ Addedonce@1.4.0(transitive)
+ Addeduuid@3.4.0(transitive)
- Removedextend@~3.0.0
- Removeddebug@2.2.0(transitive)
- Removedextend@3.0.2(transitive)
- Removedget-ready@1.0.0(transitive)
- Removedms@0.7.1(transitive)
- Removedonce@1.3.3(transitive)
- Removeduuid@2.0.3(transitive)
Updateddebug@^2.6.0
Updatedget-ready@^2.0.0
Updatedonce@^1.4.0
Updateduuid@^3.0.1