Skip to content

Commit

Permalink
Shadow CSF's ProjectAnnotations with our own.
Browse files Browse the repository at this point in the history
Also clean up sub types
  • Loading branch information
tmeasday committed Nov 1, 2022
1 parent 2c92e6e commit 1655930
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 44 deletions.
4 changes: 2 additions & 2 deletions code/lib/core-client/src/PreviewWeb.mockdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
STORY_THREW_EXCEPTION,
} from '@storybook/core-events';

import type { Store_StoryIndex, Store_TeardownrenderToRoot } from '@storybook/types';
import type { Store_StoryIndex, TeardownRenderToRoot } from '@storybook/types';

export type RenderPhase =
| 'preparing'
Expand Down Expand Up @@ -67,7 +67,7 @@ export const docsRenderer = {
render: jest.fn().mockImplementation((context, parameters, element, cb) => cb()),
unmount: jest.fn(),
};
export const teardownrenderToRoot: jest.Mock<Store_TeardownrenderToRoot> = jest.fn();
export const teardownrenderToRoot: jest.Mock<TeardownRenderToRoot> = jest.fn();
export const projectAnnotations = {
globals: { a: 'b' },
globalTypes: {},
Expand Down
6 changes: 3 additions & 3 deletions code/lib/core-client/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
ArgsStoryFn,
Loadable,
Path,
Store_WebProjectAnnotations,
ProjectAnnotations,
} from '@storybook/types';
import { createChannel } from '@storybook/channel-postmessage';
import { addons } from '@storybook/addons';
Expand All @@ -22,7 +22,7 @@ const removedApi = (name: string) => () => {
};

interface CoreClient_RendererImplementation<TFramework extends AnyFramework> {
decorateStory?: Store_WebProjectAnnotations<TFramework>['applyDecorators'];
decorateStory?: ProjectAnnotations<TFramework>['applyDecorators'];
render?: ArgsStoryFn<TFramework>;
}

Expand All @@ -43,7 +43,7 @@ interface CoreClient_StartReturnValue<TFramework extends AnyFramework> {
}

export function start<TFramework extends AnyFramework>(
renderToRoot: Store_WebProjectAnnotations<TFramework>['renderToRoot'],
renderToRoot: ProjectAnnotations<TFramework>['renderToRoot'],
{ decorateStory, render }: CoreClient_RendererImplementation<TFramework> = {}
): CoreClient_StartReturnValue<TFramework> {
if (globalWindow) {
Expand Down
17 changes: 6 additions & 11 deletions code/lib/preview-web/src/Preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import type {
AnyFramework,
Args,
Globals,
ProjectAnnotations,
Store_ModuleImportFn,
Store_PromiseLike,
Store_renderToRoot,
RenderToRoot,
Store_Story,
Store_StoryIndex,
Store_WebProjectAnnotations,
ProjectAnnotations,
StoryId,
} from '@storybook/types';
import { StoryStore } from '@storybook/store';
Expand Down Expand Up @@ -54,7 +53,7 @@ export class Preview<TFramework extends AnyFramework, TStorybookRoot = HTMLEleme

importFn?: Store_ModuleImportFn;

renderToRoot?: Store_renderToRoot<TFramework, TStorybookRoot>;
renderToRoot?: RenderToRoot<TFramework, TStorybookRoot>;

storyRenders: StoryRender<TFramework, TStorybookRoot>[] = [];

Expand Down Expand Up @@ -84,9 +83,7 @@ export class Preview<TFramework extends AnyFramework, TStorybookRoot = HTMLEleme
// getProjectAnnotations has been run, thus this slightly awkward approach
getStoryIndex?: () => Store_StoryIndex;
importFn: Store_ModuleImportFn;
getProjectAnnotations: () => MaybePromise<
Store_WebProjectAnnotations<TFramework, TStorybookRoot>
>;
getProjectAnnotations: () => MaybePromise<ProjectAnnotations<TFramework, TStorybookRoot>>;
}) {
// We save these two on initialization in case `getProjectAnnotations` errors,
// in which case we may need them later when we recover.
Expand All @@ -111,9 +108,7 @@ export class Preview<TFramework extends AnyFramework, TStorybookRoot = HTMLEleme
}

