Fix the infinite loop in dataverse-react

This commit is contained in:
Aria Minaei 2021-07-05 00:30:16 +02:00
parent 8395e44614
commit 055c5392b9

View file

@ -56,8 +56,8 @@ export function usePrism<T>(
return useDerivation(derivation, debugLabel) return useDerivation(derivation, debugLabel)
} }
export const useVal: typeof val = (p) => { export const useVal: typeof val = (p, debugLabel?: string) => {
return usePrism(() => val(p), [p]) return usePrism(() => val(p), [p], debugLabel)
} }
/** /**
@ -87,19 +87,7 @@ let microtaskIsQueued = false
const pushToQueue = (item: QueueItem) => { const pushToQueue = (item: QueueItem) => {
_pushToQueue(item) _pushToQueue(item)
if (!microtaskIsQueued) { queueIfNeeded()
microtaskIsQueued = true
queueMicrotask(() => {
while (queue.length > 0) {
unstable_batchedUpdates(() => {
for (const item of queue) {
item.runUpdate()
}
})
}
microtaskIsQueued = false
})
}
} }
const _pushToQueue = (item: QueueItem) => { const _pushToQueue = (item: QueueItem) => {
@ -132,6 +120,28 @@ const removeFromQueue = (item: QueueItem) => {
queue.splice(index, 1) queue.splice(index, 1)
} }
function queueIfNeeded() {
if (!microtaskIsQueued) {
microtaskIsQueued = true
queueMicrotask(() => {
let i = 0
while (queue.length > 0) {
i++
if (i > 5) {
setTimeout(queueIfNeeded, 1)
break
}
unstable_batchedUpdates(() => {
for (const item of queue) {
item.runUpdate()
}
})
}
microtaskIsQueued = false
})
}
}
/** /**
* @remarks * @remarks
* It looks like this new implementation of useDerivation() manages to: * It looks like this new implementation of useDerivation() manages to:
@ -151,6 +161,7 @@ function useDerivation<T>(der: IDerivation<T>, debugLabel?: string): T {
const refs = useRef<{queueItem: QueueItem; unmounted: boolean}>( const refs = useRef<{queueItem: QueueItem; unmounted: boolean}>(
undefined as $IntentionalAny, undefined as $IntentionalAny,
) )
if (!refs.current) { if (!refs.current) {
lastOrder++ lastOrder++
refs.current = { refs.current = {