diff --git a/code/lib/channel-websocket/package.json b/code/lib/channel-websocket/package.json index ce1303f7558d..2cae8f9a48a2 100644 --- a/code/lib/channel-websocket/package.json +++ b/code/lib/channel-websocket/package.json @@ -46,7 +46,8 @@ "@storybook/channels": "7.1.0-alpha.12", "@storybook/client-logger": "7.1.0-alpha.12", "@storybook/global": "^5.0.0", - "telejson": "^7.0.3" + "telejson": "^7.0.3", + "tiny-invariant": "^1.3.1" }, "devDependencies": { "typescript": "~4.9.3" diff --git a/code/lib/channel-websocket/src/index.ts b/code/lib/channel-websocket/src/index.ts index 7866d50a9d65..3738fea4eaa0 100644 --- a/code/lib/channel-websocket/src/index.ts +++ b/code/lib/channel-websocket/src/index.ts @@ -3,6 +3,7 @@ import { Channel } from '@storybook/channels'; import type { ChannelHandler } from '@storybook/channels'; import { logger } from '@storybook/client-logger'; import { isJSON, parse, stringify } from 'telejson'; +import invariant from 'tiny-invariant'; const { WebSocket } = global; @@ -22,14 +23,28 @@ interface CreateChannelArgs { export class WebsocketTransport { private socket: WebSocket; - private handler: ChannelHandler; + private handler?: ChannelHandler; private buffer: string[] = []; private isReady = false; constructor({ url, onError }: WebsocketTransportArgs) { - this.connect(url, onError); + this.socket = new WebSocket(url); + this.socket.onopen = () => { + this.isReady = true; + this.flush(); + }; + this.socket.onmessage = ({ data }) => { + const event = typeof data === 'string' && isJSON(data) ? parse(data) : data; + invariant(this.handler, 'WebsocketTransport handler should be set'); + this.handler(event); + }; + this.socket.onerror = (e) => { + if (onError) { + onError(e); + } + }; } setHandler(handler: ChannelHandler) { @@ -58,23 +73,6 @@ export class WebsocketTransport { this.buffer = []; buffer.forEach((event) => this.send(event)); } - - private connect(url: string, onError: OnError) { - this.socket = new WebSocket(url); - this.socket.onopen = () => { - this.isReady = true; - this.flush(); - }; - this.socket.onmessage = ({ data }) => { - const event = typeof data === 'string' && isJSON(data) ? parse(data) : data; - this.handler(event); - }; - this.socket.onerror = (e) => { - if (onError) { - onError(e); - } - }; - } } export function createChannel({ diff --git a/code/lib/channel-websocket/tsconfig.json b/code/lib/channel-websocket/tsconfig.json index 4c6f20a1be4d..b5a2f9a70918 100644 --- a/code/lib/channel-websocket/tsconfig.json +++ b/code/lib/channel-websocket/tsconfig.json @@ -2,6 +2,6 @@ "extends": "../../tsconfig.json", "include": ["src/**/*"], "compilerOptions": { - "strict": false + "strict": true } } diff --git a/code/yarn.lock b/code/yarn.lock index 44472e7a21f2..ef2ed24bd797 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5683,6 +5683,7 @@ __metadata: "@storybook/client-logger": 7.1.0-alpha.12 "@storybook/global": ^5.0.0 telejson: ^7.0.3 + tiny-invariant: ^1.3.1 typescript: ~4.9.3 languageName: unknown linkType: soft