getProjectAnnotationsOrRenderError(
getProjectAnnotations: () => MaybePromise<
Store_WebProjectAnnotations<TFramework, TStorybookRoot>
>
getProjectAnnotations: () => MaybePromise<ProjectAnnotations<TFramework, TStorybookRoot>>
): Store_PromiseLike<ProjectAnnotations<TFramework>> {
return SynchronousPromise.resolve()
.then(getProjectAnnotations)
Expand Down Expand Up @@ -142,7 +137,7 @@ export class Preview<TFramework extends AnyFramework, TStorybookRoot = HTMLEleme

// If initialization gets as far as project annotations, this function runs.
initializeWithProjectAnnotations(
projectAnnotations: Store_WebProjectAnnotations<TFramework, TStorybookRoot>
projectAnnotations: ProjectAnnotations<TFramework, TStorybookRoot>
) {
this.storyStore.setProjectAnnotations(projectAnnotations);

Expand Down
4 changes: 2 additions & 2 deletions code/lib/preview-web/src/PreviewWeb.mockdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
STORY_THREW_EXCEPTION,
} from '@storybook/core-events';

import type { Store_StoryIndex, Store_TeardownrenderToRoot } from '@storybook/types';
import type { Store_StoryIndex, TeardownRenderToRoot } from '@storybook/types';
import type { RenderPhase } from './render/StoryRender';

export const componentOneExports = {
Expand Down Expand Up @@ -58,7 +58,7 @@ export const docsRenderer = {
render: jest.fn().mockImplementation((context, parameters, element, cb) => cb()),
unmount: jest.fn(),
};
export const teardownrenderToRoot: jest.Mock<Store_TeardownrenderToRoot> = jest.fn();
export const teardownrenderToRoot: jest.Mock<TeardownRenderToRoot> = jest.fn();
export const projectAnnotations = {
globals: { a: 'b' },
globalTypes: {},
Expand Down
5 changes: 2 additions & 3 deletions code/lib/preview-web/src/PreviewWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import type {
AnyFramework,
Args,
Globals,
ProjectAnnotations,
Store_ModuleImportFn,
Store_Selection,
Store_StoryIndex,
Store_StorySpecifier,
Store_WebProjectAnnotations,
ProjectAnnotations,
StoryId,
ViewMode,
} from '@storybook/types';
Expand Down Expand Up @@ -101,7 +100,7 @@ export class PreviewWeb<
}

initializeWithProjectAnnotations(
projectAnnotations: Store_WebProjectAnnotations<TFramework, TStorybookRoot>
projectAnnotations: ProjectAnnotations<TFramework, TStorybookRoot>
) {
return super
.initializeWithProjectAnnotations(projectAnnotations)
Expand Down
2 changes: 1 addition & 1 deletion code/lib/preview-web/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FIXME: breaks builder-vite, remove this in 7.0
export { composeConfigs } from '@storybook/store';
export type { Store_WebProjectAnnotations as WebProjectAnnotations } from '@storybook/types';
export type { ProjectAnnotations as WebProjectAnnotations } from '@storybook/types';

export { Preview } from './Preview';
export { PreviewWeb } from './PreviewWeb';
Expand Down
8 changes: 4 additions & 4 deletions code/lib/preview-web/src/render/StoryRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import global from 'global';
import type {
AnyFramework,
Store_RenderContext,
Store_renderToRoot,
RenderToRoot,
Store_Story,
Store_TeardownrenderToRoot,
TeardownRenderToRoot,
StoryContext,
StoryContextForLoaders,
StoryId,
Expand Down Expand Up @@ -64,14 +64,14 @@ export class StoryRender<TFramework extends AnyFramework, TStorybookRoot = HTMLE

public disableKeyListeners = false;

private teardownRender: Store_TeardownrenderToRoot = () => {};
private teardownRender: TeardownRenderToRoot = () => {};

public torndown = false;

constructor(
public channel: Channel,
public store: StoryStore<TFramework>,
private renderToScreen: Store_renderToRoot<TFramework, TStorybookRoot>,
private renderToScreen: RenderToRoot<TFramework, TStorybookRoot>,
private callbacks: RenderContextCallbacks<TFramework>,
public id: StoryId,
public viewMode: ViewMode,
Expand Down
8 changes: 2 additions & 6 deletions code/lib/store/src/csf/composeConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
AnyFramework,
Store_ModuleExports,
Store_WebProjectAnnotations,
} from '@storybook/types';
import type { AnyFramework, Store_ModuleExports, ProjectAnnotations } from '@storybook/types';

import { combineParameters } from '../parameters';
import { composeStepRunners } from './stepRunners';
Expand Down Expand Up @@ -37,7 +33,7 @@ export function getSingletonField<TFieldType = any>(

export function composeConfigs<TFramework extends AnyFramework>(
moduleExportList: Store_ModuleExports[]
): Store_WebProjectAnnotations<TFramework> {
): ProjectAnnotations<TFramework> {
const allArgTypeEnhancers = getArrayField(moduleExportList, 'argTypesEnhancers');
const stepRunners = getField(moduleExportList, 'runStep');

Expand Down
2 changes: 1 addition & 1 deletion code/lib/types/src/modules/core-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { Store_RenderContext } from './store';
// import { Store_RenderContext, Store_WebProjectAnnotations } from './store';
// import { Store_RenderContext, ProjectAnnotations } from './store';
// import { ArgsStoryFn } from './csf';

export interface CoreClient_PreviewError {
Expand Down
2 changes: 0 additions & 2 deletions code/lib/types/src/modules/csf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import type {
PartialStoryFn,
PlayFunction,
PlayFunctionContext,
ProjectAnnotations,
SBArrayType,
SBEnumType,
SBIntersectionType,
Expand Down Expand Up @@ -87,7 +86,6 @@ export type {
PartialStoryFn,
PlayFunction,
PlayFunctionContext,
ProjectAnnotations,
SBArrayType,
SBEnumType,
SBIntersectionType,
Expand Down
18 changes: 9 additions & 9 deletions code/lib/types/src/modules/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import type { SynchronousPromise } from 'synchronous-promise';
import type { ProjectAnnotations as CsfProjectAnnotations } from '@storybook/csf';

import type { Addon_IndexEntry, Addon_StoryIndexEntry } from './addons';
import type {
AnnotatedStoryFn,
Expand All @@ -13,7 +15,6 @@ import type {
Parameters,
PartialStoryFn,
Path,
ProjectAnnotations,
StoryAnnotations,
StoryContext,
StoryContextForEnhancers,
Expand All @@ -34,21 +35,20 @@ export type Store_ModuleImportFn = (path: Path) => Store_PromiseLike<Store_Modul

type Store_MaybePromise<T> = Promise<T> | T;

export type Store_TeardownrenderToRoot = () => Store_MaybePromise<void>;
// We should consider renaming this to `RenderToRootElement` or similar
export type Store_renderToRoot<TFramework extends AnyFramework, TStorybookRoot = HTMLElement> = (
export type TeardownRenderToRoot = () => Store_MaybePromise<void>;
export type RenderToRoot<TFramework extends AnyFramework, TStorybookRoot = HTMLElement> = (
context: Store_RenderContext<TFramework>,
element: TStorybookRoot
) => Store_MaybePromise<void | Store_TeardownrenderToRoot>;
) => Store_MaybePromise<void | TeardownRenderToRoot>;

export type Store_WebProjectAnnotations<
export type ProjectAnnotations<
TFramework extends AnyFramework,
TStorybookRoot = HTMLElement
> = ProjectAnnotations<TFramework> & {
renderToRoot?: Store_renderToRoot<TFramework, TStorybookRoot>;
> = CsfProjectAnnotations<TFramework> & {
renderToRoot?: RenderToRoot<TFramework, TStorybookRoot>;

/* @deprecated use renderToRoot */
renderToDOM?: Store_renderToRoot<TFramework, TStorybookRoot>;
renderToDOM?: RenderToRoot<TFramework, TStorybookRoot>;
};

export type Store_NormalizedProjectAnnotations<TFramework extends AnyFramework = AnyFramework> =
Expand Down

0 comments on commit 1655930

Please sign in to comment.