Files
flipper/desktop/flipper-plugin/src/state/atom.tsx
Andrey Goncharov 97b8b8a1c4 Split flipper-plugin package
Summary:
flipper-server-companion depends on flipper-plugin. flipper-plugin includes dependencies that run only in a browser. Splitting flipper-plugin into core and browser packages helps to avoid including browser-only dependencies into flipper-server bundle.
As a result, bundle size could be cut in half. Subsequently, RSS usage drops as there is twice as less code to process for V8.

Note: it currently breaks external flipper-data-source package. It will be restored in subsequent diffs

Reviewed By: lblasa

Differential Revision: D38658285

fbshipit-source-id: 751b11fa9f3a2d938ce166687b8310ba8b059dee
2022-09-15 10:02:19 -07:00

39 lines
1.0 KiB
TypeScript

/**
* 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.
*
* @format
*/
import {_AtomValue, _ReadOnlyAtom} from 'flipper-plugin-core';
import {useState, useEffect} from 'react';
export function useValue<T>(atom: _ReadOnlyAtom<T>): T;
export function useValue<T>(
atom: _ReadOnlyAtom<T> | undefined,
defaultValue: T,
): T;
export function useValue<T>(
atom: _ReadOnlyAtom<T> | undefined,
defaultValue?: T,
): T {
const [localValue, setLocalValue] = useState<T>(
atom ? atom.get() : defaultValue!,
);
useEffect(() => {
if (!atom) {
return;
}
// atom might have changed between mounting and effect setup
// in that case, this will cause a re-render, otherwise not
setLocalValue(atom.get());
(atom as _AtomValue<T>).subscribe(setLocalValue);
return () => {
(atom as _AtomValue<T>).unsubscribe(setLocalValue);
};
}, [atom]);
return localValue;
}