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

@@ -0,0 +1,44 @@
/**
* 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 type LoggerTypes = 'error' | 'warn' | 'info' | 'debug';
export type LoggerTrackType =
| 'duration'
| 'usage'
| 'performance'
| 'success-rate'
| 'operation-cancelled';
export type LoggerArgs = {
isTest?: boolean;
};
export interface Logger {
track(
type: LoggerTrackType,
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;
}

View File

@@ -7,6 +7,12 @@
* @format
*/
export function helloWorld() {
return true;
}
export {
Logger,
LoggerTrackType,
LoggerTypes,
LoggerArgs,
} from './fb-interfaces/Logger';
export * from './server-types';
export {sleep} from './utils/sleep';
export {timeout} from './utils/timeout';

View File

@@ -0,0 +1,156 @@
/**
* 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 {
DeviceSpec,
DeviceType as PluginDeviceType,
OS as PluginOS,
} from 'flipper-plugin-lib';
// In the future, this file would deserve it's own package, as it doesn't really relate to plugins.
// Since flipper-plugin however is currently shared among server, client and defines a lot of base types, leaving it here for now.
export type FlipperServerState =
| 'pending'
| 'starting'
| 'started'
| 'error'
| 'closed';
export type DeviceType = PluginDeviceType;
export type DeviceOS = PluginOS | 'Windows' | 'MacOS';
export type DeviceDescription = {
readonly os: DeviceOS;
readonly title: string;
readonly deviceType: DeviceType;
readonly serial: string;
readonly icon?: string;
// Android specific information
readonly specs?: DeviceSpec[];
readonly abiList?: string[];
readonly sdkVersion?: string;
};
export type DeviceLogEntry = {
readonly date: Date;
readonly type: DeviceLogLevel;
readonly message: string;
readonly pid: number;
readonly tid: number;
readonly app?: string;
readonly tag: string;
};
export type DeviceLogLevel =
| 'unknown'
| 'verbose'
| 'debug'
| 'info'
| 'warn'
| 'error'
| 'fatal';
export type UninitializedClient = {
os: string;
deviceName: string;
appName: string;
};
export type ClientQuery = {
readonly app: string;
readonly os: DeviceOS;
readonly device: string;
readonly device_id: string;
readonly sdk_version?: number;
};
export type ClientDescription = {
readonly id: string;
readonly query: ClientQuery;
};
export type ClientErrorType = {
message: string;
stacktrace: string;
name: string;
};
export type ClientResponseType = {
success?: Object;
error?: ClientErrorType;
length: number;
};
export type FlipperServerEvents = {
'server-state': {state: FlipperServerState; error?: Error};
'server-error': any;
notification: {
type: 'error';
title: string;
description: string;
};
'device-connected': DeviceDescription;
'device-disconnected': DeviceDescription;
'device-log': {
serial: string;
entry: DeviceLogEntry;
};
'client-setup': UninitializedClient;
'client-connected': ClientDescription;
'client-disconnected': {id: string};
'client-message': {
id: string;
message: string;
};
};
export type FlipperServerCommands = {
'device-start-logging': (serial: string) => Promise<void>;
'device-stop-logging': (serial: string) => Promise<void>;
'device-supports-screenshot': (serial: string) => Promise<boolean>;
'device-supports-screencapture': (serial: string) => Promise<boolean>;
'device-take-screenshot': (serial: string) => Promise<string>; // base64 encoded buffer
'device-start-screencapture': (
serial: string,
destination: string,
) => Promise<void>;
'device-stop-screencapture': (serial: string) => Promise<string>; // file path
'device-shell-exec': (serial: string, command: string) => Promise<string>;
'device-forward-port': (
serial: string,
local: string,
remote: string,
) => Promise<boolean>;
'device-clear-logs': (serial: string) => Promise<void>;
'device-navigate': (serial: string, location: string) => Promise<void>;
'metro-command': (serial: string, command: string) => Promise<void>;
'client-request': (clientId: string, payload: any) => Promise<void>;
'client-request-response': (
clientId: string,
payload: any,
) => Promise<ClientResponseType>;
};
export interface FlipperServer {
on<Event extends keyof FlipperServerEvents>(
event: Event,
callback: (payload: FlipperServerEvents[Event]) => void,
): void;
off<Event extends keyof FlipperServerEvents>(
event: Event,
callback: (payload: FlipperServerEvents[Event]) => void,
): void;
exec<Event extends keyof FlipperServerCommands>(
event: Event,
...args: Parameters<FlipperServerCommands[Event]>
): ReturnType<FlipperServerCommands[Event]>;
close(): void;
}

View File

@@ -0,0 +1,14 @@
/**
* 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

@@ -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 {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]);
}