diff --git a/lib/channel-websocket/package.json b/lib/channel-websocket/package.json index 0e64908a2b61..c4f687171dbf 100644 --- a/lib/channel-websocket/package.json +++ b/lib/channel-websocket/package.json @@ -15,7 +15,7 @@ }, "license": "MIT", "main": "dist/index.js", - "jsnext:main": "src/index.js", + "types": "dist/index.d.ts", "scripts": { "prepare": "node ../../scripts/prepare.js" }, diff --git a/lib/channel-websocket/src/index.js b/lib/channel-websocket/src/index.js deleted file mode 100644 index 4c2907ea9963..000000000000 --- a/lib/channel-websocket/src/index.js +++ /dev/null @@ -1,63 +0,0 @@ -/* eslint-disable no-underscore-dangle */ - -import { WebSocket } from 'global'; -import Channel from '@storybook/channels'; - -export class WebsocketTransport { - constructor({ url, onError }) { - this._socket = null; - this._buffer = []; - this._handler = null; - this._isReady = false; - this._connect(url, onError); - } - - setHandler(handler) { - this._handler = handler; - } - - send(event) { - if (!this._isReady) { - this._sendLater(event); - } else { - this._sendNow(event); - } - } - - _sendLater(event) { - this._buffer.push(event); - } - - _sendNow(event) { - const data = JSON.stringify(event); - this._socket.send(data); - } - - _flush() { - const buffer = this._buffer; - this._buffer = []; - buffer.forEach(event => this.send(event)); - } - - _connect(url, onError) { - this._socket = new WebSocket(url); - this._socket.onopen = () => { - this._isReady = true; - this._flush(); - }; - this._socket.onmessage = e => { - const event = JSON.parse(e.data); - this._handler(event); - }; - this._socket.onerror = e => { - if (onError) { - onError(e); - } - }; - } -} - -export default function createChannel({ url, async, onError }) { - const transport = new WebsocketTransport({ url, onError }); - return new Channel({ transport, async }); -} diff --git a/lib/channel-websocket/src/index.ts b/lib/channel-websocket/src/index.ts new file mode 100644 index 000000000000..9a4121b7ad75 --- /dev/null +++ b/lib/channel-websocket/src/index.ts @@ -0,0 +1,78 @@ +import { WebSocket } from 'global'; +import { Channel, ChannelHandler } from '@storybook/channels'; + +type OnError = (message: Event) => void; + +interface WebsocketTransportArgs { + url: string; + onError: OnError; +} + +interface CreateChannelArgs { + url: string; + async: boolean; + onError: OnError; +} + +export class WebsocketTransport { + private socket: WebSocket; + private handler: ChannelHandler; + private buffer: string[] = []; + private isReady = false; + + constructor({ url, onError }: WebsocketTransportArgs) { + this.connect( + url, + onError + ); + } + + setHandler(handler: ChannelHandler) { + this.handler = handler; + } + + send(event: any) { + if (!this.isReady) { + this.sendLater(event); + } else { + this.sendNow(event); + } + } + + private sendLater(event: any) { + this.buffer.push(event); + } + + private sendNow(event: any) { + const data = JSON.stringify(event); + this.socket.send(data); + } + + private flush() { + const buffer = this.buffer; + 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 = e => { + const event = JSON.parse(e.data); + this.handler(event); + }; + this.socket.onerror = e => { + if (onError) { + onError(e); + } + }; + } +} + +export default function createChannel({ url, async, onError }: CreateChannelArgs) { + const transport = new WebsocketTransport({ url, onError }); + return new Channel({ transport, async }); +} diff --git a/lib/channel-websocket/src/typings.d.ts b/lib/channel-websocket/src/typings.d.ts new file mode 100644 index 000000000000..2f4eb9cf4fd9 --- /dev/null +++ b/lib/channel-websocket/src/typings.d.ts @@ -0,0 +1 @@ +declare module 'global'; diff --git a/lib/channel-websocket/tsconfig.json b/lib/channel-websocket/tsconfig.json new file mode 100644 index 000000000000..11744d2a8c36 --- /dev/null +++ b/lib/channel-websocket/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src" + }, + "include": [ + "src/**/*.ts" + ] +} diff --git a/scripts/build-pack.sh b/scripts/build-pack.sh index e33e7ae4c089..f77e609c161a 100755 --- a/scripts/build-pack.sh +++ b/scripts/build-pack.sh @@ -5,7 +5,4 @@ PACK_DIR=$1 FILE=$(npm pack | tail -n 1) -echo $PACK_DIR -echo $FILE - mv "$FILE" "$PACK_DIR/${FILE%-[0-9]*\.[0-9]*\.[0-9]*\.tgz}.tgz"