Skip to content

Handler

Brod edited this page Jul 20, 2018 · 3 revisions

Overview

A Handler is similar to a Reducer in redux, it's purpose is to change the state of the application, it recieves data from a parser and decides what needs to change in the state.

The state passed into a Handler is a copy of the current application state, Jackle will ensure the state is not mutated so you don't need to worry about keeping things immutable.

Register

The jackle.handler() method accepts a single Handler or an array or Handlers.

jackel.handler(my_handler);
// or
jackel.handler([ my_handler, my_other_handler ]);

Interface

The Handler interface and it's types are available under the Jagwah.handler namespace.

export module Jackle {
  ...
  export module handler {
    export type func = (state: Jackle.state, data: Jackle.parser.output) => Promise<Jackle.state>;
  }
  export interface handler {
    name: string;
    handlers: Jackle.handler.func[];
  }
}

Example

The example below receives the current application state and the returned value from a Parser with the name update:temp, and returns a modified state.

const update_temp_handler: Jackle.handler = {
  name: 'update:temp',
  handlers: [
    (state, text: string) => {
      state.temp = {
        text: text
      }
      return state;
    }
  ]
}
// register
jackle.handler(update_temp_handler);

// usage
jackle.change('update:temp', document.querySelector('input'));