From f3a6135cf259ca92c7572f3fbcedc984ba150b08 Mon Sep 17 00:00:00 2001 From: weyheyhey Date: Thu, 21 May 2020 19:43:47 +0300 Subject: [PATCH] feat: transfer start-unit declaration to initialProps --- src/index.ts | 3 ++- src/lib/get-start-units.ts | 26 ++++++++++++-------------- src/lib/render-page-with-scope.tsx | 8 ++------ src/{types.ts => types/custom.ts} | 0 src/types/extracted.ts | 7 +++++++ src/types/index.ts | 2 ++ src/with-fork.tsx | 6 +++--- src/with-start.ts | 4 +--- 8 files changed, 29 insertions(+), 27 deletions(-) rename src/{types.ts => types/custom.ts} (100%) create mode 100644 src/types/extracted.ts create mode 100644 src/types/index.ts diff --git a/src/index.ts b/src/index.ts index 7673422..3e010d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,8 @@ const { createDomain, createStore, createEffect, createEvent } = domain; export * from "effector"; export { domain, createDomain, createStore, createEffect, createEvent }; +export * from "./lib/constants"; +export * from "./types/custom"; export * from "./with-hydrate"; export * from "./with-start"; export * from "./with-fork"; -export * from "./types"; diff --git a/src/lib/get-start-units.ts b/src/lib/get-start-units.ts index 3dc8d06..5097f16 100644 --- a/src/lib/get-start-units.ts +++ b/src/lib/get-start-units.ts @@ -1,30 +1,28 @@ -import { DocumentContext } from "next/document"; -import { NextComponentType } from "next"; -import { Unit } from "effector"; +import { AppInitialProps } from "next/app"; +import { Unit, is } from "effector"; -import { PageContext } from "../types"; +import { AppType, Enhancer, RenderPage, PageContext } from "../types"; import { START_UNIT_KEY } from "./constants"; -/* eslint-disable @typescript-eslint/no-explicit-any */ - type StartUnits = Array>; -type RenderPage = DocumentContext["renderPage"]; -type Enhancer> = (Component: C) => C; export function getStartUnits(originalRenderPage: RenderPage) { const units: StartUnits = []; originalRenderPage({ enhanceApp: getStartUnit(units) }); - originalRenderPage({ enhanceComponent: getStartUnit(units) }); - return units; + return units.filter(is.unit); } -function getStartUnit(units: StartUnits): Enhancer { - return (Component) => () => { - if (START_UNIT_KEY in Component) { - units.push(Component[START_UNIT_KEY]); +function getStartUnit

(units: StartUnits): Enhancer> { + return () => (props) => { + if (START_UNIT_KEY in props) { + units.push(props[START_UNIT_KEY]); + } + + if (START_UNIT_KEY in props.pageProps) { + units.push(props.pageProps[START_UNIT_KEY]); } return null; diff --git a/src/lib/render-page-with-scope.tsx b/src/lib/render-page-with-scope.tsx index 6799ca1..83a975b 100644 --- a/src/lib/render-page-with-scope.tsx +++ b/src/lib/render-page-with-scope.tsx @@ -1,13 +1,9 @@ import * as React from "react"; -import { AppContext, AppInitialProps } from "next/app"; -import { DocumentContext } from "next/document"; import { Provider } from "effector-react/ssr"; -import { NextComponentType } from "next"; +import { AppInitialProps } from "next/app"; import { Scope } from "effector/fork"; -type Enhancer = (Component: C) => C; -type RenderPage = DocumentContext["renderPage"]; -type AppType

= NextComponentType; +import { AppType, Enhancer, RenderPage } from "../types"; export function renderPageWithScope(scope: Scope, originalRenderPage: RenderPage): RenderPage { return (params) => { diff --git a/src/types.ts b/src/types/custom.ts similarity index 100% rename from src/types.ts rename to src/types/custom.ts diff --git a/src/types/extracted.ts b/src/types/extracted.ts new file mode 100644 index 0000000..fea9523 --- /dev/null +++ b/src/types/extracted.ts @@ -0,0 +1,7 @@ +import { AppContext, AppInitialProps } from "next/app"; +import { DocumentContext } from "next/document"; +import { NextComponentType } from "next"; + +export type Enhancer = (Component: C) => C; +export type RenderPage = DocumentContext["renderPage"]; +export type AppType

= NextComponentType; diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..3e20fb8 --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,2 @@ +export * from "./extracted"; +export * from "./custom"; diff --git a/src/with-fork.tsx b/src/with-fork.tsx index b2323d9..d90e541 100644 --- a/src/with-fork.tsx +++ b/src/with-fork.tsx @@ -14,7 +14,7 @@ type InitialStateKey = typeof INITIAL_STATE_KEY; type InitialState = ReturnType; type ExtendedNextData = NextData & { [key in InitialStateKey]: InitialState }; -interface WrappedDocumentProps extends DocumentProps { +interface CustomDocumentProps extends DocumentProps { initialState: InitialState; __NEXT_DATA__: ExtendedNextData; } @@ -25,7 +25,7 @@ export interface WithForkConfig { export function withFork({ debug }: WithForkConfig = {}) { return (Document: typeof NextDocument) => - class WithForkDocument extends React.Component { + class WithForkDocument extends React.Component { static renderDocument = Document.renderDocument; static headTagsMiddleware = Document.headTagsMiddleware; static bodyTagsMiddleware = Document.bodyTagsMiddleware; @@ -80,7 +80,7 @@ export function withFork({ debug }: WithForkConfig = {}) { }; } - constructor(props: WrappedDocumentProps) { + constructor(props: CustomDocumentProps) { super(props); props.__NEXT_DATA__[INITIAL_STATE_KEY] = props.initialState; diff --git a/src/with-start.ts b/src/with-start.ts index 1aa1212..6eacb4c 100644 --- a/src/with-start.ts +++ b/src/with-start.ts @@ -21,11 +21,9 @@ export function withStart(unit: Unit) { initialProps = await originalGetInitialProps(ctx); } - return initialProps; + return Object.assign({}, initialProps, { [START_UNIT_KEY]: unit }); }; - component[START_UNIT_KEY] = unit; - return component; }; }