forked from gronxb/webview-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support ssr frameworks, createLinkBridgeProvider (gronxb#33)
* fix: modify package dependency paths * fix: use-sync-external-store shim index.js module * fix(vue): exports field * refactor: use-sync-external-store-with-selector * fix: tsconfig module resolution node * fix: lint * fix: exports cts * feat: createLinkBridgeProvider * chore: lock
- Loading branch information
Showing
15 changed files
with
135 additions
and
63 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { | ||
Bridge, | ||
BridgeStore, | ||
LinkBridgeOptions, | ||
} from "@webview-bridge/web"; | ||
import { linkBridge } from "@webview-bridge/web"; | ||
import { createContext, type ReactNode, useContext, useRef } from "react"; | ||
|
||
import { useBridge } from "./useBridge"; | ||
|
||
export interface BridgeProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
export const createLinkBridgeProvider = < | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
T extends BridgeStore<T extends Bridge ? T : any>, | ||
>( | ||
options?: LinkBridgeOptions<T>, | ||
) => { | ||
const bridge = linkBridge<T>(options); | ||
const BridgeContext = createContext<BridgeStore | null>(null); | ||
|
||
type BridgeStore = typeof bridge; | ||
type BridgeSelector = ReturnType<typeof bridge.store.getState>; | ||
|
||
const BridgeProvider = ({ children }: BridgeProviderProps) => { | ||
const storeRef = useRef<BridgeStore>(); | ||
if (!storeRef.current) { | ||
storeRef.current = bridge; | ||
} | ||
|
||
return ( | ||
<BridgeContext.Provider value={storeRef.current}> | ||
{children} | ||
</BridgeContext.Provider> | ||
); | ||
}; | ||
|
||
const useBridgeStore = <U,>(selector: (state: BridgeSelector) => U): U => { | ||
const bridgeStoreContext = useContext(BridgeContext); | ||
|
||
if (!bridgeStoreContext) { | ||
throw new Error(`useBridgeStore must be used within a BridgeProvider`); | ||
} | ||
|
||
return useBridge(bridgeStoreContext.store, selector as (state: T) => U); | ||
}; | ||
|
||
const useBridgeStatus = () => { | ||
const bridgeStoreContext = useContext(BridgeContext); | ||
|
||
if (!bridgeStoreContext) { | ||
throw new Error(`useBridgeStatus must be used within a BridgeProvider`); | ||
} | ||
|
||
const { isNativeMethodAvailable, isWebViewBridgeAvailable, loose } = | ||
bridgeStoreContext; | ||
return { | ||
isNativeMethodAvailable, | ||
isWebViewBridgeAvailable, | ||
loose, | ||
}; | ||
}; | ||
|
||
return { bridge, BridgeProvider, useBridgeStore, useBridgeStatus }; | ||
}; |
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 +1,2 @@ | ||
export * from "./createLinkBridgeProvider"; | ||
export * from "./useBridge"; |
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,22 +1,14 @@ | ||
import type { Bridge, BridgeStore, ExtractStore } from "@webview-bridge/web"; | ||
import { useSyncExternalStore } from "use-sync-external-store/shim"; | ||
import type { Bridge, BridgeStore } from "@webview-bridge/web"; | ||
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector.js"; | ||
|
||
export function useBridge<T extends Bridge>( | ||
export const useBridge = <T extends Bridge, U>( | ||
store: Omit<BridgeStore<T>, "setState">, | ||
): ExtractStore<BridgeStore<T>>; | ||
|
||
export function useBridge< | ||
T extends Bridge, | ||
U extends ExtractStore<BridgeStore<T>>, | ||
V, | ||
>(store: Omit<BridgeStore<T>, "setState">, selector?: (state: U) => V): V; | ||
|
||
export function useBridge< | ||
T extends Bridge, | ||
U extends ExtractStore<BridgeStore<T>>, | ||
V, | ||
>(store: Omit<BridgeStore<T>, "setState">, selector?: (state: U) => V): V { | ||
const getSnapshot = () => | ||
selector?.(store.getState() as U) ?? store.getState(); | ||
return useSyncExternalStore(store.subscribe, getSnapshot) as V; | ||
} | ||
selector?: (state: T) => U, | ||
): U => { | ||
return useSyncExternalStoreWithSelector( | ||
store.subscribe, | ||
store.getState, | ||
store.getState, | ||
selector ?? ((state: T) => state as unknown as U), | ||
); | ||
}; |
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
File renamed without changes.
File renamed without changes.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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