-
-
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.
- Loading branch information
Showing
7 changed files
with
156 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AnyCommand } from './Command' | ||
import { Transaction } from './Transaction' | ||
|
||
|
||
/** | ||
* ExecutionStack is a stack of executed commands. | ||
*/ | ||
export interface ExecutionHistoryLine { | ||
command: AnyCommand, | ||
transactionId: Transaction | undefined | ||
} | ||
|
||
/** | ||
* ExecutionStack is a stack of executed commands. | ||
*/ | ||
export class ExecutionStack { | ||
private lines: ExecutionHistoryLine[] = [] | ||
|
||
public push(command: AnyCommand, transactionId?: Transaction) { | ||
this.lines.push({ command, transactionId }) | ||
} | ||
|
||
public includes(command: AnyCommand) { | ||
return this.lines.map(r => r.command).includes(command) | ||
} | ||
|
||
public pop(): readonly AnyCommand[] { | ||
const lastRow = this.lines.pop() | ||
if (!lastRow) { return [] } | ||
if (!lastRow.transactionId) { return [lastRow.command] } | ||
|
||
const result = [lastRow.command] | ||
while (this.lines.at(-1)?.transactionId === lastRow.transactionId) { | ||
result.push((this.lines.pop() as ExecutionHistoryLine).command) | ||
} | ||
return result | ||
} | ||
} |
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,35 @@ | ||
import { AnyResult, NoResult, Result } from '@lib/core' | ||
|
||
|
||
|
||
export class ProcessorResult<TCommandResult extends AnyResult> { | ||
constructor( | ||
public readonly processorResult: Result<boolean, string>, | ||
public readonly commandResult: TCommandResult = NoResult as TCommandResult | ||
) { } | ||
|
||
/** | ||
* Indicates if the command is executed. | ||
* @returns True if the command is executed, otherwise false. | ||
* @note This property is true if the command is executed successfully or failed. | ||
*/ | ||
get isCommandExecuted(): boolean { | ||
return this.processorResult.isSuccess | ||
} | ||
|
||
/** | ||
* Indicates if the command is executed successfully. | ||
* @returns True if the command is executed successfully, otherwise false. | ||
*/ | ||
get isCommandSucceeded(): boolean { | ||
return this.commandResult.isSuccess | ||
} | ||
|
||
/** | ||
* Gets the result of the executed command. | ||
* @returns {TCommandResult['value']} Returns the result of the executed command. | ||
*/ | ||
get value(): TCommandResult['value'] | TCommandResult['error'] { | ||
return this.commandResult.value | ||
} | ||
} |
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,12 @@ | ||
/** | ||
* Transaction is a marker for a group of commands. | ||
*/ | ||
|
||
|
||
export class Transaction { | ||
/** | ||
* Initialize a new instance of the Transaction class. | ||
* @param value Unique identifier of the transaction. | ||
*/ | ||
constructor(public readonly value: string) { } | ||
} |
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,2 +1,5 @@ | ||
export * from './Command' | ||
export * from './Processor' | ||
export * from './Processor' | ||
export * from './ExecutionStack' | ||
export * from './ProcessorResult' | ||
export * from './Transaction' |
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