Skip to content

Commit

Permalink
fix: dapp connection event bus (#6589)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidmorizon authored Jan 23, 2025
1 parent 3157cb9 commit 2791715
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 5 additions & 1 deletion apps/ext/src/ui/uiJsBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function init() {
if (method === GLOBAL_EVENT_BUS_SYNC_BROADCAST_METHOD_NAME) {
console.log('background event bus sync', params);
const p = params as IGlobalEventBusSyncBroadcastParams;
appEventBus.emitToSelf(p.type as any, p.payload);
appEventBus.emitToSelf({
type: p.type as any,
payload: p.payload,
isRemote: true,
});
}
};
// TODO rename global.$extensionJsBridgeUiToBg
Expand Down
16 changes: 11 additions & 5 deletions packages/shared/src/eventBus/appEventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,9 @@ class AppEventBus extends CrossEventEmitter {
type: T,
payload: IAppEventBusPayload[T],
): boolean {
void this.emitToRemote(type, payload);
void this.emitToRemote({ type, payload });
if (this.shouldEmitToSelf) {
this.emitToSelf(type, payload);
this.emitToSelf({ type, payload });
}
return true;
}
Expand Down Expand Up @@ -391,15 +391,20 @@ class AppEventBus extends CrossEventEmitter {
return super.removeListener(type, listener);
}

emitToSelf(type: EAppEventBusNames, payload: any) {
emitToSelf(params: {
type: EAppEventBusNames;
payload: any;
isRemote?: boolean;
}) {
const { type, payload, isRemote } = params;
defaultLogger.app.eventBus.emitToSelf({
eventName: type,
});
const payloadCloned = cloneDeep(payload);
try {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (payloadCloned?.$$isRemoteEvent) {
if (payloadCloned?.$$isRemoteEvent && !isRemote) {
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
payloadCloned.$$isRemoteEvent = undefined;
Expand All @@ -413,7 +418,8 @@ class AppEventBus extends CrossEventEmitter {

//

async emitToRemote(type: string, payload: any) {
async emitToRemote(params: { type: string; payload: any }) {
const { type, payload } = params;
const convertToRemoteEventPayload = (p: any) => {
const payloadCloned = cloneDeep(p);
try {
Expand Down
11 changes: 4 additions & 7 deletions packages/shared/src/storage/syncStorage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { isPlainObject } from 'lodash';

import appGlobals from '../appGlobals';
import platformEnv from '../platformEnv';
import { ensureRunOnBackground } from '../utils/assertUtils';
import dbPerfMonitor from '../utils/debug/dbPerfMonitor';
import resetUtils from '../utils/resetUtils';

Expand Down Expand Up @@ -31,16 +27,17 @@ export const buildAppStorageFactory = (
const setItem: IAppStorage['setItem'] = (key, value, callback) => {
resetUtils.checkNotInResetting();
dbPerfMonitor.logAppStorageCall('setItem', key);
ensureRunOnBackground();
// ensureRunOnBackground();
return originalSetItem.call(storage, key, value, callback);
};
const getItem: IAppStorage['getItem'] = (key, callback) => {
dbPerfMonitor.logAppStorageCall('getItem', key);
ensureRunOnBackground();
// ensureRunOnBackground();
return originalGetItem.call(storage, key, callback);
};
// eslint-disable-next-line arrow-body-style
const removeItem: IAppStorage['removeItem'] = (key, callback) => {
ensureRunOnBackground();
// ensureRunOnBackground();
return originalRemoveItem.call(storage, key, callback);
};

Expand Down

0 comments on commit 2791715

Please sign in to comment.