Skip to content

Commit 5339878

Browse files
committed
refactor(update): Update to Cycle Unified
1 parent 3da6083 commit 5339878

File tree

10 files changed

+123
-350
lines changed

10 files changed

+123
-350
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# cyclic-router
22
cyclic-router is a Router Driver built for Cycle.js
33

4-
**Disclaimer** v2.x.x and v3 are for Cycle Diversity!
4+
**Disclaimer** Use v4 for Cycle Unified. v2.x.x is for Cycle and v3 is for Cycle Diversity.
55
If you are still using @cycle/core please continue to use v1.x.x
66

77
## Installation
@@ -10,7 +10,7 @@ Using [npm](https://www.npmjs.com/):
1010

1111
$ npm install --save cyclic-router
1212

13-
Version 3 requires users to inject the route matcher. We'll use `switch-path` for our examples but other
13+
Versions 3 and 4 requires users to inject the route matcher. We'll use `switch-path` for our examples but other
1414
matching libraries could be adapted to be used here:
1515

1616
$ npm install --save switch-path

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
},
1919
"homepage": "https://github.com/TylorS/cyclic-router#readme",
2020
"dependencies": {
21-
"@cycle/history": "^4.0.0",
22-
"history": "^3.0.0"
21+
"@cycle/history": "^6.1.0",
22+
"@types/history": "^4.5.0",
23+
"history": "^4.5.0",
24+
"@cycle/run": "^3.0.0"
2325
},
2426
"devDependencies": {
25-
"@cycle/base": "^4.0.0",
26-
"@cycle/rx-adapter": "^3.0.0",
27-
"@cycle/rxjs-adapter": "^3.0.0",
28-
"@cycle/xstream-adapter": "^3.0.1",
27+
"@cycle/rxjs-run": "^6.1.0",
2928
"assert": "^1.4.1",
3029
"babel-preset-es2015": "^6.16.0",
3130
"babel-register": "^6.18.0",
@@ -35,15 +34,14 @@
3534
"ghooks": "^1.2.1",
3635
"mkdirp": "^0.5.1",
3736
"mocha": "^2.5.3",
38-
"rx": "^4.1.0",
39-
"rxjs": "^5.0.0-beta.8",
37+
"rxjs": "^5.2.0",
4038
"testem": "^1.10.4",
4139
"tslint": "^3.10.2",
4240
"typescript": "^2.0.7",
4341
"typings": "^0.8.1",
4442
"uglify-js": "^2.7.1",
4543
"validate-commit-message": "^3.0.1",
46-
"xstream": "^7.0.0",
44+
"xstream": "^10.3.0",
4745
"switch-path": "^1.1.8"
4846
},
4947
"config": {

src/RouterSource.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import {StreamAdapter} from '@cycle/base';
2-
import {Location, Pathname} from '@cycle/history/lib/interfaces';
1+
import {Location, Pathname} from '@cycle/history';
2+
import {LocationDescriptorObject} from 'history';
33
import {RouteDefinitionsMap, RouteDefinitionsArray, RouteMatcher} from './interfaces';
44
import * as util from './util';
5+
import {adapt} from '@cycle/run/lib/adapt';
56

67
function isStrictlyInScope(namespace: Pathname[], path: Pathname): boolean {
78
const pathParts = util.splitPath(path);
@@ -16,36 +17,40 @@ function getFilteredPath(namespace: Pathname[], path: Pathname): Pathname {
1617
}
1718

1819
export class RouterSource {
19-
constructor(public history$: any,
20+
constructor(private _history$: any,
2021
private _namespace: Pathname[],
21-
private _createHref: (path: Pathname) => Pathname,
22-
private _runSA: StreamAdapter,
22+
private _createHref: (path: LocationDescriptorObject) => Pathname,
2323
private _routeMatcher: RouteMatcher) {}
2424

25+
history$ = adapt(this._history$);
26+
2527
path(pathname: Pathname): RouterSource {
2628
const scopedNamespace = this._namespace.concat(util.splitPath(pathname));
27-
const scopedHistory$ = this._runSA.remember(this.history$
28-
.filter(({pathname: _path}: Location) => isStrictlyInScope(scopedNamespace, _path)));
29+
const scopedHistory$ = this._history$
30+
.filter(({pathname: _path}: Location) => isStrictlyInScope(scopedNamespace, _path))
31+
.remember();
2932

3033
const createHref = this._createHref;
31-
return new RouterSource(scopedHistory$, scopedNamespace, createHref, this._runSA, this._routeMatcher);
34+
return new RouterSource(scopedHistory$, scopedNamespace, createHref, this._routeMatcher);
3235
}
3336

3437
define(routes: RouteDefinitionsMap | RouteDefinitionsArray, routeMatcher?: RouteMatcher): any {
3538
const namespace = this._namespace;
3639
const _createHref = this._createHref;
3740
const createHref = util.makeCreateHref(namespace, _createHref);
3841

39-
let match$ = this._runSA.remember(this.history$
42+
let match$ = this._history$
4043
.map((location: Location) => {
4144
const matcher = routeMatcher || this._routeMatcher;
4245
const filteredPath = getFilteredPath(namespace, location.pathname);
4346
const {path, value} = matcher(filteredPath, routes);
4447
return {path, value, location, createHref};
45-
}));
48+
})
49+
.remember();
4650

47-
match$.createHref = createHref;
48-
return match$;
51+
const out$ = adapt(match$);
52+
out$.createHref = createHref;
53+
return out$;
4954
}
5055

5156
createHref(path: Pathname): Pathname {

src/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
export {makeRouterDriver} from './makeRouterDriver';
2-
export {
3-
supportsHistory,
4-
createLocation,
5-
createServerHistory,
6-
} from '@cycle/history'

src/makeRouterDriver.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import {StreamAdapter} from '@cycle/base';
21
import {makeHistoryDriver} from '@cycle/history';
3-
import {History, HistoryDriverOptions} from '@cycle/history/lib/interfaces';
42
import {RouteMatcher} from './interfaces';
53
import {RouterSource} from './RouterSource';
4+
import {History} from '@types/history';
65

76
/**
87
* Instantiates an new router driver function using the same arguments required
@@ -11,8 +10,11 @@ import {RouterSource} from './RouterSource';
1110
* @method makeRouterDriver
1211
* @return {routerDriver} The router driver function
1312
*/
14-
function makeRouterDriver(history: History, routeMatcher: RouteMatcher, options?: HistoryDriverOptions) {
15-
const historyDriver = makeHistoryDriver(history, options);
13+
function makeRouterDriver(history: History, routeMatcher: RouteMatcher) {
14+
if (!history) {
15+
throw new Error('Cyclic router must be given a history object');
16+
}
17+
const historyDriver = makeHistoryDriver(history);
1618
/**
1719
* The actual router driver.
1820
* @public
@@ -23,9 +25,9 @@ function makeRouterDriver(history: History, routeMatcher: RouteMatcher, options?
2325
* history driver would expect.
2426
* @return {routerAPI}
2527
*/
26-
return function routerDriver(sink$: any, runSA: StreamAdapter) {
27-
const history$ = runSA.remember(historyDriver(sink$, runSA));
28-
return new RouterSource(history$, [], history.createHref, runSA, routeMatcher);
28+
return function routerDriver(sink$: any) {
29+
const history$ = historyDriver(sink$).remember();
30+
return new RouterSource(history$, [], history.createHref, routeMatcher);
2931
};
3032
}
3133

src/util.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {Pathname} from '@cycle/history/lib/interfaces';
1+
import {Pathname} from '@cycle/history';
2+
import {LocationDescriptorObject} from 'history';
23

34
function splitPath(path: Pathname): Pathname[] {
45
return path.split('/').filter(p => p.length > 0);
@@ -13,7 +14,7 @@ const startsWith = (param: string, value: string) => param[0] === value;
1314
const startsWith2 = (param: string, value1: string, value2: string) =>
1415
param[0] === value1 && param[1] === value2;
1516

16-
function makeCreateHref(namespace: Pathname[], _createHref: (pathname: Pathname) => Pathname) {
17+
function makeCreateHref(namespace: Pathname[], _createHref: (pathname: LocationDescriptorObject) => Pathname) {
1718
/**
1819
* Function used to create HREFs that are properly namespaced
1920
* @typedef {createHref}
@@ -24,11 +25,28 @@ function makeCreateHref(namespace: Pathname[], _createHref: (pathname: Pathname)
2425
* @return {string} a fully qualified HREF composed from the current
2526
* namespace and the path provided
2627
*/
27-
return function createHref(path: Pathname): Pathname {
28-
const fullPath = `${namespace.join('/')}${path}`;
29-
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
30-
? _createHref(fullPath)
31-
: _createHref('/' + fullPath);
28+
return function createHref(location: Pathname | LocationDescriptorObject): Pathname {
29+
if (typeof location === 'object' && location !== null) {
30+
const fullPath = `${namespace.join('/')}${location.pathname}`;
31+
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
32+
? _createHref({
33+
...location,
34+
pathname: fullPath,
35+
})
36+
: _createHref({
37+
...location,
38+
pathname: '/' + fullPath
39+
});
40+
} else if (typeof location === 'string') {
41+
const fullPath = `${namespace.join('/')}${location}`;
42+
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
43+
? _createHref({
44+
pathname: fullPath
45+
})
46+
: _createHref({
47+
pathname: '/' + fullPath
48+
});
49+
}
3250
};
3351
}
3452

test/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
import './rx';
21
import './rxjs';
32
import './xstream';

0 commit comments

Comments
 (0)