Skip to content

Commit

Permalink
Add reset method to base component
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixHenninger committed Sep 30, 2022
1 parent 4cbfbd1 commit 05e767b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/library/src/base/component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Component } from './component'
import { Controller } from './controller'

it('can reset component', async () => {
const c = new Component()
const controller = new Controller({ root: c })

const c_prepare = jest.fn()
const c_run = jest.fn()
const c_end = jest.fn()
const c_reset = jest.fn()
c.on('prepare', c_prepare)
c.on('run', c_run)
c.on('end', c_end)
c.on('reset', c_reset)

// We are testing that the component can be re-run,
// and cycles through run and end events correctly
await controller.run()
expect(c_prepare).toHaveBeenCalledTimes(1)
expect(c_run).toHaveBeenCalledTimes(1)
expect(c_end).toHaveBeenCalledTimes(0)
expect(c_reset).toHaveBeenCalledTimes(0)
await c.reset()
expect(c_run).toHaveBeenCalledTimes(2)
expect(c_end).toHaveBeenCalledTimes(1)
expect(c_reset).toHaveBeenCalledTimes(1)
await c.respond('foo', { timestamp: 123, action: 'someAction' })
expect(c_run).toHaveBeenCalledTimes(2)
expect(c_end).toHaveBeenCalledTimes(2)
// Preparation should only happen once
expect(c_prepare).toHaveBeenCalledTimes(1)
expect(c_reset).toHaveBeenCalledTimes(1)
})
11 changes: 11 additions & 0 deletions packages/library/src/base/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum PrivateEventName {
beforeRun = 'before:run',
show = 'show',
endUncontrolled = 'end:uncontrolled',
reset = 'reset',
}

const EventName = { ...PublicEventName, ...PrivateEventName }
Expand Down Expand Up @@ -300,6 +301,16 @@ export class Component {
this.log(`Ending with reason ${flipData.reason}`)
}

async reset() {
await this.end('reset', { controlled: true })
await this.#emitter.trigger(
PrivateEventName.reset,
{},
this.#controller.global,
)
await this.run({ controlled: true })
}

async lock(data: any = {}) {
const { timestamp } = data

Expand Down

0 comments on commit 05e767b

Please sign in to comment.