-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathmain.ts
78 lines (60 loc) · 1.86 KB
/
main.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
import { global } from '@storybook/global';
import type { Channel } from '@storybook/channels';
import { mockChannel } from './storybook-channel-mock';
export class AddonStore {
constructor() {
this.promise = new Promise((res) => {
this.resolve = () => res(this.getChannel());
}) as Promise<Channel>;
}
private channel: Channel | undefined;
/**
* @deprecated will be removed in 8.0, please use channel instead
*/
private serverChannel: Channel | undefined;
private promise: any;
private resolve: any;
getChannel = (): Channel => {
// this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), set a mock instead.
if (!this.channel) {
const channel = mockChannel();
this.setChannel(channel);
return channel;
}
return this.channel;
};
/**
* @deprecated will be removed in 8.0, please use getChannel instead
*/
getServerChannel = (): Channel => {
if (!this.serverChannel) {
throw new Error('Accessing non-existent serverChannel');
}
return this.serverChannel;
};
ready = (): Promise<Channel> => this.promise;
hasChannel = (): boolean => !!this.channel;
/**
* @deprecated will be removed in 8.0, please use the normal channel instead
*/
hasServerChannel = (): boolean => !!this.serverChannel;
setChannel = (channel: Channel): void => {
this.channel = channel;
this.resolve();
};
/**
* @deprecated will be removed in 8.0, please use the normal channel instead
*/
setServerChannel = (channel: Channel): void => {
this.serverChannel = channel;
};
}
// Enforce addons store to be a singleton
const KEY = '__STORYBOOK_ADDONS_PREVIEW';
function getAddonsStore(): AddonStore {
if (!global[KEY]) {
global[KEY] = new AddonStore();
}
return global[KEY];
}
export const addons = getAddonsStore();