@atlaskit/editor-plugin-interactivity 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +65 -6
- package/afm-cc/tsconfig.json +12 -0
- package/afm-products/tsconfig.json +12 -0
- package/dist/cjs/analytics/fire-interactivity-event.js +39 -0
- package/dist/cjs/analytics/interactivity-snapshot.js +1 -0
- package/dist/cjs/collector/bucket-boundaries.js +137 -0
- package/dist/cjs/collector/editor-event-observer.js +63 -0
- package/dist/cjs/collector/interaction-events.js +62 -0
- package/dist/cjs/collector/interaction-group.js +185 -0
- package/dist/cjs/collector/interaction-observer.js +114 -0
- package/dist/cjs/collector/interaction-tracker.js +185 -0
- package/dist/cjs/collector/interactivity-collector.js +341 -0
- package/dist/cjs/collector/interactivity-session.js +55 -0
- package/dist/cjs/collector/lifecycle-observer.js +66 -0
- package/dist/cjs/collector/snapshot-scheduler.js +70 -0
- package/dist/cjs/interactivityPlugin.js +93 -6
- package/dist/es2019/analytics/fire-interactivity-event.js +33 -0
- package/dist/es2019/analytics/interactivity-snapshot.js +0 -0
- package/dist/es2019/collector/bucket-boundaries.js +117 -0
- package/dist/es2019/collector/editor-event-observer.js +46 -0
- package/dist/es2019/collector/interaction-events.js +55 -0
- package/dist/es2019/collector/interaction-group.js +125 -0
- package/dist/es2019/collector/interaction-observer.js +90 -0
- package/dist/es2019/collector/interaction-tracker.js +156 -0
- package/dist/es2019/collector/interactivity-collector.js +274 -0
- package/dist/es2019/collector/interactivity-session.js +45 -0
- package/dist/es2019/collector/lifecycle-observer.js +46 -0
- package/dist/es2019/collector/snapshot-scheduler.js +48 -0
- package/dist/es2019/interactivityPlugin.js +87 -6
- package/dist/esm/analytics/fire-interactivity-event.js +33 -0
- package/dist/esm/analytics/interactivity-snapshot.js +0 -0
- package/dist/esm/collector/bucket-boundaries.js +130 -0
- package/dist/esm/collector/editor-event-observer.js +57 -0
- package/dist/esm/collector/interaction-events.js +55 -0
- package/dist/esm/collector/interaction-group.js +179 -0
- package/dist/esm/collector/interaction-observer.js +108 -0
- package/dist/esm/collector/interaction-tracker.js +179 -0
- package/dist/esm/collector/interactivity-collector.js +334 -0
- package/dist/esm/collector/interactivity-session.js +48 -0
- package/dist/esm/collector/lifecycle-observer.js +59 -0
- package/dist/esm/collector/snapshot-scheduler.js +63 -0
- package/dist/esm/interactivityPlugin.js +93 -6
- package/dist/types/analytics/fire-interactivity-event.d.ts +9 -0
- package/dist/types/analytics/interactivity-snapshot.d.ts +66 -0
- package/dist/types/collector/bucket-boundaries.d.ts +35 -0
- package/dist/types/collector/editor-event-observer.d.ts +19 -0
- package/dist/types/collector/interaction-events.d.ts +19 -0
- package/dist/types/collector/interaction-group.d.ts +37 -0
- package/dist/types/collector/interaction-observer.d.ts +39 -0
- package/dist/types/collector/interaction-tracker.d.ts +65 -0
- package/dist/types/collector/interactivity-collector.d.ts +90 -0
- package/dist/types/collector/interactivity-session.d.ts +42 -0
- package/dist/types/collector/lifecycle-observer.d.ts +25 -0
- package/dist/types/collector/snapshot-scheduler.d.ts +15 -0
- package/dist/types/interactivityPlugin.d.ts +6 -4
- package/dist/types/interactivityPluginType.d.ts +11 -2
- package/docs/0-intro.tsx +26 -7
- package/package.json +8 -3
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import { REPORTING_THRESHOLD_MS } from './bucket-boundaries';
|
|
4
|
+
/** `performance.interactionCount` is Chromium-only and absent from the DOM typings. */
|
|
5
|
+
var interactionCount = function interactionCount() {
|
|
6
|
+
return performance.interactionCount;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
/** `durationThreshold` is absent from the DOM typings for `PerformanceObserverInit`. */
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reports the interactions the browser observes to `onEntries`.
|
|
13
|
+
*
|
|
14
|
+
* `drain` and `stop` are safe to call before `start` and after each other, so a session
|
|
15
|
+
* that never started collecting needs no special handling.
|
|
16
|
+
*/
|
|
17
|
+
export var InteractionObserver = /*#__PURE__*/function () {
|
|
18
|
+
function InteractionObserver(onEntries) {
|
|
19
|
+
_classCallCheck(this, InteractionObserver);
|
|
20
|
+
this.onEntries = onEntries;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Starts reporting interactions from this point on. Does nothing when already started, so
|
|
25
|
+
* a second call cannot leave an observer running with nobody to disconnect it.
|
|
26
|
+
*
|
|
27
|
+
* `buffered` is `false`: the entries the browser collected earlier are interactions with
|
|
28
|
+
* the page while the editor was still loading, and they belong to no session of ours.
|
|
29
|
+
*/
|
|
30
|
+
return _createClass(InteractionObserver, [{
|
|
31
|
+
key: "start",
|
|
32
|
+
value: function start() {
|
|
33
|
+
var _this = this;
|
|
34
|
+
if (this.observer) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.observer = new PerformanceObserver(function (list) {
|
|
38
|
+
// Delay by a microtask to work around a Safari bug where the callback is
|
|
39
|
+
// invoked synchronously rather than in a separate task.
|
|
40
|
+
// See: https://github.com/GoogleChrome/web-vitals/issues/277
|
|
41
|
+
Promise.resolve().then(function () {
|
|
42
|
+
_this.onEntries(list.getEntries());
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
var init = {
|
|
46
|
+
type: 'event',
|
|
47
|
+
buffered: false,
|
|
48
|
+
// 16 ms is also the smallest value the spec honours; lower values are clamped.
|
|
49
|
+
durationThreshold: REPORTING_THRESHOLD_MS
|
|
50
|
+
};
|
|
51
|
+
this.observer.observe(init);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Synchronously reports the entries the browser has produced but not yet dispatched to
|
|
56
|
+
* the callback. A snapshot taken because the page is going away has to include them,
|
|
57
|
+
* because there is no later chance to.
|
|
58
|
+
*/
|
|
59
|
+
}, {
|
|
60
|
+
key: "drain",
|
|
61
|
+
value: function drain() {
|
|
62
|
+
var _this$observer;
|
|
63
|
+
var entries = (_this$observer = this.observer) === null || _this$observer === void 0 ? void 0 : _this$observer.takeRecords();
|
|
64
|
+
if (entries) {
|
|
65
|
+
this.onEntries(entries);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}, {
|
|
69
|
+
key: "stop",
|
|
70
|
+
value: function stop() {
|
|
71
|
+
var _this$observer2;
|
|
72
|
+
(_this$observer2 = this.observer) === null || _this$observer2 === void 0 || _this$observer2.disconnect();
|
|
73
|
+
this.observer = undefined;
|
|
74
|
+
}
|
|
75
|
+
}], [{
|
|
76
|
+
key: "isSupported",
|
|
77
|
+
value:
|
|
78
|
+
/**
|
|
79
|
+
* Whether the browser reports both things a session needs: `interactionId`, which groups
|
|
80
|
+
* entries into interactions, and `performance.interactionCount`, which counts the ones
|
|
81
|
+
* below the reporting threshold. Both are Chromium-only, and without the count
|
|
82
|
+
* `totalCount` would be indistinguishable from `observedCount`.
|
|
83
|
+
*/
|
|
84
|
+
function isSupported() {
|
|
85
|
+
if (typeof window === 'undefined' || typeof PerformanceObserver === 'undefined') {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (!('PerformanceEventTiming' in window) || !('interactionId' in PerformanceEventTiming.prototype)) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (!PerformanceObserver.supportedEntryTypes.includes('event')) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
return typeof interactionCount() === 'number';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Total interactions on the page since page load, including those below the reporting
|
|
99
|
+
* threshold that no observer ever sees. A property of the page, not of an observer.
|
|
100
|
+
*/
|
|
101
|
+
}, {
|
|
102
|
+
key: "readPageInteractionCount",
|
|
103
|
+
value: function readPageInteractionCount() {
|
|
104
|
+
var _interactionCount;
|
|
105
|
+
return (_interactionCount = interactionCount()) !== null && _interactionCount !== void 0 ? _interactionCount : 0;
|
|
106
|
+
}
|
|
107
|
+
}]);
|
|
108
|
+
}();
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
4
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
5
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
6
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
7
|
+
import { interactionEventKind } from './interaction-events';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Event Timing fields this package reads. `interactionId` is missing from the DOM
|
|
11
|
+
* typings' `PerformanceEventTiming`, so it is declared here as optional, which also makes
|
|
12
|
+
* a `PerformanceEntry` from `getEntries()` assignable without a cast.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* What an entry did to the interaction it belongs to: either it is the first entry of a new
|
|
17
|
+
* interaction, or it measured an interaction that was already counted as slower than it was
|
|
18
|
+
* known to be. Both carry the editor group of the interaction, if it is one of the editor's.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* How many interactions are remembered, so their growth can still be applied, and how many of
|
|
23
|
+
* the editor's events. Entries of one interaction arrive within the interaction itself, so
|
|
24
|
+
* anything older than the last few hundred is not needed.
|
|
25
|
+
*/
|
|
26
|
+
var MAX_TRACKED = 256;
|
|
27
|
+
/**
|
|
28
|
+
* How many are dropped per cleanup. Dropping one at a time would run a cleanup on every new
|
|
29
|
+
* interaction once the limit is reached; a batch makes it one cleanup per 64 of them.
|
|
30
|
+
*/
|
|
31
|
+
var PRUNE_BATCH = 64;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Identifies the event an entry measured: an entry's `startTime` is that event's timestamp and its
|
|
35
|
+
* `name` is its type. The type is in the key because browsers coarsen the timestamp, so two events
|
|
36
|
+
* of one task can share it — and events of one type are always of one group, so a collision cannot
|
|
37
|
+
* move an interaction into another group.
|
|
38
|
+
*/
|
|
39
|
+
function eventKey(type, timeStamp) {
|
|
40
|
+
return "".concat(type, "|").concat(timeStamp);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Makes interactions out of what the two observers report, for one session.
|
|
45
|
+
*
|
|
46
|
+
* Entries sharing a non-zero `interactionId` are one interaction whose latency is the
|
|
47
|
+
* maximum `duration` among them. Entries arrive incrementally, so an interaction's latency
|
|
48
|
+
* can grow after it was first reported — callers apply that to what they already counted
|
|
49
|
+
* rather than counting the interaction twice.
|
|
50
|
+
*
|
|
51
|
+
* A non-zero `interactionId` is the browser's own definition of an interaction, which is
|
|
52
|
+
* also what INP filters on: it is assigned to the pointer and keyboard events that make one
|
|
53
|
+
* up, and never to scrolling or pointer movement.
|
|
54
|
+
*
|
|
55
|
+
* The editor's events answer what an entry cannot: which interactions were with the editor, and
|
|
56
|
+
* how many there were, including the ones below the Event Timing reporting threshold.
|
|
57
|
+
*/
|
|
58
|
+
export var InteractionTracker = /*#__PURE__*/function () {
|
|
59
|
+
/**
|
|
60
|
+
* @param startsAfterInteractionId interactions up to and including this one belong to the
|
|
61
|
+
* previous tracker and are ignored. `interactionId` counts up over the life of the page, so
|
|
62
|
+
* a session opening mid-page passes the highest id the one before it saw; without that, an
|
|
63
|
+
* entry still arriving for an interaction from the previous session would look new here and
|
|
64
|
+
* be counted in both.
|
|
65
|
+
*/
|
|
66
|
+
function InteractionTracker() {
|
|
67
|
+
var startsAfterInteractionId = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
68
|
+
_classCallCheck(this, InteractionTracker);
|
|
69
|
+
_defineProperty(this, "interactions", new Map());
|
|
70
|
+
_defineProperty(this, "groupByEvent", new Map());
|
|
71
|
+
_defineProperty(this, "highestInteractionId", 0);
|
|
72
|
+
this.startsAfterInteractionId = startsAfterInteractionId;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The highest `interactionId` this tracker has seen. */
|
|
76
|
+
return _createClass(InteractionTracker, [{
|
|
77
|
+
key: "lastInteractionId",
|
|
78
|
+
get: function get() {
|
|
79
|
+
return Math.max(this.highestInteractionId, this.startsAfterInteractionId);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Merges an entry into the interaction it belongs to.
|
|
84
|
+
*
|
|
85
|
+
* @returns what that did to the interaction's latency, or nothing when the entry is not
|
|
86
|
+
* part of an interaction, belongs to a previous tracker, or does not change one.
|
|
87
|
+
*/
|
|
88
|
+
}, {
|
|
89
|
+
key: "merge",
|
|
90
|
+
value: function merge(entry) {
|
|
91
|
+
var interactionId = entry.interactionId;
|
|
92
|
+
// `first-input` and non-interaction events report `interactionId` 0.
|
|
93
|
+
if (!interactionId) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
if (interactionId <= this.startsAfterInteractionId) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
this.highestInteractionId = Math.max(this.highestInteractionId, interactionId);
|
|
100
|
+
|
|
101
|
+
// Every latency is counted through here, so this is where one that cannot be measured
|
|
102
|
+
// is rejected: a `NaN` getting through becomes a `NaN` bucket key and a `NaN` `sumMs`
|
|
103
|
+
// for the rest of the session.
|
|
104
|
+
if (!Number.isFinite(entry.duration) || entry.duration < 0) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
var tracked = this.interactions.get(interactionId);
|
|
108
|
+
if (tracked === undefined) {
|
|
109
|
+
// Taken once: an entry that only makes the interaction slower has to move its count
|
|
110
|
+
// within the group it was counted in, not into another one.
|
|
111
|
+
var group = this.groupByEvent.get(eventKey(entry.name, entry.startTime));
|
|
112
|
+
this.interactions.set(interactionId, {
|
|
113
|
+
latencyMs: entry.duration,
|
|
114
|
+
group: group
|
|
115
|
+
});
|
|
116
|
+
this.prune(this.interactions);
|
|
117
|
+
return {
|
|
118
|
+
type: 'new',
|
|
119
|
+
latencyMs: entry.duration,
|
|
120
|
+
group: group
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (entry.duration <= tracked.latencyMs) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
var fromMs = tracked.latencyMs;
|
|
127
|
+
tracked.latencyMs = entry.duration;
|
|
128
|
+
return {
|
|
129
|
+
type: 'remeasured',
|
|
130
|
+
fromMs: fromMs,
|
|
131
|
+
toMs: entry.duration,
|
|
132
|
+
group: tracked.group
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @returns the group of an interaction to count, when this is the event its group counts on. */
|
|
137
|
+
}, {
|
|
138
|
+
key: "recordEditorEvent",
|
|
139
|
+
value: function recordEditorEvent(event) {
|
|
140
|
+
var kind = interactionEventKind(event.type);
|
|
141
|
+
if (!kind) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Every event of the interaction, because any of them can be the one Event Timing reports
|
|
146
|
+
// as the slowest: for a pointer press that is usually the click.
|
|
147
|
+
this.groupByEvent.set(eventKey(event.type, event.timeStamp), kind.group);
|
|
148
|
+
this.prune(this.groupByEvent);
|
|
149
|
+
return kind.counts ? kind.group : undefined;
|
|
150
|
+
}
|
|
151
|
+
}, {
|
|
152
|
+
key: "prune",
|
|
153
|
+
value: function prune(entries) {
|
|
154
|
+
if (entries.size <= MAX_TRACKED) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// `Map` preserves insertion order, so the entries inserted first are the least likely to
|
|
159
|
+
// see another entry or another event.
|
|
160
|
+
var remaining = PRUNE_BATCH;
|
|
161
|
+
var _iterator = _createForOfIteratorHelper(entries.keys()),
|
|
162
|
+
_step;
|
|
163
|
+
try {
|
|
164
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
165
|
+
var key = _step.value;
|
|
166
|
+
entries.delete(key);
|
|
167
|
+
remaining -= 1;
|
|
168
|
+
if (remaining === 0) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} catch (err) {
|
|
173
|
+
_iterator.e(err);
|
|
174
|
+
} finally {
|
|
175
|
+
_iterator.f();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}]);
|
|
179
|
+
}();
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
2
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
4
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
5
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
6
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
7
|
+
import { getDocument } from '@atlaskit/browser-apis';
|
|
8
|
+
import { EditorEventObserver } from './editor-event-observer';
|
|
9
|
+
import { InteractionObserver } from './interaction-observer';
|
|
10
|
+
import { InteractivitySession } from './interactivity-session';
|
|
11
|
+
import { SCHEMA_VERSION } from './bucket-boundaries';
|
|
12
|
+
import { LifecycleObserver } from './lifecycle-observer';
|
|
13
|
+
import { SnapshotScheduler } from './snapshot-scheduler';
|
|
14
|
+
/**
|
|
15
|
+
* Collects interaction latencies for one editor mount and emits session-to-date snapshots.
|
|
16
|
+
*
|
|
17
|
+
* Every interaction is counted in `page`, and the ones with the editor again in one of the editor
|
|
18
|
+
* groups, which is what tells a slow editor apart from a slow page around it.
|
|
19
|
+
*
|
|
20
|
+
* `start` and `stop` bound the collecting, which happens once. Within it there can be several
|
|
21
|
+
* sessions, because a session covers one document in one mode: a Confluence live page
|
|
22
|
+
* navigates and switches between reading and editing without ever remounting the editor, and
|
|
23
|
+
* each of those closes the current session and opens the next.
|
|
24
|
+
*/
|
|
25
|
+
export var InteractivityCollector = /*#__PURE__*/function () {
|
|
26
|
+
function InteractivityCollector(options) {
|
|
27
|
+
var _this = this;
|
|
28
|
+
_classCallCheck(this, InteractivityCollector);
|
|
29
|
+
_defineProperty(this, "started", false);
|
|
30
|
+
_defineProperty(this, "stopped", false);
|
|
31
|
+
this.emit = options.emit;
|
|
32
|
+
this.getEditorDomSize = options.getEditorDomSize;
|
|
33
|
+
this.getNodeSize = options.getNodeSize;
|
|
34
|
+
this.getObjectId = options.getObjectId;
|
|
35
|
+
this.getSessionMode = options.getSessionMode;
|
|
36
|
+
this.interactionObserver = new InteractionObserver(function (entries) {
|
|
37
|
+
return _this.recordEntries(entries);
|
|
38
|
+
});
|
|
39
|
+
this.editorEvents = new EditorEventObserver(function (event) {
|
|
40
|
+
return _this.recordEditorEvent(event);
|
|
41
|
+
});
|
|
42
|
+
this.snapshotScheduler = new SnapshotScheduler(function () {
|
|
43
|
+
return _this.onTimer();
|
|
44
|
+
});
|
|
45
|
+
this.lifecycleObserver = new LifecycleObserver({
|
|
46
|
+
onHidden: function onHidden() {
|
|
47
|
+
return _this.onHidden();
|
|
48
|
+
},
|
|
49
|
+
onVisible: function onVisible() {
|
|
50
|
+
return _this.onVisible();
|
|
51
|
+
},
|
|
52
|
+
onPageHide: function onPageHide() {
|
|
53
|
+
return _this.onPageHide();
|
|
54
|
+
},
|
|
55
|
+
onPageShow: function onPageShow() {
|
|
56
|
+
return _this.onPageShow();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
this.session = this.createSession(0);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Starts collecting. Calling it again while collecting changes nothing.
|
|
64
|
+
*
|
|
65
|
+
* @returns whether collecting is running; `false` when the browser cannot support it, or
|
|
66
|
+
* when it has already been stopped.
|
|
67
|
+
*/
|
|
68
|
+
return _createClass(InteractivityCollector, [{
|
|
69
|
+
key: "start",
|
|
70
|
+
value: function start() {
|
|
71
|
+
// Collecting is over for good once stopped: the session it covered has been reported.
|
|
72
|
+
if (this.stopped) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (this.started) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (!InteractionObserver.isSupported()) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Opened again so the session's timings start with the collecting, not with whenever
|
|
83
|
+
// this object was constructed.
|
|
84
|
+
this.session = this.createSession(0);
|
|
85
|
+
this.interactionObserver.start();
|
|
86
|
+
this.editorEvents.observe(this.editorRoot);
|
|
87
|
+
this.snapshotScheduler.start();
|
|
88
|
+
this.lifecycleObserver.start();
|
|
89
|
+
this.started = true;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Reports the session as ended by the editor unmounting, and stops collecting. */
|
|
94
|
+
}, {
|
|
95
|
+
key: "stop",
|
|
96
|
+
value: function stop() {
|
|
97
|
+
if (this.stopped) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.takeSnapshot('unmount');
|
|
101
|
+
this.stopped = true;
|
|
102
|
+
this.interactionObserver.stop();
|
|
103
|
+
this.editorEvents.stop();
|
|
104
|
+
this.snapshotScheduler.stop();
|
|
105
|
+
this.lifecycleObserver.stop();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The element the editor renders itself into is what makes an interaction one of the editor's.
|
|
110
|
+
* The session is not tied to it: interactions from before it arrives are counted in `page`.
|
|
111
|
+
*/
|
|
112
|
+
}, {
|
|
113
|
+
key: "setEditorRoot",
|
|
114
|
+
value: function setEditorRoot(root) {
|
|
115
|
+
this.editorRoot = root !== null && root !== void 0 ? root : undefined;
|
|
116
|
+
if (this.started && !this.stopped) {
|
|
117
|
+
this.editorEvents.observe(this.editorRoot);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Rotates the session when the editor is pointed at different content.
|
|
123
|
+
*
|
|
124
|
+
* An unknown object id is no information, never a change. The provider resolves
|
|
125
|
+
* asynchronously after mount, and `contextIdentifierPlugin` resets its state to the
|
|
126
|
+
* configured provider on transactions that do not carry a new one, so the id read here goes
|
|
127
|
+
* missing for a moment on a document that never changed.
|
|
128
|
+
*/
|
|
129
|
+
}, {
|
|
130
|
+
key: "onObjectIdChanged",
|
|
131
|
+
value: function onObjectIdChanged() {
|
|
132
|
+
if (this.stopped) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
var objectId = this.getObjectId();
|
|
136
|
+
if (objectId === undefined || objectId === this.session.objectId) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (this.session.objectId === undefined) {
|
|
140
|
+
this.session.objectId = objectId;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this.rotateSession('navigation');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Rotates the session when the editor switches between reading and editing. Interactions
|
|
148
|
+
* with a read-only page are a different population from interactions while editing, so one
|
|
149
|
+
* session never covers both.
|
|
150
|
+
*/
|
|
151
|
+
}, {
|
|
152
|
+
key: "onViewModeChanged",
|
|
153
|
+
value: function onViewModeChanged() {
|
|
154
|
+
if (this.stopped) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
var mode = this.getSessionMode();
|
|
158
|
+
if (mode === undefined || mode === this.session.mode) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (this.session.mode === undefined) {
|
|
162
|
+
this.session.mode = mode;
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
this.rotateSession('modeChange');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param startsAfterInteractionId the highest interaction the previous session saw, so
|
|
170
|
+
* entries still arriving for it are not counted here as well.
|
|
171
|
+
*/
|
|
172
|
+
}, {
|
|
173
|
+
key: "createSession",
|
|
174
|
+
value: function createSession(startsAfterInteractionId) {
|
|
175
|
+
var _getDocument;
|
|
176
|
+
return new InteractivitySession({
|
|
177
|
+
objectId: this.getObjectId(),
|
|
178
|
+
mode: this.getSessionMode(),
|
|
179
|
+
hidden: ((_getDocument = getDocument()) === null || _getDocument === void 0 ? void 0 : _getDocument.visibilityState) === 'hidden',
|
|
180
|
+
startsAfterInteractionId: startsAfterInteractionId
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Reports the current session as ended by `reason` and puts a new one in its place. */
|
|
185
|
+
}, {
|
|
186
|
+
key: "rotateSession",
|
|
187
|
+
value: function rotateSession(reason) {
|
|
188
|
+
this.takeSnapshot(reason);
|
|
189
|
+
this.session = this.createSession(this.session.tracker.lastInteractionId);
|
|
190
|
+
this.snapshotScheduler.restart();
|
|
191
|
+
}
|
|
192
|
+
}, {
|
|
193
|
+
key: "onTimer",
|
|
194
|
+
value: function onTimer() {
|
|
195
|
+
this.takeSnapshot('timer');
|
|
196
|
+
}
|
|
197
|
+
}, {
|
|
198
|
+
key: "onHidden",
|
|
199
|
+
value: function onHidden() {
|
|
200
|
+
if (this.session.hiddenSince === undefined) {
|
|
201
|
+
this.session.hiddenSince = performance.now();
|
|
202
|
+
}
|
|
203
|
+
this.takeSnapshot('hidden');
|
|
204
|
+
}
|
|
205
|
+
}, {
|
|
206
|
+
key: "onVisible",
|
|
207
|
+
value: function onVisible() {
|
|
208
|
+
if (this.session.hiddenSince !== undefined) {
|
|
209
|
+
this.session.hiddenMs += performance.now() - this.session.hiddenSince;
|
|
210
|
+
this.session.hiddenSince = undefined;
|
|
211
|
+
}
|
|
212
|
+
this.session.lifecycleSnapshotEmitted = false;
|
|
213
|
+
}
|
|
214
|
+
}, {
|
|
215
|
+
key: "onPageHide",
|
|
216
|
+
value: function onPageHide() {
|
|
217
|
+
this.takeSnapshot('pagehide');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The page came back from the back/forward cache, which never raises a visibility change.
|
|
222
|
+
* The session continues, and the signal that suspended it has been reported, so the next
|
|
223
|
+
* one is due.
|
|
224
|
+
*/
|
|
225
|
+
}, {
|
|
226
|
+
key: "onPageShow",
|
|
227
|
+
value: function onPageShow() {
|
|
228
|
+
this.session.lifecycleSnapshotEmitted = false;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Counts an interaction with the editor, including the ones Event Timing never reports because
|
|
233
|
+
* they were faster than its threshold — which is why a count alone moves the session on.
|
|
234
|
+
*/
|
|
235
|
+
}, {
|
|
236
|
+
key: "recordEditorEvent",
|
|
237
|
+
value: function recordEditorEvent(event) {
|
|
238
|
+
if (this.stopped) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
var group = this.session.tracker.recordEditorEvent(event);
|
|
242
|
+
if (!group) {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// The session names its editor groups after the fields they are reported in.
|
|
247
|
+
this.session[group].countTotal();
|
|
248
|
+
this.session.revision += 1;
|
|
249
|
+
}
|
|
250
|
+
}, {
|
|
251
|
+
key: "recordEntries",
|
|
252
|
+
value: function recordEntries(entries) {
|
|
253
|
+
var _this$session = this.session,
|
|
254
|
+
tracker = _this$session.tracker,
|
|
255
|
+
page = _this$session.page;
|
|
256
|
+
var _iterator = _createForOfIteratorHelper(entries),
|
|
257
|
+
_step;
|
|
258
|
+
try {
|
|
259
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
260
|
+
var entry = _step.value;
|
|
261
|
+
var update = tracker.merge(entry);
|
|
262
|
+
if (!update) {
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
if (update.type === 'new') {
|
|
266
|
+
page.add(update.latencyMs);
|
|
267
|
+
if (update.group) {
|
|
268
|
+
this.session[update.group].add(update.latencyMs);
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
page.remeasure(update.fromMs, update.toMs);
|
|
272
|
+
if (update.group) {
|
|
273
|
+
this.session[update.group].remeasure(update.fromMs, update.toMs);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
this.session.revision += 1;
|
|
277
|
+
}
|
|
278
|
+
} catch (err) {
|
|
279
|
+
_iterator.e(err);
|
|
280
|
+
} finally {
|
|
281
|
+
_iterator.f();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}, {
|
|
285
|
+
key: "takeSnapshot",
|
|
286
|
+
value: function takeSnapshot(reason) {
|
|
287
|
+
if (this.stopped) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
var session = this.session;
|
|
291
|
+
|
|
292
|
+
// Checked before anything is consumed: `visibilitychange` and `pagehide` fire back to
|
|
293
|
+
// back on the same transition and it is reported once, and draining first would take
|
|
294
|
+
// entries out of the browser's queue only to suppress the snapshot carrying them.
|
|
295
|
+
var lifecycleSignal = reason === 'hidden' || reason === 'pagehide';
|
|
296
|
+
if (lifecycleSignal && session.lifecycleSnapshotEmitted) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Must run before the change check: the browser may be holding entries that have not
|
|
301
|
+
// reached the observer callback yet, and on `pagehide` there is no later chance to
|
|
302
|
+
// pick them up.
|
|
303
|
+
this.interactionObserver.drain();
|
|
304
|
+
if (session.revision === session.emittedRevision) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (lifecycleSignal) {
|
|
308
|
+
session.lifecycleSnapshotEmitted = true;
|
|
309
|
+
}
|
|
310
|
+
session.seq += 1;
|
|
311
|
+
session.emittedRevision = session.revision;
|
|
312
|
+
var now = performance.now();
|
|
313
|
+
var hiddenMs = session.hiddenMs + (session.hiddenSince === undefined ? 0 : now - session.hiddenSince);
|
|
314
|
+
var pageTotalCount = InteractionObserver.readPageInteractionCount() - session.interactionCountAtStart;
|
|
315
|
+
this.emit({
|
|
316
|
+
schema: SCHEMA_VERSION,
|
|
317
|
+
interactivitySessionId: session.id,
|
|
318
|
+
objectId: session.objectId,
|
|
319
|
+
seq: session.seq,
|
|
320
|
+
reason: reason,
|
|
321
|
+
sessionMode: session.mode,
|
|
322
|
+
activeMs: Math.round(Math.max(0, now - session.startedAt - hiddenMs)),
|
|
323
|
+
hiddenMs: Math.round(hiddenMs),
|
|
324
|
+
nodeSize: this.getNodeSize(),
|
|
325
|
+
editorDomSize: this.getEditorDomSize(),
|
|
326
|
+
// Only `page` is told its total; each editor group has counted its own.
|
|
327
|
+
page: session.page.snapshot(pageTotalCount),
|
|
328
|
+
editorTyping: session.editorTyping.snapshot(),
|
|
329
|
+
editorPointer: session.editorPointer.snapshot(),
|
|
330
|
+
editorOther: session.editorOther.snapshot()
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}]);
|
|
334
|
+
}();
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import _createClass from "@babel/runtime/helpers/createClass";
|
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
4
|
+
import { InteractionObserver } from './interaction-observer';
|
|
5
|
+
import { InteractionTracker } from './interaction-tracker';
|
|
6
|
+
import { InteractionGroup } from './interaction-group';
|
|
7
|
+
function createSessionId() {
|
|
8
|
+
if (typeof crypto.randomUUID === 'function') {
|
|
9
|
+
return crypto.randomUUID();
|
|
10
|
+
}
|
|
11
|
+
var bytes = new Uint8Array(16);
|
|
12
|
+
crypto.getRandomValues(bytes);
|
|
13
|
+
return Array.from(bytes, function (byte) {
|
|
14
|
+
return byte.toString(16).padStart(2, '0');
|
|
15
|
+
}).join('');
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Everything that belongs to one session.
|
|
19
|
+
*
|
|
20
|
+
* Nothing here is reset — the next session is a new instance — so a field added here cannot
|
|
21
|
+
* be left carrying the previous session's value.
|
|
22
|
+
*/
|
|
23
|
+
export var InteractivitySession = /*#__PURE__*/_createClass(function InteractivitySession(start) {
|
|
24
|
+
_classCallCheck(this, InteractivitySession);
|
|
25
|
+
_defineProperty(this, "id", createSessionId());
|
|
26
|
+
_defineProperty(this, "startedAt", performance.now());
|
|
27
|
+
/** Page interaction count when the session opened, subtracted to get its own total. */
|
|
28
|
+
_defineProperty(this, "interactionCountAtStart", InteractionObserver.readPageInteractionCount());
|
|
29
|
+
_defineProperty(this, "page", new InteractionGroup());
|
|
30
|
+
_defineProperty(this, "editorTyping", new InteractionGroup());
|
|
31
|
+
_defineProperty(this, "editorPointer", new InteractionGroup());
|
|
32
|
+
_defineProperty(this, "editorOther", new InteractionGroup());
|
|
33
|
+
/** Increments per snapshot; a query takes the highest one per session. */
|
|
34
|
+
_defineProperty(this, "seq", 0);
|
|
35
|
+
_defineProperty(this, "hiddenMs", 0);
|
|
36
|
+
/**
|
|
37
|
+
* Bumped on every change to the accumulated data. A snapshot is emitted only when this
|
|
38
|
+
* has moved past `emittedRevision`, so identical snapshots are never sent twice.
|
|
39
|
+
*/
|
|
40
|
+
_defineProperty(this, "revision", 0);
|
|
41
|
+
_defineProperty(this, "emittedRevision", 0);
|
|
42
|
+
/** One lifecycle snapshot per hidden episode; cleared when the page comes back. */
|
|
43
|
+
_defineProperty(this, "lifecycleSnapshotEmitted", false);
|
|
44
|
+
this.objectId = start.objectId;
|
|
45
|
+
this.mode = start.mode;
|
|
46
|
+
this.hiddenSince = start.hidden ? this.startedAt : undefined;
|
|
47
|
+
this.tracker = new InteractionTracker(start.startsAfterInteractionId);
|
|
48
|
+
});
|