Events


The Events feature allows decoupled parts of the CMS to:

  1. Notify the rest of the system that something has happened, and
  2. React to things that happen elsewhere in the system.

Interface

interface Events {
  dispatch(event: Event): void
  subscribe(eventType: string, listener: Listener): Unsubscribe
}

interface Event {
  type: string
  // Any other data may be added to the event.
  [key: string]: any
}

type Listener = (event: Event) => void

type Unsubscribe = () => void

Usage

Dispatching Events

cms.events.dispatch({
  type: 'something-happened',
})

Subscribing to Events

The cms.events.subscribe function can be used to start listening for certain events.

cms.events.subscribe(EVENT_NAME, event => {
  // ...
})

The EVENT_NAME is a string that matches a pattern for the event name.

Checkout the tests for specific examples of how the matching happens.

Log all Events

cms.events.subscribe('*', event => {
  console.log(event)
})

Log when a Form Plugin is added

cms.events.subscribe("plugins:add:form", (event) => {
  console.log(`Added a Form called "${event.plugin.name}"`
})

Log all Form Events

cms.events.subscribe('plugins:*:form', event => {
  console.log(`Something happened to the form plugins`)
})

Log all Plugin Events

cms.events.subscribe('plugins', event => {
  console.log(`Something happened to the plugins`)
})

Note that the string pluginsis equivalent to plugins:* or plugins:*:*.

Existing Events

tinacms

TypeDescription
cms:enabledThe CMS has been enabled.
cms:disabledThe CMS has been disabled
sidebar:openedThe Sidebar has been opened
sidebar:closedThe Sidebar has been closed.
plugin:add:{__type}A Plugin of a given __type has been added.
plugin:remove:{__type}A Plugin of a given __type has been removed.

react-tinacms-github

Event NameDecription
github:errorAn error has occurred when making requests to the GitHub API.
github:branch:checkoutThe current branch has been switched.