Skip to content

Commit

Permalink
fix(isomorphic): Remove use of CustomEvent. Detect root scope (global…
Browse files Browse the repository at this point in the history
…/window/self) for nodejs, browser, or web-worker.

One ramification of this change is that `onChange` listeners no longer receive a 'real' fake locationchange Event object when `fireAfterUpdate` is true.
  • Loading branch information
christopherthielen committed Oct 4, 2017
1 parent 943d782 commit 2d206ba
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 47 deletions.
7 changes: 5 additions & 2 deletions src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import { all, any, prop, curry, not } from "./hof";
import { services } from "./coreservices";
import { StateObject } from "../state/stateObject";

let w: any = typeof window === 'undefined' ? {} : window;
let angular = w.angular || {};
declare const global;
export const root: any = (typeof self === 'object' && self.self === self && self) ||
(typeof global === 'object' && global.global === global && global) || this;
const angular = root.angular || {};

export const fromJson = angular.fromJson || JSON.parse.bind(JSON);
export const toJson = angular.toJson || JSON.stringify.bind(JSON);
export const forEach = angular.forEach || _forEach;
Expand Down
21 changes: 8 additions & 13 deletions src/vanilla/baseLocationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@
* @module vanilla
*/ /** */

import { LocationServices } from "../common/coreservices";
import { Disposable } from "../interface";
import { UIRouter } from "../router";
import { LocationLike, HistoryLike } from "./interface";
import { parseUrl, getParams, buildUrl, getCustomEventCtor } from "./utils";
import { isDefined } from "../common/predicates";
import { extend, deregAll, removeFrom } from "../common/common";

const Evt: typeof CustomEvent = getCustomEventCtor();
import { deregAll, isDefined, LocationServices, removeFrom, root } from '../common';
import { Disposable } from '../interface';
import { UIRouter } from '../router';
import { HistoryLike, LocationLike } from './interface';
import { buildUrl, getParams, parseUrl } from './utils';

/** A base `LocationServices` */
export abstract class BaseLocationServices implements LocationServices, Disposable {
constructor(router: UIRouter, public fireAfterUpdate: boolean) {
this._location = self && self.location;
this._history = self && self.history;
this._location = root.location;
this._history = root.history;
}

_listener = evt => this._listeners.forEach(cb => cb(evt));
Expand Down Expand Up @@ -62,8 +58,7 @@ export abstract class BaseLocationServices implements LocationServices, Disposab
this._set(null, null, url, replace);

if (this.fireAfterUpdate) {
let evt = extend(new Evt("locationchange"), { url });
this._listeners.forEach(cb => cb(evt));
this._listeners.forEach(cb => cb({ url }));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/vanilla/hashLocationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* @module vanilla
*/
/** */
import { trimHashVal } from "../common/strings";
import { UIRouter } from "../router";
import { BaseLocationServices } from "./baseLocationService";
import { root, trimHashVal } from '../common';
import { UIRouter } from '../router';
import { BaseLocationServices } from './baseLocationService';

/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
export class HashLocationService extends BaseLocationServices {
constructor(router: UIRouter) {
super(router, false);
self.addEventListener('hashchange', this._listener, false);
root.addEventListener('hashchange', this._listener, false);
}

_get() {
Expand All @@ -23,7 +23,7 @@ export class HashLocationService extends BaseLocationServices {

dispose (router: UIRouter) {
super.dispose(router);
self.removeEventListener('hashchange', this._listener);
root.removeEventListener('hashchange', this._listener);
}
}

4 changes: 2 additions & 2 deletions src/vanilla/memoryLocationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @module vanilla
*/
/** */
import { BaseLocationServices } from "./baseLocationService";
import { UIRouter } from "../router";
import { BaseLocationServices } from './baseLocationService';
import { UIRouter } from '../router';

/** A `LocationServices` that gets/sets the current location from an in-memory object */
export class MemoryLocationService extends BaseLocationServices {
Expand Down
13 changes: 6 additions & 7 deletions src/vanilla/pushStateLocationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
* @module vanilla
*/
/** */
import { LocationConfig } from "../common/coreservices";
import { UIRouter } from "../router";
import { BaseLocationServices } from "./baseLocationService";
import { splitQuery, splitHash, stripFile } from "../common/strings";
import { UIRouter } from '../router';
import { BaseLocationServices } from './baseLocationService';
import { LocationConfig, root, splitHash, splitQuery, stripFile } from '../common';

/**
* A `LocationServices` that gets/sets the current location using the browser's `location` and `history` apis
Expand All @@ -19,7 +18,7 @@ export class PushStateLocationService extends BaseLocationServices {
constructor(router: UIRouter) {
super(router, true);
this._config = router.urlService.config;
self.addEventListener("popstate", this._listener, false);
root.addEventListener('popstate', this._listener, false);
};

/**
Expand Down Expand Up @@ -47,7 +46,7 @@ export class PushStateLocationService extends BaseLocationServices {
let startsWith = pathname.startsWith(basePrefix);
pathname = exactMatch ? '/' : startsWith ? pathname.substring(basePrefix.length) : pathname;

return pathname + (search ? "?" + search : "") + (hash ? "#" + hash : "");
return pathname + (search ? '?' + search : '') + (hash ? '#' + hash : '');
}

_set(state: any, title: string, url: string, replace: boolean) {
Expand All @@ -62,7 +61,7 @@ export class PushStateLocationService extends BaseLocationServices {

dispose(router: UIRouter) {
super.dispose(router);
self.removeEventListener("popstate", this._listener);
root.removeEventListener('popstate', this._listener);
}
}

18 changes: 0 additions & 18 deletions src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,3 @@ export function locationPluginFactory(
return { name, service, configuration, dispose };
};
}

export function getCustomEventCtor(): typeof CustomEvent {
// CustomEvent Polyfill
function _CustomEvent(event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
let evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
_CustomEvent.prototype = Event.prototype;

try {
new CustomEvent('foo');
return CustomEvent;
} catch (_err) {
return _CustomEvent as any;
}
}

0 comments on commit 2d206ba

Please sign in to comment.