Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript migration: @storybook/channel-websocket #5046

Merged
merged 3 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"