Skip to content

Commit 943c709

Browse files
authored
feat(routerify): Add routedComponent API (#218)
1 parent 992011e commit 943c709

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

README.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,13 @@ import {makeHistoryDriver} from '@cycle/history';
3434
import switchPath from 'switch-path';
3535

3636
function main(sources) {
37-
const match$ = sources.router.define({
37+
const pageSinks$ = sources.router.routedComponent({
3838
'/': HomeComponent,
3939
'/other': OtherComponent
40-
});
41-
42-
const page$ = match$.map(({path, value}) => {
43-
return value(Object.assign({}, sources, {
44-
router: sources.router.path(path) // notice use of 'router' source name,
45-
// which proxies raw 'history' source with
46-
// additional functionality
47-
}));
48-
});
40+
})(sources);
4941

5042
return {
51-
DOM: page$.map(c => c.DOM).flatten(),
43+
DOM: pageSinks$.map(c => c.DOM).flatten(),
5244
router: xs.of('/other') // Notice use of 'router' sink name,
5345
// which proxies the original 'history' sink
5446
};

src/RouterSource.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export class RouterSource {
2525
private _history$: any,
2626
private _namespace: Pathname[],
2727
private _createHref: (path: LocationDescriptorObject) => Pathname,
28-
private _routeMatcher: RouteMatcher
28+
private _routeMatcher: RouteMatcher,
29+
private _name: string
2930
) {}
3031

3132
history$ = adapt(this._history$);
@@ -45,19 +46,20 @@ export class RouterSource {
4546
scopedHistory$,
4647
scopedNamespace,
4748
createHref,
48-
this._routeMatcher
49+
this._routeMatcher,
50+
this._name
4951
);
5052
}
5153

52-
define(
54+
private _define(
5355
routes: RouteDefinitionsMap | RouteDefinitionsArray,
5456
routeMatcher?: RouteMatcher
5557
): any {
5658
const namespace = this._namespace;
5759
const _createHref = this._createHref;
5860
const createHref = util.makeCreateHref(namespace, _createHref);
5961

60-
let match$ = this._history$
62+
return this._history$
6163
.map((location: Location) => {
6264
const matcher = routeMatcher || this._routeMatcher;
6365
const filteredPath = getFilteredPath(
@@ -69,12 +71,37 @@ export class RouterSource {
6971
})
7072
.filter(({ path }: any) => path !== undefined && path !== null)
7173
.remember();
74+
}
75+
76+
define(
77+
routes: RouteDefinitionsMap | RouteDefinitionsArray,
78+
routeMatcher?: RouteMatcher
79+
): any {
80+
const _createHref = this._createHref;
81+
const createHref = util.makeCreateHref(this._namespace, _createHref);
7282

73-
const out$ = adapt(match$);
83+
const out$ = adapt(this._define(routes, routeMatcher));
7484
out$.createHref = createHref;
7585
return out$;
7686
}
7787

88+
routedComponent(
89+
routes: RouteDefinitionsMap | RouteDefinitionsArray,
90+
routeMatcher?: RouteMatcher
91+
): (sources: any) => any {
92+
const name = this._name;
93+
return sources => {
94+
const match$ = this._define(routes, routeMatcher);
95+
const page$ = match$.map(({ path, value }: any) => {
96+
return value({
97+
...sources,
98+
[name]: sources[name].path(path)
99+
});
100+
});
101+
return adapt(page$);
102+
};
103+
}
104+
78105
createHref(path: Pathname): Pathname {
79106
return util.makeCreateHref(this._namespace, this._createHref)(path);
80107
}

src/routerify.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ function routerify(
4747
xs.fromObservable(sources[opts.historyName]).remember(),
4848
[],
4949
createHref,
50-
routeMatcher
50+
routeMatcher,
51+
opts.routerName
5152
);
5253
let srcs = sources;
5354
if (opts.omitHistory) {

0 commit comments

Comments
 (0)