forked from withspectrum/callback-to-async-iterator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c6c193a
Showing
31 changed files
with
7,344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"presets": ["flow", ["env", { | ||
"targets": { | ||
"node": "6" | ||
} | ||
}]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[ignore] | ||
.*/node_modules/.* | ||
|
||
[include] | ||
|
||
[libs] | ||
|
||
[options] | ||
suppress_comment=.*\\$FlowFixMe | ||
esproposal.class_instance_fields=enable | ||
esproposal.class_static_fields=enable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.swp | ||
*~ | ||
*.iml | ||
.*.haste_cache.* | ||
.DS_Store | ||
.idea | ||
npm-debug.log | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*.swp | ||
*~ | ||
*.iml | ||
.*.haste_cache.* | ||
.DS_Store | ||
.idea | ||
.babelrc | ||
.eslintrc | ||
npm-debug.log | ||
lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: node_js | ||
node_js: | ||
- 8 | ||
script: | ||
- node --version | ||
- yarn --version | ||
- yarn run test | ||
notifications: | ||
email: false | ||
cache: | ||
yarn: true | ||
directories: | ||
- node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Maximilian Stoiber | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# `callback-to-async-iterator` | ||
|
||
Turn any callback-based listener into an async iterator. | ||
|
||
We needed this module to turn our database listeners into async iterators, which is what GraphQL subscriptions expect to be passed. It might be useful for you too! | ||
|
||
```sh | ||
npm install callback-to-async-iterator | ||
``` | ||
|
||
## Usage | ||
|
||
Imagine a standard callback-based listener like this: | ||
|
||
```JS | ||
// callback will be called with each new message added to the database | ||
const listenToNewMessages = (callback) => { | ||
return db.messages.listen(message => callback(message)); | ||
} | ||
``` | ||
|
||
The problem is that callbacks are _push_ based, they push values to the listener whenever a new value is availabe. Async Iterators on the other hand are _pull_ based, they request a new value and wait until it is available. | ||
|
||
This module reconciliates that difference so you can turn your standard callback-based listener into an async iterator: | ||
|
||
```JS | ||
import asyncify from 'callback-to-async-iterator'; | ||
|
||
const messages = asyncify(listenToNewMessages); | ||
|
||
// Wait until the first message is sent | ||
const firstMessage = await messages.next(); | ||
|
||
// Asynchronously iterate over new messages and log them as they come in | ||
for await (let message of messages) { | ||
console.log(message); | ||
} | ||
|
||
console.log('Done!') | ||
``` | ||
|
||
This module will automatically buffer incoming data if `.next` hasn't been called yet. | ||
|
||
## Credits | ||
|
||
This module is heavily based on the [event emitter to async iterator](https://github.com/apollographql/graphql-subscriptions/blob/master/src/event-emitter-to-async-iterator.ts) utility used in `graphql-js`. Also big shoutout to [@ForbesLindesay](https://github.com/ForbesLindesay) who helped a ton with the initial implementation and understanding the problem. | ||
|
||
## License | ||
|
||
Licensed under the MIT License, Copyright ©️ 2017 Maximilian Stoiber. See [LICENSE.md](LICENSE.md) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// flow-typed signature: 904df3d3f1a067e0190edaa0e695a96e | ||
// flow-typed version: <<STUB>>/babel-cli_v^6.26.0/flow_v0.66.0 | ||
|
||
/** | ||
* This is an autogenerated libdef stub for: | ||
* | ||
* 'babel-cli' | ||
* | ||
* Fill this stub out by replacing all the `any` types. | ||
* | ||
* Once filled out, we encourage you to share your work with the | ||
* community by sending a pull request to: | ||
* https://github.com/flowtype/flow-typed | ||
*/ | ||
|
||
declare module 'babel-cli' { | ||
declare module.exports: any; | ||
} | ||
|
||
/** | ||
* We include stubs for each file inside this npm package in case you need to | ||
* require those files directly. Feel free to delete any files that aren't | ||
* needed. | ||
*/ | ||
declare module 'babel-cli/bin/babel-doctor' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/bin/babel-external-helpers' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/bin/babel-node' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/bin/babel' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/_babel-node' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel-external-helpers' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel-node' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel/dir' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel/file' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel/index' { | ||
declare module.exports: any; | ||
} | ||
|
||
declare module 'babel-cli/lib/babel/util' { | ||
declare module.exports: any; | ||
} | ||
|
||
// Filename aliases | ||
declare module 'babel-cli/bin/babel-doctor.js' { | ||
declare module.exports: $Exports<'babel-cli/bin/babel-doctor'>; | ||
} | ||
declare module 'babel-cli/bin/babel-external-helpers.js' { | ||
declare module.exports: $Exports<'babel-cli/bin/babel-external-helpers'>; | ||
} | ||
declare module 'babel-cli/bin/babel-node.js' { | ||
declare module.exports: $Exports<'babel-cli/bin/babel-node'>; | ||
} | ||
declare module 'babel-cli/bin/babel.js' { | ||
declare module.exports: $Exports<'babel-cli/bin/babel'>; | ||
} | ||
declare module 'babel-cli/index' { | ||
declare module.exports: $Exports<'babel-cli'>; | ||
} | ||
declare module 'babel-cli/index.js' { | ||
declare module.exports: $Exports<'babel-cli'>; | ||
} | ||
declare module 'babel-cli/lib/_babel-node.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/_babel-node'>; | ||
} | ||
declare module 'babel-cli/lib/babel-external-helpers.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel-external-helpers'>; | ||
} | ||
declare module 'babel-cli/lib/babel-node.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel-node'>; | ||
} | ||
declare module 'babel-cli/lib/babel/dir.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel/dir'>; | ||
} | ||
declare module 'babel-cli/lib/babel/file.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel/file'>; | ||
} | ||
declare module 'babel-cli/lib/babel/index.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel/index'>; | ||
} | ||
declare module 'babel-cli/lib/babel/util.js' { | ||
declare module.exports: $Exports<'babel-cli/lib/babel/util'>; | ||
} |
Oops, something went wrong.