-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathchannel.ts
86 lines (81 loc) · 3.29 KB
/
channel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* eslint-disable no-param-reassign */
import { STORIES_COLLAPSE_ALL, STORIES_EXPAND_ALL } from '@storybook/core-events';
import type { Listener } from '@storybook/channels';
import type { API_Provider } from '@storybook/types';
import type { API } from '../index';
import type { ModuleFn } from '../lib/types';
export interface SubAPI {
/**
* Returns the channel object.
* @protected Please do not use, it's for internal use only.
*/
getChannel: () => API_Provider<API>['channel'];
/**
* Adds a listener to the channel for the given event type.
* Returns a function that can be called to remove the listener.
* @param type - The event type to listen for. If using a core event, import it from `@storybook/core-events`.
* @param handler - The callback function to be called when the event is emitted.
* @returns A function that can be called to remove the listener.
*/
on: (type: string, handler: Listener) => () => void;
/**
* Removes a listener from the channel for the given event type.
* @param type - The event type to remove the listener from. If using a core event, import it from `@storybook/core-events`.
* @param handler - The callback function to be removed.
*/
off: (type: string, handler: Listener) => void;
/**
* Emits an event on the channel for the given event type.
* @param type - The event type to emit. If using a core event, import it from `@storybook/core-events`.
* @param args - The arguments to pass to the event listener.
*/
emit: (type: string, ...args: any[]) => void;
/**
* Adds a one-time listener to the channel for the given event type.
* @param type - The event type to listen for. If using a core event, import it from `@storybook/core-events`.
* @param handler - The callback function to be called when the event is emitted.
*/
once: (type: string, handler: Listener) => void;
/**
* Emits an event to collapse all stories in the UI.
* @deprecated Use `emit(STORIES_COLLAPSE_ALL)` instead. This API will be removed in Storybook 8.0.
*/
collapseAll: () => void;
/**
* Emits an event to expand all stories in the UI.
* @deprecated Use `emit(STORIES_EXPAND_ALL)` instead. This API will be removed in Storybook 8.0.
*/
expandAll: () => void;
}
export type SubState = Record<string, never>;
export const init: ModuleFn<SubAPI, SubState> = ({ provider }) => {
const api: SubAPI = {
getChannel: () => provider.channel,
on: (type, handler) => {
provider.channel?.on(type, handler);
return () => provider.channel?.off(type, handler);
},
off: (type, handler) => provider.channel?.off(type, handler),
once: (type, handler) => provider.channel?.once(type, handler),
emit: (type, data, ...args) => {
if (
data?.options?.target &&
data.options.target !== 'storybook-preview-iframe' &&
!data.options.target.startsWith('storybook-ref-')
) {
data.options.target =
data.options.target !== 'storybook_internal'
? `storybook-ref-${data.options.target}`
: 'storybook-preview-iframe';
}
provider.channel?.emit(type, data, ...args);
},
collapseAll: () => {
api.emit(STORIES_COLLAPSE_ALL, {});
},
expandAll: () => {
api.emit(STORIES_EXPAND_ALL);
},
};
return { api, state: {} };
};