Skip to content

Commit

Permalink
refactor(connect): don't rename createLazy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrjpolak committed Jul 1, 2024
1 parent b581192 commit 3185c54
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 33 deletions.
10 changes: 5 additions & 5 deletions packages/connect-iframe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const handleMessage = async (event: MessageEvent<CoreRequestMessage>) => {
// ignore messages from myself (chrome bug?)
if (event.source === window || !event.data) return;
const { data } = event;
const core = coreManager.getCore();
const core = coreManager.get();
const id = 'id' in data && typeof data.id === 'number' ? data.id : 0;

const fail = (error: string) => {
Expand Down Expand Up @@ -199,7 +199,7 @@ const handleMessage = async (event: MessageEvent<CoreRequestMessage>) => {
}

// pass data to Core
coreManager.getCore()?.handleMessage(message);
coreManager.get()?.handleMessage(message);
};

// Communication with 3rd party window and Trezor Popup.
Expand All @@ -212,7 +212,7 @@ const postMessage = (message: CoreEventMessage) => {

// popup handshake is resolved automatically
if (!usingPopup) {
const core = coreManager.getCore();
const core = coreManager.get();
if (core && message.type === UI.REQUEST_UI_WINDOW) {
core.handleMessage({ type: POPUP.HANDSHAKE });

Expand Down Expand Up @@ -331,7 +331,7 @@ const init = async (payload: IFrameInit['payload'], origin: string) => {

try {
// initialize core
await coreManager.getOrInitCore(parsedSettings, postMessage, logWriterFactory);
await coreManager.getOrInit(parsedSettings, postMessage, logWriterFactory);
postMessage(
createIFrameMessage(IFRAME.LOADED, {
useBroadcastChannel: !!_popupMessagePort,
Expand All @@ -345,5 +345,5 @@ const init = async (payload: IFrameInit['payload'], origin: string) => {

window.addEventListener('message', handleMessage, false);
window.addEventListener('beforeunload', () => {
coreManager.getCore()?.dispose();
coreManager.get()?.dispose();
});
2 changes: 1 addition & 1 deletion packages/connect-popup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ const initCoreInPopup = async (
}
};
const coreManager = initCoreState();
const core: Core = await coreManager.getOrInitCore(
const core: Core = await coreManager.getOrInit(
{ ...payload.settings, trustedHost: false },
onCoreEvent,
logWriterFactory,
Expand Down
18 changes: 9 additions & 9 deletions packages/connect/src/core/__tests__/Core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@ const { createTestTransport } = global.JestMocks;
describe('Core', () => {
beforeAll(async () => {});

it('getOrInitCore throws error on DataManager load', async () => {
it('getOrInit throws error on DataManager load', async () => {
jest.spyOn(DataManager, 'load').mockImplementation(() => {
throw new Error('DataManager init error');
});

const coreManager = initCoreState();
await expect(() =>
coreManager.getOrInitCore(parseConnectSettings(), jest.fn()),
coreManager.getOrInit(parseConnectSettings(), jest.fn()),
).rejects.toThrow('DataManager init error');

jest.restoreAllMocks();
});

// TODO: this test breaks other tests. DeviceList loading is not disposed while disposing core.
it.skip('getOrInitCore throws error when disposed before initialization', done => {
it.skip('getOrInit throws error when disposed before initialization', done => {
const coreManager = initCoreState();
coreManager.getOrInitCore(parseConnectSettings(), jest.fn()).catch(error => {
coreManager.getOrInit(parseConnectSettings(), jest.fn()).catch(error => {
expect(error.message).toMatch('Core disposed');
done();
});
coreManager.dispose();
});

it('calling getOrInitCore multiple times synchronously', async () => {
it('calling getOrInit multiple times synchronously', async () => {
const coreManager = initCoreState();
const transport = createTestTransport();
const settings = parseConnectSettings({ transports: [transport], lazyLoad: true });
const [c1, c2] = await Promise.all([
coreManager.getOrInitCore(settings, jest.fn()),
coreManager.getOrInitCore(settings, jest.fn()),
coreManager.getOrInit(settings, jest.fn()),
coreManager.getOrInit(settings, jest.fn()),
]);

// the same instance
expect(c1).toEqual(c2);
coreManager.dispose();
});

it('successful getOrInitCore', async () => {
it('successful getOrInit', async () => {
const coreManager = initCoreState();
const transport = createTestTransport();
const eventsSpy = jest.fn();
const core = await coreManager.getOrInitCore(
const core = await coreManager.getOrInit(
parseConnectSettings({ transports: [transport] }),
eventsSpy,
);
Expand Down
11 changes: 1 addition & 10 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1253,13 +1253,4 @@ const disposeCore = (core: Core) => {
/**
* State initialization
*/
export const initCoreState = () => {
const { get, getPending, getOrInit, dispose } = createLazy(initCore, disposeCore);

return {
getCore: get,
getInitPromise: getPending,
getOrInitCore: getOrInit,
dispose,
};
};
export const initCoreState = () => createLazy(initCore, disposeCore);
16 changes: 8 additions & 8 deletions packages/connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const onCoreEvent = (message: CoreEventMessage) => {
const { event, type, payload } = message;

if (type === UI.REQUEST_UI_WINDOW) {
coreManager.getCore()?.handleMessage({ type: POPUP.HANDSHAKE });
coreManager.get()?.handleMessage({ type: POPUP.HANDSHAKE });

return;
}
Expand Down Expand Up @@ -107,27 +107,27 @@ const initSettings = (settings: Partial<ConnectSettings> = {}) => {
};

const init = async (settings: Partial<ConnectSettings> = {}) => {
if (coreManager.getCore() || coreManager.getInitPromise()) {
if (coreManager.get() || coreManager.getPending()) {
throw ERRORS.TypedError('Init_AlreadyInitialized');
}

initSettings(settings);

if (!_settings.lazyLoad) {
await coreManager.getOrInitCore(_settings, onCoreEvent);
await coreManager.getOrInit(_settings, onCoreEvent);
}
};

const initCore = () => {
initSettings({ lazyLoad: false });

return coreManager.getOrInitCore(_settings, onCoreEvent);
return coreManager.getOrInit(_settings, onCoreEvent);
};

const call: CallMethod = async params => {
let core;
try {
core = coreManager.getCore() ?? (await coreManager.getInitPromise()) ?? (await initCore());
core = coreManager.get() ?? (await coreManager.getPending()) ?? (await initCore());
} catch (error) {
return createErrorMessage(error);
}
Expand All @@ -150,7 +150,7 @@ const call: CallMethod = async params => {
};

const uiResponse = (response: UiResponseEvent) => {
const core = coreManager.getCore();
const core = coreManager.get();
if (!core) {
throw ERRORS.TypedError('Init_NotInitialized');
}
Expand All @@ -160,7 +160,7 @@ const uiResponse = (response: UiResponseEvent) => {
const requestLogin = async (params: any) => {
if (typeof params.callback === 'function') {
const { callback } = params;
const core = coreManager.getCore();
const core = coreManager.get();

// TODO: set message listener only if _core is loaded correctly
const loginChallengeListener = async (event: MessageEvent<CoreEventMessage>) => {
Expand Down Expand Up @@ -197,7 +197,7 @@ const requestLogin = async (params: any) => {
};

const cancel = (error?: string) => {
const core = coreManager.getCore();
const core = coreManager.get();
if (!core) {
throw ERRORS.TypedError('Runtime', 'postMessage: _core not found');
}
Expand Down

0 comments on commit 3185c54

Please sign in to comment.