-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Stream): implement really simply Stream and interval() factory
- Loading branch information
Andre Medeiros
committed
Feb 24, 2016
1 parent
6b9c54a
commit a3a08e7
Showing
8 changed files
with
145 additions
and
6 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 |
---|---|---|
|
@@ -28,6 +28,7 @@ lib/ | |
|
||
# Dependency directory | ||
node_modules | ||
typings/ | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
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
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 |
---|---|---|
@@ -1,3 +1,81 @@ | ||
export function Stream(subscribe: () => void) { | ||
this.subscribe = subscribe; | ||
export interface Observer<T> { | ||
next: (x: T) => void; | ||
error: (err: any) => void; | ||
complete: () => void; | ||
} | ||
|
||
export interface Machine<T> { | ||
start: (observer: Observer<T>) => void; | ||
stop: () => void; | ||
} | ||
|
||
export class Stream<T> implements Observer<T> { | ||
public observers: Array<Observer<T>>; | ||
|
||
constructor(public machine: Machine<T>) { | ||
this.observers = []; | ||
} | ||
|
||
next(x: T): void { | ||
const len = this.observers.length; | ||
for (let i = len - 1; i >= 0; i--) { | ||
this.observers[i].next(x); | ||
} | ||
} | ||
|
||
error(err: any): void { | ||
const len = this.observers.length; | ||
for (let i = len - 1; i >= 0; i--) { | ||
this.observers[i].error(err); | ||
} | ||
} | ||
|
||
complete(): void { | ||
const len = this.observers.length; | ||
for (let i = len - 1; i >= 0; i--) { | ||
this.observers[i].complete(); | ||
} | ||
} | ||
|
||
subscribe(observer: Observer<T>): void { | ||
this.observers.push(observer); | ||
if (this.observers.length === 1) this.machine.start(this); | ||
} | ||
|
||
unsubscribe(observer: Observer<T>): void { | ||
const i = this.observers.indexOf(observer); | ||
if (i > -1) { | ||
this.observers.splice(i, 1); | ||
if (!this.observers.length) this.machine.stop(); | ||
} | ||
} | ||
} | ||
|
||
class IntervalMachine implements Machine<number> { | ||
on: boolean; | ||
intervalID: any; | ||
i: number; | ||
|
||
constructor(public period: number) { | ||
this.intervalID = -1; | ||
this.i = 0; | ||
} | ||
|
||
start(observer: Observer<number>): void { | ||
this.intervalID = setInterval(() => observer.next(this.i++), this.period); | ||
} | ||
|
||
stop(): void { | ||
this.i = 0; | ||
if (this.intervalID !== -1) clearInterval(this.intervalID); | ||
} | ||
} | ||
|
||
export function interval(period: number) { | ||
const intervalMachine = new IntervalMachine(period); | ||
return new Stream<number>(intervalMachine); | ||
} | ||
|
||
export default { | ||
interval | ||
}; |
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,22 @@ | ||
"use strict"; | ||
var index_1 = require('../src/index'); | ||
var assert = require('assert'); | ||
describe('Stream', function () { | ||
it('can be subscribed and unsubscribed with one observer', function (done) { | ||
var stream = index_1.default.interval(100); | ||
var i = 0; | ||
var observer = { | ||
next: function (x) { | ||
assert.equal(x, i); | ||
i += 1; | ||
if (i === 2) { | ||
stream.unsubscribe(observer); | ||
done(); | ||
} | ||
}, | ||
error: done.fail, | ||
complete: done.fail, | ||
}; | ||
stream.subscribe(observer); | ||
}); | ||
}); |
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,22 @@ | ||
import xs from '../src/index'; | ||
import * as assert from 'assert'; | ||
|
||
describe('Stream', () => { | ||
it('can be subscribed and unsubscribed with one observer', (done) => { | ||
const stream = xs.interval(100); | ||
let i = 0; | ||
let observer = { | ||
next: (x: number) => { | ||
assert.equal(x, i); | ||
i += 1; | ||
if (i === 2) { | ||
stream.unsubscribe(observer); | ||
done(); | ||
} | ||
}, | ||
error: done.fail, | ||
complete: done.fail, | ||
}; | ||
stream.subscribe(observer); | ||
}); | ||
}); |
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
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
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 @@ | ||
{ | ||
"name": "xstream", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"ambientDevDependencies": { | ||
"mocha": "github:DefinitelyTyped/DefinitelyTyped/mocha/mocha.d.ts#d6dd320291705694ba8e1a79497a908e9f5e6617", | ||
"node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#20e1eb9616922d382d918cc5a21870a9dbe255f5" | ||
} | ||
} |