Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/easywebapp/iterable-observer
Observable Proposal implement based on Async Generator (ES 2018) & TypeScript
https://github.com/easywebapp/iterable-observer
async generator iterator observable observer proposal
Last synced: 9 days ago
JSON representation
Observable Proposal implement based on Async Generator (ES 2018) & TypeScript
- Host: GitHub
- URL: https://github.com/easywebapp/iterable-observer
- Owner: EasyWebApp
- Created: 2018-07-30T21:56:20.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-08-16T17:53:00.000Z (3 months ago)
- Last Synced: 2024-10-30T00:45:00.732Z (17 days ago)
- Topics: async, generator, iterator, observable, observer, proposal
- Language: TypeScript
- Homepage: https://web-cell.dev/iterable-observer/
- Size: 458 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: ReadMe.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# Iterable Observer
[Observable Proposal][1] implement based on [Async Generator (ES 2018)][2] & [TypeScript][3]
[![NPM Dependency](https://img.shields.io/librariesio/github/EasyWebApp/iterable-observer.svg)][4]
[![CI & CD](https://github.com/EasyWebApp/iterable-observer/actions/workflows/main.yml/badge.svg)][5]
[![](https://data.jsdelivr.com/v1/package/npm/iterable-observer/badge?style=rounded)][6][![NPM](https://nodei.co/npm/iterable-observer.png?downloads=true&downloadRank=true&stars=true)][7]
## Usage
### Basic
```javascript
import { Observable } from 'iterable-observer';var count = 0,
list = [];const observable = new Observable(({ next, complete }) => {
const timer = setInterval(
() => (++count < 5 ? next(count) : complete(count)),
0
);
return () => clearInterval(timer);
});(async () => {
for await (const item of observable) list.push(item);console.log(list); // [1, 2, 3, 4, 5]
})();
```### Enhance Run-time platforms
#### Transform events
```javascript
import { Observable } from 'iterable-observer';const reader = new FileReader(),
{ files } = document.querySelector('input[type="file"]');reader.readAsBlob(files[0]);
(async () => {
for await (const { loaded } of Observable.fromEvent(reader, 'progress'))
console.log((loaded / file.size) * 100 + '%');
})();
```#### Transform streams
```typescript
import { Observable } from 'iterable-observer';(async () => {
const { body } = await fetch('https://example.com/path/to/blob'),
chunks: Uint8Array[] = [];for await (const chunk of Observable.fromStream(body)) chunks.push(chunk);
const blob = new Blob(chunks);
console.log(blob);
}();
```### Concurrent Task to Serial Queue
```javascript
import { createQueue } from 'iterable-observer';
import Koa from 'koa';
import BodyParser from 'koa-bodyparser';const { process, observable } = createQueue(),
app = new Koa();(async () => {
for await (const { defer, data } of observable)
defer.resolve(JSON.stringify(data));
})();app.use(BodyParser)
.use(async context => (context.body = await process(context.request.body)))
.listen(80);
```[1]: https://github.com/tc39/proposal-observable
[2]: https://tc39.es/ecma262/#sec-asyncgeneratorfunction-objects
[3]: https://www.typescriptlang.org/
[4]: https://libraries.io/npm/iterable-observer
[5]: https://github.com/EasyWebApp/iterable-observer/actions/workflows/main.yml
[6]: https://www.jsdelivr.com/package/npm/iterable-observer
[7]: https://nodei.co/npm/iterable-observer/