Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
chore: enforce source format
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Dec 19, 2016
1 parent c0604f5 commit 50b168a
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 75 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ before_script:
- ./scripts/sauce/sauce_connect_block.sh

script:
- node_modules/.bin/gulp format:enforce
- node_modules/.bin/gulp build
- node_modules/.bin/karma start karma-dist-sauce-jasmine.conf.js --single-run
- node_modules/.bin/karma start karma-build-sauce-mocha.conf.js --single-run
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function patchXHR(window: any) {
// remove existing event listener
var listener = data.target[XHR_LISTENER];
if (listener) {
data.target.removeEventListener('readystatechange', listener);
data.target.removeEventListener('readystatechange', listener);
}
var newListener = data.target[XHR_LISTENER] = () => {
if (data.target.readyState === data.target.DONE) {
Expand Down
99 changes: 52 additions & 47 deletions lib/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export const isWebWorker: boolean =
(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope);

export const isNode: boolean =
(!('nw' in _global) && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]');
(!('nw' in _global) && typeof process !== 'undefined' &&
{}.toString.call(process) === '[object process]');

export const isBrowser: boolean =
!isNode && !isWebWorker && !!(typeof window !== 'undefined' && window['HTMLElement']);
Expand All @@ -56,11 +57,8 @@ export function patchProperty(obj, prop) {

const originalDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
if (!originalDesc && desc.get) {
Object.defineProperty(obj, 'original' + prop, {
enumerable: false,
configurable: true,
get: desc.get
});
Object.defineProperty(
obj, 'original' + prop, {enumerable: false, configurable: true, get: desc.get});
}

// A property descriptor cannot have getter/setter and be writable
Expand Down Expand Up @@ -144,9 +142,7 @@ const EVENT_TASKS = zoneSymbol('eventTasks');
const ADD_EVENT_LISTENER = 'addEventListener';
const REMOVE_EVENT_LISTENER = 'removeEventListener';

export interface NestedEventListener {
listener?: EventListenerOrEventListenerObject;
}
export interface NestedEventListener { listener?: EventListenerOrEventListenerObject; }

export declare type NestedEventListenerOrEventListenerObject =
NestedEventListener | EventListener | EventListenerObject;
Expand All @@ -157,9 +153,10 @@ export interface ListenerTaskMeta extends TaskData {
handler: NestedEventListenerOrEventListenerObject;
target: any;
name: string;
invokeAddFunc: (addFnSymbol: any, delegate: Task |
NestedEventListenerOrEventListenerObject) => any,
invokeRemoveFunc: (removeFnSymbol: any, delegate: Task | NestedEventListenerOrEventListenerObject) => any
invokeAddFunc: (addFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) => any,
invokeRemoveFunc:
(removeFnSymbol: any, delegate: Task|
NestedEventListenerOrEventListenerObject) => any
}

function findExistingRegisteredTask(
Expand Down Expand Up @@ -214,29 +211,34 @@ function attachRegisteredEvent(target: any, eventTask: Task, isPrepend: boolean)
}
}

const defaultListenerMetaCreator = (self: any, args: any[]) => {
return {
useCapturing: args[2],
eventName: args[0],
handler: args[1],
target: self || _global,
name: args[0],
invokeAddFunc: function (addFnSymbol: any, delegate: Task | NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol](this.eventName, (<Task>delegate).invoke, this.useCapturing);
} else {
return this.target[addFnSymbol](this.eventName, delegate, this.useCapturing);
}
},
invokeRemoveFunc: function (removeFnSymbol:any, delegate: Task | NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol](this.eventName, (<Task>delegate).invoke, this.useCapturing);
} else {
return this.target[removeFnSymbol](this.eventName, delegate, this.useCapturing);
}
}
};
}
const defaultListenerMetaCreator =
(self: any, args: any[]) => {
return {
useCapturing: args[2],
eventName: args[0],
handler: args[1],
target: self || _global,
name: args[0],
invokeAddFunc: function(
addFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[addFnSymbol](
this.eventName, (<Task>delegate).invoke, this.useCapturing);
} else {
return this.target[addFnSymbol](this.eventName, delegate, this.useCapturing);
}
},
invokeRemoveFunc: function(
removeFnSymbol: any, delegate: Task|NestedEventListenerOrEventListenerObject) {
if (delegate && (<Task>delegate).invoke) {
return this.target[removeFnSymbol](
this.eventName, (<Task>delegate).invoke, this.useCapturing);
} else {
return this.target[removeFnSymbol](this.eventName, delegate, this.useCapturing);
}
}
};
}

