| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type Store from './store'; |
| 11 | import type {FrontendBridge} from 'react-devtools-shared/src/bridge'; |
| 12 | |
| 13 | import {logErrorEvent} from 'react-devtools-shared/src/Logger'; |
| 14 | |
| 15 | export function subscribeToStoreErrors( |
| 16 | store: Store, |
| 17 | bridge: FrontendBridge, |
| 18 | ): () => void { |
| 19 | const onError = (error: Error) => logErrorEvent(error, null); |
| 20 | let isSubscribed = true; |
| 21 | |
| 22 | const unsubscribe = () => { |
| 23 | if (isSubscribed) { |
| 24 | isSubscribed = false; |
| 25 | store.removeListener('error', onError); |
| 26 | bridge.removeListener('shutdown', unsubscribe); |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | store.addListener('error', onError); |
| 31 | bridge.addListener('shutdown', unsubscribe); |
| 32 | return unsubscribe; |
| 33 | } |