Skip to content

Commit

Permalink
Merge pull request #65 from oneut/2.0-dev-create_runner
Browse files Browse the repository at this point in the history
Create runner
  • Loading branch information
oneut committed Sep 13, 2018
2 parents cfcab2a + e702918 commit 92cd8c4
Show file tree
Hide file tree
Showing 11 changed files with 1,130 additions and 2,027 deletions.
2,429 changes: 636 additions & 1,793 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions src/lib/Connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ export default class Connector {
}

init(history) {
// Settings HistoryManager.
this.initHistory(history);

// Settings RxJS Stream.
this.initRouteMatcher();

// Initial RouteMatcher
this.initStream();

return this;
}

Expand Down
38 changes: 24 additions & 14 deletions src/lib/Router.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from "react";
import RootComponent from "./RootComponent";
import InitialPropsRunner from "./runner/InitialPropsRunner";
import FirstComponentLoadRunner from "./runner/FirstComponentLoadRunner";
import LoadRunner from "./runner/LoadRunner";

export default class Router {
constructor(connector) {
this.connector = connector;
this.initialProps = null;
this.firstComponent = null;
}

route(path, component, name) {
Expand All @@ -15,20 +18,27 @@ export default class Router {
}

setFirstComponent(component) {
this.connector.componentResolver.setComponent(component);
this.firstComponent = component;
}

setInitialProps(initialProps) {
this.initialProps = initialProps;
}

run(callback) {
// Request current route component.
const location = this.connector.historyManager.getLocation();
this.connector.request(location.pathname);

// Create root component.
callback((props) => {
return React.createElement(RootComponent, {
connector: this.connector,
...props
});
});
const runner = this.getRunner();
runner.run(callback);
}

getRunner() {
if (!!this.initialProps) {
return new InitialPropsRunner(this.connector, this.initialProps);
}

if (!!this.firstComponent) {
return new FirstComponentLoadRunner(this.connector, this.firstComponent);
}

return new LoadRunner(this.connector);
}
}
23 changes: 23 additions & 0 deletions src/lib/runner/FirstComponentLoadRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from "react";
import RootComponent from "../RootComponent";

export default class FirstComponentLoadRunner {
constructor(connector, firstComponent) {
this.connector = connector;
this.firstComponent = firstComponent;
}

run(callback) {
const location = this.connector.historyManager.getLocation();
this.connector.request(location.pathname);

this.connector.componentResolver.setComponent(this.firstComponent);

callback((props) => {
return React.createElement(RootComponent, {
connector: this.connector,
...props
});
});
}
}
22 changes: 5 additions & 17 deletions src/ssr/Router.js → src/lib/runner/InitialPropsRunner.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import React from "react";
import RootComponent from "../lib/RootComponent";
import { map, skipWhile, switchMap } from "rxjs/operators";
import { of } from "rxjs";
import { map, skipWhile, switchMap } from "rxjs/operators";
import React from "react";
import RootComponent from "../RootComponent";

export default class Router {
constructor(connector) {
export default class InitialPropsRunner {
constructor(connector, initialProps) {
this.connector = connector;
this.initialProps = {};
}

route(path, component, name) {
this.connector.routeMatcher.addRoute(path, component, name, false);
}

asyncRoute(path, component, name) {
this.connector.routeMatcher.addRoute(path, component, name, true);
}

setInitialProps(initialProps) {
this.initialProps = initialProps;
}

Expand Down
34 changes: 34 additions & 0 deletions src/lib/runner/LoadRunner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { of } from "rxjs";
import { map, mergeMap, skipWhile, switchMap } from "rxjs/operators";
import React from "react";
import RootComponent from "../RootComponent";

export default class LoadRunner {
constructor(connector) {
this.connector = connector;
}

run(callback) {
const location = this.connector.historyManager.getLocation();

of(location.pathname)
.pipe(
switchMap((pathname) =>
this.connector.routeMatcher.createRenderer(pathname)
),
skipWhile((renderer) => renderer === null),
map((renderer) => renderer.fireInitialPropsWillGet()),
mergeMap((renderer) => renderer.fireGetInitialProps()),
map((renderer) => renderer.fireInitialPropsDidGet())
)
.subscribe((renderer) => {
this.connector.componentResolver.setComponentFromRenderer(renderer);
callback((props) => {
return React.createElement(RootComponent, {
connector: this.connector,
...props
});
});
});
}
}
2 changes: 1 addition & 1 deletion src/ssr/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ServerRouter from "./ServerRouter";
import Router from "./Router";
import Router from "../lib/Router";
import { connector } from "../lib/Facade";
import { createBrowserHistory, createMemoryHistory } from "history";

Expand Down

0 comments on commit 92cd8c4

Please sign in to comment.