Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed channel api proposal #6189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/api/src/modules/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
type: string;
}

const SELECT_STORY: Event<Story> = {
type: 'selectStory',
};

const SET_STORIES: Event<StoriesList> = {
type: 'setStories',
};

// we wouldn't actually use a map..
const callbacks = new Map<string, any>();

const on = <T>(event: Event<T>, callback: (param: T) => void) => {
callbacks.set(event.type, callback);
};

const emit = <T>(event: Event<T>, 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;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/src/modules/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface StoryInput {
isLeaf: boolean;
}

type Story = StoryInput & Group;
export type Story = StoryInput & Group;

export interface StoriesHash {
[id: string]: Group | Story;
Expand Down