From b25bf1304b932910fac1d54c7eca1de4d5859560 Mon Sep 17 00:00:00 2001 From: Nathan Bierema Date: Sat, 21 Sep 2024 11:17:43 -0400 Subject: [PATCH] Send state from background when monitor connects (#1771) * Send state from background on connection * Create green-hats-kick.md --- .changeset/green-hats-kick.md | 5 ++++ .../src/background/store/apiMiddleware.ts | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 .changeset/green-hats-kick.md diff --git a/.changeset/green-hats-kick.md b/.changeset/green-hats-kick.md new file mode 100644 index 00000000..f13e6a41 --- /dev/null +++ b/.changeset/green-hats-kick.md @@ -0,0 +1,5 @@ +--- +'remotedev-redux-devtools-extension': patch +--- + +Send state from background when monitor connects diff --git a/extension/src/background/store/apiMiddleware.ts b/extension/src/background/store/apiMiddleware.ts index f6b02f6d..b01853bf 100644 --- a/extension/src/background/store/apiMiddleware.ts +++ b/extension/src/background/store/apiMiddleware.ts @@ -592,6 +592,33 @@ function onConnect>(port: chrome.runtime.Port) { }; port.onMessage.addListener(listener); port.onDisconnect.addListener(disconnect('panel', id, listener)); + + const { current } = store.getState().instances; + if (current !== 'default') { + const connectionId = Object.entries( + store.getState().instances.connections, + ).find(([, instanceIds]) => instanceIds.includes(current))?.[0]; + const options = store.getState().instances.options[current]; + const state = store.getState().instances.states[current]; + const { actionsById, computedStates, committedState, ...rest } = state; + toMonitors({ + type: UPDATE_STATE, + request: { + type: 'STATE', + payload: rest as Omit< + LiftedState, + 'actionsById' | 'computedStates' | 'committedState' + >, + source: '@devtools-page', + instanceId: + typeof current === 'number' ? current.toString() : current, + actionsById: stringifyJSON(actionsById, options.serialize), + computedStates: stringifyJSON(computedStates, options.serialize), + committedState: typeof committedState !== 'undefined', + }, + id: connectionId ?? current, + }); + } } }