The Events feature allows decoupled parts of the CMS to:
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
cms.events.dispatch({
type: 'something-happened',
})
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.
cms.events.subscribe('*', event => {
console.log(event)
})
cms.events.subscribe("plugins:add:form", (event) => {
console.log(`Added a Form called "${event.plugin.name}"`
})
cms.events.subscribe('plugins:*:form', event => {
console.log(`Something happened to the form plugins`)
})
cms.events.subscribe('plugins', event => {
console.log(`Something happened to the plugins`)
})
Note that the string plugins
is equivalent to plugins:*
or plugins:*:*
.
Type | Description |
---|---|
cms:enabled | The CMS has been enabled. |
cms:disabled | The CMS has been disabled |
sidebar:opened | The Sidebar has been opened |
sidebar:closed | The 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. |
Event Name | Decription |
---|---|
github:error | An error has occurred when making requests to the GitHub API. |
github:branch:checkout | The current branch has been switched. |
github:branch:create | A new branch has been created. |
Event Name | Decription |
---|---|
git:commit | A commit has been attempted. |
Below is an example of how you might subscribe to the git:commit
event in your App. The event passed to the callback function is a Fetch Response; you can parse the status of the commit from this to trigger various alerts or functions.
Example
React.useEffect(() => {
cms.events.subscribe("git:commit", function handleCommitAlerts(event) {
if (!event.response.ok) {
cms.alerts.error("Something went wrong! Changes weren't saved")
} else {
cms.alerts.info("Content saved successfully!")
}
})
}, [])