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

(WIP) Suspense #96

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/react-dom/.size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"gzipped": 961
},
"dist/curi-react-dom.umd.js": {
"bundled": 10609,
"minified": 4121,
"gzipped": 1741
"bundled": 11203,
"minified": 4257,
"gzipped": 1780
},
"dist/curi-react-dom.min.js": {
"bundled": 10579,
"minified": 4091,
"gzipped": 1725
"bundled": 11173,
"minified": 4227,
"gzipped": 1766
}
}
24 changes: 12 additions & 12 deletions packages/react-universal/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dist/curi-react-universal.es.js": {
"bundled": 5331,
"minified": 2647,
"gzipped": 1000,
"bundled": 5835,
"minified": 2810,
"gzipped": 1048,
"treeshaked": {
"rollup": {
"code": 110,
Expand All @@ -14,18 +14,18 @@
}
},
"dist/curi-react-universal.js": {
"bundled": 5748,
"minified": 3001,
"gzipped": 1111
"bundled": 6282,
"minified": 3192,
"gzipped": 1162
},
"dist/curi-react-universal.umd.js": {
"bundled": 6337,
"minified": 2670,
"gzipped": 1144
"bundled": 6901,
"minified": 2806,
"gzipped": 1190
},
"dist/curi-react-universal.min.js": {
"bundled": 6327,
"minified": 2660,
"gzipped": 1128
"bundled": 6891,
"minified": 2796,
"gzipped": 1174
}
}
15 changes: 13 additions & 2 deletions packages/react-universal/src/curiProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from "react";
import { Provider } from "./Context";

import { CuriRouter, Emitted } from "@curi/router";
import {
CuriRouter,
Emitted,
} from "@curi/router";

export interface RouterProps {
children: React.ReactNode;
suspend?: boolean;
}

export default function curiProvider(
Expand All @@ -27,7 +31,14 @@ export default function curiProvider(
const stopResponding = router.observe(
(emitted: Emitted) => {
if (!removed) {
setState(emitted);
if (props.suspend) {
// setTimeout until schedule is published
setTimeout(() => {
setState(emitted);
});
} else {
setState(emitted);
}
}
},
{ initial: false }
Expand Down
10 changes: 10 additions & 0 deletions packages/react-universal/src/hooks/useFinishNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";

import useCuri from "./useCuri";

export default function useFinishNavigation() {
const { navigation } = useCuri();
React.useEffect(() => {
navigation.finish();
}, [navigation]);
}
4 changes: 3 additions & 1 deletion packages/react-universal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useActive from "./hooks/useActive";
import useLocation from "./hooks/useLocation";
import useHref from "./hooks/useHref";
import useNavigating from "./hooks/useNavigating";
import useFinishNavigation from "./hooks/useFinishNavigation";
import {
useStatefulNavigationHandler,
useNavigationHandler
Expand All @@ -24,5 +25,6 @@ export {
useHref,
useNavigating,
useStatefulNavigationHandler,
useNavigationHandler
useNavigationHandler,
useFinishNavigation
};
2 changes: 0 additions & 2 deletions packages/react-universal/tests/curiProvider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ describe("curiProvider()", () => {
ReactDOM.unmountComponentAtNode(node);
});

describe("router argument", () => {});

describe("children prop", () => {
it("renders children", () => {
const history = InMemory();
Expand Down
96 changes: 96 additions & 0 deletions packages/react-universal/tests/useFinishNavigation.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import "jest";
import React from "react";
import ReactDOM from "react-dom";
import { act } from "react-dom/test-utils"
import { curi, prepareRoutes } from "@curi/router";
import { InMemory } from "@hickory/in-memory";

// @ts-ignore (resolved by jest)
import { curiProvider, useFinishNavigation, useCuri } from "@curi/react-universal";

describe("useFinishNavigation", () => {
let node;
const routes = prepareRoutes([
{ name: "Home", path: "" },
{ name: "About", path: "about" }
]);

beforeEach(() => {
node = document.createElement("div");
});

afterEach(() => {
ReactDOM.unmountComponentAtNode(node);
});

it('"finishes" navigation after mounting', () => {
const history = InMemory();
const router = curi(history, routes, { suspend: true });
const Router = curiProvider(router);

let navigations = [];
// intercept and mock navigation.finish()
router.observe(
({ navigation }) => {
navigation.finish = jest.fn();
navigations.push(navigation);
}
);

function App() {
const { navigation } = useCuri();
expect((navigation.finish as jest.Mock).mock.calls.length).toBe(0);
useFinishNavigation();
return null;
}

act(() => {
ReactDOM.render(
<Router>
<App />
</Router>,
node
);
});

expect(navigations[0].finish.mock.calls.length).toBe(1);
});

it('"finishes" navigation after updating', () => {
const history = InMemory();
const router = curi(history, routes, { suspend: true });
const Router = curiProvider(router);

let navigations = [];
// intercept and mock navigation.finish()
router.observe(
({ navigation }) => {
navigation.finish = jest.fn();
navigations.push(navigation);
}
);

function App() {
const { navigation } = useCuri();
expect((navigation.finish as jest.Mock).mock.calls.length).toBe(0);
useFinishNavigation();
return null;
}

act(() => {
ReactDOM.render(
<Router>
<App />
</Router>,
node
);
});
expect(navigations[0].finish.mock.calls.length).toBe(1);

act(() => {
router.navigate({ name: "About" });
});

expect(navigations[1].finish.mock.calls.length).toBe(1);
});
});
1 change: 1 addition & 0 deletions packages/react-universal/types/curiProvider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import React from "react";
import { CuriRouter } from "@curi/router";
export interface RouterProps {
children: React.ReactNode;
suspend?: boolean;
}
export default function curiProvider(router: CuriRouter): React.FunctionComponent<RouterProps>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function useFinishNavigation(): void;
3 changes: 2 additions & 1 deletion packages/react-universal/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import useActive from "./hooks/useActive";
import useLocation from "./hooks/useLocation";
import useHref from "./hooks/useHref";
import useNavigating from "./hooks/useNavigating";
import useFinishNavigation from "./hooks/useFinishNavigation";
import { useStatefulNavigationHandler, useNavigationHandler } from "./hooks/useNavigationHandler";
export { curiProvider, Curious, useCuri, useActive, useLocation, useHref, useNavigating, useStatefulNavigationHandler, useNavigationHandler };
export { curiProvider, Curious, useCuri, useActive, useLocation, useHref, useNavigating, useStatefulNavigationHandler, useNavigationHandler, useFinishNavigation };
24 changes: 12 additions & 12 deletions packages/router/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"dist/curi-router.es.js": {
"bundled": 18305,
"minified": 6196,
"gzipped": 2469,
"bundled": 19603,
"minified": 6550,
"gzipped": 2548,
"treeshaked": {
"rollup": {
"code": 23,
Expand All @@ -14,18 +14,18 @@
}
},
"dist/curi-router.js": {
"bundled": 18540,
"minified": 6386,
"gzipped": 2552
"bundled": 19838,
"minified": 6740,
"gzipped": 2630
},
"dist/curi-router.umd.js": {
"bundled": 30053,
"minified": 8979,
"gzipped": 3699
"bundled": 31415,
"minified": 9333,
"gzipped": 3784
},
"dist/curi-router.min.js": {
"bundled": 30013,
"minified": 8939,
"gzipped": 3681
"bundled": 31375,
"minified": 9293,
"gzipped": 3763
}
}
Loading