Skip to content

Commit 2a5b3eb

Browse files
committed
Change router to main wrapper
1 parent 542b096 commit 2a5b3eb

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export * from './makeRouterDriver';
1+
export * from './routerify';
22
export {RouterSource} from './RouterSource';
33
export * from './interfaces';

src/makeRouterDriver.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/routerify.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import xs from 'xstream';
2+
import {adapt} from '@cycle/run/lib/adapt';
3+
import {RouteMatcher} from './interfaces';
4+
import {RouterSource} from './RouterSource';
5+
import {Location, createPath} from 'history';
6+
import {HistoryInput, GenericInput} from '@cycle/history';
7+
import {Stream} from 'xstream';
8+
9+
export declare type HistoryAction = HistoryInput | GenericInput | string;
10+
export declare type RouterSink = Stream<HistoryAction>;
11+
12+
/**
13+
* Wraps main to provide an advanced interface over @cycle/history
14+
* @public
15+
* @method routerify
16+
* @return {main} The augmented main function
17+
*/
18+
function routerify(main: (a: any) => any, routeMatcher: RouteMatcher, basename = '/', historyName = 'history', routerName = 'router') {
19+
const createHref = (location: Location) => basename + createPath(location);
20+
return function(sources: any): any {
21+
const routerSource = new RouterSource(sources[historyName], [], createHref, routeMatcher);
22+
const sinks = main({ ...sources, [routerName]: routerSource });
23+
return {
24+
...sinks,
25+
[historyName]: adapt(xs.merge(
26+
xs.fromObservable(sinks[historyName]),
27+
xs.fromObservable(sinks[routerName])
28+
))
29+
};
30+
};
31+
}
32+
33+
export {routerify}

0 commit comments

Comments
 (0)