Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create runner #65

Merged
merged 1 commit into from
Sep 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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