From 2e545b10f5ca1111f766f4ec76ee0c4c758b5479 Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Tue, 27 Aug 2024 11:02:15 -0700 Subject: [PATCH] DebuggerSessionObserver: JavaScript API (#46043) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46043 # Changelog: [Internal] Defined JavaScript interface, which can be used by other modules, such as LogBox, which will be migrated in the next diff Reviewed By: robhogan Differential Revision: D61301333 fbshipit-source-id: 63bb8581b893d0fdcb36e1fa16d243f1a5b08ac4 --- .../private/fusebox/FuseboxSessionObserver.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 packages/react-native/src/private/fusebox/FuseboxSessionObserver.js diff --git a/packages/react-native/src/private/fusebox/FuseboxSessionObserver.js b/packages/react-native/src/private/fusebox/FuseboxSessionObserver.js new file mode 100644 index 00000000000..498901a8221 --- /dev/null +++ b/packages/react-native/src/private/fusebox/FuseboxSessionObserver.js @@ -0,0 +1,42 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + * @format + * @oncall react_native + */ + +class FuseboxSessionObserver { + #hasNativeSupport: boolean; + + constructor() { + this.#hasNativeSupport = global.hasOwnProperty( + '__DEBUGGER_SESSION_OBSERVER__', + ); + } + + hasActiveSession(): boolean { + if (!this.#hasNativeSupport) { + return false; + } + + return global.__DEBUGGER_SESSION_OBSERVER__.hasActiveSession; + } + + subscribe(callback: (status: boolean) => void): () => void { + if (!this.#hasNativeSupport) { + return () => {}; + } + + global.__DEBUGGER_SESSION_OBSERVER__.subscribers.add(callback); + return () => { + global.__DEBUGGER_SESSION_OBSERVER__.subscribers.delete(callback); + }; + } +} + +const observerInstance: FuseboxSessionObserver = new FuseboxSessionObserver(); +export default observerInstance;