wait-for-stuff
Advanced tools
Comparing version 1.2.0 to 1.2.1
40
index.js
@@ -75,2 +75,26 @@ /////////////////////////////////////////////////////////// | ||
} | ||
}, | ||
compose: function compose(...waiters){ | ||
waiters.reverse(); | ||
// first, let's make sure that all the requested waiters really exist, | ||
// otherwise the user might get bad side-effects | ||
waiters.forEach(waiter => { | ||
if (typeof this.for[waiter] !== 'function'){ | ||
throw new Error(`wait.compose() :: cannot compose unknown waiter "${waiter}"`); | ||
} | ||
}); | ||
// the composed function simply passes the result from each waiter to the next, | ||
// and once all waiters have completed we extract the final return value and return that | ||
return function(...args){ | ||
var result = args; | ||
waiters.forEach(waiter => { | ||
result = [$wait.for[waiter].apply(null, result)]; | ||
}); | ||
return result[0]; | ||
} | ||
} | ||
@@ -88,14 +112,2 @@ }; | ||
// | ||
// - time (wait x seconds) | ||
// - date (wait for some future date) | ||
// - promise (wait for promise to settle) | ||
// - predicate (wait for predicate function to return true) | ||
// - value (wait for value to match) | ||
// - property (wait for property to exist) | ||
// - event (wait for event to be emitted) | ||
// - stream (wait for a readable stream to end) | ||
// | ||
// * generator (coming soon) | ||
// * callback (coming soon) | ||
// | ||
// each of these are added to the main $wait module by | ||
@@ -265,2 +277,6 @@ // calling the ".use(waiter-middleware)" api of the main | ||
$wait.use('array', (array, value) => { | ||
deasync.loopWhile(() => !array.includes(value)); | ||
}); | ||
// as a convenience, we add 'condition' as an alias to 'predicate' | ||
@@ -267,0 +283,0 @@ $wait.alias('predicate', 'condition'); |
{ | ||
"name": "wait-for-stuff", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "an extendable library that can wait for stuff to happen in a synchronous-but-not-blocking manner", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,6 +19,7 @@ [![Build Status](https://travis-ci.org/ujc/wait-for-stuff.svg?branch=master)](https://travis-ci.org/ujc/wait-for-stuff) | ||
* **function** *(wait for custom callback `function` to be called)* | ||
* **array** *(wait for `array` to contain some value)* | ||
* **compose** *(compose a new waiter from two or more existing waiters)* | ||
* *(chainable \ follow-through waiters are coming soon)* | ||
* *(composable \ chainable \ follow-through waiters are coming soon)* | ||
--- | ||
@@ -283,2 +284,15 @@ ## Why ? | ||
**`wait.for.array(array, value)`** waits until `array` contains `value` | ||
```javascript | ||
var myArray = []; | ||
setTimeout(() => myArray.push('hello world'), 1000); | ||
wait.for.array(myArray, 'hello world'); | ||
``` | ||
<br /><br /> | ||
--- | ||
@@ -315,2 +329,40 @@ ## Middleware | ||
--- | ||
## Composition | ||
you can compose your own "complex" waiter by combining the work of two or more waiters. | ||
this is done by calling `wait.compose(waiter1, waiter2, ...waiterN)`. the result is a new waiter that passes | ||
the return value from each waiter to the next, until all waiters have completed. | ||
**`wait.compose(waiter1, waiter2, ...waiterN)`** composes a new waiter from the waiters that are passed in. | ||
waiters are exhausted from right-to-left - just like you would expect from the functionl-programming `compose` function | ||
```javascript | ||
function myComplexFunction(path, callback){ | ||
fs.exists(path, result => { | ||
var stream = fs.createReadStream(path); | ||
callback(stream); | ||
}); | ||
}; | ||
// first we create a composed waiter | ||
// it will first expect the arguments that should be passed into wait.for.callback. | ||
// the result of wait.for.callback is then passed into wait.for.stream. | ||
// the final result is what wait.for.stream will have returned | ||
// | ||
// in our case, myComplexFunction() expects a callback, which then gets a stream | ||
// composition allows us to wait on both 'waiters' | ||
var streamAndCallbackWaiter = wait.compose('stream', 'callback'); | ||
var result = streamAndCallback(myComplexFunction, __filename); // arguments for wait.for.callback | ||
// result is the return value from wait.for.stream | ||
result.toString().should.include('extension: compose'); | ||
``` | ||
<br /><br /> | ||
-- | ||
@@ -317,0 +369,0 @@ ## Contribute |
@@ -154,2 +154,42 @@ var EventEmitter = require('events').EventEmitter; | ||
}); | ||
it('waits-for: array', () => { | ||
var myArray = []; | ||
setTimeout(() => myArray.push('hello world'), 1000); | ||
wait.for.array(myArray, 'hello world'); | ||
myArray.should.contain('hello world'); | ||
}); | ||
it('extension: middleware', () => { | ||
wait.use('twoSeconds', () => { | ||
wait.for.time(2); | ||
}); | ||
wait.for.twoSeconds.should.be.a('function'); | ||
var start = new Date().getTime(); | ||
wait.for.twoSeconds(); | ||
var end = new Date().getTime(); | ||
(end - start).should.be.above(1999); | ||
}); | ||
it('extension: alias', () => {- | ||
wait.alias('time', 'anotherTime'); | ||
wait.for.anotherTime.should.equal(wait.for.time); | ||
}); | ||
it('extension: compose', () => { | ||
function myComplexFunction(path, callback){ | ||
fs.exists(path, result => { | ||
var stream = fs.createReadStream(path); | ||
callback(stream); | ||
}); | ||
}; | ||
var streamAndCallback = wait.compose('stream', 'callback'); | ||
var result = streamAndCallback(myComplexFunction, __filename); | ||
result.toString().should.include('extension: compose'); | ||
}); | ||
}); |
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
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
28594
362
387