-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): implement map operator with MapMachine
- Loading branch information
Andre Medeiros
committed
Feb 24, 2016
1 parent
63502a9
commit 76df500
Showing
9 changed files
with
141 additions
and
105 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
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,6 @@ | ||
import {Observer} from './Observer'; | ||
|
||
export interface Machine<T> { | ||
start: (observer: Observer<T>) => void; | ||
stop: () => void; | ||
} |
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,5 @@ | ||
export interface Observer<T> { | ||
next: (x: T) => void; | ||
error: (err: any) => void; | ||
complete: () => void; | ||
} |
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,51 @@ | ||
import {Observer} from './Observer'; | ||
import {Machine} from './Machine'; | ||
import {MapMachine} from './operator/MapMachine'; | ||
|
||
export class Stream<T> implements Observer<T> { | ||
public observers: Array<Observer<T>>; | ||
public num: number; // Number of non-operator subscribers | ||
|
||
constructor(public machine: Machine<T>) { | ||
this.observers = []; | ||
this.num = 0; | ||
} | ||
|
||
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.num === 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.num <= 0) this.machine.stop(); | ||
} | ||
} | ||
|
||
map<U>(projection: (t: T) => U): Stream<U> { | ||
return new Stream<U>(new MapMachine(projection, this)); | ||
} | ||
} |
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,28 @@ | ||
import {Machine} from '../Machine'; | ||
import {Observer} from '../Observer'; | ||
import {Stream} from '../Stream'; | ||
|
||
class IntervalMachine implements Machine<number> { | ||
on: boolean; | ||
intervalID: any; | ||
i: number; | ||
|
||
constructor(public period: number) { | ||
this.intervalID = -1; | ||
this.i = 0; | ||
} | ||
|
||
start(stream: Observer<number>): void { | ||
this.intervalID = setInterval(() => stream.next(this.i++), this.period); | ||
} | ||
|
||
stop(): void { | ||
this.i = 0; | ||
if (this.intervalID !== -1) clearInterval(this.intervalID); | ||
} | ||
} | ||
|
||
export default function interval(period: number) { | ||
const intervalMachine = new IntervalMachine(period); | ||
return new Stream<number>(intervalMachine); | ||
} |
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,81 +1,7 @@ | ||
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); | ||
} | ||
import interval from './factory/interval'; | ||
import {Stream} from './Stream'; | ||
|
||
export default { | ||
interval | ||
Stream, | ||
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,24 @@ | ||
import {Observer} from '../Observer'; | ||
import {Machine} from '../Machine'; | ||
import {Stream} from '../Stream'; | ||
|
||
export class MapMachine<T, U> implements Machine<U> { | ||
public proxy: Observer<T>; | ||
|
||
constructor(public projection: (t: T) => U, | ||
public inStream: Stream<T>) { | ||
} | ||
|
||
start(outStream: Observer<U>): void { | ||
this.proxy = { | ||
next: (t: T) => outStream.next(this.projection(t)), | ||
error: outStream.error, | ||
complete: outStream.complete, | ||
}; | ||
this.inStream.subscribe(this.proxy); | ||
} | ||
|
||
stop(): void { | ||
this.inStream.unsubscribe(this.proxy); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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