Skip to content

Commit

Permalink
feat(ZoneOperators): Add enterZone and leaveZone operators
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRyanDev committed May 8, 2016
1 parent fa8da40 commit f95a429
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 28 deletions.
11 changes: 11 additions & 0 deletions lib/add/operator/enter-zone.ts
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>;
}
}
11 changes: 11 additions & 0 deletions lib/add/operator/leave-zone.ts
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>;
}
}
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { ZoneOperator } from './operator/zone';

30 changes: 30 additions & 0 deletions lib/operator/enter-zone.ts
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));
}
}
30 changes: 30 additions & 0 deletions lib/operator/leave-zone.ts
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));
}
}
26 changes: 0 additions & 26 deletions lib/operator/zone.ts

This file was deleted.

27 changes: 27 additions & 0 deletions spec/enter-zone.spec.ts
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)
}
});
});
});
27 changes: 27 additions & 0 deletions spec/leave-zone.spec.ts
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)
}
}));
});
});
2 changes: 2 additions & 0 deletions spec/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
],
"files": [
"./index.spec.ts",
"./enter-zone.spec.ts",
"./leave-zone.spec.ts",
"../typings/main.d.ts"
],
"atom": {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.es5.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"exclude": [
"node_modules",
"spec",
"release",
"typings/browser",
"typings/browser.d.ts"
]
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"exclude": [
"node_modules",
"spec",
"typings"
"typings",
"release"
]
}

0 comments on commit f95a429

Please sign in to comment.