Skip to content

Commit

Permalink
Merge pull request #9867 from storybookjs/websocket-cyclic-support
Browse files Browse the repository at this point in the history
Core: Use telejson for websockets channel
  • Loading branch information
shilman committed Feb 16, 2020
2 parents 71aa906 + f985d67 commit ab9c9cb
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 340 deletions.
2 changes: 1 addition & 1 deletion lib/channel-websocket/package.json
Expand Up @@ -30,7 +30,7 @@
"@storybook/channels": "6.0.0-alpha.13",
"core-js": "^3.0.1",
"global": "^4.3.2",
"json-fn": "^1.1.1"
"telejson": "^3.2.0"
},
"publishConfig": {
"access": "public"
Expand Down
8 changes: 4 additions & 4 deletions lib/channel-websocket/src/index.ts
@@ -1,6 +1,6 @@
import { WebSocket } from 'global';
import JSON from 'json-fn';
import { Channel, ChannelHandler } from '@storybook/channels';
import { isJSON, parse, stringify } from 'telejson';

type OnError = (message: Event) => void;

Expand Down Expand Up @@ -45,7 +45,7 @@ export class WebsocketTransport {
}

private sendNow(event: any) {
const data = JSON.stringify(event);
const data = stringify(event, { maxDepth: 15, allowFunction: true });
this.socket.send(data);
}

Expand All @@ -61,8 +61,8 @@ export class WebsocketTransport {
this.isReady = true;
this.flush();
};
this.socket.onmessage = e => {
const event = JSON.parse(e.data);
this.socket.onmessage = ({ data }) => {
const event = typeof data === 'string' && isJSON(data) ? parse(data) : data;
this.handler(event);
};
this.socket.onerror = e => {
Expand Down

0 comments on commit ab9c9cb

Please sign in to comment.