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

fix: add arbitrary delay for retrieval of router object #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@

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
Loading