diff --git a/lib/api/src/modules/channel.ts b/lib/api/src/modules/channel.ts index 9bcfcacfe2bd..ef551f5e1d27 100644 --- a/lib/api/src/modules/channel.ts +++ b/lib/api/src/modules/channel.ts @@ -3,6 +3,37 @@ import { STORY_CHANGED } from '@storybook/core-events'; import { Channel, Listener } from '@storybook/channels'; import { Module } from '../index'; +import { Story, StoriesList } from './stories'; + +interface Event { + type: string; +} + +const SELECT_STORY: Event = { + type: 'selectStory', +}; + +const SET_STORIES: Event = { + type: 'setStories', +}; + +// we wouldn't actually use a map.. +const callbacks = new Map(); + +const on = (event: Event, callback: (param: T) => void) => { + callbacks.set(event.type, callback); +}; + +const emit = (event: Event, payload: T) => { + const callback = callbacks.get(event.type) as ((param: T) => void); + callback(payload); +}; + +on(SELECT_STORY, story => { + // story type inferred as `Story` +}); + +emit(SET_STORIES, 'foo'); // Err, Argument of type '"foo"' is not assignable to parameter of type '(Group | Story) export interface SubAPI { getChannel: () => Channel; diff --git a/lib/api/src/modules/stories.ts b/lib/api/src/modules/stories.ts index de9ecf450690..fe128e97e009 100644 --- a/lib/api/src/modules/stories.ts +++ b/lib/api/src/modules/stories.ts @@ -60,7 +60,7 @@ interface StoryInput { isLeaf: boolean; } -type Story = StoryInput & Group; +export type Story = StoryInput & Group; export interface StoriesHash { [id: string]: Group | Story;