Skip to content

Commit

Permalink
feat: commandReverted from Processor (#61)
Browse files Browse the repository at this point in the history
Fixes #60
  • Loading branch information
akdasa authored Jan 26, 2023
1 parent 3ab89f4 commit 8cbd60c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
8 changes: 3 additions & 5 deletions lib/commands/Processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { Transaction } from './Transaction'
*/
export class Processor<TContext> {
private stack = new ExecutionStack()
private commandExecutedEvent = new Event<Command<TContext, AnyResult>>()
public readonly commandExecuted = new Event<Command<TContext, AnyResult>>()
public readonly commandReverted = new Event<Command<TContext, AnyResult>>()

/**
* Initialize a new instance of the Processor class.
Expand All @@ -20,10 +21,6 @@ export class Processor<TContext> {
public readonly context: TContext
) { }

get commandExecuted(): Event<Command<TContext, AnyResult>> {
return this.commandExecutedEvent
}

/**
* Excecute the command.
* @param command Command to process.
Expand Down Expand Up @@ -54,6 +51,7 @@ export class Processor<TContext> {

for (const command of commands) {
await command.revert(this.context)
this.commandReverted.notify(command)
}
return new ProcessorResult(Ok())
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@akdasa-studios/framework",
"version": "0.2.7",
"version": "0.2.8",
"description": "Framework to build every app",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
26 changes: 25 additions & 1 deletion tests/commands/Processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ describe('Processor', () => {
const command = new DivCommand(2)
let lastCommand: Command<CalculatorContext, AnyResult>|undefined = undefined
function commandHandler(command) {
console.log('OLOLO', command)
lastCommand = command
}

Expand All @@ -163,4 +162,29 @@ describe('Processor', () => {
expect(lastCommand).toBeUndefined()
})
})

describe('.commandReverted', () => {
const command = new DivCommand(2)
let lastCommand: Command<CalculatorContext, AnyResult>|undefined = undefined
function commandHandler(command) {
lastCommand = command
}

beforeEach(async () => {
lastCommand = undefined
processor.commandReverted.subscribe(commandHandler)
await processor.execute(command)
})

it('notifies subscribers', async () => {
await processor.revert()
expect(lastCommand).not.toBeUndefined()
})

it('notifies subscribers', async () => {
processor.commandReverted.unsubscribe(commandHandler)
await processor.revert()
expect(lastCommand).toBeUndefined()
})
})
})

0 comments on commit 8cbd60c

Please sign in to comment.