Skip to content
This repository has been archived by the owner on Nov 8, 2024. It is now read-only.

Commit

Permalink
fix: add arbitrary delay for retrieval of router object
Browse files Browse the repository at this point in the history
this delay is added to avoid a race condition during startup of insomnia and the loading of plugins
  • Loading branch information
Lode Vanhove committed Dec 6, 2023
1 parent 31f1c8f commit 6a7a7a5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const initAsync = async () => {

let tries = 0
const waitForConnectionAsync = async () => {
if (!insomnia.connect()) {
const connectionIsUp = await insomnia.connect()
if (!connectionIsUp) {
if (tries++ < 25) window.setTimeout(waitForConnectionAsync, 200)
else console.error('[plugin-navigator]', 'cannot connect to Insomnia')
} else await initAsync()
Expand Down
11 changes: 8 additions & 3 deletions src/services/insomnia/connector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { initConnection } from './refs-common'
import { initRouter } from './refs-router'
import { initEvents } from './refs-events'

export const connect = (): boolean => {
export const connect = async () => {
initConnection()
if (!initRouter()) return false

const routerWasFound = await initRouter()
if (!routerWasFound) {
return false
}

initEvents()

return true
}
};

Check failure on line 16 in src/services/insomnia/connector/index.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
52 changes: 35 additions & 17 deletions src/services/insomnia/connector/refs-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,41 @@ import { isCurrentConnectionStillActive } from './refs-common'

let internalRouter: InsomniaRouter = {} as InsomniaRouter

export const initRouter = (): boolean => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rootElement = document.querySelector('#root') as any
if (!rootElement) {
console.error('[plugin-navigator]', 'root element not found')
return false
}

const containerElement = Object.getOwnPropertyNames(rootElement).findLast(x => x.startsWith('__reactContainer'))
if (!containerElement) {
console.warn('[plugin-navigator]', 'store container element not found')
return false
}

internalRouter = rootElement[containerElement].memoizedState.element.props.router
return true
}
// A delay is needed to wait for the router to be initialized, this is a
// workaround for the fact that the router is not available immediately after
// the plugin is loaded.
const ROUTER_INIT_DELAY = 1000;

Check failure on line 9 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon

export const initRouter = (): Promise<boolean> => {
return new Promise((resolve) => {
setTimeout(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rootElement = document.querySelector('#root') as any
if (!rootElement) {
console.error('[plugin-navigator]', 'root element not found')
return resolve(false)
}

const containerElement = Object.getOwnPropertyNames(rootElement).findLast(
(x) => x.startsWith('__reactContainer')
);

Check failure on line 23 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
if (!containerElement) {
console.warn('[plugin-navigator]', 'store container element not found')
return resolve(false)
}

const memoizedStateElement =
rootElement[containerElement].memoizedState.element;

Check failure on line 30 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
if (!memoizedStateElement) {
console.warn('[plugin-navigator]', 'memoized state element not found')
return resolve(false)
}

internalRouter = memoizedStateElement.props.router;

Check failure on line 36 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
return resolve(true)
}, ROUTER_INIT_DELAY)
});

Check failure on line 39 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon
};

Check failure on line 40 in src/services/insomnia/connector/refs-router.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Extra semicolon

export const getRouter = (): InsomniaRouter => internalRouter

Expand Down

0 comments on commit 6a7a7a5

Please sign in to comment.