-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
index.ts
44 lines (33 loc) · 1.54 KB
/
index.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
/* eslint-disable @typescript-eslint/triple-slash-reference */
/// <reference path="./typings.d.ts" />
import { global } from '@storybook/global';
import { Channel } from './main';
import { PostMessageTransport } from './postmessage';
import type { ChannelTransport, Config } from './types';
import { WebsocketTransport } from './websocket';
const { CONFIG_TYPE } = global;
export * from './main';
export default Channel;
export { PostMessageTransport } from './postmessage';
export { WebsocketTransport } from './websocket';
type Options = Config & {
extraTransports?: ChannelTransport[];
};
/**
* Creates a new browser channel instance.
* @param {Options} options - The options object.
* @param {Page} options.page - page identifier.
* @param {ChannelTransport[]} [options.extraTransports=[]] - An optional array of extra channel transports.
* @returns {Channel} - The new channel instance.
*/
export function createBrowserChannel({ page, extraTransports = [] }: Options): Channel {
const transports: ChannelTransport[] = [new PostMessageTransport({ page }), ...extraTransports];
if (CONFIG_TYPE === 'DEVELOPMENT') {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;
transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {}, page }));
}
return new Channel({ transports });
}
export type { Listener, ChannelEvent, ChannelTransport, ChannelHandler } from './types';