Skip to content

Commit

Permalink
fix: update Command
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Command signature changed

The merge context mechanism does not work anymore
  • Loading branch information
unional committed Apr 22, 2022
1 parent 077647f commit e48201a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 49 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"@types/fbemitter": "^2.0.32",
"fbemitter": "^3.0.0",
"flux-standard-action": "^2.1.2",
"lodash.merge": "^4.6.2",
"tersify": "^3.8.3",
"type-plus": "^4.9.0"
},
Expand Down
11 changes: 7 additions & 4 deletions ts/Command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import t from 'assert'
import { AssertOrder } from 'assertron'
import { Emitter } from './Emitter'

import { Command, createEvent, TestEmitter } from './index'

Expand All @@ -13,21 +14,23 @@ test('Command provides emitter to subclass', () => {

const order = new AssertOrder(1)
const emitter = new TestEmitter()
const command = new TestCommand({ emitter })
const command = new TestCommand(emitter)
emitter.addListener(event, () => order.once(1))
command.run()
order.end()
})

test('Command specifying additional context', () => {
class TestCommand extends Command<{ foo: string }> {
foo!: string
class TestCommand extends Command {
constructor(emitter: Emitter, public foo: string) {
super(emitter)
}
run() {
this.emitter.emit({ type: 'e', payload: this.foo, error: false, meta: undefined })
}
}
const emitter = new TestEmitter()
const command = new TestCommand({ emitter, foo: 'foo' })
const command = new TestCommand(emitter, 'foo')
emitter.on('e', foo => {
t.strictEqual(foo, 'foo')
})
Expand Down
38 changes: 2 additions & 36 deletions ts/Command.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import merge from 'lodash.merge'
import { Emitter } from './Emitter'

// import { LogPresenter, HelpPresenter } from './Presenter'

// export interface ViewContext {
// ui: LogPresenter & HelpPresenter
// }

// export type ViewBuilder<Context extends ViewContext = ViewContext> = (emitter: Emitter, context: Context) => void
export interface CommandContext {
emitter: Emitter
}

export type CommandConstructor<Context extends CommandContext = CommandContext, Cmd extends Command<Context> = Command<Context>> = new (context: Context) => Cmd

/**
* Task to run pure logic.
* Communication to UI is done through emitter.
*/
export abstract class Command<Context = any> {
protected emitter!: Emitter
constructor(context: Context & CommandContext) {
merge(this, context)
export abstract class Command {
constructor(protected emitter: Emitter) {
}

/**
Expand All @@ -30,21 +14,3 @@ export abstract class Command<Context = any> {
*/
abstract run(...args: any[]): void | Promise<any>
}

// export function createTaskRunner<T extends Task, VC extends ViewContext = ViewContext>(context: VC, Task: TaskConstructor<T>, emitterBuilder: ViewBuilder<VC> = () => { return }) {
// const emitter = new Emitter()
// emitterBuilder(emitter, context)

// return {
// run(...args: any[]) {
// try {
// const task = new Task({ emitter })
// return task.run(...args)
// }
// catch (e) {
// // istanbul ignore next
// context.ui.error(e)
// }
// }
// } as T
// }
9 changes: 6 additions & 3 deletions ts/setupCommandTest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import t from 'assert'
import { Emitter } from './Emitter'

import { Command, setupCommandTest, TestEmitter } from './index'

Expand All @@ -14,14 +15,16 @@ test('provides a TestEmitter', () => {
})

test('setupCommandTest gets completion support for context', () => {
class TestCommand extends Command<{ foo: string }> {
foo!: string
class TestCommand extends Command {
constructor(emitter: Emitter, public foo: string) {
super(emitter)
}
run() {
this.emitter.emit({ type: 'e', payload: this.foo, error: false, meta: undefined })
}
}

const { command, emitter } = setupCommandTest(TestCommand, { foo: 'foo' })
const { command, emitter } = setupCommandTest(TestCommand, 'foo')
emitter.on('e', foo => {
t.strictEqual(foo, 'foo')
})
Expand Down
9 changes: 4 additions & 5 deletions ts/setupCommandTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { unpartial } from 'unpartial'
import { Command, CommandConstructor, CommandContext } from './Command'
import { Command } from './Command'
import { Emitter } from './Emitter'
import { TestEmitter } from './TestEmitter'

export function setupCommandTest<Context extends CommandContext, Cmd extends Command>(Command: CommandConstructor<Context, Cmd>, givenContext: Partial<Context> = {}) {
export function setupCommandTest<Cmd extends Command, Args extends any[]>(Command: new (emitter: Emitter, ...args: Args) => Cmd, ...args: Args) {
const emitter = new TestEmitter()
const context = unpartial<Context>({ emitter } as any, givenContext)
const command = new Command(context)
const command = new Command(emitter, ...args)
return { emitter, command }
}

0 comments on commit e48201a

Please # to comment.