Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```js
- class DataSource {
- constructor() {
- let i = 0
- this._id = setInterval(() => {
- this.emit(i++);
- }, 1000);
- }
- emit(n) {
- let limit = 10;
- if (this.ondata) {
- this.ondata(n);
- }
- if (limit == n) {
- if (this.oncomplete) {
- this.oncomplete();
- }
- this.destroy();
- }
- }
- destroy() {
- clearInterval(this._id);
- }
- }
- let Producer = consumer => {
- let dataSource = new DataSource()
- dataSource.ondata = data => consumer.next(data)
- dataSource.onerror = err => consumer.error(err)
- dataSource.oncomplete = () => consumer.complete()
- return () => {
- dataSource.destroy();
- }
- }
- let consumer = {
- next(data) { console.log(data) },
- complete() { console.log("Done") },
- error(err) { console.error(err) }
- };
- Producer(consumer); // 0 1 2 3 4 5 .........n
Add Comment
Please, Sign In to add comment