Files
react-native-devtools-frontend/front_end/panels/application/BackgroundServiceModel.ts
T
Sigurd Schneider 79e812efee Move Target to its own file
Bug: chromium:1213943
Change-Id: I309436e8608c3f89ac42a117a3f454b5e43eef16
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2933138
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
2021-06-04 07:24:06 +00:00

63 lines
2.6 KiB
TypeScript

// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/* eslint-disable rulesdir/no_underscored_properties */
import * as SDK from '../../core/sdk/sdk.js';
import type * as ProtocolProxyApi from '../../generated/protocol-proxy-api.js';
import type * as Protocol from '../../generated/protocol.js';
export class BackgroundServiceModel extends SDK.SDKModel.SDKModel implements
ProtocolProxyApi.BackgroundServiceDispatcher {
_backgroundServiceAgent: ProtocolProxyApi.BackgroundServiceApi;
_events: Map<Protocol.BackgroundService.ServiceName, Protocol.BackgroundService.BackgroundServiceEvent[]>;
constructor(target: SDK.Target.Target) {
super(target);
this._backgroundServiceAgent = target.backgroundServiceAgent();
target.registerBackgroundServiceDispatcher(this);
this._events = new Map();
}
enable(service: Protocol.BackgroundService.ServiceName): void {
this._events.set(service, []);
this._backgroundServiceAgent.invoke_startObserving({service});
}
setRecording(shouldRecord: boolean, service: Protocol.BackgroundService.ServiceName): void {
this._backgroundServiceAgent.invoke_setRecording({shouldRecord, service});
}
clearEvents(service: Protocol.BackgroundService.ServiceName): void {
this._events.set(service, []);
this._backgroundServiceAgent.invoke_clearEvents({service});
}
getEvents(service: Protocol.BackgroundService.ServiceName): Protocol.BackgroundService.BackgroundServiceEvent[] {
return this._events.get(service) || [];
}
recordingStateChanged({isRecording, service}: Protocol.BackgroundService.RecordingStateChangedEvent): void {
this.dispatchEventToListeners(Events.RecordingStateChanged, {isRecording, serviceName: service});
}
backgroundServiceEventReceived({backgroundServiceEvent}:
Protocol.BackgroundService.BackgroundServiceEventReceivedEvent): void {
// TODO(crbug.com/1172300) Ignored during the jsdoc to ts migration)
// @ts-expect-error
this._events.get(backgroundServiceEvent.service).push(backgroundServiceEvent);
this.dispatchEventToListeners(Events.BackgroundServiceEventReceived, backgroundServiceEvent);
}
}
SDK.SDKModel.SDKModel.register(BackgroundServiceModel, {capabilities: SDK.Target.Capability.Browser, autostart: false});
// TODO(crbug.com/1167717): Make this a const enum again
// eslint-disable-next-line rulesdir/const_enum
export enum Events {
RecordingStateChanged = 'RecordingStateChanged',
BackgroundServiceEventReceived = 'BackgroundServiceEventReceived',
}