Skip to content

Commit

Permalink
Add global args module
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Feb 27, 2020
1 parent 1d04267 commit 7673dbc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/api/package.json
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@reach/router": "^1.2.1",
"@storybook/addons": "6.0.0-alpha.20",
"@storybook/channels": "6.0.0-alpha.20",
"@storybook/client-logger": "6.0.0-alpha.20",
"@storybook/core-events": "6.0.0-alpha.20",
Expand Down
9 changes: 8 additions & 1 deletion lib/api/src/index.tsx
Expand Up @@ -58,6 +58,10 @@ import initVersions, {
SubState as VersionsSubState,
SubAPI as VersionsAPI,
} from './modules/versions';
import initGlobalArgs, {
SubState as GlobalArgsSubState,
SubAPI as GlobalArgsAPI,
} from './modules/globalArgs';

export { Options as StoreOptions, Listener as ChannelListener };
export { ActiveTabs };
Expand All @@ -77,7 +81,8 @@ export type State = Other &
NotificationState &
VersionsSubState &
RouterData &
ShortcutsSubState;
ShortcutsSubState &
GlobalArgsSubState;

export type API = AddonsAPI &
ChannelAPI &
Expand All @@ -88,6 +93,7 @@ export type API = AddonsAPI &
ShortcutsAPI &
VersionsAPI &
UrlAPI &
GlobalArgsAPI &
OtherAPI;

interface OtherAPI {
Expand Down Expand Up @@ -179,6 +185,7 @@ class ManagerProvider extends Component<ManagerProviderProps, State> {
initStories,
initURL,
initVersions,
initGlobalArgs,
].map(initModule => initModule({ ...routeData, ...apiData, state: this.state }));

// Create our initial state by combining the initial state of all modules, then overlaying any saved state
Expand Down
43 changes: 43 additions & 0 deletions lib/api/src/modules/globalArgs.ts
@@ -0,0 +1,43 @@
import { Args } from '@storybook/addons';
import { CHANGE_GLOBAL_ARGS, GLOBAL_ARGS_CHANGED } from '@storybook/core-events';

import { Module, API } from '../index';

export interface SubState {
globalArgs: Args;
}

export interface SubAPI {
setGlobalArgs: (newGlobalArgs: Args) => void;
}

const initGlobalArgsApi = ({ store }: Module) => {
let fullApi: API;
const setGlobalArgs = (newGlobalArgs: Args) => {
if (!fullApi) throw new Error('Cannot set global args until api has been initialized');

This comment has been minimized.

Copy link
@tmeasday

tmeasday Mar 2, 2020

Author Member

@ndelangen is there a better way to do this? (I need fullApi so I can call fullApi.emit -- the "smaller" API that is passed into initGlobalArgs does not yet have a channel..

This comment has been minimized.

Copy link
@ndelangen

ndelangen Mar 2, 2020

Member

Yes, there's a better way:

export default ({ provider, store, fullAPI }: Module) => {

That's in my inception branch, but could be ported. I needed this too, and cleaned it up so the init & scoped let aren't needed.

This comment has been minimized.

Copy link
@tmeasday

tmeasday Mar 3, 2020

Author Member

Maybe let's clean this up (and a similar spot in shortcuts.js) when we merge that then. I don't think it's so bad that we need to port it over now.


fullApi.emit(CHANGE_GLOBAL_ARGS, newGlobalArgs);
};

const api: SubAPI = {
setGlobalArgs,
};

const state: SubState = {
// Currently global args always start empty. TODO -- should this be set on the channel at init time?
globalArgs: {},
};

const init = ({ api: inputApi }: { api: API }) => {
fullApi = inputApi;
fullApi.on(GLOBAL_ARGS_CHANGED, (globalArgs: Args) => store.setState({ globalArgs }));
};

return {
api,
state,
init,
};
};

export default initGlobalArgsApi;

0 comments on commit 7673dbc

Please sign in to comment.