-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[net]
transitionEntryFire
-- try to fire one transition entry
- Loading branch information
Showing
4 changed files
with
51 additions
and
12 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 was deleted.
Oops, something went wrong.
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,45 @@ | ||
import { TransitionEntry } from "./Net" | ||
|
||
export type FiringResult = "Fired" | "NotFired" | "FiringError" | ||
|
||
export async function fireTransitionEntry( | ||
transitionEntry: TransitionEntry, | ||
): Promise<FiringResult> { | ||
if (!transitionEntry.task) return "NotFired" | ||
|
||
const inputs = transitionEntryGetInputs(transitionEntry) | ||
if (inputs === undefined) return "NotFired" | ||
|
||
try { | ||
const outputs = await transitionEntry.task.fn(...inputs) | ||
transitionEntryPutOutputs(transitionEntry, outputs) | ||
return "Fired" | ||
} catch (error) { | ||
return "FiringError" | ||
} | ||
} | ||
|
||
function transitionEntryGetInputs( | ||
transitionEntry: TransitionEntry, | ||
): Array<any> | undefined { | ||
if ( | ||
transitionEntry.inputPlaceEntries.some( | ||
(placeEntry) => placeEntry.queue.length === 0, | ||
) | ||
) { | ||
return undefined | ||
} | ||
|
||
return transitionEntry.inputPlaceEntries.map((placeEntry) => | ||
placeEntry.queue.pop(), | ||
) | ||
} | ||
|
||
function transitionEntryPutOutputs( | ||
transitionEntry: TransitionEntry, | ||
outputs: Array<any>, | ||
): void { | ||
for (const placeEntry of [...transitionEntry.outputPlaceEntries].reverse()) { | ||
placeEntry.queue.push(outputs.pop()) | ||
} | ||
} |
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