-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5046 from storybooks/ts-migration/channel-websocket
TypeScript migration: @storybook/channel-websocket
- Loading branch information
Showing
6 changed files
with
89 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
declare module 'global'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src" | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters