forked from storybookjs/storybook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a queue instead of loops and timeouts
- Loading branch information
1 parent
3e338db
commit 37e29e4
Showing
3 changed files
with
59 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 18 additions & 45 deletions
63
code/frameworks/angular/src/client/angular-beta/utils/BootstrapLock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,28 @@ | ||
import { ApplicationRef } from '@angular/core'; | ||
|
||
let lock: string | null = null; | ||
const queue: Array<() => Promise<void>> = []; | ||
let isProcessing = false; | ||
|
||
/** | ||
* Returns the selector of the component being bootstrapped, or null if no | ||
* component is being bootstrapped. | ||
*/ | ||
export const getCurrentLock = (): string | null => { | ||
return lock; | ||
}; | ||
export const queueBootstrapping = (fn: () => Promise<ApplicationRef>): Promise<ApplicationRef> => { | ||
return new Promise<ApplicationRef>((resolve, reject) => { | ||
queue.push(() => fn().then(resolve).catch(reject)); | ||
|
||
/** | ||
* Waits for chance to acquire lock for a component to bootstrap. | ||
* | ||
* @param selector the selectory of the component requesting a lock to bootstrap | ||
* @returns | ||
*/ | ||
const acquireLock = (selector: string) => { | ||
return new Promise<void>((resolve) => { | ||
function checkLock() { | ||
if (lock === null) { | ||
lock = selector; | ||
resolve(); | ||
} else { | ||
setTimeout(checkLock, 30); | ||
} | ||
if (!isProcessing) { | ||
processQueue(); | ||
} | ||
|
||
checkLock(); | ||
}); | ||
}; | ||
|
||
/** | ||
* Delays bootstrapping until a lock is acquired, so that only one application | ||
* can be bootstrapped at a time. | ||
* | ||
* Bootstrapping multiple applications at once can cause Angular to throw an | ||
* error that a component is declared in multiple modules. | ||
* | ||
* @param selector the selectory of the component requesting a lock to bootstrap | ||
* @param fn callback that should complete the bootstrap process | ||
* @returns ApplicationRef from the completed bootstrap process | ||
*/ | ||
export const bootstrapLock = async (selector: string, fn: () => Promise<ApplicationRef>) => { | ||
await acquireLock(selector); | ||
try { | ||
const ref = await fn(); | ||
lock = null; | ||
return ref; | ||
} catch (e) { | ||
lock = null; | ||
throw e; | ||
const processQueue = async () => { | ||
isProcessing = true; | ||
|
||
while (queue.length > 0) { | ||
const bootstrappingFn = queue.shift(); | ||
if (bootstrappingFn) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await bootstrappingFn(); | ||
} | ||
} | ||
|
||
isProcessing = false; | ||
}; |