-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optimize react scene use context code
- Loading branch information
1 parent
64382dc
commit 1d3e885
Showing
24 changed files
with
147 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,11 @@ | ||
declare module '_build/create-context' { | ||
import type { Context, Dispatch } from 'react' | ||
interface IContext<T = any> { | ||
state?: T | ||
dispatch?: Dispatch<Action> | ||
} | ||
interface Action { | ||
type: string | ||
payload: object | ||
} | ||
const STORE_CONTEXT: Context<IContext> | ||
} | ||
|
||
declare module 'ssr-deepclone' { | ||
const deepClone: (obj: any) => any | ||
} | ||
|
||
declare module '_build/ssr-declare-routes' { } | ||
declare module '_build/ssr-manual-routes' { } | ||
declare module '_build/staticConfig' {} | ||
declare module '_build/staticConfig' { } | ||
declare module 'koa2-connect' { | ||
export default (params: any): any => { } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createContext } from 'react' | ||
import type { Context } from 'react' | ||
import type { IContext } from 'ssr-types' | ||
import { proxy } from 'valtio' | ||
import { deepClone } from 'ssr-deepclone' | ||
import { combineRoutes } from 'ssr-common-utils' | ||
import * as declareRoutes from '_build/ssr-declare-routes' | ||
import * as ManualRoutes from '_build/ssr-manual-routes' | ||
import { ReactRoutesType } from 'ssr-types' | ||
|
||
export const Routes = combineRoutes(declareRoutes, ManualRoutes) as ReactRoutesType | ||
|
||
export const ssrCreateContext = () => { | ||
let STORE_CONTEXT: Context<IContext> | ||
|
||
if (__isBrowser__) { | ||
STORE_CONTEXT = window.STORE_CONTEXT || createContext<IContext>({ | ||
state: {} | ||
}) | ||
window.STORE_CONTEXT = STORE_CONTEXT | ||
return STORE_CONTEXT | ||
} else { | ||
STORE_CONTEXT = createContext<IContext>({ | ||
state: {} | ||
}) | ||
} | ||
return STORE_CONTEXT | ||
} | ||
|
||
export function createStore (initialData?: any) { | ||
const { store } = Routes | ||
const storeInstance = initialData ? store : deepClone(store) | ||
for (const key in storeInstance) { | ||
storeInstance[key] = initialData ? proxy(initialData[key]) : proxy(storeInstance[key]) | ||
} | ||
return storeInstance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.