Skip to content

Commit

Permalink
wrapWithSemaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrmendy committed Feb 5, 2024
1 parent 3326a42 commit 3ac2e57
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions editor/src/core/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,24 @@ export function isImage(str: string): boolean {
const lowerCaseStr = str.toLowerCase()
return imgPattern.test(lowerCaseStr) || lowerCaseStr.startsWith('data:image/')
}

export function wrapWithSemaphore<Args extends unknown[], ReturnValue>(
label: string,
inner: (...args: Args) => ReturnValue,
): (...args: Args) => ReturnValue {
// when this is set to `true`, `inner` can be called
let running: boolean = false

return (...args: Args): ReturnValue => {
if (running) {
// console.trace('x')
throw new Error(`${label} is forbidden from being called recursively`)
}

running = false

const result = inner(...args)
running = true
return result
}
}

0 comments on commit 3ac2e57

Please sign in to comment.