after-ready
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.whenReady = exports.awaitReady = exports.setup = exports.SET_ERROR = exports.SET_READY = void 0; | ||
exports.whenReady = exports.awaitReady = exports.setup = exports.RESET_READY = exports.SET_ERROR = exports.SET_READY = void 0; | ||
@@ -19,2 +19,4 @@ const assert = require('assert'); // Ref: | ||
exports.SET_ERROR = SET_ERROR; | ||
const RESET_READY = symbol('RESET_READY'); | ||
exports.RESET_READY = RESET_READY; | ||
const READY_ERROR = symbol('READY_ERROR'); | ||
@@ -86,2 +88,7 @@ const READY_CALLBACKS = symbol('READY_CALLBACKS'); | ||
function resetReady() { | ||
this[IS_READY] = false; | ||
this[READY_ERROR] = null; | ||
} | ||
const NOOP = () => {}; | ||
@@ -107,5 +114,6 @@ | ||
const setErrorMethodDescriptor = createMethodDescriptor(SET_ERROR, setError); | ||
const resetReadyMethodDescriptor = createMethodDescriptor(RESET_READY, resetReady); | ||
return { | ||
kind, | ||
elements: [...elements, isReadyElementDescriptor, readyErrorElementDescriptor, readyCallbacksElementDescriptor, onReadyElementDescriptor, setReadyMethodDescriptor, setErrorMethodDescriptor] | ||
elements: [...elements, isReadyElementDescriptor, readyErrorElementDescriptor, readyCallbacksElementDescriptor, onReadyElementDescriptor, setReadyMethodDescriptor, setErrorMethodDescriptor, resetReadyMethodDescriptor] | ||
}; | ||
@@ -112,0 +120,0 @@ }; |
{ | ||
"name": "after-ready", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "TC39 decorators for making classes to support one-time ready event callback handlers.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -33,6 +33,4 @@ [![Build Status](https://travis-ci.org/kaelzhang/after-ready.svg?branch=master)](https://travis-ci.org/kaelzhang/after-ready) | ||
setup, | ||
afterReady, | ||
whenReady, | ||
SET_READY, | ||
SET_ERROR | ||
awaitReady, | ||
SET_READY | ||
} from 'after-ready' | ||
@@ -42,12 +40,92 @@ | ||
class Foo { | ||
@afterReady | ||
constructor () { | ||
this.init() | ||
} | ||
// `doSomething` will not resolve before `this[SET_READY]()` | ||
@awaitReady | ||
doSomething () { | ||
return 1 | ||
} | ||
init () { | ||
setTimeout(() => { | ||
this[SET_READY]() | ||
}, 500) | ||
} | ||
} | ||
``` | ||
## @setup | ||
## @setup(onReady) | ||
- **onReady** `Function(err, ...args)` The function to be called when the instance of the class is set as ready or errored. | ||
Setup the class. A class must be setup with the `@setup` decorator then use the `@awaitReady` or `@whenReady` | ||
After the class is `@setup`d, **THREE** methods are added to the prototype of the class. | ||
### [SET_READY](...args) | ||
Set the class instance as ready. | ||
If `onReady` function is set, it will be invoked as `onReady.call(this, null, ...args)`. | ||
And the methods which applied with `@awaitReady` and have been called before ready will resume to execute. | ||
### [SET_ERROR](error) | ||
Set the class instance as error encountered. | ||
If `onReady` function is set, it will be invoked as `onReady.call(this, error)`. | ||
And the methods which applied with `@awaitReady` and have been called before ready will be rejected. | ||
### [RESET_READY]() | ||
Reset the ready status, or reset the error status. | ||
## @awaitReady | ||
The method applied which this decorator will always returns a `Promise`. | ||
If `this[SET_READY]()` has been invoked, the original method will be executed immediately. | ||
If `this[SET_ERROR](error)` has been invoked, then the method will returns `Promise.reject(error)` | ||
Otherwise, the original method will be paused, waiting for `this[SET_READY]()` or `this[SET_ERROR](error)` to be called. | ||
```js | ||
@setup | ||
class Foo { | ||
constructor () { | ||
this.init() | ||
} | ||
// `doSomething` will not resolve before `this[SET_READY]()` | ||
@awaitReady | ||
doSomething () { | ||
return 1 | ||
} | ||
init () { | ||
setTimeout(() => { | ||
this[SET_READY](new Error('Boooooom!!')) | ||
}, 500) | ||
} | ||
} | ||
new Foo().doSomething().then(n => { | ||
console.log('the result is', n, 'but it will never reach here') | ||
}).catch(err => { | ||
console.log(err.message, 'it will log "Boooooom!!"') | ||
}) | ||
``` | ||
## @whenReady | ||
If no `this[SET_READY]()` or `this[SET_ERROR](error)` has been called, the original method will never be invoked. | ||
## License | ||
MIT |
@@ -10,2 +10,3 @@ const assert = require('assert') | ||
export const SET_ERROR = symbol('SET_ERROR') | ||
export const RESET_READY = symbol('RESET_READY') | ||
@@ -78,2 +79,7 @@ const READY_ERROR = symbol('READY_ERROR') | ||
function resetReady () { | ||
this[IS_READY] = false | ||
this[READY_ERROR] = null | ||
} | ||
const NOOP = () => {} | ||
@@ -108,2 +114,4 @@ | ||
const setErrorMethodDescriptor = createMethodDescriptor(SET_ERROR, setError) | ||
const resetReadyMethodDescriptor = createMethodDescriptor( | ||
RESET_READY, resetReady) | ||
@@ -119,3 +127,4 @@ return { | ||
setReadyMethodDescriptor, | ||
setErrorMethodDescriptor | ||
setErrorMethodDescriptor, | ||
resetReadyMethodDescriptor | ||
] | ||
@@ -122,0 +131,0 @@ } |
15129
297
130