wait-for-stuff
Advanced tools
Comparing version 1.0.0 to 1.1.0
55
index.js
@@ -125,3 +125,2 @@ /////////////////////////////////////////////////////////// | ||
deasync.loopWhile(() => !isDone); | ||
return; | ||
}); | ||
@@ -142,4 +141,12 @@ | ||
$wait.use('predicate', predicate => { | ||
deasync.loopWhile(() => !predicate()); | ||
return; | ||
var isDone = false; | ||
deasync.loopWhile(() => { | ||
if (isDone){ | ||
return false; | ||
} | ||
isDone = predicate(); | ||
return !isDone; | ||
}); | ||
}); | ||
@@ -149,3 +156,2 @@ | ||
deasync.loopWhile(() => owner[propertyName] !== valueToWaitFor); | ||
return; | ||
}); | ||
@@ -155,3 +161,2 @@ | ||
deasync.loopWhile(() => (property in owner) === false); | ||
return; | ||
}); | ||
@@ -174,3 +179,2 @@ | ||
deasync.loopWhile(() => new Date().getTime() < date.getTime() ); | ||
return; | ||
}); | ||
@@ -198,3 +202,42 @@ | ||
$wait.use('yield', (generator, value) => { | ||
var nextValue = null; | ||
var isDone = false; | ||
if (typeof generator === 'function') { | ||
generator = generator(); | ||
} | ||
deasync.loopWhile(() => { | ||
if (nextValue === value) { | ||
isDone = true; | ||
} | ||
if (isDone){ | ||
return false; | ||
} | ||
else { | ||
nextValue = generator.next().value; | ||
return true; | ||
} | ||
}); | ||
return nextValue; | ||
}); | ||
$wait.use('generator', generator => { | ||
var nextValue = null; | ||
if (typeof generator === 'function') { | ||
generator = generator(); | ||
} | ||
deasync.loopWhile(() => { | ||
nextValue = generator.next(); | ||
return !nextValue.done; | ||
}); | ||
return nextValue.value; | ||
}); | ||
// as a convenience, we add 'condition' as an alias to 'predicate' | ||
@@ -201,0 +244,0 @@ $wait.alias('predicate', 'condition'); |
{ | ||
"name": "wait-for-stuff", | ||
"version": "1.0.0", | ||
"version": "1.1.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", |
@@ -7,13 +7,16 @@ [![Build Status](https://travis-ci.org/ujc/wait-for-stuff.svg?branch=master)](https://travis-ci.org/ujc/wait-for-stuff) | ||
* time | ||
* date | ||
* event | ||
* predicate | ||
* promise | ||
* stream | ||
* value | ||
* property | ||
* **time** *(wait for x seconds to pass)* | ||
* **date** *(wait until `date` is reached)* | ||
* **event** *(wait until `event` emits)* | ||
* **predicate** *(wait until `prediacte` returns true)* | ||
* **promise** *(wait for `promise` to settle)* | ||
* **stream** *(wait until `readable-stream` is fully read)* | ||
* **value** *(wait for `object.property` to equal `value`)* | ||
* **property** *(wait for `object.property` to exist)* | ||
* **yield** *(wait for a generator to `yield` a speific value)* | ||
* **generator** *(wait for `generator` to fully exhaust all values)* | ||
*(generators and node-style callbacks coming soon)* | ||
* (node-style callbacks coming soon)* | ||
--- | ||
@@ -159,2 +162,66 @@ ## Why ? | ||
**`wait.for.yield(generator, value)`** waits until the `generator` has yielded the specified `value`. | ||
`generator` can either be a generator-function, or an actuale iterable-generator *(the result of a generator-function)* | ||
```javascript | ||
function* myGeneratorFunction(){ | ||
count = 0; | ||
while (true) { yield ++count } | ||
} | ||
wait.for.yield(myGeneratorFunction, 5); | ||
// count is now 5 | ||
////////////////////////////////////////////////////// | ||
// alternative (pass in the actual iterable-generator) | ||
function* myGeneratorFunction(){ | ||
count = 0; | ||
while (true) { yield ++count } | ||
} | ||
var iterable = myGeneratorFunction(); | ||
wait.for.yield(iterable, 5); | ||
``` | ||
<br /><br /> | ||
**`wait.for.generator(generator)`** waits until the `generator` has fully exhausted all of it's yielded values. returns the value that the generator function returns. | ||
`generator` can either be a generator-function, or an actuale iterable-generator *(the result of a generator-function)* | ||
```javascript | ||
function* myGeneratorFunction(){ | ||
count = 0; | ||
while (count < 10) { yield ++count } | ||
return 'complete!'; | ||
} | ||
var result = wait.for.generator(myGeneratorFunction); | ||
// result === 'complete!' | ||
////////////////////////////////////////////////////// | ||
// alternative (pass in the actual iterable-generator) | ||
function* myGeneratorFunction(){ | ||
count = 0; | ||
while (count < 10) { yield ++count } | ||
return 'complete!'; | ||
} | ||
var iterable = myGeneratorFunction(); | ||
var result = wait.for.generator(iterable); | ||
// result === 'complete!' | ||
``` | ||
<br /><br /> | ||
--- | ||
@@ -161,0 +228,0 @@ ## Middleware |
@@ -106,2 +106,36 @@ var EventEmitter = require('events').EventEmitter; | ||
}); | ||
it('waits-for: yield', () => { | ||
var count = 0; | ||
function* myGenerator(){ | ||
while (true) { yield ++count } | ||
} | ||
wait.for.yield(myGenerator, 3); | ||
count.should.equal(3); | ||
}); | ||
it('waits-for: generator', () => { | ||
function* myGenerator(){ | ||
var count; | ||
while (count++ < 5) { yield count } | ||
return 'foo'; | ||
} | ||
var value = wait.for.generator(myGenerator); | ||
value.should.equal('foo'); | ||
}); | ||
it('waits-for: generator (as iterable)', () => { | ||
function* myGenerator(){ | ||
var count; | ||
while (count++ < 5) { yield count } | ||
return 'foo'; | ||
} | ||
var iterable = myGenerator(); | ||
var value = wait.for.generator(iterable); | ||
value.should.equal('foo'); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21765
290
278
0