forked from ember-fastboot/ember-cli-fastboot
-
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.
Merge pull request ember-fastboot#264 from ember-fastboot/add-queue-m…
…anagement Add sandbox queue management when using buildSandboxPerVisit
- Loading branch information
Showing
7 changed files
with
348 additions
and
19 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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const debug = require('debug')('fastboot:ember-app'); | ||
|
||
/** | ||
* Utility Queue class to store queue of sandboxes that can be leveraged when using `buildSandboxPerVisit`. | ||
* | ||
* @public | ||
*/ | ||
class Queue { | ||
constructor(builderFn, maxSize = 1) { | ||
this.items = []; | ||
this.maxSize = maxSize; | ||
this.builderFn = builderFn; | ||
} | ||
|
||
_buildItem() { | ||
return this.builderFn(); | ||
} | ||
|
||
_addToQueue() { | ||
this.items.push(this._buildItem()); | ||
} | ||
|
||
enqueue() { | ||
// when the queue is not full, we add the item into the queue, otherwise ignore adding | ||
// to the queue. | ||
if (!this.isFull()) { | ||
this._addToQueue(); | ||
} else { | ||
debug('Ignoring adding appInstance to queue as Queue is already full!'); | ||
} | ||
} | ||
|
||
dequeue() { | ||
if (this.isEmpty()) { | ||
// build on demand if the queue does not have a pre-warmed version to avoid starving | ||
// the system | ||
return { item: this._buildItem(), isItemPreBuilt: false }; | ||
} else { | ||
// return a pre-warmed version | ||
return { item: this.items.shift(), isItemPreBuilt: true }; | ||
} | ||
} | ||
|
||
isEmpty() { | ||
return this.size() === 0; | ||
} | ||
|
||
size() { | ||
return this.items.length; | ||
} | ||
|
||
isFull() { | ||
return this.size() === this.maxSize; | ||
} | ||
} | ||
|
||
module.exports = Queue; |
Oops, something went wrong.