Skip to content

Commit

Permalink
Merge pull request #22364 from EDuToit/etoit/strict-ts-channel-websocket
Browse files Browse the repository at this point in the history
Build: Migrate @storybook/channel-websocket to strict TS
  • Loading branch information
kasperpeulen authored May 4, 2023
2 parents e9afed3 + 896d121 commit 67a8d08
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
3 changes: 2 additions & 1 deletion code/lib/channel-websocket/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 17 additions & 19 deletions code/lib/channel-websocket/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion code/lib/channel-websocket/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": "../../tsconfig.json",
"include": ["src/**/*"],
"compilerOptions": {
"strict": false
"strict": true
}
}
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5684,6 +5684,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
Expand Down

0 comments on commit 67a8d08

Please sign in to comment.