From e5904f40892ca6f53a82d6c5a490d3344a4240fa Mon Sep 17 00:00:00 2001 From: Rob Wormald Date: Thu, 26 May 2016 09:34:04 -0700 Subject: [PATCH] fix(facade): change EventEmitter to be sync by default (#8761) --- .../core/test/linker/integration_spec.ts | 5 +-- modules/@angular/facade/src/async.ts | 2 +- modules/@angular/facade/test/async_spec.ts | 37 ++++++++++--------- .../test/location/location_spec.ts | 5 ++- modules/@angular/upgrade/test/upgrade_spec.ts | 18 +++------ 5 files changed, 31 insertions(+), 36 deletions(-) diff --git a/modules/@angular/core/test/linker/integration_spec.ts b/modules/@angular/core/test/linker/integration_spec.ts index 6a4da2854a..04be3a6f80 100644 --- a/modules/@angular/core/test/linker/integration_spec.ts +++ b/modules/@angular/core/test/linker/integration_spec.ts @@ -1527,11 +1527,10 @@ function declareTests(isJit: boolean) { tick(); var tc = fixture.debugElement.children[0]; - tc.inject(DirectiveEmittingEvent).fireEvent("boom"); + try { - tick(); - throw "Should throw"; + tc.inject(DirectiveEmittingEvent).fireEvent("boom"); } catch (e) { clearPendingTimers(); diff --git a/modules/@angular/facade/src/async.ts b/modules/@angular/facade/src/async.ts index 6121565521..ce738c1547 100644 --- a/modules/@angular/facade/src/async.ts +++ b/modules/@angular/facade/src/async.ts @@ -115,7 +115,7 @@ export class EventEmitter extends Subject { * Creates an instance of [EventEmitter], which depending on [isAsync], * delivers events synchronously or asynchronously. */ - constructor(isAsync: boolean = true) { + constructor(isAsync: boolean = false) { super(); this.__isAsync = isAsync; } diff --git a/modules/@angular/facade/test/async_spec.ts b/modules/@angular/facade/test/async_spec.ts index 67358d7c48..89fbc4f923 100644 --- a/modules/@angular/facade/test/async_spec.ts +++ b/modules/@angular/facade/test/async_spec.ts @@ -46,27 +46,27 @@ export function main() { ObservableWrapper.callComplete(emitter); })); - it("should subscribe to the wrapper asynchronously", () => { + it("should subscribe to the wrapper synchronously", () => { var called = false; ObservableWrapper.subscribe(emitter, (value) => { called = true; }); ObservableWrapper.callEmit(emitter, 99); - expect(called).toBe(false); + expect(called).toBe(true); }); // Makes Edge to disconnect when running the full unit test campaign // TODO: remove when issue is solved: https://github.com/angular/angular/issues/4756 if (!browserDetection.isEdge) { - it("delivers next and error events asynchronously", inject([AsyncTestCompleter], (async) => { + it("delivers next and error events synchronously", inject([AsyncTestCompleter], (async) => { let log = []; ObservableWrapper.subscribe(emitter, (x) => { log.push(x); - expect(log).toEqual([1, 3, 5, 2]); + expect(log).toEqual([1, 2]); }, (err) => { log.push(err); - expect(log).toEqual([1, 3, 5, 2, 4]); + expect(log).toEqual([1, 2, 3, 4]); async.done(); }); log.push(1); @@ -76,36 +76,39 @@ export function main() { log.push(5); })); - it("delivers next and complete events asynchronously", - inject([AsyncTestCompleter], (async) => { + it("delivers next and complete events synchronously", () => { let log = []; ObservableWrapper.subscribe(emitter, (x) => { log.push(x); - expect(log).toEqual([1, 3, 5, 2]); + expect(log).toEqual([1, 2]); }, null, () => { log.push(4); - expect(log).toEqual([1, 3, 5, 2, 4]); - async.done(); - }); + expect(log).toEqual([1, 2, 3, 4]); + }); log.push(1); ObservableWrapper.callEmit(emitter, 2); log.push(3); ObservableWrapper.callComplete(emitter); log.push(5); - })); + expect(log).toEqual([1, 2, 3, 4, 5]); + }); } - it('delivers events synchronously', () => { - var e = new EventEmitter(false); + it('delivers events asynchronously when forced to async mode', inject([AsyncTestCompleter], (async) => { + var e = new EventEmitter(true); var log = []; - ObservableWrapper.subscribe(e, (x) => { log.push(x); }); + ObservableWrapper.subscribe(e, (x) => { + log.push(x); + expect(log).toEqual([1, 3, 2]); + async.done(); + }); log.push(1); ObservableWrapper.callEmit(e, 2); log.push(3); - expect(log).toEqual([1, 2, 3]); - }); + + })); it('reports whether it has subscribers', () => { var e = new EventEmitter(false); diff --git a/modules/@angular/router-deprecated/test/location/location_spec.ts b/modules/@angular/router-deprecated/test/location/location_spec.ts index fd5bec2dae..dd249d6995 100644 --- a/modules/@angular/router-deprecated/test/location/location_spec.ts +++ b/modules/@angular/router-deprecated/test/location/location_spec.ts @@ -44,11 +44,12 @@ export function main() { }); it('should normalize urls on popstate', inject([AsyncTestCompleter], (async) => { - locationStrategy.simulatePopState('/my/app/user/btford'); + location.subscribe((ev) => { expect(ev['url']).toEqual('/user/btford'); async.done(); - }) + }); + locationStrategy.simulatePopState('/my/app/user/btford'); })); it('should revert to the previous path when a back() operation is executed', () => { diff --git a/modules/@angular/upgrade/test/upgrade_spec.ts b/modules/@angular/upgrade/test/upgrade_spec.ts index ab7cc8cb35..828d200190 100644 --- a/modules/@angular/upgrade/test/upgrade_spec.ts +++ b/modules/@angular/upgrade/test/upgrade_spec.ts @@ -193,19 +193,11 @@ export function main() { expect(multiTrim(document.body.textContent)) .toEqual( "ignore: -; " + "literal: Text; interpolate: Hello world; " + - "oneWayA: A; oneWayB: B; twoWayA: initModelA; twoWayB: initModelB; (1) | " + - "modelA: initModelA; modelB: initModelB; eventA: ?; eventB: ?;"); - setTimeout(() => { - // we need to do setTimeout, because the EventEmitter uses setTimeout to schedule - // events, and so without this we would not see the events processed. - expect(multiTrim(document.body.textContent)) - .toEqual("ignore: -; " + "literal: Text; interpolate: Hello world; " + - "oneWayA: A; oneWayB: B; twoWayA: newA; twoWayB: newB; (3) | " + - "modelA: newA; modelB: newB; eventA: aFired; eventB: bFired;"); - ref.dispose(); - async.done(); - }); - }); + "oneWayA: A; oneWayB: B; twoWayA: newA; twoWayB: newB; (2) | " + + "modelA: newA; modelB: newB; eventA: aFired; eventB: bFired;") + ref.dispose(); + async.done(); + }); }));