Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-ready

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-ready - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

6

History.md
2.0.0 / 2017-02-08
==================
* feat: [BREAKING_CHANGE] reimplement get-ready (#2)
* fix typo on readme
1.0.0 / 2015-09-29

@@ -3,0 +9,0 @@ ==================

93

index.js
'use strict';
function ready(flagOrFunction) {
this._ready = !!this._ready;
this._readyCallbacks = this._readyCallbacks || [];
const is = require('is-type-of');
if (arguments.length === 0) {
// return a promise
const IS_READY = Symbol('isReady');
const READY_CALLBACKS = Symbol('readyCallbacks');
const READY_ARG = Symbol('readyArg');
class Ready {
constructor() {
this[IS_READY] = false;
this[READY_CALLBACKS] = [];
}
ready(flagOrFunction) {
// register a callback
if (flagOrFunction === undefined || is.function(flagOrFunction)) {
return this.register(flagOrFunction);
}
// emit callbacks
this.emit(flagOrFunction);
}
/**
* Register a callback to the callback stack, it will be called when emit.
* It will return promise when no argument passing.
* @param {Function|Undefined} func - a callback
* @return {Undefined|Promise} promise
*/
register(func) {
// support `this.ready().then(onready);` and `yield this.ready()`;
return new Promise(function (resolve) {
if (this._ready) {
return resolve();
}
this._readyCallbacks.push(resolve);
}.bind(this));
} else if (typeof flagOrFunction === 'function') {
this._readyCallbacks.push(flagOrFunction);
} else {
this._ready = !!flagOrFunction;
if (!func) {
return new Promise(resolve => {
if (this[IS_READY]) {
return resolve(this[READY_ARG]);
}
this[READY_CALLBACKS].push(resolve);
});
}
// this.ready(fn)
if (this[IS_READY]) {
func(this[READY_ARG]);
} else {
this[READY_CALLBACKS].push(func);
}
}
if (this._ready) {
this._readyCallbacks.splice(0, Infinity).forEach(function(callback) {
process.nextTick(callback);
});
/**
* Call the callbacks that has been registerd, and clean the callback stack.
* If the flag is not false, it will be marked as ready. Then the callbacks will be called immediatly when register.
* @param {Boolean|Error} flag - Set a flag whether it had been ready. If the flag is an error, it's also ready, but the callback will be called with argument `error`
*/
emit(flag) {
// this.ready(true);
// this.ready(false);
// this.ready(err);
this[IS_READY] = flag !== false;
this[READY_ARG] = flag instanceof Error ? flag : undefined;
// this.ready(true)
if (this[IS_READY]) {
this[READY_CALLBACKS]
.splice(0, Infinity)
.forEach(callback => process.nextTick(() => callback(this[READY_ARG])));
}
}
/**
* @param {Object} obj - an object that be mixed
*/
static mixin(obj) {
if (!obj) return;
const ready = new Ready();
// delegate method
obj.ready = flagOrFunction => ready.ready(flagOrFunction);
}
}
function mixin(object) {
object.ready = ready;
Ready.mixin(object);
}

@@ -35,1 +87,2 @@

module.exports.mixin = mixin;
module.exports.Ready = Ready;
{
"name": "get-ready",
"version": "1.0.0",
"version": "2.0.0",
"description": "mixin to add one-time ready event callback handler",

@@ -9,14 +9,22 @@ "main": "index.js",

],
"dependencies": {},
"dependencies": {
"is-type-of": "^1.0.0"
},
"engines": {
"node": ">= 4.0.0"
},
"devDependencies": {
"eslint": "1",
"istanbul": "0",
"mocha": "2",
"should": "7",
"thunk-mocha": "0"
"autod": "^2.7.1",
"egg": "^0.11.0",
"egg-bin": "^2.0.2",
"egg-ci": "^1.1.0",
"eslint": "^3.15.0",
"eslint-config-egg": "^3.2.0"
},
"scripts": {
"lint": "eslint index.js test",
"test": "mocha -r thunk-mocha test/*.test.js",
"test-cov": "istanbul cover node_modules/.bin/_mocha -- -r thunk-mocha test/*.test.js"
"autod": "autod",
"lint": "eslint .",
"test": "npm run lint && egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && npm run cov"
},

@@ -36,3 +44,6 @@ "repository": {

"url": "https://github.com/node-modules/ready/issues"
},
"ci": {
"version": "4, 6, 7"
}
}

@@ -1,2 +0,2 @@

get-ready
# get-ready
=====

@@ -25,50 +25,64 @@

## Purpose
Events are great. You should use events, but not for signaling ready! Ready implies state, and once you are ready, you stay ready.
## Usage
This is a module for everyone who has bound an event handler.on('ready', function() {}) that doesn't execute because you added the handler after the 'ready' event already fired.
Using `ready` or `ready.mixin` to add `ready` method to the given object.
## Warning
If you use this mixin, you must have 'ready', '_ready', and '_readyCallbacks' available on your class. Ready will stomp on these variables if you're trying to use them in your class.
```js
const ready = require('get-ready');
const obj = {};
ready.mixin(obj);
## Example
```javascript
var ready = require('ready');
// register a callback
obj.ready(() => console.log('ready'));
// example class that uses Ready
function MyClass() {
this.someProperty = 'some value';
}
ready.mixin(MyClass.prototype);
// mark ready
obj.ready(true);
```
// Normal class prototype functions
MyClass.prototype.doSomeWork = function() {};
### Register
// Create a new class that uses ready mixin
var myClass = new MyClass();
Register a callback to the callback stack, it will be called when mark as ready, see example above.
// Add callback for when myClass is ready
myClass.ready(function() {
console.log('I am now ready');
});
If the callback is undefined, register will return a promise.
myClass.doSomeWork();
```js
obj.ready().then(() => console.log('ready'));
obj.ready(true);
```
// We are now ready, fire callbacks!
myClass.ready(true);
If it has been ready, the callback will be called immediately.
// Adding a new callback once we're already ready gets executed immediately
myClass.ready(function() {
console.log('I came late to the party, but I will still execute.');
});
```js
// already ready
obj.ready(true);
obj.ready().then(() => console.log('ready'));
```
// Ok, you can set the ready state to false now as well... for whatever reason
myClass.ready(false);
myClass.ready(function() {
console.log('I will not fire until you set ready to true again.');
});
**Warning: the callback is called after nextTick**
### Emit
Mark it as ready, you can simply using `.ready(true)`.
You can also mark it not ready.
```js
obj.ready(true);
// call immediately
obj.ready(() => console.log('ready'));
obj.ready(false);
obj.ready(() => throw 'don\'t run');
```
When exception throws, you can pass an error object, then the callback will receive it as the first argument.
```js
obj.ready(err => console.log(err));
obj.ready(new Error('err'));
```
## License
[MIT](LICENSE)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc