Options
All
  • Public
  • Public/Protected
  • All
Menu

Iterable Observer

Iterable Observer

Observable Proposal implement based on Async Generator (ES 2018) & TypeScript

Build Status

NPM

Usage

Basic

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

import { Observable } from 'iterable-observer';

const reader = new FileReader(),
    {
        files: [file]
    } = document.querySelector('input[type="file"]');

reader.readAsBlob(file);

(async () => {
    for await (const { loaded } of Observable.fromEvent(reader, 'progress'))
        console.log((loaded / file.size) * 100 + '%');
})();

Generated using TypeDoc