-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dematerialize): add dematerialize operator
- add dematerialize operator - add test coverage for dematerialize operator closes #475
- Loading branch information
Showing
5 changed files
with
115 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* globals describe, it, expect, expectObservable, hot */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
var Notification = Rx.Notification; | ||
|
||
describe('Observable.prototype.dematerialize()', function () { | ||
it('should dematerialize a happy stream', function () { | ||
var values = { | ||
a: Notification.createNext('w'), | ||
b: Notification.createNext('x'), | ||
c: Notification.createNext('y'), | ||
d: Notification.createComplete() | ||
}; | ||
|
||
var e1 = hot('--a--b--c--d--|', values); | ||
var expected = '--w--x--y--|'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize a sad stream', function () { | ||
var values = { | ||
a: Notification.createNext('w'), | ||
b: Notification.createNext('x'), | ||
c: Notification.createNext('y'), | ||
d: Notification.createError('error') | ||
}; | ||
|
||
var e1 = hot('--a--b--c--d--|', values); | ||
var expected = '--w--x--y--#'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize stream does not completes', function () { | ||
var e1 = hot('------'); | ||
var expected = '-'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize stream never completes', function () { | ||
var e1 = Observable.never(); | ||
var expected = '-'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize stream does not emit', function () { | ||
var e1 = hot('----|'); | ||
var expected = '----|)'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize empty stream', function () { | ||
var e1 = Observable.empty(); | ||
var expected = '|'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize stream throws', function () { | ||
var error = 'error'; | ||
var e1 = hot('(x|)', {x: Notification.createError(error)}); | ||
var expected = '#'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected, null, error); | ||
}); | ||
|
||
it('should dematerialize and completes when stream compltes with complete notification', function () { | ||
var e1 = hot('----(a|)', { a: Notification.createComplete() }); | ||
var expected = '----|'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
|
||
it('should dematerialize and completes when stream emits complete notification', function () { | ||
var e1 = hot('----a--|', { a: Notification.createComplete() }); | ||
var expected = '----|'; | ||
|
||
expectObservable(e1.dematerialize()).toBe(expected); | ||
}); | ||
}); |
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
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 Operator from '../Operator'; | ||
import Observer from '../Observer'; | ||
import Subscriber from '../Subscriber'; | ||
import Notification from '../Notification'; | ||
|
||
export default function dematerialize<T>() { | ||
return this.lift(new DeMaterializeOperator()); | ||
} | ||
|
||
class DeMaterializeOperator<T extends Notification<any>, R> implements Operator<T, R> { | ||
call(subscriber: Subscriber<R>) { | ||
return new DeMaterializeSubscriber(subscriber); | ||
} | ||
} | ||
|
||
class DeMaterializeSubscriber<T extends Notification<any>> extends Subscriber<T> { | ||
constructor(destination: Subscriber<any>) { | ||
super(destination); | ||
} | ||
|
||
_next(value: T) { | ||
value.observe(this.destination); | ||
} | ||
} |