Skip to content

Commit

Permalink
move code downstream so the channel packages do not unify
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Jun 12, 2023
1 parent 9f97490 commit dd2d339
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 24 deletions.
1 change: 1 addition & 0 deletions code/builders/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@storybook/channel-postmessage": "7.1.0-alpha.30",
"@storybook/channel-websocket": "7.1.0-alpha.30",
"@storybook/client-logger": "7.1.0-alpha.30",
"@storybook/core-common": "7.1.0-alpha.30",
"@storybook/csf-plugin": "7.1.0-alpha.30",
Expand Down
18 changes: 16 additions & 2 deletions code/builders/builder-vite/src/codegen-set-addon-channel.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
export async function generateAddonSetupCode() {
return `
import { createChannel as createPostMessageChannel } from '@storybook/channel-postmessage';
import { Channel } from '@storybook/channels';
import { PostmsgTransport } from '@storybook/channel-postmessage';
import { WebsocketTransport } from '@storybook/channel-websocket';
import { addons } from '@storybook/preview-api';
const channel = createPostMessageChannel({ page: 'preview' });
const page = 'preview';
const transports = [new PostmsgTransport({ page })];
if (CONFIG_TYPE === 'DEVELOPMENT') {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = \`$\{protocol}://$\{hostname}:$\{port}/storybook-server-channel\`;
transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {} }));
}
const channel = new Channel({ transports });
addons.setChannel(channel);
window.__STORYBOOK_ADDONS_CHANNEL__ = channel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { global } from '@storybook/global';

import { ClientApi, PreviewWeb, addons, composeConfigs } from '@storybook/preview-api';
import { createChannel as createPostMessageChannel } from '@storybook/channel-postmessage';
import { Channel } from '@storybook/channels';
import { PostmsgTransport } from '@storybook/channel-postmessage';
import { WebsocketTransport } from '@storybook/channel-websocket';

import { importFn } from './{{storiesFilename}}';

const getProjectAnnotations = () =>
composeConfigs([{{#each previewAnnotations}}require('{{this}}'),{{/each}}]);

const channel = createPostMessageChannel({ page: 'preview' });
const page = 'preview';
const transports = [new PostmsgTransport({ page })];

if (global.CONFIG_TYPE === 'DEVELOPMENT') {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;

transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {} }));
}

const channel = new Channel({ transports });
addons.setChannel(channel);

if (global.CONFIG_TYPE === 'DEVELOPMENT'){
Expand Down
13 changes: 12 additions & 1 deletion code/lib/channel-postmessage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,13 @@ const getEventSourceUrl = (event: MessageEvent) => {

/**
* Creates a channel which communicates with an iframe or child window.
*
* @deprecated This function is deprecated. Use the `Channel` class instead.
* @example
* const channel = new Channel({ transports: new PostmsgTransport({ page }) });
*
* @param {Config} config - The configuration object.
* @returns {Channel} The channel instance.
*/
export function createChannel({ page }: Config): Channel {
const transports: ChannelTransport[] = [new PostmsgTransport({ page })];
Expand All @@ -303,5 +310,9 @@ export function createChannel({ page }: Config): Channel {
return new Channel({ transports });
}

// backwards compat with builder-vite
/**
* @deprecated This function is deprecated. Use the `Channel` class instead.
* @example
* const channel = new Channel({ transports: new PostmsgTransport({ page }) });
*/
export default createChannel;
18 changes: 5 additions & 13 deletions code/lib/channel-websocket/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,15 @@ export function createChannel({
async = false,
onError = (err) => logger.warn(err),
}: CreateChannelArgs) {
const transports: ChannelTransport[] = [];

if (url) {
transports.push(new WebsocketTransport({ url, onError }));
}

const isUrlServerChannel = !!url?.includes('storybook-server-channel');

if (CONFIG_TYPE === 'DEVELOPMENT' && isUrlServerChannel === false) {
let channelUrl = url;
if (!channelUrl) {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;

transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {} }));
channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;
}

return new Channel({ transports, async });
const transport = new WebsocketTransport({ url: channelUrl, onError });
return new Channel({ transport, async });
}

// backwards compat with builder-vite
Expand Down
1 change: 1 addition & 0 deletions code/lib/preview-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"dependencies": {
"@storybook/channel-postmessage": "7.1.0-alpha.30",
"@storybook/channel-websocket": "7.1.0-alpha.30",
"@storybook/channels": "7.1.0-alpha.30",
"@storybook/client-logger": "7.1.0-alpha.30",
"@storybook/core-events": "7.1.0-alpha.30",
Expand Down
20 changes: 17 additions & 3 deletions code/lib/preview-api/src/modules/core-client/start.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-underscore-dangle, @typescript-eslint/naming-convention */
import { global } from '@storybook/global';
import type { Renderer, ArgsStoryFn, Path, ProjectAnnotations } from '@storybook/types';
import { createChannel } from '@storybook/channel-postmessage';
import { PostmsgTransport } from '@storybook/channel-postmessage';
import { WebsocketTransport } from '@storybook/channel-websocket';
import type { ChannelTransport } from '@storybook/channels';
import { Channel } from '@storybook/channels';
import { FORCE_RE_RENDER } from '@storybook/core-events';
import { addons } from '../../addons';
import { PreviewWeb } from '../../preview-web';
Expand All @@ -10,7 +13,7 @@ import { ClientApi } from '../../client-api';
import { executeLoadableForChanges } from './executeLoadable';
import type { Loadable } from './executeLoadable';

const { FEATURES } = global;
const { FEATURES, CONFIG_TYPE } = global;

const removedApi = (name: string) => () => {
throw new Error(`@storybook/client-api:${name} was removed in storyStoreV7.`);
Expand Down Expand Up @@ -94,7 +97,18 @@ export function start<TRenderer extends Renderer>(
};
}

const channel = createChannel({ page: 'preview' });
const page = 'preview';
const transports: ChannelTransport[] = [new PostmsgTransport({ page })];

if (CONFIG_TYPE === 'DEVELOPMENT') {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;

transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {} }));
}

const channel = new Channel({ transports });
addons.setChannel(channel);

const clientApi = global?.__STORYBOOK_CLIENT_API__ || new ClientApi<TRenderer>();
Expand Down
1 change: 1 addition & 0 deletions code/lib/preview-api/src/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare var __STORYBOOK_PREVIEW__: import('./modules/preview-web/PreviewWeb').Pr
declare var __STORYBOOK_STORY_STORE__: any;
declare var STORYBOOK_HOOKS_CONTEXT: any;
declare var LOGLEVEL: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'silent' | undefined;
declare var CONFIG_TYPE: 'DEVELOPMENT' | 'PRODUCTION';

declare module 'ansi-to-html';
declare class AnsiToHtml {
Expand Down
1 change: 1 addition & 0 deletions code/ui/manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@storybook/addons": "7.1.0-alpha.30",
"@storybook/api": "7.1.0-alpha.30",
"@storybook/channel-postmessage": "7.1.0-alpha.30",
"@storybook/channel-websocket": "7.1.0-alpha.30",
"@storybook/channels": "7.1.0-alpha.30",
"@storybook/client-logger": "7.1.0-alpha.30",
"@storybook/components": "7.1.0-alpha.30",
Expand Down
19 changes: 16 additions & 3 deletions code/ui/manager/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { global } from '@storybook/global';

import type { Channel } from '@storybook/channels';
import { Channel } from '@storybook/channels';
import type { ChannelTransport } from '@storybook/channels';
import type { AddonStore } from '@storybook/manager-api';
import { addons } from '@storybook/manager-api';
import type { Addon_Types, Addon_Config } from '@storybook/types';
import * as postMessage from '@storybook/channel-postmessage';
import { PostmsgTransport } from '@storybook/channel-postmessage';
import { CHANNEL_CREATED } from '@storybook/core-events';
import { WebsocketTransport } from '@storybook/channel-websocket';
import Provider from './provider';
import { renderStorybookUI } from './index';

Expand All @@ -27,7 +29,18 @@ class ReactProvider extends Provider {
constructor() {
super();

const channel = postMessage.createChannel({ page: 'manager' });
const page = 'manager';
const transports: ChannelTransport[] = [new PostmsgTransport({ page })];

if (CONFIG_TYPE === 'DEVELOPMENT') {
const protocol = window.location.protocol === 'http:' ? 'ws' : 'wss';
const { hostname, port } = window.location;
const channelUrl = `${protocol}://${hostname}:${port}/storybook-server-channel`;

transports.push(new WebsocketTransport({ url: channelUrl, onError: () => {} }));
}

