forked from ChromeDevTools/devtools-frontend
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathReactNativeApplicationModel.ts
More file actions
68 lines (54 loc) · 2.15 KB
/
ReactNativeApplicationModel.ts
File metadata and controls
68 lines (54 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Meta Platforms, Inc. and affiliates.
// Copyright 2024 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.
import * as Host from '../../core/host/host.js';
import type * as ProtocolProxyApi from '../../generated/protocol-proxy-api.js';
import type * as Protocol from '../../generated/protocol.js';
import {SDKModel} from './SDKModel.js';
import type {Target} from './Target.js';
export class ReactNativeApplicationModel extends SDKModel<EventTypes> implements ProtocolProxyApi.ReactNativeApplicationDispatcher {
#enabled: boolean;
readonly #agent: ProtocolProxyApi.ReactNativeApplicationApi;
metadataCached: Protocol.ReactNativeApplication.MetadataUpdatedEvent | null = null;
constructor(target: Target) {
super(target);
Host.rnPerfMetrics.fuseboxSetClientMetadataStarted();
this.#enabled = false;
this.#agent = target.reactNativeApplicationAgent();
target.registerReactNativeApplicationDispatcher(this);
// Auto-init. Paired with registering this model immediately in rn_fusebox.ts.
this.ensureEnabled();
}
ensureEnabled(): void {
if (this.#enabled) {
return;
}
void this.#agent.invoke_enable()
.then(result => {
const maybeError = result.getError();
const success = !maybeError;
Host.rnPerfMetrics.fuseboxSetClientMetadataFinished(success, maybeError);
})
.catch(reason => {
const success = false;
Host.rnPerfMetrics.fuseboxSetClientMetadataFinished(success, reason);
});
this.#enabled = true;
}
metadataUpdated(metadata: Protocol.ReactNativeApplication.MetadataUpdatedEvent): void {
this.metadataCached = metadata;
this.dispatchEventToListeners(Events.METADATA_UPDATED, metadata);
}
traceRequested(): void {
this.dispatchEventToListeners(Events.TRACE_REQUESTED);
}
}
export const enum Events {
METADATA_UPDATED = 'MetadataUpdated',
TRACE_REQUESTED = 'TraceRequested',
}
export interface EventTypes {
[Events.METADATA_UPDATED]: Protocol.ReactNativeApplication.MetadataUpdatedEvent;
[Events.TRACE_REQUESTED]: void;
}