add plugin example for intercept
This commit is contained in:
parent
4d336e4a26
commit
a0438e1d45
1 changed files with 32 additions and 0 deletions
|
@ -43,6 +43,8 @@ Likewise, adding a style to `app:stylesheets` is done as follows:
|
|||
|
||||
## Examples
|
||||
|
||||
### Print the selected channel
|
||||
|
||||
Plugin that prints the name of the currently selected channel each time it changes:
|
||||
```javascript
|
||||
// We wrap our plugin in a function that runs immediately so that we don't pollute the scope.
|
||||
|
@ -93,3 +95,33 @@ Plugin that prints the name of the currently selected channel each time it chang
|
|||
pluginLoaders.push(() => plugin); // window.__wafflePluginLoaders is an array of functions which return a plugin
|
||||
})();
|
||||
```
|
||||
|
||||
### Message signature
|
||||
|
||||
Plugin that adds a signature after every message. This demonstrates the store.pipe() method and leaves some comments out for the sake of showcasing more ways to structure your plugin.
|
||||
Note: please don't use this in actual chats.
|
||||
|
||||
```javascript
|
||||
(window.__wafflePluginLoaders || __wafflePluginLoaders || []).push(() => ({
|
||||
name: "messageSignature",
|
||||
description: "Adds a signature to every message before sending",
|
||||
state: {
|
||||
cleanup: []
|
||||
},
|
||||
options: {
|
||||
signature: "\n\n-Waffle user"
|
||||
},
|
||||
main(waffle) {
|
||||
// store.pipe() allows us to add a function to "intercept" the store value before it reaches all of its subscribers.
|
||||
// As with store.subscribe(), store.pipe() returns a function that removes the pipe once we're done with it.
|
||||
this.state.cleanup.push(waffle.stores.sendMessageAction.pipe(message => {
|
||||
message.content = `${message.content}${this.options.signature}`;
|
||||
return message;
|
||||
}));
|
||||
},
|
||||
dispose(_waffle) {
|
||||
this.state.cleanup.forEach(f => f());
|
||||
this.state.cleanup.length = 0;
|
||||
}
|
||||
}));
|
||||
```
|
Loading…
Reference in a new issue