storybook 9.0.0-alpha.1 → 9.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -29
- package/assets/browser/favicon.svg +1 -0
- package/assets/browser/nunito-sans-bold-italic.woff2 +0 -0
- package/assets/browser/nunito-sans-bold.woff2 +0 -0
- package/assets/browser/nunito-sans-italic.woff2 +0 -0
- package/assets/browser/nunito-sans-regular.woff2 +0 -0
- package/assets/docs/message-reference.png +0 -0
- package/assets/server/addon.tsconfig.json +6 -0
- package/assets/server/base-preview-body.html +119 -0
- package/assets/server/base-preview-head.html +451 -0
- package/assets/server/template.ejs +87 -0
- package/bin/index.cjs +1 -1
- package/dist/babel/index.cjs +54034 -0
- package/dist/babel/index.d.ts +7488 -0
- package/dist/babel/index.js +54044 -0
- package/dist/bin/index.cjs +123 -0
- package/dist/bin/index.js +117 -0
- package/dist/builder-manager/index.cjs +2167 -0
- package/dist/builder-manager/index.d.ts +19 -0
- package/dist/builder-manager/index.js +2189 -0
- package/dist/channels/index.cjs +1936 -0
- package/dist/channels/index.d.ts +108 -0
- package/dist/channels/index.js +1883 -0
- package/dist/cli/bin/index.cjs +2687 -0
- package/dist/cli/bin/index.d.ts +2 -0
- package/dist/cli/bin/index.js +2707 -0
- package/dist/cli/index.cjs +17327 -0
- package/dist/cli/index.d.ts +209 -0
- package/dist/cli/index.js +17349 -0
- package/dist/client-logger/index.cjs +112 -0
- package/dist/client-logger/index.d.ts +29 -0
- package/dist/client-logger/index.js +70 -0
- package/dist/common/index.cjs +20560 -0
- package/dist/common/index.d.ts +954 -0
- package/dist/common/index.js +20645 -0
- package/dist/components/index.cjs +30056 -0
- package/dist/components/index.d.ts +1423 -0
- package/dist/components/index.js +25416 -0
- package/dist/core-events/index.cjs +167 -0
- package/dist/core-events/index.d.ts +313 -0
- package/dist/core-events/index.js +149 -0
- package/dist/core-server/index.cjs +37870 -0
- package/dist/core-server/index.d.ts +462 -0
- package/dist/core-server/index.js +37964 -0
- package/dist/core-server/presets/common-manager.js +28 -0
- package/dist/core-server/presets/common-override-preset.cjs +4824 -0
- package/dist/core-server/presets/common-override-preset.js +4829 -0
- package/dist/core-server/presets/common-preset.cjs +10228 -0
- package/dist/core-server/presets/common-preset.js +10306 -0
- package/dist/csf/index.cjs +223 -0
- package/dist/csf/index.d.ts +752 -0
- package/dist/csf/index.js +216 -0
- package/dist/csf-tools/index.cjs +1422 -0
- package/dist/csf-tools/index.d.ts +250 -0
- package/dist/csf-tools/index.js +1439 -0
- package/dist/docs-tools/index.cjs +923 -0
- package/dist/docs-tools/index.d.ts +162 -0
- package/dist/docs-tools/index.js +909 -0
- package/dist/index.cjs +17 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -7
- package/dist/instrumenter/index.cjs +3273 -0
- package/dist/instrumenter/index.d.ts +104 -0
- package/dist/instrumenter/index.js +4800 -0
- package/dist/manager/globals-module-info.cjs +666 -0
- package/dist/manager/globals-module-info.d.ts +36 -0
- package/dist/manager/globals-module-info.js +653 -0
- package/dist/manager/globals-runtime.js +41565 -0
- package/dist/manager/globals.cjs +43 -0
- package/dist/manager/globals.d.ts +21 -0
- package/dist/manager/globals.js +30 -0
- package/dist/manager/runtime.js +12043 -0
- package/dist/manager-api/index.cjs +10770 -0
- package/dist/manager-api/index.d.ts +1211 -0
- package/dist/manager-api/index.js +4545 -0
- package/dist/manager-errors.d.ts +65 -0
- package/dist/manager-errors.js +83 -0
- package/dist/node-logger/index.cjs +1642 -0
- package/dist/node-logger/index.d.ts +117 -0
- package/dist/node-logger/index.js +1652 -0
- package/dist/preview/globals.cjs +34 -0
- package/dist/preview/globals.d.ts +12 -0
- package/dist/preview/globals.js +21 -0
- package/dist/preview/runtime.js +7721 -0
- package/dist/preview-api/index.cjs +5656 -0
- package/dist/preview-api/index.d.ts +1356 -0
- package/dist/preview-api/index.js +5663 -0
- package/dist/preview-errors.cjs +486 -0
- package/dist/preview-errors.d.ts +188 -0
- package/dist/preview-errors.js +433 -0
- package/dist/router/index.cjs +3320 -0
- package/dist/router/index.d.ts +385 -0
- package/dist/router/index.js +1849 -0
- package/dist/server-errors.cjs +710 -0
- package/dist/server-errors.d.ts +272 -0
- package/dist/server-errors.js +711 -0
- package/dist/telemetry/index.cjs +3683 -0
- package/dist/telemetry/index.d.ts +112 -0
- package/dist/telemetry/index.js +3720 -0
- package/dist/test/index.cjs +34915 -0
- package/dist/test/index.d.ts +186 -0
- package/dist/test/index.js +32798 -0
- package/dist/theming/create.cjs +2422 -0
- package/dist/theming/create.d.ts +50 -0
- package/dist/theming/create.js +990 -0
- package/dist/theming/index.cjs +4832 -0
- package/dist/theming/index.d.ts +11939 -0
- package/dist/theming/index.js +3253 -0
- package/dist/types/index.cjs +27 -0
- package/dist/types/index.d.ts +2541 -0
- package/dist/types/index.js +7 -0
- package/package.json +329 -212
- package/.eslintrc.cjs +0 -31
- package/__mocks__/@aw-web-design/x-default-browser.js +0 -2
- package/core/babel/index.cjs +0 -1
- package/core/babel/index.d.ts +0 -2
- package/core/babel/index.js +0 -1
- package/core/builder-manager/index.cjs +0 -1
- package/core/builder-manager/index.d.ts +0 -2
- package/core/builder-manager/index.js +0 -1
- package/core/channels/index.cjs +0 -1
- package/core/channels/index.d.ts +0 -2
- package/core/channels/index.js +0 -1
- package/core/cli/bin/index.cjs +0 -1
- package/core/cli/bin/index.d.ts +0 -2
- package/core/cli/bin/index.js +0 -1
- package/core/cli/index.cjs +0 -1
- package/core/cli/index.d.ts +0 -2
- package/core/cli/index.js +0 -1
- package/core/client-logger/index.cjs +0 -1
- package/core/client-logger/index.d.ts +0 -2
- package/core/client-logger/index.js +0 -1
- package/core/common/index.cjs +0 -1
- package/core/common/index.d.ts +0 -2
- package/core/common/index.js +0 -1
- package/core/components/index.cjs +0 -1
- package/core/components/index.d.ts +0 -2
- package/core/components/index.js +0 -1
- package/core/core-events/index.cjs +0 -1
- package/core/core-events/index.d.ts +0 -2
- package/core/core-events/index.js +0 -1
- package/core/core-server/index.cjs +0 -1
- package/core/core-server/index.d.ts +0 -2
- package/core/core-server/index.js +0 -1
- package/core/core-server/presets/common-manager.js +0 -1
- package/core/core-server/presets/common-override-preset.cjs +0 -1
- package/core/core-server/presets/common-override-preset.js +0 -1
- package/core/core-server/presets/common-preset.cjs +0 -1
- package/core/core-server/presets/common-preset.js +0 -1
- package/core/csf/index.cjs +0 -1
- package/core/csf/index.d.ts +0 -2
- package/core/csf/index.js +0 -1
- package/core/csf-tools/index.cjs +0 -1
- package/core/csf-tools/index.d.ts +0 -2
- package/core/csf-tools/index.js +0 -1
- package/core/docs-tools/index.cjs +0 -1
- package/core/docs-tools/index.d.ts +0 -2
- package/core/docs-tools/index.js +0 -1
- package/core/index.cjs +0 -1
- package/core/index.d.ts +0 -2
- package/core/index.js +0 -1
- package/core/manager/globals-module-info.cjs +0 -1
- package/core/manager/globals-module-info.d.ts +0 -2
- package/core/manager/globals-module-info.js +0 -1
- package/core/manager/globals-runtime.js +0 -1
- package/core/manager/globals.cjs +0 -1
- package/core/manager/globals.d.ts +0 -2
- package/core/manager/globals.js +0 -1
- package/core/manager-api/index.cjs +0 -1
- package/core/manager-api/index.d.ts +0 -2
- package/core/manager-api/index.js +0 -1
- package/core/manager-errors.d.ts +0 -2
- package/core/manager-errors.js +0 -1
- package/core/node-logger/index.cjs +0 -1
- package/core/node-logger/index.d.ts +0 -2
- package/core/node-logger/index.js +0 -1
- package/core/preview/globals.cjs +0 -1
- package/core/preview/globals.d.ts +0 -2
- package/core/preview/globals.js +0 -1
- package/core/preview/runtime.js +0 -1
- package/core/preview-api/index.cjs +0 -1
- package/core/preview-api/index.d.ts +0 -2
- package/core/preview-api/index.js +0 -1
- package/core/preview-errors.cjs +0 -1
- package/core/preview-errors.d.ts +0 -2
- package/core/preview-errors.js +0 -1
- package/core/router/index.cjs +0 -1
- package/core/router/index.d.ts +0 -2
- package/core/router/index.js +0 -1
- package/core/server-errors.cjs +0 -1
- package/core/server-errors.d.ts +0 -2
- package/core/server-errors.js +0 -1
- package/core/telemetry/index.cjs +0 -1
- package/core/telemetry/index.d.ts +0 -2
- package/core/telemetry/index.js +0 -1
- package/core/theming/create.cjs +0 -1
- package/core/theming/create.d.ts +0 -2
- package/core/theming/create.js +0 -1
- package/core/theming/index.cjs +0 -1
- package/core/theming/index.d.ts +0 -2
- package/core/theming/index.js +0 -1
- package/core/types/index.cjs +0 -1
- package/core/types/index.d.ts +0 -2
- package/core/types/index.js +0 -1
- package/core.cjs +0 -0
- package/core.d.ts +0 -0
- package/core.js +0 -1
- package/dist/chunk-OWLSIX54.js +0 -8
- package/dist/core-path.cjs +0 -1
- package/dist/core-path.d.ts +0 -3
- package/dist/core-path.js +0 -9
- package/dist/proxy.cjs +0 -1
- package/dist/proxy.d.ts +0 -2
- package/dist/proxy.js +0 -9
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
import { loadAllPresets } from 'storybook/internal/common';
|
|
2
|
+
export { getPreviewBodyTemplate, getPreviewHeadTemplate } from 'storybook/internal/common';
|
|
3
|
+
import * as storybook_internal_types from 'storybook/internal/types';
|
|
4
|
+
import { CLIOptions, LoadOptions, BuilderOptions, StorybookConfigRaw, IndexInputStats, NormalizedStoriesSpecifier, Path, Indexer, DocsOptions, StoryIndexEntry, DocsIndexEntry, Tag, IndexEntry, StoryIndex, Options } from 'storybook/internal/types';
|
|
5
|
+
import { EventType } from 'storybook/internal/telemetry';
|
|
6
|
+
import { Channel } from 'storybook/internal/channels';
|
|
7
|
+
|
|
8
|
+
type BuildStaticStandaloneOptions = CLIOptions & LoadOptions & BuilderOptions & {
|
|
9
|
+
outputDir: string;
|
|
10
|
+
};
|
|
11
|
+
declare function buildStaticStandalone(options: BuildStaticStandaloneOptions): Promise<void>;
|
|
12
|
+
|
|
13
|
+
declare function buildDevStandalone(options: CLIOptions & LoadOptions & BuilderOptions & {
|
|
14
|
+
storybookVersion?: string;
|
|
15
|
+
previewConfigPath?: string;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
port: number;
|
|
18
|
+
address: string;
|
|
19
|
+
networkAddress: string;
|
|
20
|
+
}>;
|
|
21
|
+
|
|
22
|
+
type BuildIndexOptions = CLIOptions & LoadOptions & BuilderOptions;
|
|
23
|
+
declare const buildIndex: (options: BuildIndexOptions) => Promise<storybook_internal_types.StoryIndex>;
|
|
24
|
+
declare const buildIndexStandalone: (options: BuildIndexOptions & {
|
|
25
|
+
outputFile: string;
|
|
26
|
+
}) => Promise<void>;
|
|
27
|
+
|
|
28
|
+
type TelemetryOptions = {
|
|
29
|
+
cliOptions: CLIOptions;
|
|
30
|
+
presetOptions?: Parameters<typeof loadAllPresets>[0];
|
|
31
|
+
printError?: (err: any) => void;
|
|
32
|
+
skipPrompt?: boolean;
|
|
33
|
+
};
|
|
34
|
+
type ErrorLevel = 'none' | 'error' | 'full';
|
|
35
|
+
declare function getErrorLevel({ cliOptions, presetOptions, skipPrompt, }: TelemetryOptions): Promise<ErrorLevel>;
|
|
36
|
+
declare function sendTelemetryError(_error: unknown, eventType: EventType, options: TelemetryOptions): Promise<void>;
|
|
37
|
+
declare function withTelemetry<T>(eventType: EventType, options: TelemetryOptions, run: () => Promise<T>): Promise<T | undefined>;
|
|
38
|
+
|
|
39
|
+
declare function build(options?: any, frameworkOptions?: any): Promise<void | {
|
|
40
|
+
port: number;
|
|
41
|
+
address: string;
|
|
42
|
+
networkAddress: string;
|
|
43
|
+
}>;
|
|
44
|
+
|
|
45
|
+
declare const mapStaticDir: (staticDir: NonNullable<StorybookConfigRaw["staticDirs"]>[number], configDir: string) => {
|
|
46
|
+
staticDir: string;
|
|
47
|
+
staticPath: string;
|
|
48
|
+
targetDir: string;
|
|
49
|
+
targetEndpoint: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A function that json from a file
|
|
54
|
+
*/
|
|
55
|
+
interface ReadJsonSync {
|
|
56
|
+
(packageJsonPath: string): any | undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Function that can match a path
|
|
61
|
+
*/
|
|
62
|
+
interface MatchPath {
|
|
63
|
+
(requestedModule: string, readJson?: ReadJsonSync, fileExists?: (name: string) => boolean, extensions?: ReadonlyArray<string>): string | undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
declare class IndexingError extends Error {
|
|
67
|
+
importPaths: string[];
|
|
68
|
+
constructor(message: string, importPaths: string[], stack?: string);
|
|
69
|
+
pathsString(): string;
|
|
70
|
+
toString(): string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type IndexStatsSummary = Record<keyof IndexInputStats, number>;
|
|
74
|
+
|
|
75
|
+
type StoryIndexEntryWithExtra = StoryIndexEntry & {
|
|
76
|
+
extra: {
|
|
77
|
+
metaId?: string;
|
|
78
|
+
stats: IndexInputStats;
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/** A .mdx file will produce a docs entry */
|
|
82
|
+
type DocsCacheEntry = DocsIndexEntry;
|
|
83
|
+
/** A `_.stories._` file will produce a list of stories and possibly a docs entry */
|
|
84
|
+
type StoriesCacheEntry = {
|
|
85
|
+
entries: (StoryIndexEntryWithExtra | DocsIndexEntry)[];
|
|
86
|
+
dependents: Path[];
|
|
87
|
+
type: 'stories';
|
|
88
|
+
};
|
|
89
|
+
type ErrorEntry = {
|
|
90
|
+
type: 'error';
|
|
91
|
+
err: IndexingError;
|
|
92
|
+
};
|
|
93
|
+
type CacheEntry = false | StoriesCacheEntry | DocsCacheEntry | ErrorEntry;
|
|
94
|
+
type StoryIndexGeneratorOptions = {
|
|
95
|
+
workingDir: Path;
|
|
96
|
+
configDir: Path;
|
|
97
|
+
indexers: Indexer[];
|
|
98
|
+
docs: DocsOptions;
|
|
99
|
+
build?: StorybookConfigRaw['build'];
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* The StoryIndexGenerator extracts stories and docs entries for each file matching (one or more)
|
|
103
|
+
* stories "specifiers", as defined in main.js.
|
|
104
|
+
*
|
|
105
|
+
* The output is a set of entries (see above for the types).
|
|
106
|
+
*
|
|
107
|
+
* Each file is treated as a stories or a (modern) docs file.
|
|
108
|
+
*
|
|
109
|
+
* A stories file is indexed by an indexer (passed in), which produces a list of stories.
|
|
110
|
+
*
|
|
111
|
+
* - If the stories have the `parameters.docsOnly` setting, they are disregarded.
|
|
112
|
+
* - If the stories have `autodocs` enabled, a docs entry is added pointing to the story file.
|
|
113
|
+
*
|
|
114
|
+
* A (modern) docs (.mdx) file is indexed, a docs entry is added.
|
|
115
|
+
*
|
|
116
|
+
* In the preview, a docs entry with the `autodocs` tag will be rendered as a CSF file that exports
|
|
117
|
+
* an MDX template on the `docs.page` parameter, whereas other docs entries are rendered as MDX
|
|
118
|
+
* files directly.
|
|
119
|
+
*
|
|
120
|
+
* The entries are "uniq"-ed and sorted. Stories entries are preferred to docs entries and MDX docs
|
|
121
|
+
* entries are preferred to CSF templates (with warnings).
|
|
122
|
+
*/
|
|
123
|
+
declare class StoryIndexGenerator {
|
|
124
|
+
readonly specifiers: NormalizedStoriesSpecifier[];
|
|
125
|
+
readonly options: StoryIndexGeneratorOptions;
|
|
126
|
+
private specifierToCache;
|
|
127
|
+
private lastIndex?;
|
|
128
|
+
private lastStats?;
|
|
129
|
+
private lastError?;
|
|
130
|
+
constructor(specifiers: NormalizedStoriesSpecifier[], options: StoryIndexGeneratorOptions);
|
|
131
|
+
initialize(): Promise<void>;
|
|
132
|
+
/** Run the updater function over all the empty cache entries */
|
|
133
|
+
updateExtracted(updater: (specifier: NormalizedStoriesSpecifier, absolutePath: Path, existingEntry: CacheEntry) => Promise<CacheEntry>, overwrite?: boolean): Promise<void>;
|
|
134
|
+
isDocsMdx(absolutePath: Path): boolean;
|
|
135
|
+
ensureExtracted({ projectTags, }: {
|
|
136
|
+
projectTags?: Tag[];
|
|
137
|
+
}): Promise<{
|
|
138
|
+
entries: (IndexEntry | ErrorEntry)[];
|
|
139
|
+
stats: IndexStatsSummary;
|
|
140
|
+
}>;
|
|
141
|
+
findDependencies(absoluteImports: Path[]): StoriesCacheEntry[];
|
|
142
|
+
/**
|
|
143
|
+
* Try to find the component path from a raw import string and return it in the same format as
|
|
144
|
+
* `importPath`. Respect tsconfig paths if available.
|
|
145
|
+
*
|
|
146
|
+
* If no such file exists, assume that the import is from a package and return the raw
|
|
147
|
+
*/
|
|
148
|
+
resolveComponentPath(rawComponentPath: Path, absolutePath: Path, matchPath: MatchPath | undefined): string;
|
|
149
|
+
extractStories(specifier: NormalizedStoriesSpecifier, absolutePath: Path, projectTags?: Tag[]): Promise<StoriesCacheEntry | DocsCacheEntry>;
|
|
150
|
+
extractDocs(specifier: NormalizedStoriesSpecifier, absolutePath: Path, projectTags?: Tag[]): Promise<false | DocsIndexEntry>;
|
|
151
|
+
chooseDuplicate(firstEntry: IndexEntry, secondEntry: IndexEntry, projectTags: Tag[]): IndexEntry;
|
|
152
|
+
sortStories(entries: StoryIndex['entries'], storySortParameter: any): Promise<Record<string, IndexEntry>>;
|
|
153
|
+
getIndex(): Promise<StoryIndex>;
|
|
154
|
+
getIndexAndStats(): Promise<{
|
|
155
|
+
storyIndex: StoryIndex;
|
|
156
|
+
stats: IndexStatsSummary;
|
|
157
|
+
}>;
|
|
158
|
+
invalidateAll(): void;
|
|
159
|
+
invalidate(specifier: NormalizedStoriesSpecifier, importPath: Path, removed: boolean): void;
|
|
160
|
+
getPreviewCode(): Promise<string | undefined>;
|
|
161
|
+
getProjectTags(previewCode?: string): string[];
|
|
162
|
+
storyFileNames(): string[];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
declare function loadStorybook(options: CLIOptions & LoadOptions & BuilderOptions & {
|
|
166
|
+
storybookVersion?: string;
|
|
167
|
+
previewConfigPath?: string;
|
|
168
|
+
}): Promise<Options>;
|
|
169
|
+
|
|
170
|
+
type EnvironmentType = (typeof UniversalStore.Environment)[keyof typeof UniversalStore.Environment];
|
|
171
|
+
type StatusType = (typeof UniversalStore.Status)[keyof typeof UniversalStore.Status];
|
|
172
|
+
type StateUpdater<TState> = (prevState: TState) => TState;
|
|
173
|
+
type Actor = {
|
|
174
|
+
id: string;
|
|
175
|
+
type: (typeof UniversalStore.ActorType)[keyof typeof UniversalStore.ActorType];
|
|
176
|
+
environment: EnvironmentType;
|
|
177
|
+
};
|
|
178
|
+
type EventInfo = {
|
|
179
|
+
actor: Actor;
|
|
180
|
+
forwardingActor?: Actor;
|
|
181
|
+
};
|
|
182
|
+
type Listener<TEvent> = (event: TEvent, eventInfo: EventInfo) => void;
|
|
183
|
+
type BaseEvent = {
|
|
184
|
+
type: string;
|
|
185
|
+
payload?: any;
|
|
186
|
+
};
|
|
187
|
+
interface SetStateEvent<TState> extends BaseEvent {
|
|
188
|
+
type: typeof UniversalStore.InternalEventType.SET_STATE;
|
|
189
|
+
payload: {
|
|
190
|
+
state: TState;
|
|
191
|
+
previousState: TState;
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
interface ExistingStateRequestEvent extends BaseEvent {
|
|
195
|
+
type: typeof UniversalStore.InternalEventType.EXISTING_STATE_REQUEST;
|
|
196
|
+
payload: never;
|
|
197
|
+
}
|
|
198
|
+
interface ExistingStateResponseEvent<TState> extends BaseEvent {
|
|
199
|
+
type: typeof UniversalStore.InternalEventType.EXISTING_STATE_RESPONSE;
|
|
200
|
+
payload: TState;
|
|
201
|
+
}
|
|
202
|
+
interface LeaderCreatedEvent extends BaseEvent {
|
|
203
|
+
type: typeof UniversalStore.InternalEventType.LEADER_CREATED;
|
|
204
|
+
payload: never;
|
|
205
|
+
}
|
|
206
|
+
interface FollowerCreatedEvent extends BaseEvent {
|
|
207
|
+
type: typeof UniversalStore.InternalEventType.FOLLOWER_CREATED;
|
|
208
|
+
payload: never;
|
|
209
|
+
}
|
|
210
|
+
type InternalEvent<TState> = SetStateEvent<TState> | ExistingStateRequestEvent | ExistingStateResponseEvent<TState> | FollowerCreatedEvent | LeaderCreatedEvent;
|
|
211
|
+
type Event<TState, TEvent extends BaseEvent> = TEvent | InternalEvent<TState>;
|
|
212
|
+
type ChannelLike = Pick<Channel, 'on' | 'off' | 'emit'>;
|
|
213
|
+
type StoreOptions<TState> = {
|
|
214
|
+
id: string;
|
|
215
|
+
leader?: boolean;
|
|
216
|
+
initialState?: TState;
|
|
217
|
+
debug?: boolean;
|
|
218
|
+
};
|
|
219
|
+
type EnvironmentOverrides = {
|
|
220
|
+
channel: ChannelLike;
|
|
221
|
+
environment: EnvironmentType;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* A universal store implementation that synchronizes state across different environments using a
|
|
226
|
+
* channel-based communication.
|
|
227
|
+
*
|
|
228
|
+
* The store follows a leader-follower pattern where:
|
|
229
|
+
*
|
|
230
|
+
* - Leader: The main store instance that owns and manages the state
|
|
231
|
+
* - Follower: Store instances that mirror the leader's state
|
|
232
|
+
*
|
|
233
|
+
* Features:
|
|
234
|
+
*
|
|
235
|
+
* - State synchronization across environments
|
|
236
|
+
* - Event-based communication
|
|
237
|
+
* - Type-safe state and custom events
|
|
238
|
+
* - Subscription system for state changes and custom events
|
|
239
|
+
*
|
|
240
|
+
* @remarks
|
|
241
|
+
* - The store must be created using the static `create()` method, not the constructor
|
|
242
|
+
* - Follower stores will automatically sync with their leader's state. If they have initial state, it
|
|
243
|
+
* will be replaced immediately when it has synced with the leader.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
*
|
|
247
|
+
* ```typescript
|
|
248
|
+
* interface MyState {
|
|
249
|
+
* count: number;
|
|
250
|
+
* }
|
|
251
|
+
* interface MyCustomEvent {
|
|
252
|
+
* type: 'INCREMENT';
|
|
253
|
+
* payload: number;
|
|
254
|
+
* }
|
|
255
|
+
*
|
|
256
|
+
* // Create a leader store
|
|
257
|
+
* const leaderStore = UniversalStore.create<MyState, MyCustomEvent>({
|
|
258
|
+
* id: 'my-store',
|
|
259
|
+
* leader: true,
|
|
260
|
+
* initialState: { count: 0 },
|
|
261
|
+
* });
|
|
262
|
+
*
|
|
263
|
+
* // Create a follower store
|
|
264
|
+
* const followerStore = UniversalStore.create<MyState, MyCustomEvent>({
|
|
265
|
+
* id: 'my-store',
|
|
266
|
+
* leader: false,
|
|
267
|
+
* });
|
|
268
|
+
* ```
|
|
269
|
+
*
|
|
270
|
+
* @template State - The type of state managed by the store
|
|
271
|
+
* @template CustomEvent - Custom events that can be sent through the store. Must have a `type`
|
|
272
|
+
* string and optional `payload`
|
|
273
|
+
* @throws {Error} If constructed directly instead of using `create()`
|
|
274
|
+
* @throws {Error} If created without setting a channel first
|
|
275
|
+
* @throws {Error} If a follower is created with initial state
|
|
276
|
+
* @throws {Error} If a follower cannot find its leader within 1 second
|
|
277
|
+
*/
|
|
278
|
+
declare class UniversalStore<State, CustomEvent extends {
|
|
279
|
+
type: string;
|
|
280
|
+
payload?: any;
|
|
281
|
+
} = {
|
|
282
|
+
type: string;
|
|
283
|
+
payload?: any;
|
|
284
|
+
}> {
|
|
285
|
+
/**
|
|
286
|
+
* Defines the possible actor types in the store system
|
|
287
|
+
*
|
|
288
|
+
* @readonly
|
|
289
|
+
*/
|
|
290
|
+
static readonly ActorType: {
|
|
291
|
+
readonly LEADER: "LEADER";
|
|
292
|
+
readonly FOLLOWER: "FOLLOWER";
|
|
293
|
+
};
|
|
294
|
+
/**
|
|
295
|
+
* Defines the possible environments the store can run in
|
|
296
|
+
*
|
|
297
|
+
* @readonly
|
|
298
|
+
*/
|
|
299
|
+
static readonly Environment: {
|
|
300
|
+
readonly SERVER: "SERVER";
|
|
301
|
+
readonly MANAGER: "MANAGER";
|
|
302
|
+
readonly PREVIEW: "PREVIEW";
|
|
303
|
+
readonly UNKNOWN: "UNKNOWN";
|
|
304
|
+
readonly MOCK: "MOCK";
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Internal event types used for store synchronization
|
|
308
|
+
*
|
|
309
|
+
* @readonly
|
|
310
|
+
*/
|
|
311
|
+
static readonly InternalEventType: {
|
|
312
|
+
readonly EXISTING_STATE_REQUEST: "__EXISTING_STATE_REQUEST";
|
|
313
|
+
readonly EXISTING_STATE_RESPONSE: "__EXISTING_STATE_RESPONSE";
|
|
314
|
+
readonly SET_STATE: "__SET_STATE";
|
|
315
|
+
readonly LEADER_CREATED: "__LEADER_CREATED";
|
|
316
|
+
readonly FOLLOWER_CREATED: "__FOLLOWER_CREATED";
|
|
317
|
+
};
|
|
318
|
+
static readonly Status: {
|
|
319
|
+
readonly UNPREPARED: "UNPREPARED";
|
|
320
|
+
readonly SYNCING: "SYNCING";
|
|
321
|
+
readonly READY: "READY";
|
|
322
|
+
readonly ERROR: "ERROR";
|
|
323
|
+
};
|
|
324
|
+
protected static isInternalConstructing: boolean;
|
|
325
|
+
/**
|
|
326
|
+
* The preparation construct is used to keep track of all store's preparation state the promise is
|
|
327
|
+
* resolved when the store is prepared with the static __prepare() method which will also change
|
|
328
|
+
* the state from PENDING to RESOLVED
|
|
329
|
+
*/
|
|
330
|
+
private static preparation;
|
|
331
|
+
private static setupPreparationPromise;
|
|
332
|
+
/** Enable debug logs for this store */
|
|
333
|
+
debugging: boolean;
|
|
334
|
+
/** The actor object representing the store instance with a unique ID and a type */
|
|
335
|
+
get actor(): Actor;
|
|
336
|
+
/**
|
|
337
|
+
* The current state of the store, that signals both if the store is prepared by Storybook and
|
|
338
|
+
* also - in the case of a follower - if the state has been synced with the leader's state.
|
|
339
|
+
*/
|
|
340
|
+
get status(): StatusType;
|
|
341
|
+
/**
|
|
342
|
+
* A promise that resolves when the store is fully ready. A leader will be ready when the store
|
|
343
|
+
* has been prepared by Storybook, which is almost instantly.
|
|
344
|
+
*
|
|
345
|
+
* A follower will be ready when the state has been synced with the leader's state, within a few
|
|
346
|
+
* hundred milliseconds.
|
|
347
|
+
*/
|
|
348
|
+
untilReady(): Promise<[{
|
|
349
|
+
channel: ChannelLike;
|
|
350
|
+
environment: EnvironmentType;
|
|
351
|
+
}, void | undefined]>;
|
|
352
|
+
/**
|
|
353
|
+
* The syncing construct is used to keep track of if the instance's state has been synced with the
|
|
354
|
+
* other instances. A leader will immediately have the promise resolved. A follower will initially
|
|
355
|
+
* be in a PENDING state, and resolve the the leader has sent the existing state, or reject if no
|
|
356
|
+
* leader has responded before the timeout.
|
|
357
|
+
*/
|
|
358
|
+
private syncing?;
|
|
359
|
+
private channelEventName;
|
|
360
|
+
private state;
|
|
361
|
+
private channel?;
|
|
362
|
+
private environment?;
|
|
363
|
+
private listeners;
|
|
364
|
+
private id;
|
|
365
|
+
private actorId;
|
|
366
|
+
private actorType;
|
|
367
|
+
protected constructor(options: StoreOptions<State>, environmentOverrides?: EnvironmentOverrides);
|
|
368
|
+
/** Creates a new instance of UniversalStore */
|
|
369
|
+
static create<State = any, CustomEvent extends {
|
|
370
|
+
type: string;
|
|
371
|
+
payload?: any;
|
|
372
|
+
} = {
|
|
373
|
+
type: string;
|
|
374
|
+
payload?: any;
|
|
375
|
+
}>(options: StoreOptions<State>): UniversalStore<State, CustomEvent>;
|
|
376
|
+
/** Gets the current state */
|
|
377
|
+
getState: () => State;
|
|
378
|
+
/**
|
|
379
|
+
* Updates the store's state
|
|
380
|
+
*
|
|
381
|
+
* Either a new state or a state updater function can be passed to the method.
|
|
382
|
+
*/
|
|
383
|
+
setState(updater: State | StateUpdater<State>): void;
|
|
384
|
+
/**
|
|
385
|
+
* Subscribes to store events
|
|
386
|
+
*
|
|
387
|
+
* @returns A function to unsubscribe
|
|
388
|
+
*/
|
|
389
|
+
subscribe: {
|
|
390
|
+
(listener: Listener<Event<State, CustomEvent>>): () => void;
|
|
391
|
+
<EventType extends Event<State, CustomEvent>['type']>(eventType: EventType, listener: Listener<Extract<Event<State, CustomEvent>, {
|
|
392
|
+
type: EventType;
|
|
393
|
+
}>>): () => void;
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* Subscribes to state changes
|
|
397
|
+
*
|
|
398
|
+
* @returns Unsubscribe function
|
|
399
|
+
*/
|
|
400
|
+
onStateChange(listener: (state: State, previousState: State, eventInfo: EventInfo) => void): () => void;
|
|
401
|
+
/** Sends a custom event to the other stores */
|
|
402
|
+
send: (event: CustomEvent) => void;
|
|
403
|
+
private emitToChannel;
|
|
404
|
+
private prepareThis;
|
|
405
|
+
private emitToListeners;
|
|
406
|
+
private handleChannelEvents;
|
|
407
|
+
private debug;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* A mock universal store that can be used when testing code that relies on a universal store. It
|
|
412
|
+
* functions exactly like a normal universal store, with a few exceptions:
|
|
413
|
+
*
|
|
414
|
+
* - It is fully isolated, meaning that it doesn't interact with any channel, and it is always a
|
|
415
|
+
* leader.
|
|
416
|
+
*
|
|
417
|
+
* If the second testUtils argument is provided, all the public methods are spied on, so they can be
|
|
418
|
+
* asserted.
|
|
419
|
+
*
|
|
420
|
+
* When a mock store is re-used across tests (eg. in stories), you manually need to reset the state
|
|
421
|
+
* after each test.
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
*
|
|
425
|
+
* ```ts
|
|
426
|
+
* import * as testUtils from 'storybook/test'; // in stories
|
|
427
|
+
* import { vi as testUtils } from 'vitest'; // ... or in Vitest tests
|
|
428
|
+
*
|
|
429
|
+
* const initialState = { ... };
|
|
430
|
+
* const store = new MockUniversalStore({ initialState }, testUtils);
|
|
431
|
+
*
|
|
432
|
+
* export default {
|
|
433
|
+
* title: 'My story',
|
|
434
|
+
* beforeEach: () => {
|
|
435
|
+
* return () => {
|
|
436
|
+
* store.setState(initialState);
|
|
437
|
+
* };
|
|
438
|
+
* }
|
|
439
|
+
* }
|
|
440
|
+
* ```
|
|
441
|
+
*/
|
|
442
|
+
declare class MockUniversalStore<State, CustomEvent extends {
|
|
443
|
+
type: string;
|
|
444
|
+
payload?: any;
|
|
445
|
+
} = {
|
|
446
|
+
type: string;
|
|
447
|
+
payload?: any;
|
|
448
|
+
}> extends UniversalStore<State, CustomEvent> {
|
|
449
|
+
private testUtils;
|
|
450
|
+
constructor(options: StoreOptions<State>, testUtils?: any);
|
|
451
|
+
/** Create a mock universal store. This is just an alias for the constructor */
|
|
452
|
+
static create<State = any, CustomEvent extends {
|
|
453
|
+
type: string;
|
|
454
|
+
payload?: any;
|
|
455
|
+
} = {
|
|
456
|
+
type: string;
|
|
457
|
+
payload?: any;
|
|
458
|
+
}>(options: StoreOptions<State>, testUtils?: any): MockUniversalStore<State, CustomEvent>;
|
|
459
|
+
unsubscribeAll(): void;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export { type BuildIndexOptions, type BuildStaticStandaloneOptions, StoryIndexGenerator, build, buildDevStandalone, buildIndex, buildIndexStandalone, buildStaticStandalone, MockUniversalStore as experimental_MockUniversalStore, UniversalStore as experimental_UniversalStore, loadStorybook as experimental_loadStorybook, getErrorLevel, mapStaticDir, sendTelemetryError, withTelemetry };
|