From 8cbd60cd553d9f19d14ed053b213364c105189d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Advaita=20K=E1=B9=9B=E1=B9=A3=E1=B9=87a=20D=C4=81sa?= Date: Thu, 26 Jan 2023 17:13:45 +0100 Subject: [PATCH] feat: `commandReverted` from Processor (#61) Fixes #60 --- lib/commands/Processor.ts | 8 +++----- package-lock.json | 4 ++-- package.json | 2 +- tests/commands/Processor.spec.ts | 26 +++++++++++++++++++++++++- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/commands/Processor.ts b/lib/commands/Processor.ts index 82335cc..d1cfe23 100644 --- a/lib/commands/Processor.ts +++ b/lib/commands/Processor.ts @@ -10,7 +10,8 @@ import { Transaction } from './Transaction' */ export class Processor { private stack = new ExecutionStack() - private commandExecutedEvent = new Event>() + public readonly commandExecuted = new Event>() + public readonly commandReverted = new Event>() /** * Initialize a new instance of the Processor class. @@ -20,10 +21,6 @@ export class Processor { public readonly context: TContext ) { } - get commandExecuted(): Event> { - return this.commandExecutedEvent - } - /** * Excecute the command. * @param command Command to process. @@ -54,6 +51,7 @@ export class Processor { for (const command of commands) { await command.revert(this.context) + this.commandReverted.notify(command) } return new ProcessorResult(Ok()) } diff --git a/package-lock.json b/package-lock.json index 639650d..b4334c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@akdasa-studios/framework", - "version": "0.2.7", + "version": "0.2.8", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@akdasa-studios/framework", - "version": "0.2.7", + "version": "0.2.8", "license": "ISC", "dependencies": { "uuid": "^9.0.0" diff --git a/package.json b/package.json index 80bb064..5768526 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/commands/Processor.spec.ts b/tests/commands/Processor.spec.ts index 49de586..eb9c465 100644 --- a/tests/commands/Processor.spec.ts +++ b/tests/commands/Processor.spec.ts @@ -143,7 +143,6 @@ describe('Processor', () => { const command = new DivCommand(2) let lastCommand: Command|undefined = undefined function commandHandler(command) { - console.log('OLOLO', command) lastCommand = command } @@ -163,4 +162,29 @@ describe('Processor', () => { expect(lastCommand).toBeUndefined() }) }) + + describe('.commandReverted', () => { + const command = new DivCommand(2) + let lastCommand: Command|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() + }) + }) }) \ No newline at end of file