@webview-bridge/react-native 1.6.2 → 1.7.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +922 -0
- package/dist/{packages/react-native/src/createWebView.d.ts → index.d.cts} +37 -7
- package/dist/index.d.ts +155 -0
- package/dist/index.js +821 -0
- package/package.json +20 -25
- package/src/createWebView.tsx +346 -0
- package/src/error.ts +6 -0
- package/{dist/packages/react-native/src/index.d.ts → src/index.ts} +1 -1
- package/src/integrations/bridge.ts +138 -0
- package/{dist/packages/react-native/src/integrations/console.js → src/integrations/console.ts} +25 -24
- package/src/integrations/handleRegisterWebMethod.ts +46 -0
- package/src/integrations/postMessageSchema.ts +8 -0
- package/src/types/webview.ts +7 -0
- package/src/useBridge.ts +26 -0
- package/dist/packages/react-native/src/createWebView.js +0 -212
- package/dist/packages/react-native/src/error.d.ts +0 -3
- package/dist/packages/react-native/src/error.js +0 -10
- package/dist/packages/react-native/src/index.js +0 -22
- package/dist/packages/react-native/src/integrations/bridge.d.ts +0 -20
- package/dist/packages/react-native/src/integrations/bridge.js +0 -95
- package/dist/packages/react-native/src/integrations/console.d.ts +0 -3
- package/dist/packages/react-native/src/integrations/handleRegisterWebMethod.d.ts +0 -4
- package/dist/packages/react-native/src/integrations/handleRegisterWebMethod.js +0 -30
- package/dist/packages/react-native/src/integrations/postMessageSchema.d.ts +0 -2
- package/dist/packages/react-native/src/integrations/postMessageSchema.js +0 -56
- package/dist/packages/react-native/src/types/webview.d.ts +0 -3
- package/dist/packages/react-native/src/types/webview.js +0 -2
- package/dist/packages/react-native/src/useBridge.d.ts +0 -3
- package/dist/packages/react-native/src/useBridge.js +0 -9
- package/dist/shared/util/src/createEvents.d.ts +0 -25
- package/dist/shared/util/src/createEvents.js +0 -40
- package/dist/shared/util/src/createRandomId.d.ts +0 -1
- package/dist/shared/util/src/createRandomId.js +0 -10
- package/dist/shared/util/src/equals.d.ts +0 -1
- package/dist/shared/util/src/equals.js +0 -49
- package/dist/shared/util/src/index.d.ts +0 -6
- package/dist/shared/util/src/index.js +0 -22
- package/dist/shared/util/src/noop.d.ts +0 -1
- package/dist/shared/util/src/noop.js +0 -5
- package/dist/shared/util/src/removeUndefinedKeys.d.ts +0 -1
- package/dist/shared/util/src/removeUndefinedKeys.js +0 -12
- package/dist/shared/util/src/timeout.d.ts +0 -1
- package/dist/shared/util/src/timeout.js +0 -16
- package/dist/shared/util/src/types.d.ts +0 -36
- package/dist/shared/util/src/types.js +0 -2
package/src/useBridge.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Bridge, BridgeStore, ExtractStore } from "@webview-bridge/types";
|
|
2
|
+
import { useSyncExternalStoreWithSelector } from "use-sync-external-store/with-selector.js";
|
|
3
|
+
|
|
4
|
+
export function useBridge<T extends Bridge>(
|
|
5
|
+
store: BridgeStore<T>,
|
|
6
|
+
): ExtractStore<BridgeStore<T>>;
|
|
7
|
+
|
|
8
|
+
export function useBridge<
|
|
9
|
+
T extends Bridge,
|
|
10
|
+
U extends ExtractStore<BridgeStore<T>>,
|
|
11
|
+
V,
|
|
12
|
+
>(store: BridgeStore<T>, selector?: (state: U) => V): V;
|
|
13
|
+
|
|
14
|
+
export function useBridge<T extends Bridge, U>(
|
|
15
|
+
store: BridgeStore<T>,
|
|
16
|
+
selector?: (state: T) => U,
|
|
17
|
+
): U {
|
|
18
|
+
const $selector = selector ?? ((state: T) => state as unknown as U);
|
|
19
|
+
|
|
20
|
+
return useSyncExternalStoreWithSelector(
|
|
21
|
+
store.subscribe,
|
|
22
|
+
store.getState,
|
|
23
|
+
store.getState,
|
|
24
|
+
$selector,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.createWebView = void 0;
|
|
30
|
-
const util_1 = require("../../../shared/util/src");
|
|
31
|
-
const react_1 = __importStar(require("react"));
|
|
32
|
-
const react_native_webview_1 = __importDefault(require("react-native-webview"));
|
|
33
|
-
const bridge_1 = require("./integrations/bridge");
|
|
34
|
-
const console_1 = require("./integrations/console");
|
|
35
|
-
const handleRegisterWebMethod_1 = require("./integrations/handleRegisterWebMethod");
|
|
36
|
-
/**
|
|
37
|
-
* Create a WebView component that can communicate with the bridge.
|
|
38
|
-
* @link https://gronxb.github.io/webview-bridge/getting-started.html
|
|
39
|
-
* @example
|
|
40
|
-
import { createWebView, bridge } from "@webview-bridge/react-native";
|
|
41
|
-
import InAppBrowser from "react-native-inappbrowser-reborn";
|
|
42
|
-
|
|
43
|
-
// Register functions in the bridge object in your React Native code
|
|
44
|
-
export const appBridge = bridge({
|
|
45
|
-
async getMessage() {
|
|
46
|
-
return "Hello, I'm native";
|
|
47
|
-
},
|
|
48
|
-
async sum(a: number, b: number) {
|
|
49
|
-
return a + b;
|
|
50
|
-
},
|
|
51
|
-
async openInAppBrowser(url: string) {
|
|
52
|
-
if (await InAppBrowser.isAvailable()) {
|
|
53
|
-
await InAppBrowser.open(url);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
// ... Add more functions as needed
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export const { WebView } = createWebView({
|
|
60
|
-
bridge: appBridge,
|
|
61
|
-
debug: true, // Enable console.log visibility in the native WebView
|
|
62
|
-
});
|
|
63
|
-
*/
|
|
64
|
-
const createWebView = ({ bridge, debug, responseTimeout = 2000, postMessageSchema, fallback, }) => {
|
|
65
|
-
const WebMethod = {
|
|
66
|
-
current: {
|
|
67
|
-
isReady: false,
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
const webviewRefList = [];
|
|
71
|
-
const emitter = (0, util_1.createEvents)();
|
|
72
|
-
bridge.subscribe((state) => {
|
|
73
|
-
for (const ref of webviewRefList) {
|
|
74
|
-
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", state));
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
return {
|
|
78
|
-
/**
|
|
79
|
-
* Sends an event from React Native to the Web.
|
|
80
|
-
* @link https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
81
|
-
*/
|
|
82
|
-
postMessage: (eventName, args) => {
|
|
83
|
-
let _args = args;
|
|
84
|
-
if (postMessageSchema) {
|
|
85
|
-
_args = postMessageSchema[eventName].parse(args);
|
|
86
|
-
}
|
|
87
|
-
for (const ref of webviewRefList) {
|
|
88
|
-
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)(`postMessage/${String(eventName)}`, _args));
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
WebView: (0, react_1.forwardRef)((props, ref) => {
|
|
92
|
-
const webviewRef = (0, react_1.useRef)(null);
|
|
93
|
-
(0, react_1.useLayoutEffect)(() => {
|
|
94
|
-
webviewRefList.push(webviewRef);
|
|
95
|
-
return () => {
|
|
96
|
-
webviewRefList.pop();
|
|
97
|
-
};
|
|
98
|
-
}, []);
|
|
99
|
-
const initData = (0, react_1.useMemo)(() => {
|
|
100
|
-
const bridgeMethods = Object.entries(bridge.getState() ?? {})
|
|
101
|
-
.filter(([_, bridge]) => typeof bridge === "function")
|
|
102
|
-
.map(([name]) => name);
|
|
103
|
-
const initialState = Object.fromEntries(Object.entries(bridge.getState() ?? {}).filter(([_, value]) => typeof value !== "function"));
|
|
104
|
-
return { bridgeMethods, initialState };
|
|
105
|
-
}, []);
|
|
106
|
-
(0, react_1.useEffect)(() => {
|
|
107
|
-
webviewRef.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("hydrate", initData));
|
|
108
|
-
}, [initData]);
|
|
109
|
-
(0, react_1.useImperativeHandle)(ref, () => webviewRef.current, []);
|
|
110
|
-
const handleMessage = async (event) => {
|
|
111
|
-
props.onMessage?.(event);
|
|
112
|
-
if (!webviewRef.current) {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
const { type, body } = JSON.parse(event.nativeEvent.data);
|
|
116
|
-
switch (type) {
|
|
117
|
-
case "log": {
|
|
118
|
-
const { method, args } = body;
|
|
119
|
-
debug && (0, console_1.handleLog)(method, args);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
case "bridge": {
|
|
123
|
-
const { method, args, eventId } = body;
|
|
124
|
-
(0, bridge_1.handleBridge)({
|
|
125
|
-
bridge,
|
|
126
|
-
method,
|
|
127
|
-
args,
|
|
128
|
-
eventId,
|
|
129
|
-
webview: webviewRef.current,
|
|
130
|
-
});
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
case "getBridgeState": {
|
|
134
|
-
for (const ref of webviewRefList) {
|
|
135
|
-
ref?.current?.injectJavaScript((0, bridge_1.SAFE_NATIVE_EMITTER_EMIT)("bridgeStateChange", bridge.getState()));
|
|
136
|
-
}
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
case "registerWebMethod": {
|
|
140
|
-
const { bridgeNames } = body;
|
|
141
|
-
Object.assign(WebMethod.current, (0, handleRegisterWebMethod_1.handleRegisterWebMethod)(emitter, webviewRef.current, bridgeNames, responseTimeout));
|
|
142
|
-
WebMethod.current.isReady = true;
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
case "webMethodResponse": {
|
|
146
|
-
const { eventId, funcName, value } = body;
|
|
147
|
-
emitter.emit(`${funcName}-${eventId}`, value);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
case "webMethodError": {
|
|
151
|
-
const { eventId, funcName } = body;
|
|
152
|
-
emitter.emit(`${funcName}-${eventId}`, {}, true);
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
case "fallback": {
|
|
156
|
-
const { method } = body;
|
|
157
|
-
fallback?.(method);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
};
|
|
162
|
-
return (<react_native_webview_1.default {...props} ref={webviewRef} onMessage={handleMessage} injectedJavaScriptBeforeContentLoaded={[
|
|
163
|
-
(0, bridge_1.INJECT_BRIDGE_METHODS)(initData.bridgeMethods),
|
|
164
|
-
(0, bridge_1.INJECT_BRIDGE_STATE)(initData.initialState),
|
|
165
|
-
props.injectedJavaScriptBeforeContentLoaded,
|
|
166
|
-
"true;",
|
|
167
|
-
]
|
|
168
|
-
.filter(Boolean)
|
|
169
|
-
.join("\n")} injectedJavaScript={[
|
|
170
|
-
debug && console_1.INJECT_DEBUG,
|
|
171
|
-
props.injectedJavaScript,
|
|
172
|
-
"true;",
|
|
173
|
-
]
|
|
174
|
-
.filter(Boolean)
|
|
175
|
-
.join("\n")}/>);
|
|
176
|
-
}),
|
|
177
|
-
/**
|
|
178
|
-
* @deprecated Use `postMessage` instead. And complete the type through the `postMessageSchema` option.
|
|
179
|
-
* @see https://gronxb.github.io/webview-bridge/using-a-post-message.html
|
|
180
|
-
* @example
|
|
181
|
-
import { createWebView, postMessageSchema } from "@webview-bridge/react-native";
|
|
182
|
-
import { z } from "zod";
|
|
183
|
-
|
|
184
|
-
const appPostMessageSchema = postMessageSchema({
|
|
185
|
-
eventName1: z.object({
|
|
186
|
-
message: z.string(),
|
|
187
|
-
}),
|
|
188
|
-
eventName2: z.string(),
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
// Export the event schema to be used in the web application
|
|
193
|
-
export type AppPostMessageSchema = typeof appPostMessageSchema;
|
|
194
|
-
|
|
195
|
-
// When you bridge a webview, a postMessage is extracted.
|
|
196
|
-
export const { postMessage } = createWebView({
|
|
197
|
-
postMessageSchema: appPostMessageSchema, // Pass in the your schema. This is optional, so if the type doesn't matter to you, you don't need to include it.
|
|
198
|
-
// ..
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
// usage
|
|
202
|
-
postMessage("eventName1", {
|
|
203
|
-
message: "test",
|
|
204
|
-
});
|
|
205
|
-
postMessage("eventName2", "test");
|
|
206
|
-
*/
|
|
207
|
-
linkWebMethod() {
|
|
208
|
-
return WebMethod;
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
};
|
|
212
|
-
exports.createWebView = createWebView;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WebMethodError = void 0;
|
|
4
|
-
class WebMethodError extends Error {
|
|
5
|
-
constructor(methodName) {
|
|
6
|
-
super(`An error occurred in the WebMethod.current.${methodName}`);
|
|
7
|
-
this.name = "WebMethodError";
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
exports.WebMethodError = WebMethodError;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./createWebView"), exports);
|
|
18
|
-
__exportStar(require("./integrations/bridge"), exports);
|
|
19
|
-
__exportStar(require("./integrations/postMessageSchema"), exports);
|
|
20
|
-
__exportStar(require("./types/webview"), exports);
|
|
21
|
-
__exportStar(require("./useBridge"), exports);
|
|
22
|
-
__exportStar(require("../../../shared/util/src/types"), exports);
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { Bridge, BridgeStore, OnlyJSON, Primitive } from "../../../../shared/util/src/types";
|
|
2
|
-
import WebView from "react-native-webview";
|
|
3
|
-
export type StoreCallback<T> = ({ get, set, }: {
|
|
4
|
-
get: () => T;
|
|
5
|
-
set: (newState: Partial<OnlyJSON<T>>) => void;
|
|
6
|
-
}) => T;
|
|
7
|
-
export declare const bridge: <T extends Bridge>(procedures: T | StoreCallback<T>) => BridgeStore<T>;
|
|
8
|
-
type HandleBridgeArgs<ArgType = unknown> = {
|
|
9
|
-
bridge: BridgeStore<Bridge>;
|
|
10
|
-
method: string;
|
|
11
|
-
args?: ArgType[];
|
|
12
|
-
webview: WebView;
|
|
13
|
-
eventId: string;
|
|
14
|
-
};
|
|
15
|
-
export declare const handleBridge: ({ bridge, method, args, webview, eventId, }: HandleBridgeArgs) => Promise<void>;
|
|
16
|
-
export declare const INJECT_BRIDGE_METHODS: (bridgeNames: string[]) => string;
|
|
17
|
-
export declare const INJECT_BRIDGE_STATE: (initialState: Record<string, Primitive>) => string;
|
|
18
|
-
export declare const SAFE_NATIVE_EMITTER_EMIT: (eventName: string, data: unknown) => string;
|
|
19
|
-
export declare const SAFE_NATIVE_EMITTER_THROW: (eventName: string) => string;
|
|
20
|
-
export {};
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SAFE_NATIVE_EMITTER_THROW = exports.SAFE_NATIVE_EMITTER_EMIT = exports.INJECT_BRIDGE_STATE = exports.INJECT_BRIDGE_METHODS = exports.handleBridge = exports.bridge = void 0;
|
|
4
|
-
const util_1 = require("../../../../shared/util/src");
|
|
5
|
-
const bridge = (procedures) => {
|
|
6
|
-
const getState = () => state;
|
|
7
|
-
const setState = (newState) => {
|
|
8
|
-
const _newState = {
|
|
9
|
-
...state,
|
|
10
|
-
...(0, util_1.removeUndefinedKeys)(newState),
|
|
11
|
-
};
|
|
12
|
-
if ((0, util_1.equals)(state, _newState)) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
const prevState = state;
|
|
16
|
-
state = _newState;
|
|
17
|
-
emitChange(state, prevState);
|
|
18
|
-
};
|
|
19
|
-
let state = typeof procedures === "function"
|
|
20
|
-
? procedures({
|
|
21
|
-
get: getState,
|
|
22
|
-
set: setState,
|
|
23
|
-
})
|
|
24
|
-
: procedures;
|
|
25
|
-
const listeners = new Set();
|
|
26
|
-
const emitChange = (newState, prevState) => {
|
|
27
|
-
for (const listener of listeners) {
|
|
28
|
-
listener(newState, prevState);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
const subscribe = (listener) => {
|
|
32
|
-
listeners.add(listener);
|
|
33
|
-
return () => listeners.delete(listener);
|
|
34
|
-
};
|
|
35
|
-
return {
|
|
36
|
-
getState,
|
|
37
|
-
setState,
|
|
38
|
-
subscribe,
|
|
39
|
-
};
|
|
40
|
-
};
|
|
41
|
-
exports.bridge = bridge;
|
|
42
|
-
const handleBridge = async ({ bridge, method, args, webview, eventId, }) => {
|
|
43
|
-
const _bridge = bridge.getState();
|
|
44
|
-
const _method = _bridge[method];
|
|
45
|
-
const handleThrow = () => {
|
|
46
|
-
webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_THROW)(`${method}-${eventId}`));
|
|
47
|
-
};
|
|
48
|
-
if (!(method in _bridge)) {
|
|
49
|
-
handleThrow();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (typeof _method !== "function") {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
const response = await _method?.(...(args ?? []));
|
|
57
|
-
webview.injectJavaScript((0, exports.SAFE_NATIVE_EMITTER_EMIT)(`${method}-${eventId}`, response));
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
handleThrow();
|
|
61
|
-
console.error(error);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
exports.handleBridge = handleBridge;
|
|
65
|
-
const INJECT_BRIDGE_METHODS = (bridgeNames) => `
|
|
66
|
-
window.__bridgeMethods__ = ${JSON.stringify(bridgeNames)};
|
|
67
|
-
`;
|
|
68
|
-
exports.INJECT_BRIDGE_METHODS = INJECT_BRIDGE_METHODS;
|
|
69
|
-
const INJECT_BRIDGE_STATE = (initialState) => `
|
|
70
|
-
window.__bridgeInitialState__ = ${JSON.stringify(initialState)};
|
|
71
|
-
`;
|
|
72
|
-
exports.INJECT_BRIDGE_STATE = INJECT_BRIDGE_STATE;
|
|
73
|
-
const SAFE_NATIVE_EMITTER_EMIT = (eventName, data) => {
|
|
74
|
-
const dataString = JSON.stringify(data);
|
|
75
|
-
return `
|
|
76
|
-
if (window.nativeEmitter) {
|
|
77
|
-
window.nativeEmitter.emit('${eventName}', ${dataString});
|
|
78
|
-
} else {
|
|
79
|
-
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
80
|
-
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
|
|
81
|
-
}
|
|
82
|
-
true;
|
|
83
|
-
`;
|
|
84
|
-
};
|
|
85
|
-
exports.SAFE_NATIVE_EMITTER_EMIT = SAFE_NATIVE_EMITTER_EMIT;
|
|
86
|
-
const SAFE_NATIVE_EMITTER_THROW = (eventName) => `
|
|
87
|
-
if (window.nativeEmitter) {
|
|
88
|
-
window.nativeEmitter.emit('${eventName}', {}, true);
|
|
89
|
-
} else {
|
|
90
|
-
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
|
|
91
|
-
window.nativeBatchedEvents.push(['${eventName}', {}, true]);
|
|
92
|
-
}
|
|
93
|
-
true;
|
|
94
|
-
`;
|
|
95
|
-
exports.SAFE_NATIVE_EMITTER_THROW = SAFE_NATIVE_EMITTER_THROW;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
export declare const INJECT_DEBUG = "\n{\n const originalConsoleLog = console.log;\n const originalConsoleError = console.error;\n const originalConsoleWarn = console.warn;\n\n console.log = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: \"log\", body: { method: \"log\", args: message } }),\n );\n originalConsoleLog.apply(console, arguments);\n };\n\n console.error = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: \"log\", body: { method: \"error\", args: message } }),\n );\n originalConsoleError.apply(console, arguments);\n };\n\n console.warn = function() {\n var message = JSON.stringify(Array.from(arguments));\n window.ReactNativeWebView?.postMessage(\n JSON.stringify({ type: \"log\", body: { method: \"warn\", args: message } }),\n );\n originalConsoleWarn.apply(console, arguments);\n };\n};\n";
|
|
2
|
-
export type LogType = "log" | "error" | "warn";
|
|
3
|
-
export declare const handleLog: (type: LogType, message: string) => void;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Bridge } from "../../../../shared/util/src/types";
|
|
2
|
-
import { type EventEmitter } from "../../../../shared/util/src";
|
|
3
|
-
import WebView from "react-native-webview";
|
|
4
|
-
export declare const handleRegisterWebMethod: (emitter: EventEmitter, webview: WebView, bridgeNames: string[], responseTimeout: number) => Bridge;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.handleRegisterWebMethod = void 0;
|
|
4
|
-
const util_1 = require("../../../../shared/util/src");
|
|
5
|
-
const error_1 = require("../error");
|
|
6
|
-
const handleRegisterWebMethod = (emitter, webview, bridgeNames, responseTimeout) => {
|
|
7
|
-
return bridgeNames.reduce((acc, methodName) => {
|
|
8
|
-
acc[methodName] = async (...args) => {
|
|
9
|
-
const eventId = (0, util_1.createRandomId)();
|
|
10
|
-
return Promise.race([
|
|
11
|
-
(0, util_1.createResolver)({
|
|
12
|
-
emitter,
|
|
13
|
-
methodName,
|
|
14
|
-
eventId,
|
|
15
|
-
evaluate: () => {
|
|
16
|
-
webview.injectJavaScript(`
|
|
17
|
-
window.webEmitter.emit('${methodName}', '${eventId}', ${JSON.stringify(args)});
|
|
18
|
-
|
|
19
|
-
true;
|
|
20
|
-
`);
|
|
21
|
-
},
|
|
22
|
-
failHandler: new error_1.WebMethodError(methodName),
|
|
23
|
-
}),
|
|
24
|
-
(0, util_1.timeout)(responseTimeout),
|
|
25
|
-
]);
|
|
26
|
-
};
|
|
27
|
-
return acc;
|
|
28
|
-
}, {});
|
|
29
|
-
};
|
|
30
|
-
exports.handleRegisterWebMethod = handleRegisterWebMethod;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.postMessageSchema = void 0;
|
|
4
|
-
const postMessageSchema = (schema) => {
|
|
5
|
-
const parserSchema = Object.entries(schema).map(([eventName, eventSchema]) => {
|
|
6
|
-
// zod
|
|
7
|
-
if ("parse" in eventSchema && typeof eventSchema.parse === "function") {
|
|
8
|
-
return [
|
|
9
|
-
eventName,
|
|
10
|
-
{
|
|
11
|
-
parse: eventSchema.parse,
|
|
12
|
-
schema: eventSchema,
|
|
13
|
-
},
|
|
14
|
-
];
|
|
15
|
-
}
|
|
16
|
-
// yup
|
|
17
|
-
else if ("validateSync" in eventSchema &&
|
|
18
|
-
typeof eventSchema.validateSync === "function") {
|
|
19
|
-
return [
|
|
20
|
-
eventName,
|
|
21
|
-
{
|
|
22
|
-
parse: (data) => {
|
|
23
|
-
return eventSchema.validateSync(data, { abortEarly: true });
|
|
24
|
-
},
|
|
25
|
-
schema: eventSchema,
|
|
26
|
-
},
|
|
27
|
-
];
|
|
28
|
-
}
|
|
29
|
-
// superstruct
|
|
30
|
-
else if ("assert" in eventSchema &&
|
|
31
|
-
typeof eventSchema.assert === "function" &&
|
|
32
|
-
"validate" in eventSchema &&
|
|
33
|
-
typeof eventSchema.validate === "function") {
|
|
34
|
-
return [
|
|
35
|
-
eventName,
|
|
36
|
-
{
|
|
37
|
-
parse: (data) => {
|
|
38
|
-
void eventSchema.assert(data);
|
|
39
|
-
const [_, result] = eventSchema.validate(data);
|
|
40
|
-
return result;
|
|
41
|
-
},
|
|
42
|
-
schema: eventSchema,
|
|
43
|
-
},
|
|
44
|
-
];
|
|
45
|
-
}
|
|
46
|
-
return [
|
|
47
|
-
eventName,
|
|
48
|
-
{
|
|
49
|
-
parse: (data) => data,
|
|
50
|
-
schema: void 0,
|
|
51
|
-
},
|
|
52
|
-
];
|
|
53
|
-
});
|
|
54
|
-
return Object.fromEntries(parserSchema);
|
|
55
|
-
};
|
|
56
|
-
exports.postMessageSchema = postMessageSchema;
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Bridge, BridgeStore, ExtractStore } from "../../../shared/util/src/types";
|
|
2
|
-
export declare function useBridge<T extends Bridge>(store: BridgeStore<T>): ExtractStore<BridgeStore<T>>;
|
|
3
|
-
export declare function useBridge<T extends Bridge, U extends ExtractStore<BridgeStore<T>>, V>(store: BridgeStore<T>, selector?: (state: U) => V): V;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useBridge = void 0;
|
|
4
|
-
const with_selector_js_1 = require("use-sync-external-store/with-selector.js");
|
|
5
|
-
function useBridge(store, selector) {
|
|
6
|
-
const $selector = selector ?? ((state) => state);
|
|
7
|
-
return (0, with_selector_js_1.useSyncExternalStoreWithSelector)(store.subscribe, store.getState, store.getState, $selector);
|
|
8
|
-
}
|
|
9
|
-
exports.useBridge = useBridge;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
interface EventsMap {
|
|
2
|
-
[event: string]: any;
|
|
3
|
-
}
|
|
4
|
-
export interface DefaultEvents extends EventsMap {
|
|
5
|
-
[event: string]: (...args: any) => void;
|
|
6
|
-
}
|
|
7
|
-
export interface EventEmitter<Events extends EventsMap = DefaultEvents> {
|
|
8
|
-
emit<K extends keyof Events>(this: this, event: K, ...args: Parameters<Events[K]>): void;
|
|
9
|
-
events: Partial<{
|
|
10
|
-
[E in keyof Events]: Events[E][];
|
|
11
|
-
}>;
|
|
12
|
-
on<K extends keyof Events>(this: this, event: K, cb: Events[K]): () => void;
|
|
13
|
-
}
|
|
14
|
-
export type DefaultEmitter = EventEmitter<DefaultEvents>;
|
|
15
|
-
export declare const createEvents: <Events extends EventsMap = DefaultEvents>() => EventEmitter<Events>;
|
|
16
|
-
export interface CreateResolverOptions {
|
|
17
|
-
emitter: DefaultEmitter;
|
|
18
|
-
evaluate: () => void;
|
|
19
|
-
eventId: string;
|
|
20
|
-
failHandler?: Error | false;
|
|
21
|
-
methodName: string;
|
|
22
|
-
onFallback?: () => void;
|
|
23
|
-
}
|
|
24
|
-
export declare const createResolver: ({ emitter, evaluate, eventId, failHandler, methodName, onFallback, }: CreateResolverOptions) => Promise<unknown>;
|
|
25
|
-
export {};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createResolver = exports.createEvents = void 0;
|
|
4
|
-
const createEvents = () => ({
|
|
5
|
-
events: {},
|
|
6
|
-
emit(event, ...args) {
|
|
7
|
-
const callbacks = this.events[event] || [];
|
|
8
|
-
for (let i = 0, length = callbacks.length; i < length; i++) {
|
|
9
|
-
callbacks[i](...args);
|
|
10
|
-
}
|
|
11
|
-
},
|
|
12
|
-
on(event, cb) {
|
|
13
|
-
this.events[event]?.push(cb) || (this.events[event] = [cb]);
|
|
14
|
-
return () => {
|
|
15
|
-
this.events[event] = this.events[event]?.filter((i) => cb !== i);
|
|
16
|
-
};
|
|
17
|
-
},
|
|
18
|
-
});
|
|
19
|
-
exports.createEvents = createEvents;
|
|
20
|
-
const createResolver = ({ emitter, evaluate, eventId, failHandler = false, methodName, onFallback, }) => {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
const unbind = emitter.on(`${methodName}-${eventId}`, (data, throwOccurred) => {
|
|
23
|
-
unbind();
|
|
24
|
-
if (throwOccurred) {
|
|
25
|
-
if (failHandler instanceof Error) {
|
|
26
|
-
onFallback?.();
|
|
27
|
-
reject(failHandler);
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
resolve(void 0);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
resolve(data);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
evaluate();
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
exports.createResolver = createResolver;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const createRandomId: (size?: number) => string;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createRandomId = void 0;
|
|
4
|
-
const TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
5
|
-
const ID_LENGTH = 21;
|
|
6
|
-
const createRandomId = (size = ID_LENGTH) => {
|
|
7
|
-
const randomValues = Array.from({ length: size }, () => TABLE[Math.floor(Math.random() * TABLE.length)]);
|
|
8
|
-
return randomValues.join("");
|
|
9
|
-
};
|
|
10
|
-
exports.createRandomId = createRandomId;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const equals: (a: any, b: any) => boolean;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.equals = void 0;
|
|
4
|
-
const equals = (a, b) => {
|
|
5
|
-
if (a === b) {
|
|
6
|
-
return true;
|
|
7
|
-
}
|
|
8
|
-
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
9
|
-
const arrA = Array.isArray(a);
|
|
10
|
-
const arrB = Array.isArray(b);
|
|
11
|
-
let i;
|
|
12
|
-
let length;
|
|
13
|
-
let key;
|
|
14
|
-
if (arrA && arrB) {
|
|
15
|
-
length = a.length;
|
|
16
|
-
if (length !== b.length) {
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
for (i = length; i-- !== 0;) {
|
|
20
|
-
if (!(0, exports.equals)(a[i], b[i])) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
if (arrA !== arrB) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
const keys = Object.keys(a);
|
|
30
|
-
length = keys.length;
|
|
31
|
-
if (length !== Object.keys(b).length) {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
for (i = length; i-- !== 0;) {
|
|
35
|
-
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
for (i = length; i-- !== 0;) {
|
|
40
|
-
key = keys[i];
|
|
41
|
-
if (!(0, exports.equals)(a[key], b[key])) {
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return true;
|
|
46
|
-
}
|
|
47
|
-
return a !== a && b !== b;
|
|
48
|
-
};
|
|
49
|
-
exports.equals = equals;
|