Files
flipper/desktop/flipper-plugin/src/state/createDataSource.tsx
Michel Weststrate bc647972e1 Type improvements
Summary: some type simplifications, that makes it easier to reuse data sources and helps type inference

Reviewed By: passy

Differential Revision: D28413380

fbshipit-source-id: 261a8b981bf18a00edc3075926bd668322e1c37d
2021-06-07 08:09:53 -07:00

41 lines
1.1 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its 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 {
DataSource,
createDataSource as baseCreateDataSource,
DataSourceOptions as BaseDataSourceOptions,
} from '../data-source/index';
import {registerStorageAtom} from '../plugin/PluginBase';
type CreateDataSourceOptions<T, K extends keyof T> = BaseDataSourceOptions<
T,
K
> & {
/**
* Should this state persist when exporting a plugin?
* If set, the dataSource will be saved / loaded under the key provided
*/
persist?: string;
};
export function createDataSource<T, KEY extends keyof T = any>(
initialSet: readonly T[],
options: CreateDataSourceOptions<T, KEY>,
): DataSource<T>;
export function createDataSource<T>(initialSet?: readonly T[]): DataSource<T>;
export function createDataSource<T, KEY extends keyof T>(
initialSet: readonly T[] = [],
options?: CreateDataSourceOptions<T, KEY>,
): DataSource<T> {
const ds = baseCreateDataSource(initialSet, options);
registerStorageAtom(options?.persist, ds);
return ds;
}