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
@@ -14,6 +14,7 @@
|
||||
"@emotion/react": "^11.4.1",
|
||||
"@reach/observe-rect": "^1.2.0",
|
||||
"@types/uuid": "^8.3.1",
|
||||
"flipper-common": "0.0.0",
|
||||
"immer": "^9.0.6",
|
||||
"lodash": "^4.17.21",
|
||||
"react-color": "^2.19.3",
|
||||
|
||||
@@ -83,8 +83,11 @@ test('Correct top level API exposed', () => {
|
||||
"DefaultKeyboardAction",
|
||||
"Device",
|
||||
"DeviceLogEntry",
|
||||
"DeviceLogLevel",
|
||||
"DeviceLogListener",
|
||||
"DeviceOS",
|
||||
"DevicePluginClient",
|
||||
"DeviceType",
|
||||
"DialogResult",
|
||||
"Draft",
|
||||
"ElementAttribute",
|
||||
@@ -99,15 +102,12 @@ test('Correct top level API exposed', () => {
|
||||
"Idler",
|
||||
"InteractionReport",
|
||||
"InteractionReporter",
|
||||
"LogLevel",
|
||||
"LogTypes",
|
||||
"Logger",
|
||||
"MenuEntry",
|
||||
"MockedConsole",
|
||||
"NormalizedMenuEntry",
|
||||
"Notification",
|
||||
"PluginClient",
|
||||
"TrackType",
|
||||
]
|
||||
`);
|
||||
});
|
||||
|
||||
@@ -13,7 +13,7 @@ import {createState} from '../state/atom';
|
||||
import {PluginClient} from '../plugin/Plugin';
|
||||
import {DevicePluginClient} from '../plugin/DevicePlugin';
|
||||
import mockConsole from 'jest-mock-console';
|
||||
import {sleep} from '../utils/sleep';
|
||||
import {sleep} from 'flipper-common';
|
||||
import {createDataSource} from '../state/createDataSource';
|
||||
|
||||
test('it can start a plugin and lifecycle events', () => {
|
||||
|
||||
@@ -21,10 +21,8 @@ export {
|
||||
} from './plugin/Plugin';
|
||||
export {
|
||||
Device,
|
||||
DeviceLogEntry,
|
||||
DeviceLogListener,
|
||||
DevicePluginClient,
|
||||
LogLevel,
|
||||
SandyDevicePluginInstance as _SandyDevicePluginInstance,
|
||||
} from './plugin/DevicePlugin';
|
||||
export {SandyPluginDefinition as _SandyPluginDefinition} from './plugin/SandyPluginDefinition';
|
||||
@@ -75,16 +73,8 @@ export {
|
||||
|
||||
export {DataFormatter} from './ui/DataFormatter';
|
||||
|
||||
export {sleep} from './utils/sleep';
|
||||
export {timeout} from './utils/timeout';
|
||||
export {useLogger, _LoggerContext} from './utils/useLogger';
|
||||
|
||||
export {
|
||||
LogTypes,
|
||||
TrackType,
|
||||
Logger,
|
||||
useLogger,
|
||||
_LoggerContext,
|
||||
} from './utils/Logger';
|
||||
export {Idler} from './utils/Idler';
|
||||
|
||||
// Import from the index file directly, to make sure package.json's main field is skipped.
|
||||
@@ -143,4 +133,12 @@ export {textContent} from './utils/textContent';
|
||||
// T69106962
|
||||
export const TestUtils = TestUtilites;
|
||||
|
||||
export * from './types/server-types';
|
||||
export {
|
||||
sleep,
|
||||
timeout,
|
||||
DeviceOS,
|
||||
DeviceType,
|
||||
DeviceLogEntry,
|
||||
DeviceLogLevel,
|
||||
Logger,
|
||||
} from 'flipper-common';
|
||||
|
||||
@@ -11,29 +11,10 @@ import {SandyPluginDefinition} from './SandyPluginDefinition';
|
||||
import {BasePluginInstance, BasePluginClient} from './PluginBase';
|
||||
import {FlipperLib} from './FlipperLib';
|
||||
import {Atom, ReadOnlyAtom} from '../state/atom';
|
||||
import {DeviceOS, DeviceType} from '../types/server-types';
|
||||
import {DeviceOS, DeviceType, DeviceLogEntry} from 'flipper-common';
|
||||
|
||||
export type DeviceLogListener = (entry: DeviceLogEntry) => void;
|
||||
|
||||
export type DeviceLogEntry = {
|
||||
readonly date: Date;
|
||||
readonly type: LogLevel;
|
||||
readonly message: string;
|
||||
readonly pid: number;
|
||||
readonly tid: number;
|
||||
readonly app?: string;
|
||||
readonly tag: string;
|
||||
};
|
||||
|
||||
export type LogLevel =
|
||||
| 'unknown'
|
||||
| 'verbose'
|
||||
| 'debug'
|
||||
| 'info'
|
||||
| 'warn'
|
||||
| 'error'
|
||||
| 'fatal';
|
||||
|
||||
export interface Device {
|
||||
readonly isArchived: boolean;
|
||||
readonly isConnected: boolean;
|
||||
|
||||
@@ -30,7 +30,6 @@ import {
|
||||
import {SandyPluginRenderer} from '../plugin/PluginRenderer';
|
||||
import {act} from '@testing-library/react';
|
||||
import {
|
||||
DeviceLogEntry,
|
||||
SandyDevicePluginInstance,
|
||||
Device,
|
||||
DeviceLogListener,
|
||||
@@ -41,6 +40,7 @@ import {stubLogger} from '../utils/Logger';
|
||||
import {Idler} from '../utils/Idler';
|
||||
import {createState} from '../state/atom';
|
||||
import baseMockConsole from 'jest-mock-console';
|
||||
import {DeviceLogEntry} from 'flipper-common';
|
||||
|
||||
type Renderer = RenderResult<typeof queries>;
|
||||
|
||||
|
||||
@@ -1,138 +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 {
|
||||
DeviceSpec,
|
||||
DeviceType as PluginDeviceType,
|
||||
OS as PluginOS,
|
||||
} from 'flipper-plugin-lib';
|
||||
import {DeviceLogEntry} from '../plugin/DevicePlugin';
|
||||
|
||||
// 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 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;
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
import {render, fireEvent} from '@testing-library/react';
|
||||
import {TestUtils} from '../../';
|
||||
import {sleep} from '../../utils/sleep';
|
||||
import {sleep} from 'flipper-common';
|
||||
import React, {Component} from 'react';
|
||||
import {
|
||||
setGlobalInteractionReporter,
|
||||
|
||||
@@ -11,7 +11,7 @@ import * as React from 'react';
|
||||
import {render, fireEvent, waitFor, act} from '@testing-library/react';
|
||||
|
||||
import {DataInspector} from '../DataInspector';
|
||||
import {sleep} from '../../../utils/sleep';
|
||||
import {sleep} from 'flipper-common';
|
||||
|
||||
const json = {
|
||||
data: {
|
||||
|
||||
@@ -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