Skip to content

Commit

Permalink
fix: mock store for non-native environment (SSR) (#55)
Browse files Browse the repository at this point in the history
* fix: generic default

* chore: update use-sync-external-store dependency to version 1.2.2

* chore: mock store for non-native environment and bridgeInstance.ts refactor
  • Loading branch information
gronxb authored Jul 9, 2024
1 parent d9d3ff0 commit 425bdc8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"react": "^18.2.0"
},
"dependencies": {
"use-sync-external-store": "^1.2.0"
"use-sync-external-store": "^1.2.2"
},
"peerDependencies": {
"@webview-bridge/web": ">=1.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/createLinkBridgeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface BridgeProviderProps {

export const createLinkBridgeProvider = <
T extends BridgeStore<T extends Bridge ? T : any>,
V extends ParserSchema<any>,
V extends ParserSchema<any> = ParserSchema<any>,
>(
options?: LinkBridgeOptions<T, V>,
) => {
Expand Down
15 changes: 5 additions & 10 deletions packages/web/src/internal/bridgeInstance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
Bridge,
BridgeStore,
ExcludePrimitive,
ExtractStore,
KeyOfOrString,
Parser,
Expand All @@ -12,7 +11,6 @@ import {
createRandomId,
createResolver,
DefaultEmitter,
noop,
timeout,
} from "@webview-bridge/util";

Expand All @@ -21,6 +19,7 @@ import { LinkBridgeOptions } from "../linkBridge";
import { LinkBridge } from "../types";
import { createPromiseProxy } from "./createPromiseProxy";
import { linkBridgeStore } from "./linkBridgeStore";
import { mockStore } from "./mockStore";

export class BridgeInstance<
T extends BridgeStore<T extends Bridge ? T : any>,
Expand All @@ -38,10 +37,10 @@ export class BridgeInstance<

private defaultTimeoutMs = 2000;

public store: Omit<T, "setState"> = {
getState: () => ({}) as ExcludePrimitive<ExtractStore<T>>,
subscribe: noop,
} as unknown as Omit<T, "setState">;
public store: Omit<T, "setState"> = mockStore() as unknown as Omit<
T,
"setState"
>;

get isWebViewBridgeAvailable() {
return Boolean(window.ReactNativeWebView) && this._bridgeMethods.length > 0;
Expand Down Expand Up @@ -154,10 +153,6 @@ export class BridgeInstance<
this._bridgeMethods = bridgeMethods;
this._nativeInitialState = nativeInitialState;

if (bridgeMethods.length === 0) {
return false;
}

const { timeout: timeoutMs = this.defaultTimeoutMs, onFallback } =
this.options;

Expand Down
9 changes: 9 additions & 0 deletions packages/web/src/internal/mockStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const mockStore = () => ({
state: {},
getState() {
return this.state;
},
subscribe() {
return () => {};
},
});
8 changes: 3 additions & 5 deletions packages/web/src/linkBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import type {
ParserSchema,
PrimitiveObject,
} from "@webview-bridge/types";
import { createEvents, noop } from "@webview-bridge/util";
import { createEvents } from "@webview-bridge/util";

import { MethodNotFoundError } from "./error";
import { BridgeInstance } from "./internal/bridgeInstance";
import { mockStore } from "./internal/mockStore";
import { LinkBridge } from "./types";

export interface LinkBridgeOptions<
Expand Down Expand Up @@ -44,10 +45,7 @@ export const linkBridge = <
): LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V> => {
if (typeof window === "undefined") {
return {
store: {
getState: () => ({}) as ExcludePrimitive<ExtractStore<T>>,
subscribe: noop,
} as unknown as Omit<T, "setState">,
store: mockStore() as unknown as Omit<T, "setState">,
} as LinkBridge<ExcludePrimitive<ExtractStore<T>>, Omit<T, "setState">, V>;
}

Expand Down
13 changes: 11 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit 425bdc8

Please sign in to comment.