global.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { Adb, AdbBackend, AdbPacketData } from "@yume-chan/adb";
  2. import { action, makeAutoObservable, observable } from 'mobx';
  3. export type PacketLogItemDirection = 'in' | 'out';
  4. export {}
  5. export interface PacketLogItem extends AdbPacketData {
  6. direction: PacketLogItemDirection;
  7. commandString?: string;
  8. arg0String?: string;
  9. arg1String?: string;
  10. payloadString?: string;
  11. }
  12. declare global {
  13. interface Window {
  14. isdevice: any;//全局变量名
  15. isconnect: any;//全局变量名
  16. }
  17. }
  18. export class GlobalStateType {
  19. backend: AdbBackend | undefined = undefined;
  20. device: Adb | undefined = undefined;
  21. errorDialogVisible = false;
  22. errorDialogMessage = '';
  23. logs: PacketLogItem[] = [];
  24. constructor() {
  25. makeAutoObservable(this, {
  26. hideErrorDialog: action.bound,
  27. logs: observable.shallow,
  28. });
  29. }
  30. setDevice(backend: AdbBackend | undefined, device: Adb | undefined) {
  31. this.backend = backend;
  32. this.device = device;
  33. }
  34. showErrorDialog(message: Error | string) {
  35. this.errorDialogVisible = true;
  36. if (message instanceof Error) {
  37. this.errorDialogMessage = message.stack || message.message;
  38. } else {
  39. this.errorDialogMessage = message;
  40. }
  41. }
  42. hideErrorDialog() {
  43. this.errorDialogVisible = false;
  44. }
  45. appendLog(direction: PacketLogItemDirection, packet: AdbPacketData) {
  46. (packet as PacketLogItem).direction = direction;
  47. this.logs.push((packet as PacketLogItem));
  48. }
  49. clearLog() {
  50. this.logs = [];
  51. }
  52. }
  53. export const GlobalState = new GlobalStateType();