-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vanilla): Implement in-memory-only location api
test(resolve): Put beforeEach router init inside describe block test(stateService): re-implement url-based tests using in-memory location API
- Loading branch information
1 parent
0690917
commit f64aace
Showing
10 changed files
with
264 additions
and
187 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
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { services, isDefined } from '../common/module'; | ||
import { LocationConfig, LocationServices } from '../common/coreservices'; | ||
import { splitQuery, trimHashVal, getParams, splitHash, locationPluginFactory } from './utils'; | ||
import { removeFrom, unnestR } from "../common/common"; | ||
import { UIRouter } from "../router"; | ||
import { LocationPlugin } from "./interface"; | ||
import { isArray } from "../common/predicates"; | ||
|
||
var mlc; | ||
export const memoryLocationConfig: LocationConfig = mlc = { | ||
_hashPrefix: '', | ||
_baseHref: '', | ||
_port: 80, | ||
_protocol: "http", | ||
_host: "localhost", | ||
|
||
port: () => mlc._port, | ||
protocol: () => mlc._protocol, | ||
host: () => mlc._host, | ||
baseHref: () => mlc._baseHref, | ||
html5Mode: () => false, | ||
hashPrefix: (newprefix?: string): string => { | ||
if (isDefined(newprefix)) { | ||
mlc._hashPrefix = newprefix; | ||
} | ||
return mlc._hashPrefix; | ||
} | ||
}; | ||
|
||
var mls; | ||
export const memoryLocationService: LocationServices = mls = { | ||
_listeners: [], | ||
_url: { | ||
path: '', | ||
search: {}, | ||
hash: '' | ||
}, | ||
_changed: (newval, oldval) => { | ||
if (newval === oldval) return; | ||
let evt = new Event("locationchange"); | ||
evt['url'] = newval; | ||
mls._listeners.forEach(cb => cb(evt)); | ||
}, | ||
|
||
url: () => { | ||
let s = mls._url.search; | ||
let hash = mls._url.hash; | ||
let query = Object.keys(s).map(key => (isArray(s[key]) ? s[key] : [s[key]]) .map(val => key + "=" + val)) | ||
.reduce(unnestR, []) | ||
.join("&"); | ||
|
||
return mls._url.path + | ||
(query ? "?" + query : "") + | ||
(hash ? "#" + hash : ""); | ||
}, | ||
hash: () => mls._url.hash, | ||
path: () => mls._url.path, | ||
search: () => mls._url.search, | ||
setUrl: (url: string, replace: boolean = false) => { | ||
if (isDefined(url)) { | ||
let path = splitHash(splitQuery(url)[0])[0]; | ||
let hash = splitHash(url)[1]; | ||
let search = getParams(splitQuery(splitHash(url)[0])[1]); | ||
|
||
let oldval = mls.url(); | ||
mls._url = { path, search, hash }; | ||
let newval = mls.url(); | ||
mls._changed(newval, oldval); | ||
} | ||
}, | ||
onChange: (cb: EventListener) => (mls._listeners.push(cb), () => removeFrom(mls._listeners, cb)) | ||
}; | ||
|
||
export const memoryLocationPlugin: (router: UIRouter) => LocationPlugin = | ||
locationPluginFactory("vanilla.memoryLocation", memoryLocationService, memoryLocationConfig); |
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { UIRouter } from "../src/router"; | ||
import { UIRouterPluginBase } from "../src/interface"; | ||
import * as vanilla from "../src/vanilla"; | ||
|
||
export class TestingPlugin extends UIRouterPluginBase { | ||
name: string = 'testing'; | ||
errorsCount: number = 0; | ||
errorsThreshold: number = 1000; | ||
|
||
constructor(public router: UIRouter) { | ||
super(); | ||
router.plugin(vanilla.servicesPlugin); | ||
router.plugin(vanilla.memoryLocationPlugin); | ||
|
||
this.addErrorLoopHandler(); | ||
|
||
this.startRouter(); | ||
} | ||
|
||
startRouter() { | ||
this.router.stateRegistry.stateQueue.autoFlush(this.router.stateService); | ||
this.router.urlRouter.listen(); | ||
} | ||
|
||
addErrorLoopHandler() { | ||
let $transitions = this.router.transitionService; | ||
$transitions.onCreate({}, trans => { | ||
trans.promise.catch(() => this.errorsCount++); | ||
if (this.errorsCount > this.errorsThreshold) { | ||
throw new Error(`Over ${this.errorsThreshold} failures; creation of new transitions disabled`); | ||
} | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.