diff --git a/lib/commands/Processor.ts b/lib/commands/Processor.ts index d1cfe23..132efb9 100644 --- a/lib/commands/Processor.ts +++ b/lib/commands/Processor.ts @@ -1,7 +1,6 @@ -import { AnyResult, Fail, Ok, Result, Event } from '@lib/core' -import { Command } from './Command' +import { AnyResult, Event } from '@lib/core' +import { AnyCommand, Command } from './Command' import { ExecutionStack } from './ExecutionStack' -import { ProcessorResult } from './ProcessorResult' import { Transaction } from './Transaction' @@ -25,34 +24,32 @@ export class Processor { * Excecute the command. * @param command Command to process. * @param transaction Transaction. - * @returns {ProcessorResult} Returns the result execution. + * @returns {TResult} Returns the result of execution. */ async execute( command: Command, transaction?: Transaction - ): Promise> { + ): Promise { if (this.stack.includes(command)) { - return new ProcessorResult(Fail('Command is already executed.')) + throw new Error('Command is already executed.') } this.stack.push(command, transaction) const commandResult = await command.execute(this.context) this.commandExecuted.notify(command) - return new ProcessorResult(Ok(), commandResult) + return commandResult } /** * Revert the last executed command. + * @returns List of reverted commands. */ - async revert(): Promise>> { + async revert(): Promise { const commands = this.stack.pop() - if (commands.length === 0) { - return new ProcessorResult(Fail('No command to revert.')) - } for (const command of commands) { await command.revert(this.context) this.commandReverted.notify(command) } - return new ProcessorResult(Ok()) + return commands } } diff --git a/lib/commands/ProcessorResult.ts b/lib/commands/ProcessorResult.ts deleted file mode 100644 index f293063..0000000 --- a/lib/commands/ProcessorResult.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { AnyResult, NoResult, Result } from '@lib/core' - - - -export class ProcessorResult { - constructor( - public readonly processorResult: Result, - 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 - } -} diff --git a/lib/commands/index.ts b/lib/commands/index.ts index cd1ab95..b690890 100644 --- a/lib/commands/index.ts +++ b/lib/commands/index.ts @@ -1,5 +1,4 @@ export * from './Command' export * from './Processor' export * from './ExecutionStack' -export * from './ProcessorResult' export * from './Transaction' \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index f44c55e..7868466 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@akdasa-studios/framework", - "version": "0.3.0", + "version": "0.4.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@akdasa-studios/framework", - "version": "0.3.0", + "version": "0.4.0", "license": "ISC", "dependencies": { "uuid": "^9.0.0" diff --git a/package.json b/package.json index ab841b9..dd3d309 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@akdasa-studios/framework", - "version": "0.3.0", + "version": "0.4.0", "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 eb9c465..679f742 100644 --- a/tests/commands/Processor.spec.ts +++ b/tests/commands/Processor.spec.ts @@ -44,23 +44,17 @@ describe('Processor', () => { it('returns result of execution', async () => { const result = await processor.execute(command) - expect(result.isCommandExecuted).toBeTruthy() - expect(result.isCommandSucceeded).toBeTruthy() expect(result.value).toBe(50) // 100 / 2 }) it('returns failure if command failed', async () => { const result = await processor.execute(new DivCommand(0)) - expect(result.isCommandExecuted).toBeTruthy() - expect(result.isCommandSucceeded).toBeFalsy() expect(result.value).toBe('Cannot divide by zero.') }) - it('returns failure if command is already executed', async () => { + it('throws an error if command is already executed', async () => { await processor.execute(command) - expect((await processor.execute(command)).isCommandExecuted).toBeFalsy() - expect((await processor.execute(command)).processorResult.value).toEqual('Command is already executed.') - expect((await processor.execute(command)).value).toBeUndefined() + await expect(async () => await processor.execute(command)).rejects.toThrowError('Command is already executed') }) }) @@ -80,14 +74,15 @@ describe('Processor', () => { it('returns success if command is reverted', async () => { const result = await processor.revert() - expect(result.isCommandExecuted).toBeTruthy() + expect(result).not.toHaveLength(0) }) - it('returns failure if no command to revert', async () => { + it('returns an empty array if no command to revert', async () => { await processor.revert() const result = await processor.revert() - expect(result.isCommandExecuted).toBeFalsy() - expect(result.processorResult.value).toEqual('No command to revert.') + expect(result).toHaveLength(0) + expect(result).toBeDefined() + expect(result).toStrictEqual([]) }) })