This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
TraceUpdatesBackendManager.js
102 lines (82 loc) · 2.73 KB
/
TraceUpdatesBackendManager.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const TraceUpdatesAbstractNodeMeasurer = require('./TraceUpdatesAbstractNodeMeasurer');
const TraceUpdatesAbstractNodePresenter = require('./TraceUpdatesAbstractNodePresenter');
const TraceUpdatesWebNodeMeasurer = require('./TraceUpdatesWebNodeMeasurer');
const TraceUpdatesWebNodePresenter = require('./TraceUpdatesWebNodePresenter');
import type {
Agent,
Measurement,
Measurer,
Presenter,
} from './TraceUpdatesTypes';
import type {ControlState} from '../../frontend/types.js';
const NODE_TYPE_COMPOSITE = 'Composite';
const NODE_TYPE_SPECIAL = 'Special';
class TraceUpdatesBackendManager {
_onMeasureNode: () => void;
_measurer: Measurer;
_presenter: Presenter;
_isActive: boolean;
constructor(agent: Agent) {
this._onMeasureNode = this._onMeasureNode.bind(this);
var useDOM = agent.capabilities.dom;
this._measurer = useDOM ?
new TraceUpdatesWebNodeMeasurer() :
new TraceUpdatesAbstractNodeMeasurer();
this._presenter = useDOM ?
new TraceUpdatesWebNodePresenter() :
new TraceUpdatesAbstractNodePresenter();
this._isActive = false;
agent.on('traceupdatesstatechange', this._onTraceUpdatesStateChange.bind(this));
agent.on('update', this._onUpdate.bind(this, agent));
agent.on('shutdown', this._shutdown.bind(this));
}
_onUpdate(agent: Agent, obj: any) {
if (!this._isActive || !obj.id) {
return;
}
// Highlighting every host node would be too noisy.
// We highlight user components and context consumers
// (without consumers, a context update that renders
// only host nodes directly wouldn't highlight at all).
const shouldHighlight = obj.nodeType === NODE_TYPE_COMPOSITE || (
obj.nodeType === NODE_TYPE_SPECIAL &&
obj.name === 'Context.Consumer'
);
if (!shouldHighlight) {
return;
}
var node = agent.getNodeForID(obj.id);
if (!node) {
return;
}
this._measurer.request(node, this._onMeasureNode);
}
_onMeasureNode(measurement: Measurement): void {
this._presenter.present(measurement);
}
_onTraceUpdatesStateChange(state: ControlState): void {
this._isActive = state.enabled;
this._presenter.setEnabled(state.enabled);
}
_shutdown(): void {
this._isActive = false;
this._presenter.setEnabled(false);
}
}
function init(agent: Agent): TraceUpdatesBackendManager {
return new TraceUpdatesBackendManager(agent);
}
module.exports = {
init,
};