mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-07 07:21:17 +08:00
Fix types in lib/api
This commit is contained in:
parent
ebb79d0ed1
commit
a95d062227
@ -146,24 +146,27 @@ export const combineParameters = (...parameterSets: Parameters[]) =>
|
||||
return undefined;
|
||||
});
|
||||
|
||||
export type ModuleFn<APIType = unknown, StateType = unknown, HasInit = false> = (
|
||||
m: ModuleArgs
|
||||
) => Module<APIType, StateType, HasInit>;
|
||||
|
||||
interface FullModule<APIType = unknown, StateType = unknown> {
|
||||
init: () => Promise<void>;
|
||||
interface ModuleWithInit<APIType = unknown, StateType = unknown> {
|
||||
init: () => void | Promise<void>;
|
||||
api: APIType;
|
||||
state: StateType;
|
||||
}
|
||||
|
||||
type Module<APIType = unknown, StateType = unknown, HasInit = false> = HasInit extends true
|
||||
? FullModule<APIType, StateType>
|
||||
: Omit<FullModule<APIType, StateType>, 'init'>;
|
||||
type ModuleWithoutInit<APIType = unknown, StateType = unknown> = Omit<
|
||||
ModuleWithInit<APIType, StateType>,
|
||||
'init'
|
||||
>;
|
||||
|
||||
export type ModuleFn<APIType = unknown, StateType = unknown, HasInit = false> = (
|
||||
m: ModuleArgs
|
||||
) => HasInit extends true
|
||||
? ModuleWithInit<APIType, StateType>
|
||||
: ModuleWithoutInit<APIType, StateType>;
|
||||
|
||||
class ManagerProvider extends Component<ManagerProviderProps, State> {
|
||||
api: API = {} as API;
|
||||
|
||||
modules: Module[];
|
||||
modules: (ModuleWithInit | ModuleWithoutInit)[];
|
||||
|
||||
static displayName = 'Manager';
|
||||
|
||||
@ -265,9 +268,9 @@ class ManagerProvider extends Component<ManagerProviderProps, State> {
|
||||
initModules = () => {
|
||||
// Now every module has had a chance to set its API, call init on each module which gives it
|
||||
// a chance to do things that call other modules' APIs.
|
||||
this.modules.forEach(({ init }) => {
|
||||
if (init) {
|
||||
init();
|
||||
this.modules.forEach((module) => {
|
||||
if ('init' in module) {
|
||||
module.init();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -63,10 +63,9 @@ type Panels = Collection<Addon>;
|
||||
|
||||
type StateMerger<S> = (input: S) => S;
|
||||
|
||||
interface StoryInput {
|
||||
parameters: {
|
||||
[parameterName: string]: any;
|
||||
};
|
||||
export interface SubState {
|
||||
selectedPanel: string;
|
||||
addons: Record<string, never>;
|
||||
}
|
||||
|
||||
export interface SubAPI {
|
||||
@ -96,7 +95,7 @@ export function ensurePanel(panels: Panels, selectedPanel?: string, currentPanel
|
||||
return currentPanel;
|
||||
}
|
||||
|
||||
export const init: ModuleFn = ({ provider, store, fullAPI }) => {
|
||||
export const init: ModuleFn<SubAPI, SubState> = ({ provider, store, fullAPI }) => {
|
||||
const api: SubAPI = {
|
||||
getElements: (type) => provider.getElements(type),
|
||||
getPanels: () => api.getElements(types.PANEL),
|
||||
|
@ -14,7 +14,9 @@ export interface SubAPI {
|
||||
expandAll: () => void;
|
||||
}
|
||||
|
||||
export const init: ModuleFn = ({ provider }) => {
|
||||
export type SubState = Record<string, never>;
|
||||
|
||||
export const init: ModuleFn<SubAPI, SubState> = ({ provider }) => {
|
||||
const api: SubAPI = {
|
||||
getChannel: () => provider.channel,
|
||||
on: (type, cb) => {
|
||||
@ -33,5 +35,5 @@ export const init: ModuleFn = ({ provider }) => {
|
||||
api.emit(STORIES_EXPAND_ALL);
|
||||
},
|
||||
};
|
||||
return { api };
|
||||
return { api, state: {} };
|
||||
};
|
||||
|
@ -40,9 +40,10 @@ export interface SubAPI {
|
||||
renderPreview?: Provider['renderPreview'];
|
||||
}
|
||||
|
||||
export const init: ModuleFn = ({ provider, fullAPI }) => {
|
||||
export const init: ModuleFn<SubAPI, {}, true> = ({ provider, fullAPI }) => {
|
||||
return {
|
||||
api: provider.renderPreview ? { renderPreview: provider.renderPreview } : {},
|
||||
state: {},
|
||||
init: () => {
|
||||
provider.handleAPI(fullAPI);
|
||||
},
|
||||
|
@ -29,7 +29,7 @@ export interface SubState {
|
||||
releaseNotesViewed: string[];
|
||||
}
|
||||
|
||||
export const init: ModuleFn = ({ store }) => {
|
||||
export const init: ModuleFn<SubAPI, SubState> = ({ store }) => {
|
||||
const releaseNotesData = getReleaseNotesData();
|
||||
const getReleaseNotesViewed = () => {
|
||||
const { releaseNotesViewed: persistedReleaseNotesViewed } = store.getState();
|
||||
@ -58,7 +58,5 @@ export const init: ModuleFn = ({ store }) => {
|
||||
},
|
||||
};
|
||||
|
||||
const initModule = () => {};
|
||||
|
||||
return { init: initModule, api };
|
||||
return { state: { releaseNotesViewed: [] }, api };
|
||||
};
|
||||
|
@ -15,7 +15,7 @@ export interface SubState {
|
||||
settings: Settings;
|
||||
}
|
||||
|
||||
export const init: ModuleFn = ({ store, navigate, fullAPI }) => {
|
||||
export const init: ModuleFn<SubAPI, SubState> = ({ store, navigate, fullAPI }) => {
|
||||
const isSettingsScreenActive = () => {
|
||||
const { path } = fullAPI.getUrlState();
|
||||
return !!(path || '').match(/^\/settings/);
|
||||
@ -49,9 +49,5 @@ export const init: ModuleFn = ({ store, navigate, fullAPI }) => {
|
||||
},
|
||||
};
|
||||
|
||||
const initModule = async () => {
|
||||
await store.setState({ settings: { lastTrackedStoryId: null } });
|
||||
};
|
||||
|
||||
return { init: initModule, api };
|
||||
return { state: { settings: { lastTrackedStoryId: null } }, api };
|
||||
};
|
||||
|
@ -91,16 +91,6 @@ export interface SubAPI {
|
||||
updateStory: (storyId: StoryId, update: StoryUpdate, ref?: ComposedRef) => Promise<void>;
|
||||
}
|
||||
|
||||
interface Meta {
|
||||
ref?: ComposedRef;
|
||||
source?: string;
|
||||
sourceType?: 'local' | 'external';
|
||||
sourceLocation?: string;
|
||||
refId?: string;
|
||||
v?: number;
|
||||
type: string;
|
||||
}
|
||||
|
||||
const deprecatedOptionsParameterWarnings: Record<string, () => void> = [
|
||||
'enableShortcuts',
|
||||
'theme',
|
||||
@ -502,19 +492,19 @@ export const init: ModuleFn<SubAPI, SubState, true> = ({
|
||||
|
||||
fullAPI.on(SET_STORIES, function handler(data: SetStoriesPayload) {
|
||||
const { ref } = getEventMetadata(this, fullAPI);
|
||||
const stories = data.v ? denormalizeStoryParameters(data) : data.stories;
|
||||
const setStoriesData = data.v ? denormalizeStoryParameters(data) : data.stories;
|
||||
|
||||
if (!ref) {
|
||||
if (!data.v) {
|
||||
throw new Error('Unexpected legacy SET_STORIES event from local source');
|
||||
}
|
||||
|
||||
fullAPI.setStories(stories);
|
||||
fullAPI.setStories(setStoriesData);
|
||||
const options = fullAPI.getCurrentParameter('options');
|
||||
checkDeprecatedOptionParameters(options);
|
||||
fullAPI.setOptions(options);
|
||||
} else {
|
||||
fullAPI.setRef(ref.id, { ...ref, ...data, stories }, true);
|
||||
fullAPI.setRef(ref.id, { ...ref, setStoriesData }, true);
|
||||
}
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user