1 | # Storybook Channel
|
2 |
|
3 | Storybook Channel is similar to an EventEmitter.
|
4 | Channels are used with Storybook implementations to send/receive events between the Storybook Manager and the Storybook Renderer.
|
5 |
|
6 | ```js
|
7 | class Channel {
|
8 | addListener(type, listener) {}
|
9 | addPeerListener(type, listener) {} // ignore events from itself
|
10 | emit(type, ...args) {}
|
11 | eventNames() {}
|
12 | listenerCount(type) {}
|
13 | listeners(type) {}
|
14 | on(type, listener) {}
|
15 | once(type, listener) {}
|
16 | prependListener(type, listener) {}
|
17 | prependOnceListener(type, listener) {}
|
18 | removeAllListeners(type) {}
|
19 | removeListener(type, listener) {}
|
20 | }
|
21 | ```
|
22 |
|
23 | The channel takes a Transport object as a parameter which will be used to send/receive messages. The transport object should implement this interface.
|
24 |
|
25 | ```js
|
26 | class Transport {
|
27 | send(event) {}
|
28 | setHandler(handler) {}
|
29 | }
|
30 | ```
|
31 |
|
32 | Currently, channels are baked into storybook implementations and therefore this module is not designed to be used directly by addon developers. When developing addons, use the `getChannel` method exported by `@storybook/addons` module. For this to work, Storybook implementations should use the `setChannel` method before loading addons.
|
33 |
|
34 | ```js
|
35 | import { addons } from '@storybook/addons';
|
36 |
|
37 | const channel = addons.getChannel();
|
38 | ```
|
39 |
|
40 | * * *
|
41 |
|
42 | For more information visit: [storybook.js.org](https://storybook.js.org)
|