From 3185c54bc8a4499ef5cfd5825b01df02833cab80 Mon Sep 17 00:00:00 2001 From: Marek Polak Date: Mon, 1 Jul 2024 10:46:14 +0200 Subject: [PATCH] refactor(connect): don't rename `createLazy` methods --- packages/connect-iframe/src/index.ts | 10 +++++----- packages/connect-popup/src/index.tsx | 2 +- .../connect/src/core/__tests__/Core.test.ts | 18 +++++++++--------- packages/connect/src/core/index.ts | 11 +---------- packages/connect/src/index.ts | 16 ++++++++-------- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/packages/connect-iframe/src/index.ts b/packages/connect-iframe/src/index.ts index 38f0939e3e0..f3cb320ca5e 100644 --- a/packages/connect-iframe/src/index.ts +++ b/packages/connect-iframe/src/index.ts @@ -50,7 +50,7 @@ const handleMessage = async (event: MessageEvent) => { // 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) => { @@ -199,7 +199,7 @@ const handleMessage = async (event: MessageEvent) => { } // pass data to Core - coreManager.getCore()?.handleMessage(message); + coreManager.get()?.handleMessage(message); }; // Communication with 3rd party window and Trezor Popup. @@ -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 }); @@ -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, @@ -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(); }); diff --git a/packages/connect-popup/src/index.tsx b/packages/connect-popup/src/index.tsx index 60b29faf06e..bd924dd6070 100644 --- a/packages/connect-popup/src/index.tsx +++ b/packages/connect-popup/src/index.tsx @@ -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, diff --git a/packages/connect/src/core/__tests__/Core.test.ts b/packages/connect/src/core/__tests__/Core.test.ts index 7e17bf1e77a..54409428998 100644 --- a/packages/connect/src/core/__tests__/Core.test.ts +++ b/packages/connect/src/core/__tests__/Core.test.ts @@ -8,36 +8,36 @@ 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 @@ -45,11 +45,11 @@ describe('Core', () => { 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, ); diff --git a/packages/connect/src/core/index.ts b/packages/connect/src/core/index.ts index a7e6832bbe7..64b5c08a4b4 100644 --- a/packages/connect/src/core/index.ts +++ b/packages/connect/src/core/index.ts @@ -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); diff --git a/packages/connect/src/index.ts b/packages/connect/src/index.ts index 69b8178ebb0..4878086ac89 100644 --- a/packages/connect/src/index.ts +++ b/packages/connect/src/index.ts @@ -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; } @@ -107,27 +107,27 @@ const initSettings = (settings: Partial = {}) => { }; const init = async (settings: Partial = {}) => { - 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); } @@ -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'); } @@ -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) => { @@ -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'); }