Skip to content

A small library to simplify the definition and creation of messages (a.k.a. 'actions' in Flux/Redux etc. context)

License

Notifications You must be signed in to change notification settings

js-works/js-messages

Repository files navigation

js-messages

A small library to handle messages, message types and message creators (also known as "actions", "action types" and "action creators" in redux context). js-messages is written in TypeScript, everything is completely type safe.

Licence npm version Build status Coverage status

Installation

npm install --save js-messages

Usage

js-messages consists of three functions:

  • defineMessage: To define one type of message / to create one message creator
  • defineMessages: To define multiple related messages represented by message creators
  • props: A helper function to define a message creator where all message properties must be declared explicitly in an object.

Example 1 (using defineMessage)

import { defineMessage } from 'js-messages'

const increment = defineMessage('increment')
// for messages of shape { type: 'increment' }

const decrement = defineMessage('decrement')
// for messages of shape { type: 'decrement' }

const resetTo = defineMessage('resetTo', (value: number) => ({ value }))
// for messages of shape { type: 'resetTo', value: number }

Example 2 (using defineMessage and props)

const saveToStorage = defineMessage(
  'saveToStorage',
  props<{ tasks: Task[]; storageKey: string }>()
)

// for messages of shape
//   {
//     type: 'saveToStorage',
//     tasks: Task[],
//     storageKey: string
//   }

Example 3 (using defineMessages, without namespace):

import { defineMessages } from 'js-messages'

const Actions = defineMessages({
  increment: null,
  // for messages of shape { type: 'increment' }

  decrement: null,
  // for messages of shape { type: 'decrement' }

  resetTo: (value: number) => ({ value })
  // for messages of shape { type: 'resetTo', value: number }

  log: (value: number, message: string = null) => ({ value, message })
  // for messages of shape { type: 'log', value: number, message: string }
})

Example 4 (using defineMessages, with namespace):

import { defineMessages } from 'js-messages'

const Actions = defineMessages('counter', {
  increment: null,
  // for messages of shape { type: 'counter.increment' }

  decrement: null,
  // for messages of shape { type: 'counter.decrement' }

  resetTo: (value: number) => ({ value })
  // for messages of shape { type: 'counter.resetTo', value: number }

  log: (value: number, message: string = null) => ({ value, message })
  // for messages of shape { type: 'counter.log', value: number, message: string }
})

License

"js-messages" is licensed under LGPLv3.

Project status

"js-messages" is currently in beta status.

About

A small library to simplify the definition and creation of messages (a.k.a. 'actions' in Flux/Redux etc. context)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published