Move files to flipper-common

Summary: Moved Logger, sleep, timeout and server contract types to flipper-common packages.

Reviewed By: passy

Differential Revision: D31475790

fbshipit-source-id: 42d2147698875f9e919ad5250f9953f3bff3ec2d
This commit is contained in:
Michel Weststrate
2021-10-12 15:59:44 -07:00
committed by Facebook GitHub Bot
parent cfd44b592a
commit 91d96774f6
67 changed files with 222 additions and 179 deletions

View File

@@ -7,33 +7,8 @@
* @format
*/
import {createContext, useContext} from 'react';
export type LogTypes = 'error' | 'warn' | 'info' | 'debug';
export type TrackType =
| 'duration'
| 'usage'
| 'performance'
| 'success-rate'
| 'operation-cancelled';
export interface Logger {
track(type: TrackType, event: string, data?: any, plugin?: string): void;
trackTimeSince(
mark: string,
eventName?: string | null | undefined,
data?: any,
): void;
info(data: any, category: string): void;
warn(data: any, category: string): void;
error(data: any, category: string): void;
debug(data: any, category: string): void;
}
import {Logger} from 'flipper-common';
export {Logger} from 'flipper-common';
export const stubLogger: Logger = {
track() {},
@@ -55,15 +30,3 @@ export const stubLogger: Logger = {
console.debug.apply(console, arguments as any);
},
};
export const _LoggerContext = createContext<Logger>(stubLogger);
/**
* Provides the default logger that can be used for console logging,
* error reporting and performance measurements.
* In internal Facebook builds this is wired up to the internal statistic reporting.
* Prefer using `logger` over using `console` directly.
*/
export function useLogger(): Logger {
return useContext(_LoggerContext);
}

View File

@@ -1,14 +0,0 @@
/**
* 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
*/
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

View File

@@ -1,24 +0,0 @@
/**
* 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 {sleep} from './sleep';
export function timeout<T>(
ms: number,
promise: Promise<T>,
timeoutMessage?: string,
): Promise<T> {
// Create a promise that rejects in <ms> milliseconds
const timeout = sleep(ms).then(() => {
throw new Error(timeoutMessage || `Timed out in ${ms} ms.`);
});
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]);
}

View File

@@ -0,0 +1,24 @@
/**
* 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 {Logger} from 'flipper-common';
import {createContext, useContext} from 'react';
import {stubLogger} from './Logger';
export const _LoggerContext = createContext<Logger>(stubLogger);
/**
* Provides the default logger that can be used for console logging,
* error reporting and performance measurements.
* In internal Facebook builds this is wired up to the internal statistic reporting.
* Prefer using `logger` over using `console` directly.
*/
export function useLogger(): Logger {
return useContext(_LoggerContext);
}