Skip to content

Commit

Permalink
feat: version compat layer 1.7.2 (#88)
Browse files Browse the repository at this point in the history
* feat: version compat layer 1.7.2

* fix: nativeEmitterMap
  • Loading branch information
gronxb authored Dec 8, 2024
1 parent 58260e1 commit 31415a0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
21 changes: 15 additions & 6 deletions packages/react-native/src/integrations/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ export const INJECT_BRIDGE_STATE = (
export const SAFE_NATIVE_EMITTER_EMIT = (eventName: string, data: unknown) => {
const dataString = JSON.stringify(data);
return `
if (window.nativeEmitter && Object.keys(window.nativeEmitter).length > 0) {
for (const [_, emitter] of Object.entries(window.nativeEmitter)) {
if (window.nativeEmitterMap && Object.keys(window.nativeEmitterMap).length > 0) {
for (const [_, emitter] of Object.entries(window.nativeEmitterMap)) {
emitter.emit('${eventName}', ${dataString});
}
} else if (window.nativeEmitter) {
// @deprecated This version is not used after 1.7.2
window.nativeEmitter.emit('${eventName}', ${dataString});
} else {
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
Expand All @@ -144,8 +147,11 @@ export const SAFE_NATIVE_EMITTER_EMIT_BY_BRIDGE_ID = (
) => {
const dataString = JSON.stringify(data);
return `
if (window.nativeEmitter && window.nativeEmitter['${bridgeId}']) {
window.nativeEmitter['${bridgeId}'].emit('${eventName}', ${dataString});
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', ${dataString});
} else if (window.nativeEmitter) {
// @deprecated This version is not used after 1.7.2
window.nativeEmitter.emit('${eventName}', ${dataString});
} else {
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
window.nativeBatchedEvents.push(['${eventName}', ${dataString}]);
Expand All @@ -158,8 +164,11 @@ export const SAFE_NATIVE_EMITTER_THROW_BY_BRIDGE_ID = (
bridgeId: string,
eventName: string,
) => `
if (window.nativeEmitter['${bridgeId}']) {
window.nativeEmitter['${bridgeId}'].emit('${eventName}', {}, true);
if (window.nativeEmitterMap && window.nativeEmitterMap['${bridgeId}']) {
window.nativeEmitterMap['${bridgeId}'].emit('${eventName}', {}, true);
} else if (window.nativeEmitter) {
// @deprecated This version is not used after 1.7.2
window.nativeEmitter.emit('${eventName}', {}, true);
} else {
window.nativeBatchedEvents = window.nativeBatchedEvents || [];
window.nativeBatchedEvents.push(['${eventName}', {}, true]);
Expand Down
6 changes: 5 additions & 1 deletion packages/web/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ declare global {
interface Window {
__bridgeMethods__?: string[];
__bridgeInitialState__?: Record<string, Primitive>;
nativeEmitter?: Record<string, DefaultEmitter>;
/**
* @deprecated Use `nativeEmitterMap` instead.
*/
nativeEmitter?: DefaultEmitter;
nativeEmitterMap?: Record<string, DefaultEmitter>;
nativeBatchedEvents?: [string, ...any][];
webEmitter?: DefaultEmitter;
ReactNativeWebView: {
Expand Down
11 changes: 9 additions & 2 deletions packages/web/src/linkBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,18 @@ export const linkBridge = <
const bridgeId = createRandomId();
const emitter = createEvents();

window.nativeEmitter = {
...(window.nativeEmitter || {}),
window.nativeEmitterMap = {
...(window.nativeEmitterMap || {}),
[bridgeId]: emitter,
};

/**
* @deprecated This version is not used after 1.7.2
*/
if (!window.nativeEmitter) {
window.nativeEmitter = emitter;
}

const bridgeMethods = window.__bridgeMethods__ ?? [];
const nativeInitialState = window.__bridgeInitialState__ ?? {};

Expand Down

0 comments on commit 31415a0

Please sign in to comment.