-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ZoneOperators): Add enterZone and leaveZone operators
- Loading branch information
1 parent
fa8da40
commit f95a429
Showing
11 changed files
with
142 additions
and
28 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,11 @@ | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { enterZone, EnterZoneSignature } from '../../operator/enter-zone'; | ||
|
||
Observable.prototype.enterZone = enterZone; | ||
|
||
declare module 'rxjs/Observable' { | ||
interface Observable<T> { | ||
enterZone: EnterZoneSignature<T>; | ||
} | ||
} |
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,11 @@ | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { leaveZone, LeaveZoneSignature } from '../../operator/leave-zone'; | ||
|
||
Observable.prototype.leaveZone = leaveZone; | ||
|
||
declare module 'rxjs/Observable' { | ||
interface Observable<T> { | ||
leaveZone: LeaveZoneSignature<T>; | ||
} | ||
} |
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 +1 @@ | ||
export { ZoneOperator } from './operator/zone'; | ||
|
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,30 @@ | ||
import { NgZone } from '@angular/core'; | ||
import { Operator } from 'rxjs/Operator'; | ||
import { Subscriber } from 'rxjs/Subscriber'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
export interface EnterZoneSignature<T> { | ||
(zone: NgZone): Observable<T>; | ||
} | ||
|
||
export function enterZone<T>(zone: NgZone): Observable<T> { | ||
return this.lift(new EnterZoneOperator(zone)); | ||
} | ||
|
||
export class EnterZoneOperator<T> implements Operator<T, T> { | ||
constructor(private _zone: NgZone) { } | ||
|
||
call(subscriber: Subscriber<T>, source: any): any { | ||
return source._subscribe(new EnterZoneSubscriber(subscriber, this._zone)); | ||
} | ||
} | ||
|
||
class EnterZoneSubscriber<T> extends Subscriber<T> { | ||
constructor(destination: Subscriber<T>, private _zone: NgZone) { | ||
super(destination); | ||
} | ||
|
||
protected _next(value: T) { | ||
this._zone.run(() => this.destination.next(value)); | ||
} | ||
} |
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,30 @@ | ||
import { NgZone } from '@angular/core'; | ||
import { Operator } from 'rxjs/Operator'; | ||
import { Subscriber } from 'rxjs/Subscriber'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
export function leaveZone<T>(zone: NgZone): Observable<T> { | ||
return this.lift(new LeaveZoneOperator(zone)); | ||
} | ||
|
||
export interface LeaveZoneSignature<T> { | ||
(zone: NgZone): Observable<T>; | ||
} | ||
|
||
export class LeaveZoneOperator<T> implements Operator<T, T> { | ||
constructor(private _zone: NgZone) { } | ||
|
||
call(subscriber: Subscriber<T>, source: any): any { | ||
return source._subscribe(new LeaveZoneSubscriber(subscriber, this._zone)); | ||
} | ||
} | ||
|
||
class LeaveZoneSubscriber<T> extends Subscriber<T> { | ||
constructor(destination: Subscriber<T>, private _zone: NgZone) { | ||
super(destination); | ||
} | ||
|
||
protected _next(value: T) { | ||
this._zone.runOutsideAngular(() => this.destination.next(value)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import 'rxjs/add/observable/of'; | ||
import 'rxjs/add/operator/map'; | ||
import { createNgZone } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import '../lib/add/operator/enter-zone'; | ||
|
||
declare var Zone; | ||
|
||
describe('enterZone Operator', function() { | ||
it('should cause an observable stream to enter the ng zone', function(done) { | ||
const zone = createNgZone(); | ||
|
||
Observable.of(1) | ||
.enterZone(zone) | ||
.map(() => Zone.current.name) | ||
.subscribe({ | ||
next(name) { | ||
expect(name).toEqual('angular'); | ||
done(); | ||
}, | ||
error(err) { | ||
done(err) | ||
} | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import 'rxjs/add/observable/of'; | ||
import 'rxjs/add/operator/map'; | ||
import { createNgZone } from '@angular/core'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import '../lib/add/operator/leave-zone'; | ||
|
||
declare var Zone; | ||
|
||
describe('leaveZone Operator', function() { | ||
it('should cause an observable stream to leave the ng zone', function(done) { | ||
const zone = createNgZone(); | ||
|
||
zone.run(() => Observable.of(1) | ||
.leaveZone(zone) | ||
.map(() => Zone.current.name) | ||
.subscribe({ | ||
next(name) { | ||
expect(name).not.toEqual('angular'); | ||
done(); | ||
}, | ||
error(err) { | ||
done(err) | ||
} | ||
})); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"exclude": [ | ||
"node_modules", | ||
"spec", | ||
"release", | ||
"typings/browser", | ||
"typings/browser.d.ts" | ||
] | ||
|
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
"exclude": [ | ||
"node_modules", | ||
"spec", | ||
"typings" | ||
"typings", | ||
"release" | ||
] | ||
} |