wait-for-stuff
Advanced tools
Comparing version 1.2.3 to 1.3.0
96
index.js
@@ -21,3 +21,5 @@ /////////////////////////////////////////////////////////// | ||
/////////////////////////////////////////////////////////// | ||
var deasync = require('deasync'); | ||
var deasync = require('deasync'); | ||
var stream = require('stream'); | ||
var EventEmitter = require('events').EventEmitter; | ||
/////////////////////////////////////////////////////////// | ||
@@ -120,3 +122,3 @@ | ||
if (typeof seconds !== 'number') { | ||
throw new Error('<wait-for-sync>.for.time(..) :: invalid <seconds> argument' + seconds); | ||
throw new Error('wait.for.time(..) :: invalid <seconds> argument ' + seconds); | ||
} | ||
@@ -142,2 +144,6 @@ | ||
$wait.use('promise', promise => { | ||
if (!(promise instanceof Promise)){ | ||
throw new Error('wait.for.promise(..) :: invalid <promise> argument ' + promise); | ||
} | ||
var isDone = false; | ||
@@ -155,2 +161,6 @@ var result = null; | ||
$wait.use('predicate', predicate => { | ||
if (typeof predicate !== 'function'){ | ||
throw new Error('wait.for.predicate(..) :: invalid <promise> argument ' + predicate); | ||
} | ||
var isDone = false; | ||
@@ -169,2 +179,10 @@ | ||
$wait.use('value', (owner, propertyName, valueToWaitFor) => { | ||
if (typeof owner !== 'object'){ | ||
throw new Error('wait.for.value(..) :: invalid <owner> argument ' + owner); | ||
} | ||
if (typeof propertyName !== 'string'){ | ||
throw new Error('wait.for.value(..) :: invalid <propertyName> argument ' + propertyName); | ||
} | ||
deasync.loopWhile(() => owner[propertyName] !== valueToWaitFor); | ||
@@ -174,2 +192,10 @@ }); | ||
$wait.use('property', (owner, property) => { | ||
if (typeof owner !== 'object'){ | ||
throw new Error('wait.for.property(..) :: invalid <owner> argument ' + owner); | ||
} | ||
if (typeof property !== 'string'){ | ||
throw new Error('wait.for.property(..) :: invalid <property> argument ' + property); | ||
} | ||
deasync.loopWhile(() => (property in owner) === false); | ||
@@ -179,2 +205,10 @@ }); | ||
$wait.use('event', (emitter, eventName) => { | ||
if (!(emitter instanceof EventEmitter)){ | ||
throw new Error('wait.for.event(..) :: invalid <emitter> argument ' + emitter); | ||
} | ||
if (typeof eventName !== 'string'){ | ||
throw new Error('wait.for.value(..) :: invalid <eventName> argument ' + eventName); | ||
} | ||
var isDone = false; | ||
@@ -193,2 +227,6 @@ var eventData = null; | ||
$wait.use('date', date => { | ||
if (!(date instanceof Date)){ | ||
throw new Error('wait.for.date(..) :: invalid <date> argument ' + date); | ||
} | ||
deasync.loopWhile(() => new Date().getTime() < date.getTime() ); | ||
@@ -198,2 +236,6 @@ }); | ||
$wait.use('stream', readableStream => { | ||
if (!(readableStream instanceof stream.Readable)){ | ||
throw new Error('wait.for.stream(..) :: invalid <readableStream> argument ' + readableStream); | ||
} | ||
var isDone = false; | ||
@@ -222,6 +264,10 @@ var data = []; | ||
if (typeof generator === 'function') { | ||
if (generator.constructor.name === 'GeneratorFunction') { | ||
generator = generator(); | ||
} | ||
if (typeof generator.next !== 'function'){ | ||
throw new Error('wait.for.yield(..) :: invalid <generator> argument ' + generator); | ||
} | ||
deasync.loopWhile(() => { | ||
@@ -247,6 +293,10 @@ if (nextValue === value) { | ||
if (typeof generator === 'function') { | ||
if (generator.constructor.name === 'GeneratorFunction') { | ||
generator = generator(); | ||
} | ||
if (typeof generator.next !== 'function'){ | ||
throw new Error('wait.for.generator(..) :: invalid <generator> argument ' + generator); | ||
} | ||
deasync.loopWhile(() => { | ||
@@ -261,2 +311,6 @@ nextValue = generator.next(); | ||
$wait.use('callback', (nodeAsyncFunction, ...args) => { | ||
if (typeof nodeAsyncFunction !== 'function'){ | ||
throw new Error('wait.for.callback(..) :: invalid <nodeAsyncFunction> argument ' + nodeAsyncFunction); | ||
} | ||
var isDone = false; | ||
@@ -275,2 +329,6 @@ var result = null; | ||
$wait.use('function', (customAsyncFunction, ...args) => { | ||
if (typeof customAsyncFunction !== 'function'){ | ||
throw new Error('wait.for.function(..) :: invalid <customAsyncFunction> argument ' + customAsyncFunction); | ||
} | ||
var isDone = false; | ||
@@ -289,5 +347,35 @@ var result = null; | ||
$wait.use('array', (array, value) => { | ||
if (!(array instanceof Array)){ | ||
throw new Error('wait.for.array(..) :: invalid <array> argument ' + array); | ||
} | ||
deasync.loopWhile(() => !array.includes(value)); | ||
}); | ||
$wait.use('result', waitable => { | ||
// Promise | ||
if (waitable instanceof Promise){ | ||
return $wait.for.result( $wait.for.promise(waitable) ) | ||
} | ||
// GeneratorFunction | ||
if (waitable.constructor.name === 'GeneratorFunction') { | ||
return $wait.for.result( $wait.for.generator(waitable) ) | ||
} | ||
// Iterable | ||
if (typeof waitable.next === 'function') { | ||
return $wait.for.result( $wait.for.generator(waitable) ) | ||
} | ||
// Stream | ||
if (waitable instanceof stream.Readable){ | ||
return $wait.for.result( $wait.for.stream(waitable) ) | ||
} | ||
// non-identifiable waitable | ||
return waitable; | ||
}); | ||
// as a convenience, we add 'condition' as an alias to 'predicate' | ||
@@ -294,0 +382,0 @@ $wait.alias('predicate', 'condition'); |
{ | ||
"name": "wait-for-stuff", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"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", |
@@ -21,7 +21,6 @@ [![Build Status](https://travis-ci.org/ujc/wait-for-stuff.svg?branch=master)](https://travis-ci.org/ujc/wait-for-stuff) | ||
* **compose** *(compose a new waiter from two or more existing waiters)* | ||
* **result** *(wait for a chain of waitables to return a non-waitable result)* | ||
*(follow-through waiters are coming soon)* | ||
[//]: # (----------------------------------------------------) | ||
@@ -53,2 +52,3 @@ [//]: # (----------------------------------------------------) | ||
1. [`wait.compose()`](#wait-compose) | ||
2. [`wait.for.result()`](#wait-for-result) | ||
7. [Contribute](#contribute) | ||
@@ -507,2 +507,32 @@ 8. [Test](#test) | ||
[//]: # (----------------------------------------------------) | ||
<a id="wait-for-result">[#](#wait-for-result)</a> | ||
**`wait.for.result(waitable))`** | ||
Waits until a chain of waitables return a non-waitable result. | ||
For example, if you need to wait on a promise that returns another promise that returns a stream - you can just wait for the result of the final stream using **`wait.for.result`**. | ||
**`waitable`** can be any of the following: | ||
* Promise | ||
* Generator *(or the iterator-result of a generaotr)* | ||
* Stream | ||
```javascript | ||
// a promise that returns a promise that returns a stream | ||
var myComplexPromise = new Promise((res, rej) => { | ||
setTimeout(() => { | ||
res(new Promise((res, rej) => { | ||
setTimeout(() => res(fs.createReadStream('someFile.json')), 500); | ||
})); | ||
}, 500); | ||
}); | ||
var result = wait.for.result(myComplexPromise); | ||
// result is now a buffer holding the contents of 'someFile.json' | ||
``` | ||
<br /><br /> | ||
[//]: # (----------------------------------------------------) | ||
[//]: # (----------------------------------------------------) | ||
## <a id="contribute">[#](#contribute)</a> Contribute | ||
@@ -509,0 +539,0 @@ I hope people will find this module helpful - either as an alternative to asynchronous flow-execution patterns such as `await\async` *(until it's official release at least)* - or as a supplement to go along with what ever you're all ready using. |
@@ -162,2 +162,16 @@ var EventEmitter = require('events').EventEmitter; | ||
it('waits-for: result', () => { | ||
// a promise that returns a promise that returns a stream | ||
var myComplexPromise = new Promise((res, rej) => { | ||
setTimeout(() => { | ||
res(new Promise((res, rej) => { | ||
setTimeout(() => res(fs.createReadStream(__filename)), 500); | ||
})); | ||
}, 500); | ||
}); | ||
var result = wait.for.result(myComplexPromise); | ||
result.toString().should.contain('waits-for: result'); | ||
}); | ||
it('extension: middleware', () => { | ||
@@ -164,0 +178,0 @@ wait.use('twoSeconds', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
38426
441
558