-
Notifications
You must be signed in to change notification settings - Fork 3
removed scope from config; added pageClientStarted #1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,3 +193,5 @@ dist | |
| # End of https://www.toptal.com/developers/gitignore/api/node,linux,macos | ||
|
|
||
| dist | ||
|
|
||
| .idea | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,11 +1,36 @@ | ||||||
| import type React from "react"; | ||||||
| import { useEffect, useRef } from "react"; | ||||||
|
|
||||||
| import { fork } from "effector"; | ||||||
| import { Provider } from "effector-react"; | ||||||
| import { EffectorNext } from "@effector/next"; | ||||||
| import { createEvent } from "effector"; | ||||||
| import { useUnit } from "effector-react"; | ||||||
| import { usePageContext } from "vike-react/usePageContext"; | ||||||
|
|
||||||
| const noop = createEvent(); | ||||||
|
|
||||||
| const Inner = () => { | ||||||
| const { config } = usePageContext(); | ||||||
| const clientStartedRef = useRef(false); | ||||||
| const onClientStarted = useUnit(config.pageClientStarted ?? noop); | ||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (!clientStartedRef.current && "pageClientStarted" in config) { | ||||||
| onClientStarted(); | ||||||
| clientStartedRef.current = true; | ||||||
| } | ||||||
| }, []); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
It is required, when we can switch from page without .pageClientStarted to another with |
||||||
|
|
||||||
| return <></>; | ||||||
| }; | ||||||
|
|
||||||
| export default function WrapperEffector({ children }: { children: React.ReactNode }) { | ||||||
| const { scopeValues } = usePageContext(); | ||||||
| const pageContext = usePageContext(); | ||||||
| const { scopeValues } = pageContext; | ||||||
|
|
||||||
| return <Provider value={fork({ values: scopeValues })}>{children}</Provider>; | ||||||
| return ( | ||||||
| <EffectorNext values={scopeValues}> | ||||||
| <Inner /> | ||||||
| {children} | ||||||
| </EffectorNext> | ||||||
| ); | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { createEvent } from "effector"; | ||
|
|
||
| export const pageClientStarted = createEvent(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| import { fork } from "effector"; | ||
| import { allSettled, fork, serialize } from "effector"; | ||
|
|
||
| // https://vike.dev/onBeforeRenderClient | ||
| export function onBeforeRenderClient(pageContext: Vike.PageContext) { | ||
| export async function onBeforeRenderClient(pageContext: Vike.PageContext) { | ||
| // https://vike.dev/pageContext | ||
| if (!("scope" in pageContext)) { | ||
| return { | ||
| pageContext: { | ||
| // https://effector.dev/en/api/effector/fork/ | ||
| scope: fork({ values: pageContext.scopeValues }), | ||
| }, | ||
| }; | ||
|
|
||
| const scope = fork({ values: pageContext.scopeValues }); | ||
|
|
||
| const pageClientStarted = pageContext.config.pageClientStarted; | ||
|
|
||
| if (pageClientStarted && !pageContext.isHydration) { | ||
| await allSettled(pageClientStarted, { scope }); | ||
| pageContext.scopeValues = serialize(scope); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,15 +1,13 @@ | ||||||
| import type { EventCallable, Scope } from "effector"; | ||||||
| import type { EventCallable } from "effector"; | ||||||
|
|
||||||
| // https://vike.dev/pageContext#typescript | ||||||
| declare global { | ||||||
| namespace Vike { | ||||||
| interface PageContext { | ||||||
| config: { | ||||||
| pageStarted?: EventCallable<{ params: Record<string, string>; data: unknown }>; | ||||||
| pageClientStarted?: EventCallable<void>; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think to make universal interface with
Suggested change
|
||||||
| }; | ||||||
|
|
||||||
| // https://effector.dev/en/api/effector/scope/ | ||||||
| scope?: Scope; | ||||||
| scopeValues?: Record<string, unknown>; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why
@effector/nextis required for vike?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the same situation as in nextjs
we have to have 1 scope for an app and provide partial hydration of values