Skip to content

Commit 4c57c8e

Browse files
authored
Refactor waitFor helper away (#128)
1 parent 9f0b29e commit 4c57c8e

File tree

5 files changed

+43
-51
lines changed

5 files changed

+43
-51
lines changed

react/src/ModalRoot.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { router, usePage } from '@inertiajs/react'
55
import { mergeDataIntoQueryString } from '@inertiajs/core'
66
import { createContext, useContext } from 'react'
77
import ModalRenderer from './ModalRenderer'
8-
import { waitFor } from './helpers'
98
import { getConfig } from './config'
109

1110
const ModalStackContext = createContext(null)
@@ -14,7 +13,7 @@ ModalStackContext.displayName = 'ModalStackContext'
1413
let pageVersion = null
1514
let resolveComponent = null
1615
let baseUrl = null
17-
let newModalOnBase = null
16+
let baseModalsToWaitFor = {}
1817
let localStackCopy = []
1918
let pendingModalUpdates = {}
2019

@@ -377,7 +376,7 @@ export const ModalStackProvider = ({ children }) => {
377376
}
378377

379378
if (useInertiaRouter) {
380-
newModalOnBase = null
379+
baseModalsToWaitFor = {}
381380

382381
pendingModalUpdates[modalId] = {
383382
config,
@@ -393,8 +392,8 @@ export const ModalStackProvider = ({ children }) => {
393392
preserveScroll: true,
394393
preserveState: true,
395394
onError: reject,
396-
onFinish: () => {
397-
waitFor(() => newModalOnBase).then(resolve)
395+
onBefore: () => {
396+
baseModalsToWaitFor[modalId] = resolve
398397
},
399398
})
400399
}
@@ -443,6 +442,14 @@ export const ModalStackProvider = ({ children }) => {
443442
visitModal,
444443
registerLocalModal,
445444
removeLocalModal,
445+
onModalOnBase: (modalOnBase) => {
446+
const resolve = baseModalsToWaitFor[modalOnBase.id]
447+
448+
if (resolve) {
449+
resolve(modalOnBase)
450+
delete baseModalsToWaitFor[modalOnBase.id]
451+
}
452+
},
446453
}
447454

448455
return <ModalStackContext.Provider value={value}>{children}</ModalStackContext.Provider>
@@ -544,9 +551,7 @@ export const ModalRoot = ({ children }) => {
544551
})
545552
}
546553
})
547-
.then((newModal) => {
548-
newModalOnBase = newModal
549-
})
554+
.then(context.onModalOnBase)
550555
}),
551556
[],
552557
)

react/src/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { generateId, sameUrlPath, except, only, rejectNullValues, waitFor, kebabCase } from './../../vue/src/helpers.js'
2-
export { generateId, sameUrlPath, except, only, rejectNullValues, waitFor, kebabCase }
1+
import { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase } from './../../vue/src/helpers.js'
2+
export { generateId, sameUrlPath, except, only, rejectNullValues, kebabCase }

vue/src/ModalRoot.vue

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,21 @@ onUnmounted(
2929
previousModalOnBase = modalOnBase
3030
modalStack.setBaseUrl(modalOnBase.baseUrl)
3131
32-
modalStack.pushFromResponseData(modalOnBase, {}, () => {
33-
if (!modalOnBase.baseUrl) {
34-
console.error('No base url in modal response data so cannot navigate back')
35-
return
36-
}
32+
modalStack
33+
.pushFromResponseData(modalOnBase, {}, () => {
34+
if (!modalOnBase.baseUrl) {
35+
console.error('No base url in modal response data so cannot navigate back')
36+
return
37+
}
3738
38-
if (!isNavigating && window.location.href !== modalOnBase.baseUrl) {
39-
router.visit(modalOnBase.baseUrl, {
40-
preserveScroll: true,
41-
preserveState: true,
42-
})
43-
}
44-
})
39+
if (!isNavigating && window.location.href !== modalOnBase.baseUrl) {
40+
router.visit(modalOnBase.baseUrl, {
41+
preserveScroll: true,
42+
preserveState: true,
43+
})
44+
}
45+
})
46+
.then(modalStack.onModalOnBase)
4547
}),
4648
)
4749

vue/src/helpers.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,32 +77,6 @@ function rejectNullValues(target) {
7777
}, {})
7878
}
7979

80-
function waitFor(conditionFn, waitForSeconds = 3, checkIntervalMilliseconds = 10) {
81-
return new Promise((resolve, reject) => {
82-
const result = conditionFn()
83-
84-
if (result) {
85-
resolve(result)
86-
return
87-
}
88-
89-
let maxAttempts = (waitForSeconds * 1000) / checkIntervalMilliseconds
90-
91-
const interval = setInterval(() => {
92-
const result = conditionFn()
93-
if (result) {
94-
clearInterval(interval)
95-
resolve(result)
96-
}
97-
98-
if (--maxAttempts <= 0) {
99-
clearInterval(interval)
100-
reject(new Error('Condition not met in time'))
101-
}
102-
}, checkIntervalMilliseconds)
103-
})
104-
}
105-
10680
function kebabCase(string) {
10781
if (!string) return ''
10882

@@ -130,4 +104,4 @@ function kebabCase(string) {
130104
return string.toLowerCase()
131105
}
132106

133-
export { generateIdUsing, sameUrlPath, generateId, except, only, rejectNullValues, waitFor, kebabCase }
107+
export { generateIdUsing, sameUrlPath, generateId, except, only, rejectNullValues, kebabCase }

vue/src/modalStack.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, readonly, ref, markRaw, h, nextTick } from 'vue'
2-
import { generateId, except, waitFor, kebabCase } from './helpers'
2+
import { generateId, except, kebabCase } from './helpers'
33
import { router } from '@inertiajs/vue3'
44
import { usePage } from '@inertiajs/vue3'
55
import { mergeDataIntoQueryString } from '@inertiajs/core'
@@ -10,6 +10,7 @@ let resolveComponent = null
1010

1111
const pendingModalUpdates = ref({})
1212
const baseUrl = ref(null)
13+
const baseModalsToWaitFor = ref({})
1314
const stack = ref([])
1415
const localModals = ref({})
1516

@@ -325,7 +326,9 @@ function visit(
325326
preserveScroll: true,
326327
preserveState: true,
327328
onError: reject,
328-
onFinish: () => waitFor(() => stack.value[0]).then(resolve),
329+
onBefore: () => {
330+
baseModalsToWaitFor.value[modalId] = resolve
331+
},
329332
})
330333
}
331334

@@ -380,5 +383,13 @@ export function useModalStack() {
380383
visit,
381384
registerLocalModal,
382385
removeLocalModal: (name) => delete localModals.value[name],
386+
onModalOnBase(baseModal) {
387+
const resolve = baseModalsToWaitFor.value[baseModal.id]
388+
389+
if (resolve) {
390+
resolve(baseModal)
391+
delete baseModalsToWaitFor.value[baseModal.id]
392+
}
393+
},
383394
}
384395
}

0 commit comments

Comments
 (0)