Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,33 @@
},
"homepage": "https://github.com/TylorS/cyclic-router#readme",
"dependencies": {
"@cycle/history": "^3.0.4",
"history": "^2.1.0",
"switch-path": "^1.1.6"
"@cycle/history": "^4.0.0",
"history": "^3.0.0",
"switch-path": "^1.1.7"
},
"devDependencies": {
"@cycle/base": "^2.0.1",
"@cycle/rx-adapter": "^1.0.3",
"@cycle/rxjs-adapter": "^1.0.5",
"@cycle/xstream-adapter": "^1.0.3",
"@cycle/base": "^4.0.0",
"@cycle/rx-adapter": "^3.0.0",
"@cycle/rxjs-adapter": "^3.0.0",
"@cycle/xstream-adapter": "^3.0.1",
"assert": "^1.4.1",
"babel-preset-es2015": "^6.6.0",
"babel-register": "^6.7.2",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"babel-preset-es2015": "^6.9.0",
"babel-register": "^6.9.0",
"babelify": "^7.3.0",
"browserify": "^13.0.1",
"cli-release": "^1.0.4",
"ghooks": "^1.2.1",
"mkdirp": "^0.5.1",
"mocha": "^2.4.5",
"mocha": "^2.5.3",
"rx": "^4.1.0",
"rxjs": "^5.0.0-beta.7",
"rxjs": "^5.0.0-beta.8",
"testem": "^1.6.0",
"ts-node": "^0.7.2",
"tslint": "^3.10.2",
"typescript": "^1.8.10",
"typings": "^0.8.1",
"uglify-js": "^2.6.2",
"validate-commit-message": "^3.0.1",
"xstream": "^3.0.0"
"xstream": "^5.0.5"
},
"config": {
"ghooks": {
Expand Down
20 changes: 12 additions & 8 deletions src/RouterSource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {StreamAdapter} from '@cycle/base';
import {Location, Pathname} from '@cycle/history/lib/interfaces';
import switchPath, {RouteDefinitions} from 'switch-path';

Expand All @@ -18,30 +19,33 @@ function getFilteredPath(namespace: Pathname[], path: Pathname): Pathname {
export class RouterSource {
constructor(private _history$: any,
private _namespace: Pathname[],
private _createHref: (path: Pathname) => Pathname) {}
private _createHref: (path: Pathname) => Pathname,
private _runSA: StreamAdapter) {}

get history$() {
return this._history$;
return this._runSA.remember(this._history$);
}

path(pathname: Pathname): RouterSource {
const scopedNamespace = this._namespace.concat(util.splitPath(pathname));
const scopedHistory$ = this._history$
.filter(({pathname: _path}) => isStrictlyInScope(scopedNamespace, _path));
const scopedHistory$ = this._runSA.remember(this._history$
.filter(({pathname: _path}: Location) => isStrictlyInScope(scopedNamespace, _path)));

return new RouterSource(scopedHistory$, scopedNamespace, this._createHref);
const createHref = this._createHref;
return new RouterSource(scopedHistory$, scopedNamespace, createHref, this._runSA);
}

define(routes: RouteDefinitions): any {
const namespace = this._namespace;
const createHref = util.makeCreateHref(namespace, this._createHref);
const _createHref = this._createHref;
const createHref = util.makeCreateHref(namespace, _createHref);

let match$ = this._history$
let match$ = this._runSA.remember(this._history$
.map((location: Location) => {
const filteredPath = getFilteredPath(namespace, location.pathname);
const {path, value} = switchPath(filteredPath, routes);
return {path, value, location, createHref};
});
}));

match$.createHref = createHref;
return match$;
Expand Down
4 changes: 2 additions & 2 deletions src/makeRouterDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function makeRouterDriver(history: History, options?: HistoryDriverOptions) {
* @return {routerAPI}
*/
return function routerDriver(sink$: any, runSA: StreamAdapter) {
const history$ = historyDriver(sink$, runSA);
return new RouterSource(history$, [], history$.createHref);
const history$ = runSA.remember(historyDriver(sink$, runSA));
return new RouterSource(history$, [], history.createHref, runSA);
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function makeCreateHref(namespace: Pathname[], _createHref: (pathname: Pathname)
*/
return function createHref(path: Pathname): Pathname {
const fullPath = `${namespace.join('/')}${path}`;
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/') ?
_createHref(fullPath) :
_createHref('/' + fullPath);
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
? _createHref(fullPath)
: _createHref('/' + fullPath);
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import './rx';
import './rxjs';
import './xstream';
import './xstream';
48 changes: 27 additions & 21 deletions test/rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Cyclic Router - Rx 4', () => {
it('should return an object with `path` `define` `observable` ' +
'`createHref` and `dispose`',
() => {
const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.of('/'), RxAdapter)
assert.notStrictEqual(router.path, null)
assert.strictEqual(typeof router.path, 'function')
Expand All @@ -37,7 +37,7 @@ describe('Cyclic Router - Rx 4', () => {
it('should return an object with `path` `define` `observable` ' +
'`createHref` and `dispose`',
() => {
const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.of('/'), RxAdapter)
.path('/')
assert.notStrictEqual(router.path, null)
Expand All @@ -56,7 +56,7 @@ describe('Cyclic Router - Rx 4', () => {
'/somewhere/else',
'/path/that/is/correct',
]
const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
.path('/path')

Expand All @@ -73,7 +73,7 @@ describe('Cyclic Router - Rx 4', () => {
'/some/really/really/deeply/nested/incorrect/route',
]

const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
.path('/some').path('/really').path('/really').path('/deeply')
.path('/nested').path('/route').path('/that')
Expand All @@ -91,7 +91,7 @@ describe('Cyclic Router - Rx 4', () => {
'/some/really/really/deeply/nested/incorrect/route',
]

const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
.path('/some').path('/really').path('/really').path('/deeply')
.path('/nested').path('/route').path('/that')
Expand All @@ -110,7 +110,7 @@ describe('Cyclic Router - Rx 4', () => {
it('should return an object with `path$` `value$` `fullPath$` ' +
'`createHref` and `dispose`',
() => {
const history = createServerHistory()
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.of('/'), RxAdapter)
.define({})
assert.strictEqual(router instanceof Observable, true)
Expand All @@ -126,20 +126,18 @@ describe('Cyclic Router - Rx 4', () => {
},
}

const routes = [
'/some/route',
]

const history = createServerHistory()
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.never(), RxAdapter)
const match$ = router.define(defintion)

match$.subscribe(({path, value, location}) => {
match$.skip(1).subscribe(({path, value, location}) => {
assert.strictEqual(path, '/some/route')
assert.strictEqual(value, 123)
assert.strictEqual(location.pathname, '/some/route')
done()
})

history.push('/some/route')
})

it('should respect prior filtering by path()', done => {
Expand All @@ -154,8 +152,8 @@ describe('Cyclic Router - Rx 4', () => {
'/some/nested/correct/route',
]

const history = createServerHistory()
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.never(), RxAdapter)
const match$ = router.path('/some').path('/nested').define(defintion)

match$.subscribe(({path, value, location}) => {
Expand All @@ -164,6 +162,8 @@ describe('Cyclic Router - Rx 4', () => {
assert.strictEqual(location.pathname, '/some/nested/correct/route')
done()
})

routes.forEach(r => history.push(r))
})

it('should match a default route if one is not found', done => {
Expand All @@ -180,8 +180,8 @@ describe('Cyclic Router - Rx 4', () => {
'/some/nested/incorrect/route',
]

const history = createServerHistory()
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.never(), RxAdapter)
const match$ = router.path('/some').path('/nested').define(definition)

match$.subscribe(({path, value, location}) => {
Expand All @@ -190,6 +190,8 @@ describe('Cyclic Router - Rx 4', () => {
assert.strictEqual(location.pathname, '/some/nested/incorrect/route')
done()
})

routes.forEach(r => history.push(r))
})

it('should create a proper href using createHref()', done => {
Expand All @@ -205,8 +207,8 @@ describe('Cyclic Router - Rx 4', () => {
'/some/nested/correct/route',
]

const history = createServerHistory()
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.never(), RxAdapter)
const match$ = router
.path('/some').path('/nested').define(defintion)

Expand All @@ -215,6 +217,8 @@ describe('Cyclic Router - Rx 4', () => {
assert.strictEqual(createHref('/correct/route'), pathname)
done()
})

routes.forEach(r => history.push(r))
})

it('should match partials', done => {
Expand All @@ -230,8 +234,8 @@ describe('Cyclic Router - Rx 4', () => {
'/some/nested/correct/route/partial',
]

const history = createServerHistory()
const router = makeRouterDriver(history)(Observable.from(routes), RxAdapter)
const history = createServerHistory('/')
const router = makeRouterDriver(history)(Observable.never(), RxAdapter)
const match$ = router
.path('/some').path('/nested').define(defintion)

Expand All @@ -240,6 +244,8 @@ describe('Cyclic Router - Rx 4', () => {
assert.strictEqual(pathname, '/some/nested/correct/route/partial')
done()
})

routes.forEach(r => history.push(r))
})
})
})
Loading