Fix types in lib/api

This commit is contained in:
Tom Coleman 2022-06-19 13:33:17 +10:00
parent ebb79d0ed1
commit a95d062227
7 changed files with 33 additions and 44 deletions

View File

@ -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();
}
});
};

View File

@ -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),

View File

@ -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: {} };
};

View File

@ -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);
},

View File

@ -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 };
};

View File

@ -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 };
};

View File

@ -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);
}
});