Skip to content

Commit

Permalink
feat: simplify Processor (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
akdasa authored Apr 1, 2023
1 parent f953934 commit 1e610f7
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 63 deletions.
21 changes: 9 additions & 12 deletions lib/commands/Processor.ts
Original file line number Diff line number Diff line change
@@ -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'


Expand All @@ -25,34 +24,32 @@ export class Processor<TContext> {
* Excecute the command.
* @param command Command to process.
* @param transaction Transaction.
* @returns {ProcessorResult<TResult>} Returns the result execution.
* @returns {TResult} Returns the result of execution.
*/
async execute<TResult extends AnyResult>(
command: Command<TContext, TResult>,
transaction?: Transaction
): Promise<ProcessorResult<TResult>> {
): Promise<TResult> {
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<TResult>(Ok(), commandResult)
return commandResult
}

/**
* Revert the last executed command.
* @returns List of reverted commands.
*/
async revert(): Promise<ProcessorResult<Result<void, string>>> {
async revert(): Promise<readonly AnyCommand[]> {
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
}
}
35 changes: 0 additions & 35 deletions lib/commands/ProcessorResult.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './Command'
export * from './Processor'
export * from './ExecutionStack'
export * from './ProcessorResult'
export * from './Transaction'
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.3.0",
"version": "0.4.0",
"description": "Framework to build every app",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
19 changes: 7 additions & 12 deletions tests/commands/Processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})

Expand All @@ -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([])
})
})

Expand Down

0 comments on commit 1e610f7

Please sign in to comment.