const channel = new Channel({ transports });

addons.setChannel(channel);

Expand Down
3 changes: 3 additions & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5840,6 +5840,7 @@ __metadata:
resolution: "@storybook/builder-vite@workspace:builders/builder-vite"
dependencies:
"@storybook/channel-postmessage": 7.1.0-alpha.30
"@storybook/channel-websocket": 7.1.0-alpha.30
"@storybook/client-logger": 7.1.0-alpha.30
"@storybook/core-common": 7.1.0-alpha.30
"@storybook/csf-plugin": 7.1.0-alpha.30
Expand Down Expand Up @@ -6557,6 +6558,7 @@ __metadata:
"@storybook/addons": 7.1.0-alpha.30
"@storybook/api": 7.1.0-alpha.30
"@storybook/channel-postmessage": 7.1.0-alpha.30
"@storybook/channel-websocket": 7.1.0-alpha.30
"@storybook/channels": 7.1.0-alpha.30
"@storybook/client-logger": 7.1.0-alpha.30
"@storybook/components": 7.1.0-alpha.30
Expand Down Expand Up @@ -6934,6 +6936,7 @@ __metadata:
dependencies:
"@jest/globals": ^29.5.0
"@storybook/channel-postmessage": 7.1.0-alpha.30
"@storybook/channel-websocket": 7.1.0-alpha.30
"@storybook/channels": 7.1.0-alpha.30
"@storybook/client-logger": 7.1.0-alpha.30
"@storybook/core-common": 7.1.0-alpha.30
Expand Down

0 comments on commit dd2d339

Please sign in to comment.