Skip to content
Merged
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
12 changes: 5 additions & 7 deletions code/core/src/manager-api/modules/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,20 @@ export const init: ModuleFn<SubAPI, SubState> = ({ store, fullAPI, provider }) =
SET_GLOBALS,
function handleSetGlobals(this: any, { globals, globalTypes }: SetGlobalsPayload) {
const { ref } = getEventMetadata(this, fullAPI)!;
const currentGlobals = store.getState()?.globals;
const currentUserGlobals = store.getState()?.userGlobals;

if (!ref) {
store.setState({ globals, userGlobals: globals, globalTypes });
} else if (Object.keys(globals).length > 0) {
logger.warn('received globals from a non-local ref. This is not currently supported.');
}

// If we have stored globals different to what the preview just inited with,
// we should update it to those values
if (
currentGlobals &&
Object.keys(currentGlobals).length !== 0 &&
!deepEqual(globals, currentGlobals)
currentUserGlobals &&
Object.keys(currentUserGlobals).length !== 0 &&
!deepEqual(globals, currentUserGlobals)
) {
api.updateGlobals(currentGlobals);
api.updateGlobals(currentUserGlobals);
}
Comment thread
ghengeveld marked this conversation as resolved.
}
);
Expand Down
28 changes: 27 additions & 1 deletion code/core/src/manager-api/tests/globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('globals API', () => {
});
});

it('emits UPDATE_GLOBALS if retains a globals value different to what receives on SET_GLOBALS', () => {
it('emits UPDATE_GLOBALS if retains a user globals value different to what receives on SET_GLOBALS', () => {
const channel = new EventEmitter();
const listener = vi.fn();
channel.on(UPDATE_GLOBALS, listener);
Expand All @@ -83,6 +83,7 @@ describe('globals API', () => {
} as unknown as ModuleArgs);
store.setState({
...state,
userGlobals: { a: 'c' },
globals: { a: 'c' },
});

Expand All @@ -103,6 +104,31 @@ describe('globals API', () => {
});
});

it('does not push story globals to preview when SET_GLOBALS fires with empty globals', () => {
const channel = new EventEmitter();
const listener = vi.fn();
channel.on(UPDATE_GLOBALS, listener);

const store = createMockStore();
const { state } = initModule({
store,
provider: { channel },
} as unknown as ModuleArgs);
store.setState({
...state,
userGlobals: {},
storyGlobals: { viewport: 'mobile1' },
globals: { viewport: 'mobile1' },
});

channel.emit(SET_GLOBALS, {
globals: {},
globalTypes: {},
} satisfies SetGlobalsPayload);

expect(listener).not.toHaveBeenCalled();
});

it('ignores SET_STORIES from other refs', () => {
const channel = new EventEmitter();
const api = { findRef: vi.fn() };
Expand Down
Loading