Skip to content

Commit

Permalink
feat(EventProcessor): Add option to reverse Action processing
Browse files Browse the repository at this point in the history
Adds an option to process Actions per KeyCombo in LIFO order, calling the newest Action's callbacks
first.

Closes #22
  • Loading branch information
dannysellers committed Nov 28, 2017
1 parent 70bee1e commit 1d2e934
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/event-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ export class EventProcessor {
* Search for matching actions, given a keyCombo, and execute its callbacks
*/
public processActionCombos (ev: KeyboardEvent, actions: Map<string, Action>, options: IOptions) {
for (let action of actions.values()) {
let actionCombos = Array(...actions.values())
if (options.reverseActions) {
actionCombos.reverse()
}
for (let action of actionCombos) {
if (this.matchesComboAction(action)) {
if (options.debug) {
this.printDebugActionFound(action)
Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface IOptions {
debug?: boolean
preventDefault?: boolean
onlyStateCombos?: boolean
reverseActions?: boolean
}

// Options class
Expand All @@ -21,6 +22,11 @@ export class Options implements IOptions {
*/
public onlyStateCombos: boolean = false

/**
* Process combos in LIFO order
*/
public reverseActions: boolean = false

constructor(obj?: IOptions) {
if (obj && Object.keys(obj).some(key => typeof this[key] === 'undefined')) {
throw new Error('Some of the options are not correct. Checkout the docs at https://github.com/coosto/ShortcutJS for more info')
Expand Down
22 changes: 22 additions & 0 deletions test/event-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ describe('EventProcessor', () => {
let actions: Map<string, Action>
let cb = jest.fn()
let cb2 = jest.fn()
let cb3 = jest.fn()
let cb4 = jest.fn()

beforeEach(() => {
eventProcessor = new EventProcessor()
actions = new Map()
const action = new Action('action', KeyCombo.fromString('ctrl a'))
const newaction = new Action('newaction', KeyCombo.fromString('ctrl b'))
const othernewaction = new Action('othernewaction', KeyCombo.fromString('ctrl b'))
action.addCallback(cb)
action.addCallback(cb2)
newaction.addCallback(cb3)
othernewaction.addCallback(cb4)
actions.set('action', action)
actions.set('newaction', newaction)
actions.set('othernewaction', othernewaction)
})

afterEach(() => {
Expand Down Expand Up @@ -131,5 +139,19 @@ describe('EventProcessor', () => {
eventProcessor.processEvent(ev, actions, opt) // a
expect(ev.preventDefault).toHaveBeenCalledTimes(1)
})

it('calls the oldest callback per shortcut', () => {
const opt = new Options()
eventProcessor.processEvent(getMockedEvent(17, { ctrlKey: true } as any), actions, opt) // ctrl
eventProcessor.processEvent(getMockedEvent(66, { ctrlKey: true } as any), actions, opt) // b
expect(cb3).toBeCalled()
})

it('calls the newest callback per shortcut if reverseActions option is passed', () => {
const opt = new Options({ reverseActions: true })
eventProcessor.processEvent(getMockedEvent(17, { ctrlKey: true } as any), actions, opt) // ctrl
eventProcessor.processEvent(getMockedEvent(66, { ctrlKey: true } as any), actions, opt) // b
expect(cb4).toBeCalled()
})
})
})
7 changes: 6 additions & 1 deletion test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { Options, IOptions } from '../src/options'

describe('Options', () => {
it('has expected defaults if no params are passed', () => {
expect(new Options()).toEqual({ debug: false, preventDefault: false, onlyStateCombos: false })
expect(new Options()).toEqual({
debug: false,
preventDefault: false,
onlyStateCombos: false,
reverseActions: false
})
})

it('merges options when valid options are passed', () => {
Expand Down

0 comments on commit 1d2e934

Please sign in to comment.