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:
committed by
Facebook GitHub Bot
parent
cfd44b592a
commit
91d96774f6
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
24
desktop/flipper-plugin/src/utils/useLogger.tsx
Normal file
24
desktop/flipper-plugin/src/utils/useLogger.tsx
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user