add plugin example for intercept

This commit is contained in:
hippoz 2022-09-28 17:03:56 +03:00
parent 4d336e4a26
commit a0438e1d45
Signed by: hippoz
GPG key ID: 7C52899193467641

View file

@ -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;
}
}));
```