Skip to content

Commit

Permalink
feat: [BREAKING_CHANGE] reimplement get-ready (node-modules#2)
Browse files Browse the repository at this point in the history
- Use class
- seperate API by register and emit
- support ready(err)

eggjs/egg#331
  • Loading branch information
popomore authored Feb 8, 2017
1 parent 704daaa commit 4db8cab
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 307 deletions.
22 changes: 22 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

module.exports = {
write: true,
prefix: '^',
devprefix: '^',
exclude: [
'test/fixtures',
],
devdep: [
'autod',
'egg',
'egg-bin',
'egg-ci',
'eslint',
'eslint-config-egg',
],
keep: [
],
semver: [
],
};
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
193 changes: 1 addition & 192 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,194 +1,3 @@
{
"ecmaFeatures": {
"jsx": true
},
"parser": "espree",
"env": {
"amd": false,
"jasmine": false,
"node": true,
"mocha": true,
"browser": true,
"builtin": true,
"es6": true
},
"rules": {
"no-alert": 2,
"no-array-constructor": 2,
"no-bitwise": 2,
"no-caller": 2,
"no-catch-shadow": 2,
"no-cond-assign": [2, "except-parens"],
"no-constant-condition": 2,
"no-continue": 0,
"no-control-regex": 2,
"no-debugger": 2,
"no-delete-var": 2,
"no-div-regex": 0,
"no-dupe-keys": 2,
"no-dupe-args": 2,
"no-duplicate-case": 2,
"no-else-return": 0,
"no-empty": 2,
"no-empty-character-class": 2,
"no-empty-label": 2,
"no-eq-null": 0,
"no-eval": 2,
"no-ex-assign": 2,
"no-extend-native": 2,
"no-extra-bind": 2,
"no-extra-boolean-cast": 2,
"no-extra-parens": 2,
"no-extra-semi": 2,
"no-fallthrough": 2,
"no-floating-decimal": 0,
"no-func-assign": 2,
"no-implied-eval": 2,
"no-inline-comments": 0,
"no-inner-declarations": [2, "functions"],
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-iterator": 0,
"no-label-var": 2,
"no-labels": 2,
"no-lone-blocks": 2,
"no-lonely-if": 0,
"no-loop-func": 2,
"no-mixed-requires": [0, false],
"no-mixed-spaces-and-tabs": [2, false],
"linebreak-style": [0, "unix"],
"no-multi-spaces": 2,
"no-multi-str": 2,
"no-multiple-empty-lines": [0, {
"max": 2
}],
"no-native-reassign": 2,
"no-negated-in-lhs": 2,
"no-nested-ternary": 0,
"no-new": 0,
"no-new-func": 2,
"no-new-object": 2,
"no-new-require": 0,
"no-new-wrappers": 2,
"no-obj-calls": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-param-reassign": 0,
"no-path-concat": 0,
"no-plusplus": 0,
"no-process-env": 0,
"no-process-exit": 0,
"no-proto": 2,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-restricted-modules": 0,
"no-return-assign": 0,
"no-script-url": 2,
"no-self-compare": 0,
"no-sequences": 2,
"no-shadow": 0,
"no-shadow-restricted-names": 2,
"no-spaced-func": 2,
"no-sparse-arrays": 2,
"no-sync": 0,
"no-ternary": 0,
"no-trailing-spaces": 2,
"no-this-before-super": 0,
"no-throw-literal": 0,
"no-undef": 2,
"no-undef-init": 2,
"no-undefined": 0,
"no-unexpected-multiline": 0,
"no-underscore-dangle": 0,
"no-unneeded-ternary": 0,
"no-unreachable": 2,
"no-unused-expressions": 0,
"no-unused-vars": 2,
"no-use-before-define": [2, "nofunc"],
"no-void": 0,
"no-var": 2,
"no-const-assign": 2,
"prefer-const": 0,
"no-warning-comments": [0, {
"terms": ["todo", "fixme", "xxx"],
"location": "start"
}],
"no-with": 2,
"array-bracket-spacing": [0, "never"],
"accessor-pairs": 0,
"block-scoped-var": 0,
"brace-style": [0, "1tbs"],
"camelcase": 0,
"comma-dangle": [2, "always-multiline"],
"comma-spacing": 2,
"comma-style": 0,
"complexity": [0, 11],
"computed-property-spacing": [0, "never"],
"consistent-return": 2,
"consistent-this": [0, "that"],
"constructor-super": 0,
"curly": [2, "multi-line"],
"default-case": 0,
"dot-location": 0,
"dot-notation": 0,
"eol-last": 2,
"eqeqeq": 2,
"func-names": 0,
"func-style": [0, "declaration"],
"generator-star-spacing": 0,
"guard-for-in": 0,
"handle-callback-err": 0,
"indent": [2, 2],
"key-spacing": [2, {
"beforeColon": false,
"afterColon": true
}],
"lines-around-comment": 0,
"max-depth": [0, 4],
"max-len": [0, 80, 4],
"max-nested-callbacks": [0, 2],
"max-params": [0, 3],
"max-statements": [0, 10],
"new-cap": 0,
"new-parens": 2,
"newline-after-var": 0,
"object-curly-spacing": [0, "never"],
"object-shorthand": 0,
"one-var": 0,
"operator-assignment": [0, "always"],
"operator-linebreak": 0,
"padded-blocks": 0,
"quote-props": 0,
"quotes": [2, "single"],
"radix": 0,
"semi": 2,
"semi-spacing": [2, {
"before": false,
"after": true
}],
"sort-vars": 0,
"space-after-keywords": [0, "always"],
"space-before-blocks": [0, "always"],
"space-before-function-paren": [0, "always"],
"space-in-parens": [0, "never"],
"space-infix-ops": 2,
"space-return-throw-case": 2,
"space-unary-ops": [2, {
"words": true,
"nonwords": false
}],
"spaced-comment": 0,
"strict": [2, "global"],
"use-isnan": 2,
"valid-jsdoc": [2, {
"prefer": {
"returns": "return"
}
}],
"valid-typeof": 0,
"vars-on-top": 0,
"wrap-iife": 0,
"wrap-regex": 0,
"yoda": [2, "never"]
}
"extends": "egg"
}
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ sudo: false
language: node_js
node_js:
- '4'
- '3'
- '2'
- '1'
- '6'
- '7'
install:
- npm i npminstall && npminstall
script:
- "npm run lint"
- "npm run test-cov"
- npm run ci
after_script:
- "npm i codecov.io && cat ./coverage/coverage.json | ./node_modules/codecov.io/bin/codecov.io.js"
- npminstall codecov && codecov
79 changes: 46 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
get-ready
# get-ready
=====

[![NPM version][npm-image]][npm-url]
Expand All @@ -22,53 +22,66 @@ get-ready

NodeJS mixin to add one-time ready event

## 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);

// register a callback
obj.ready(() => console.log('ready'));

// mark ready
obj.ready(true);
```

## Example
### Register

Register a callback to the callback stack, it will be called when mark as ready, see example above.

If the callback is undefined, register will return a promise.

```js
const ready = require('get-ready');
obj.ready().then(() => console.log('ready'));
obj.ready(true);
```

// example class that uses Ready
function MyClass() {
this.someProperty = 'some value';
}
ready.mixin(MyClass.prototype);
If it has been ready, the callback will be called immediately.

// Normal class prototype functions
MyClass.prototype.doSomeWork = function() {};
```js
// already ready
obj.ready(true);
obj.ready().then(() => console.log('ready'));
```

**Warning: the callback is called after nextTick**

### Emit

// Create a new class that uses ready mixin
var myClass = new MyClass();
Mark it as ready, you can simply using `.ready(true)`.

// Add callback for when myClass is ready
myClass.ready(function() {
console.log('I am now ready');
});
You can also mark it not ready.

myClass.doSomeWork();
```js
obj.ready(true);
// call immediately
obj.ready(() => console.log('ready'));

// We are now ready, fire callbacks!
myClass.ready(true);
obj.ready(false);
obj.ready(() => throw 'don\'t run');
```

// 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.');
});
When exception throws, you can pass an error object, then the callback will receive it as the first argument.

// 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.');
});
```js
obj.ready(err => console.log(err));
obj.ready(new Error('err'));
```


## License

[MIT](LICENSE)
16 changes: 16 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
environment:
matrix:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '7'

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall

test_script:
- node --version
- npm --version
- npm run ci

build: off
Loading

0 comments on commit 4db8cab

Please sign in to comment.