Skip to content

Commit

Permalink
Merge pull request #5046 from storybooks/ts-migration/channel-websocket
Browse files Browse the repository at this point in the history
TypeScript migration: @storybook/channel-websocket
  • Loading branch information
ndelangen committed Dec 27, 2018
2 parents 87c6d93 + 3af7223 commit be73549
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 67 deletions.
2 changes: 1 addition & 1 deletion lib/channel-websocket/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
63 changes: 0 additions & 63 deletions lib/channel-websocket/src/index.js

This file was deleted.

78 changes: 78 additions & 0 deletions lib/channel-websocket/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
1 change: 1 addition & 0 deletions lib/channel-websocket/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'global';
9 changes: 9 additions & 0 deletions lib/channel-websocket/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src"
},
"include": [
"src/**/*.ts"
]
}
3 changes: 0 additions & 3 deletions scripts/build-pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit be73549

Please sign in to comment.