-
Notifications
You must be signed in to change notification settings - Fork 217
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 #710 from Agoric/mfig/better-errors-and-sync
Fix ag-solo kernel reentrancy and fake-chain non-determinism
- Loading branch information
Showing
5 changed files
with
149 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import makePromise from '@agoric/make-promise'; | ||
|
||
// Return a function that can wrap an async or sync method, but | ||
// ensures only one of them (in order) is running at a time. | ||
export const makeWithQueue = () => { | ||
const queue = []; | ||
|
||
// Execute the thunk at the front of the queue. | ||
const dequeue = () => { | ||
if (!queue.length) { | ||
return; | ||
} | ||
const [thunk, resolve, reject] = queue[0]; | ||
// Run the thunk in a new turn. | ||
Promise.resolve() | ||
.then(thunk) | ||
// Resolve or reject our caller with the thunk's value. | ||
.then(resolve, reject) | ||
// Rerun dequeue() after settling. | ||
.finally(() => { | ||
queue.shift(); | ||
dequeue(); | ||
}); | ||
}; | ||
|
||
return function withQueue(inner) { | ||
return function queueCall(...args) { | ||
// Curry the arguments into the inner function, and | ||
// resolve/reject with whatever the inner function does. | ||
const thunk = _ => inner(...args); | ||
const pr = makePromise(); | ||
queue.push([thunk, pr.res, pr.rej]); | ||
|
||
if (queue.length === 1) { | ||
// Start running immediately. | ||
dequeue(); | ||
} | ||
|
||
// Allow the caller to retrieve our thunk's results. | ||
return pr.p; | ||
}; | ||
}; | ||
}; |
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