export function makeZoneAwareAddListener(
addFnName: string, removeFnName: string, useCapturingParam: boolean = true,
Expand Down Expand Up @@ -289,8 +291,8 @@ export function makeZoneAwareAddListener(
}

if (!allowDuplicates) {
const eventTask: Task =
findExistingRegisteredTask(data.target, data.handler, data.eventName, data.useCapturing, false);
const eventTask: Task = findExistingRegisteredTask(
data.target, data.handler, data.eventName, data.useCapturing, false);
if (eventTask) {
// we already registered, so this will have noop.
return data.invokeAddFunc(addFnSymbol, eventTask);
Expand All @@ -304,8 +306,9 @@ export function makeZoneAwareAddListener(
};
}

export function makeZoneAwareRemoveListener(fnName: string, useCapturingParam: boolean = true,
metaCreator: (self: any, args: any[]) => ListenerTaskMeta = defaultListenerMetaCreator) {
export function makeZoneAwareRemoveListener(
fnName: string, useCapturingParam: boolean = true,
metaCreator: (self: any, args: any[]) => ListenerTaskMeta = defaultListenerMetaCreator) {
const symbol = zoneSymbol(fnName);
const defaultUseCapturing = useCapturingParam ? false : undefined;

Expand All @@ -315,8 +318,8 @@ export function makeZoneAwareRemoveListener(fnName: string, useCapturingParam: b
// - Inside a Web Worker, `this` is undefined, the context is `global`
// - When `addEventListener` is called on the global context in strict mode, `this` is undefined
// see https://github.com/angular/zone.js/issues/190
const eventTask = findExistingRegisteredTask(data.target, data.handler, data.eventName,
data.useCapturing, true);
const eventTask = findExistingRegisteredTask(
data.target, data.handler, data.eventName, data.useCapturing, true);
if (eventTask) {
eventTask.zone.cancelTask(eventTask);
} else {
Expand Down Expand Up @@ -370,14 +373,16 @@ const zoneAwareAddEventListener =
makeZoneAwareAddListener(ADD_EVENT_LISTENER, REMOVE_EVENT_LISTENER);
const zoneAwareRemoveEventListener = makeZoneAwareRemoveListener(REMOVE_EVENT_LISTENER);

export function patchEventTargetMethods(obj: any, addFnName: string = ADD_EVENT_LISTENER,
removeFnName: string = REMOVE_EVENT_LISTENER,
metaCreator: (self: any, args: any[]) => ListenerTaskMeta = defaultListenerMetaCreator): boolean {
export function patchEventTargetMethods(
obj: any, addFnName: string = ADD_EVENT_LISTENER, removeFnName: string = REMOVE_EVENT_LISTENER,
metaCreator: (self: any, args: any[]) => ListenerTaskMeta =
defaultListenerMetaCreator): boolean {
if (obj && obj[addFnName]) {
patchMethod(obj, addFnName,
patchMethod(
obj, addFnName,
() => makeZoneAwareAddListener(addFnName, removeFnName, true, false, false, metaCreator));
patchMethod(obj, removeFnName,
() => makeZoneAwareRemoveListener(removeFnName, true, metaCreator));
patchMethod(
obj, removeFnName, () => makeZoneAwareRemoveListener(removeFnName, true, metaCreator));
return true;
} else {
return false;
Expand Down
18 changes: 10 additions & 8 deletions lib/zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,7 @@ const Zone: ZoneType = (function(global: any) {
this._invokeZS = zoneSpec && (zoneSpec.onInvoke ? zoneSpec : parentDelegate._invokeZS);
this._invokeDlgt =
zoneSpec && (zoneSpec.onInvoke ? parentDelegate : parentDelegate._invokeDlgt);
this._invokeCurrZone =
zoneSpec && (zoneSpec.onInvoke ? this.zone : parentDelegate.zone);
this._invokeCurrZone = zoneSpec && (zoneSpec.onInvoke ? this.zone : parentDelegate.zone);

this._handleErrorZS =
zoneSpec && (zoneSpec.onHandleError ? zoneSpec : parentDelegate._handleErrorZS);
Expand Down Expand Up @@ -803,8 +802,7 @@ const Zone: ZoneType = (function(global: any) {
this._hasTaskZS = zoneSpec && (zoneSpec.onHasTask ? zoneSpec : parentDelegate._hasTaskZS);
this._hasTaskDlgt =
zoneSpec && (zoneSpec.onHasTask ? parentDelegate : parentDelegate._hasTaskDlgt);
this._hasTaskCurrZone =
zoneSpec && (zoneSpec.onHasTask ? this.zone : parentDelegate.zone);
this._hasTaskCurrZone = zoneSpec && (zoneSpec.onHasTask ? this.zone : parentDelegate.zone);
}

fork(targetZone: Zone, zoneSpec: ZoneSpec): AmbientZone {
Expand All @@ -823,13 +821,15 @@ const Zone: ZoneType = (function(global: any) {
any {
return this._invokeZS ?
this._invokeZS.onInvoke(
this._invokeDlgt, this._invokeCurrZone, targetZone, callback, applyThis, applyArgs, source) :
this._invokeDlgt, this._invokeCurrZone, targetZone, callback, applyThis, applyArgs,
source) :
callback.apply(applyThis, applyArgs);
}

handleError(targetZone: Zone, error: any): boolean {
return this._handleErrorZS ?
this._handleErrorZS.onHandleError(this._handleErrorDlgt, this._handleErrorCurrZone, targetZone, error) :
this._handleErrorZS.onHandleError(
this._handleErrorDlgt, this._handleErrorCurrZone, targetZone, error) :
true;
}

Expand Down Expand Up @@ -857,7 +857,8 @@ const Zone: ZoneType = (function(global: any) {
try {
return this._invokeTaskZS ?
this._invokeTaskZS.onInvokeTask(
this._invokeTaskDlgt, this._invokeTaskCurrZone, targetZone, task, applyThis, applyArgs) :
this._invokeTaskDlgt, this._invokeTaskCurrZone, targetZone, task, applyThis,
applyArgs) :
task.callback.apply(applyThis, applyArgs);
} finally {
if (targetZone == this.zone && (task.type != 'eventTask') &&
Expand All @@ -870,7 +871,8 @@ const Zone: ZoneType = (function(global: any) {
cancelTask(targetZone: Zone, task: Task): any {
let value;
if (this._cancelTaskZS) {
value = this._cancelTaskZS.onCancelTask(this._cancelTaskDlgt, this._cancelTaskCurrZone, targetZone, task);
value = this._cancelTaskZS.onCancelTask(
this._cancelTaskDlgt, this._cancelTaskCurrZone, targetZone, task);
} else if (!task.cancelFn) {
throw new Error('Task does not support cancellation, or is already canceled.');
} else {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@types/jasmine": "^2.2.33",
"@types/node": "^6.0.38",
"@types/systemjs": "^0.19.30",
"clang-format": "^1.0.46",
"concurrently": "^2.2.0",
"conventional-changelog": "^1.1.0",
"es6-promise": "^3.0.2",
Expand Down
23 changes: 12 additions & 11 deletions test/browser/XMLHttpRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,16 @@ describe('XMLHttpRequest', function() {
expect(XMLHttpRequest.DONE).toEqual(4);
});

it('should work properly when send request multiple times on single xmlRequest instance', function() {
testZone.run(function() {
var req = new XMLHttpRequest();
req.open('get', '/', true);
req.send();
req.onloadend = function() {
req.open('get', '/', true);
req.send();
}
});
})
it('should work properly when send request multiple times on single xmlRequest instance',
function() {
testZone.run(function() {
var req = new XMLHttpRequest();
req.open('get', '/', true);
req.send();
req.onloadend = function() {
req.open('get', '/', true);
req.send();
}
});
})
});
13 changes: 6 additions & 7 deletions test/common/zone.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@ describe('Zone', function() {
var zone = Zone.current;
var zoneA = zone.fork({
name: 'A',
onInvoke: function(parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
onInvoke: function(
parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
expect(currentZone.name).toEqual('A');
return parentDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
}
});
var zoneB = zoneA.fork({
name: 'B',
onInvoke: function(parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
onInvoke: function(
parentDelegate, currentZone, targetZone, callback, applyThis, applyArgs, source) {
expect(currentZone.name).toEqual('B');
return parentDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
}
});
var zoneC = zoneB.fork({
name: 'C'
});
zoneC.run(function() {
});
var zoneC = zoneB.fork({name: 'C'});
zoneC.run(function() {});
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare var __karma__: {
__karma__.loaded = function() {};
(window as any).global = window;

System.config({ defaultJSExtensions: true });
System.config({defaultJSExtensions: true});
var browserPatchedPromise = null;
if (window[Zone['__symbol__']('setTimeout')]) {
browserPatchedPromise = Promise.resolve('browserPatched');
Expand Down

0 comments on commit 50b168a

Please sign in to comment.