@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.
Files changed (59) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +65 -6
  3. package/afm-cc/tsconfig.json +12 -0
  4. package/afm-products/tsconfig.json +12 -0
  5. package/dist/cjs/analytics/fire-interactivity-event.js +39 -0
  6. package/dist/cjs/analytics/interactivity-snapshot.js +1 -0
  7. package/dist/cjs/collector/bucket-boundaries.js +137 -0
  8. package/dist/cjs/collector/editor-event-observer.js +63 -0
  9. package/dist/cjs/collector/interaction-events.js +62 -0
  10. package/dist/cjs/collector/interaction-group.js +185 -0
  11. package/dist/cjs/collector/interaction-observer.js +114 -0
  12. package/dist/cjs/collector/interaction-tracker.js +185 -0
  13. package/dist/cjs/collector/interactivity-collector.js +341 -0
  14. package/dist/cjs/collector/interactivity-session.js +55 -0
  15. package/dist/cjs/collector/lifecycle-observer.js +66 -0
  16. package/dist/cjs/collector/snapshot-scheduler.js +70 -0
  17. package/dist/cjs/interactivityPlugin.js +93 -6
  18. package/dist/es2019/analytics/fire-interactivity-event.js +33 -0
  19. package/dist/es2019/analytics/interactivity-snapshot.js +0 -0
  20. package/dist/es2019/collector/bucket-boundaries.js +117 -0
  21. package/dist/es2019/collector/editor-event-observer.js +46 -0
  22. package/dist/es2019/collector/interaction-events.js +55 -0
  23. package/dist/es2019/collector/interaction-group.js +125 -0
  24. package/dist/es2019/collector/interaction-observer.js +90 -0
  25. package/dist/es2019/collector/interaction-tracker.js +156 -0
  26. package/dist/es2019/collector/interactivity-collector.js +274 -0
  27. package/dist/es2019/collector/interactivity-session.js +45 -0
  28. package/dist/es2019/collector/lifecycle-observer.js +46 -0
  29. package/dist/es2019/collector/snapshot-scheduler.js +48 -0
  30. package/dist/es2019/interactivityPlugin.js +87 -6
  31. package/dist/esm/analytics/fire-interactivity-event.js +33 -0
  32. package/dist/esm/analytics/interactivity-snapshot.js +0 -0
  33. package/dist/esm/collector/bucket-boundaries.js +130 -0
  34. package/dist/esm/collector/editor-event-observer.js +57 -0
  35. package/dist/esm/collector/interaction-events.js +55 -0
  36. package/dist/esm/collector/interaction-group.js +179 -0
  37. package/dist/esm/collector/interaction-observer.js +108 -0
  38. package/dist/esm/collector/interaction-tracker.js +179 -0
  39. package/dist/esm/collector/interactivity-collector.js +334 -0
  40. package/dist/esm/collector/interactivity-session.js +48 -0
  41. package/dist/esm/collector/lifecycle-observer.js +59 -0
  42. package/dist/esm/collector/snapshot-scheduler.js +63 -0
  43. package/dist/esm/interactivityPlugin.js +93 -6
  44. package/dist/types/analytics/fire-interactivity-event.d.ts +9 -0
  45. package/dist/types/analytics/interactivity-snapshot.d.ts +66 -0
  46. package/dist/types/collector/bucket-boundaries.d.ts +35 -0
  47. package/dist/types/collector/editor-event-observer.d.ts +19 -0
  48. package/dist/types/collector/interaction-events.d.ts +19 -0
  49. package/dist/types/collector/interaction-group.d.ts +37 -0
  50. package/dist/types/collector/interaction-observer.d.ts +39 -0
  51. package/dist/types/collector/interaction-tracker.d.ts +65 -0
  52. package/dist/types/collector/interactivity-collector.d.ts +90 -0
  53. package/dist/types/collector/interactivity-session.d.ts +42 -0
  54. package/dist/types/collector/lifecycle-observer.d.ts +25 -0
  55. package/dist/types/collector/snapshot-scheduler.d.ts +15 -0
  56. package/dist/types/interactivityPlugin.d.ts +6 -4
  57. package/dist/types/interactivityPluginType.d.ts +11 -2
  58. package/docs/0-intro.tsx +26 -7
  59. package/package.json +8 -3
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @atlaskit/editor-plugin-interactivity
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`355d6f13eb8b4`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/355d6f13eb8b4) -
8
+ Report the `editorTyping`, `editorPointer` and `editorOther` interaction histograms in the
9
+ `editor interactivity` event, so a responsiveness regression in the editor can be told apart from
10
+ one elsewhere on the page.
11
+
12
+ ### Patch Changes
13
+
14
+ - Updated dependencies
15
+
16
+ ## 0.2.0
17
+
18
+ ### Minor Changes
19
+
20
+ - [`fc88b1544be96`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/fc88b1544be96) -
21
+ Report the `editor interactivity` operational event: how interaction latencies were spread over an
22
+ editor session, as bucketed counts, for the Confluence full page editor. Behind the
23
+ platform_editor_editor_interactivity experiment.
24
+
25
+ ### Patch Changes
26
+
27
+ - Updated dependencies
28
+
3
29
  ## 0.1.0
4
30
 
5
31
  ### Minor Changes
package/README.md CHANGED
@@ -7,14 +7,73 @@ be able to use this component but will not be able to submit issues.
7
7
 
8
8
  ## Overview
9
9
 
10
- The Interactivity plugin reports the `editor interactivity` operational event: how interaction
11
- latencies were spread over an editor session, as bucketed counts, per
10
+ The Interactivity plugin reports the `editor interactivity` operational event: session-to-date
11
+ interaction latency distributions for full page editor sessions, per
12
12
  [RFC 095](https://hello.atlassian.net/wiki/spaces/EDITOR/pages/7527607488/Editor+RFC+095+Confluence+editor+responsiveness+bucketed+INP+telemetry).
13
13
  The existing `editor inp` event reports a single value per session and cannot answer how many
14
- interactions were slow.
15
-
16
- The plugin is a stub at this point — it registers itself and collects nothing. The collector and the
17
- event arrive in EDITOR-8553.
14
+ interactions were slow, so this plugin keeps bucketed counts instead.
15
+
16
+ ## What it reports
17
+
18
+ - `page` — every interaction on the page, as `totalCount`, `observedCount`, `sumMs`, `maxMs`,
19
+ `buckets` and `percentilesMs`. `totalCount` comes from `performance.interactionCount` and includes
20
+ interactions below the 16 ms Event Timing reporting threshold, so `totalCount - observedCount` is
21
+ the sub-threshold count.
22
+ - `editorTyping`, `editorPointer` and `editorOther` — the interactions with the editor, in the same
23
+ shape as `page`. Event Timing observes the whole document, so `page` alone cannot say whether a
24
+ regression is in the editor or elsewhere on the page; these can. Every interaction with the editor
25
+ is in exactly one of them, and in `page` as well.
26
+ - `percentilesMs` is temporary. It holds percentiles of the same interactions, keyed by percentile
27
+ and exact to the 8 ms Event Timing reports durations at, to confirm that a percentile read off
28
+ `buckets` lands where the latencies actually are.
29
+ - Bucket keys are the upper boundary of the bucket in milliseconds and count only interactions above
30
+ the previous boundary. Buckets are not cumulative and empty buckets are omitted, so a missing
31
+ bucket means zero. The boundaries are versioned by `schema`.
32
+ - Snapshots are session-to-date, so a cohort query takes the highest `seq` per
33
+ `interactivitySessionId` and then sums bucket counts.
34
+
35
+ ## The editor groups
36
+
37
+ An interaction is the editor's when it happened inside the element the editor renders itself into,
38
+ which editor-core hands to the plugin's hook as `wrapperElement`. A second observer reports the
39
+ events of those interactions from that element, alongside the Event Timing observer that reports the
40
+ latencies, and the tracker makes interactions out of both.
41
+
42
+ The editor's events are what count the interactions, because there is no
43
+ `performance.interactionCount` per group and the 16 ms reporting threshold is the lowest the spec
44
+ allows. An interaction is several events, so it is counted on one of them: typing on `keydown`,
45
+ because a held key repeats and the browser counts every repeat; pointing on `pointerup`, because a
46
+ press taken over by a scroll never gets one — and the browser counts no interaction for it either.
47
+ The group comes from the same events, and the one table in `interaction-events.ts` is read for both
48
+ counting and grouping, so the two cannot disagree.
49
+
50
+ An entry lands in the group of the event it measured, matched by that event's type and timestamp. An
51
+ interaction whose events the editor never reported stays in `page` only.
52
+
53
+ Known gaps, all of which only move interactions out of the editor groups and never between them:
54
+
55
+ - What the editor renders outside that element — a dropdown or a dialog in a portal — is counted in
56
+ `page` only, as is everything before the element arrives, a render after the editor first paints.
57
+ - `editorOther` is the remainder slot: the browser counts only keyboard and pointer interactions
58
+ today, so it reads zero until that changes.
59
+ - While an IME composes, the browser can group key presses into fewer interactions than we count, so
60
+ `editorTyping.totalCount` can run ahead of it.
61
+
62
+ ## Cadence
63
+
64
+ Snapshots are taken 10 s, 30 s and 60 s after the session starts, then every 60 s, plus on every
65
+ transition of the tab to hidden, on `pagehide` and on editor unmount. A snapshot that would repeat
66
+ the previous one is skipped, and signals from one lifecycle transition are coalesced into a single
67
+ snapshot.
68
+
69
+ ## Sessions
70
+
71
+ A session covers one document in one mode, so its latencies are always comparable with each other.
72
+ It normally lasts an editor mount, but it also ends while the editor stays mounted when either of
73
+ those changes — Confluence live pages navigate and switch between reading and editing without
74
+ remounting the editor. That is reported as `reason: 'navigation'` or `reason: 'modeChange'`, and the
75
+ next session starts with a new `interactivitySessionId`. Sessions of one editor mount share an
76
+ `editorSessionId`, which is what stitches them back together.
18
77
 
19
78
  ## Install
20
79
 
@@ -18,6 +18,18 @@
18
18
  "noCheck": true
19
19
  },
20
20
  "references": [
21
+ {
22
+ "path": "../../../helpers/browser-apis/afm-cc/tsconfig.json"
23
+ },
24
+ {
25
+ "path": "../../editor-plugin-analytics/afm-cc/tsconfig.json"
26
+ },
27
+ {
28
+ "path": "../../editor-plugin-context-identifier/afm-cc/tsconfig.json"
29
+ },
30
+ {
31
+ "path": "../../editor-plugin-editor-viewmode/afm-cc/tsconfig.json"
32
+ },
21
33
  {
22
34
  "path": "../../editor-common/afm-cc/tsconfig.json"
23
35
  }
@@ -18,6 +18,18 @@
18
18
  "noCheck": true
19
19
  },
20
20
  "references": [
21
+ {
22
+ "path": "../../../helpers/browser-apis/afm-products/tsconfig.json"
23
+ },
24
+ {
25
+ "path": "../../editor-plugin-analytics/afm-products/tsconfig.json"
26
+ },
27
+ {
28
+ "path": "../../editor-plugin-context-identifier/afm-products/tsconfig.json"
29
+ },
30
+ {
31
+ "path": "../../editor-plugin-editor-viewmode/afm-products/tsconfig.json"
32
+ },
21
33
  {
22
34
  "path": "../../editor-common/afm-products/tsconfig.json"
23
35
  }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.fireInteractivityEvent = fireInteractivityEvent;
7
+ var _enums = require("@atlaskit/editor-common/analytics/types/enums");
8
+ /** The reasons that mean the page may not be around long enough to deliver a queued event. */
9
+ var REASONS_THE_PAGE_MAY_NOT_OUTLIVE = ['hidden', 'pagehide', 'unmount'];
10
+
11
+ /**
12
+ * The `editor interactivity` operational event.
13
+ *
14
+ * `editorSessionId` arrives without being set here: `@atlaskit/analytics-listeners` merges
15
+ * the editor analytics context into the attributes of every event on the editor channel.
16
+ * `objectId` comes both from that context and from the snapshot, which carries the value
17
+ * the session was collected against.
18
+ */
19
+
20
+ /**
21
+ * Sends a snapshot as the `editor interactivity` event.
22
+ *
23
+ * Does nothing without the analytics plugin: an editor without it has nowhere to send events,
24
+ * and the collector keeps measuring either way.
25
+ */
26
+ function fireInteractivityEvent(analytics, snapshot) {
27
+ analytics === null || analytics === void 0 || analytics.fireAnalyticsEvent({
28
+ action: 'interactivity',
29
+ actionSubject: _enums.ACTION_SUBJECT.EDITOR,
30
+ eventType: _enums.EVENT_TYPE.OPERATIONAL,
31
+ attributes: snapshot
32
+ }, undefined,
33
+ // Events are queued into an idle callback by default, which a page being unloaded never
34
+ // runs and a backgrounded tab throttles. The snapshots taken because the page is going
35
+ // away go immediately; the rest can wait their turn.
36
+ {
37
+ immediate: REASONS_THE_PAGE_MAY_NOT_OUTLIVE.includes(snapshot.reason)
38
+ });
39
+ }
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.SCHEMA_VERSION = exports.REPORTING_THRESHOLD_MS = void 0;
7
+ exports.bucketKeyForMs = bucketKeyForMs;
8
+ 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; } } }; }
9
+ 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; } }
10
+ 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; }
11
+ /**
12
+ * Bucket boundaries for the interaction latency buckets, version 1.
13
+ *
14
+ * Two ranges:
15
+ * - 16 ms to 200 ms — a boundary every 8 ms: 16, 24, 32, … 200. Event Timing rounds
16
+ * durations to 8 ms, so nothing finer is measurable.
17
+ * - above 200 ms — each boundary ~15% above the previous one, five of them per doubling:
18
+ * 222, 256, 294, 337, 388, 445, 512, … A 40 ms difference matters at 100 ms and is
19
+ * noise at 4 seconds, so buckets grow with the latency instead of staying 8 ms wide.
20
+ *
21
+ * 500 ms — the Google INP "poor" threshold — falls inside the 445…512 bucket, so that one
22
+ * bucket is split at 500 to count the threshold instead of interpolating it. 200 ms, the
23
+ * "good" threshold, is already a boundary.
24
+ *
25
+ * Bump SCHEMA_VERSION whenever any boundary moves; queries group by it.
26
+ */
27
+ var SCHEMA_VERSION = exports.SCHEMA_VERSION = 1;
28
+
29
+ /**
30
+ * Event Timing reporting threshold. Faster interactions are never delivered to the
31
+ * observer, so they reach no bucket at all — `performance.interactionCount` is what counts
32
+ * them, as `totalCount - observedCount`. This bucket holds the interactions reported at
33
+ * exactly the threshold.
34
+ */
35
+ var REPORTING_THRESHOLD_MS = exports.REPORTING_THRESHOLD_MS = 16;
36
+
37
+ /**
38
+ * Exceptions to the grid: latencies that must be a boundary of their own so that they are
39
+ * counted exactly rather than read off a bucket that spans them. Each one splits the bucket it
40
+ * falls inside. 500 ms is the Google INP "poor" threshold; the "good" one, 200 ms, needs no
41
+ * exception because the evenly spaced range already ends there.
42
+ *
43
+ * Every entry has to sit above that range, and adding one changes the reported keys, so bump
44
+ * SCHEMA_VERSION with it.
45
+ */
46
+ var EXACT_THRESHOLDS_MS = [500];
47
+ var EVENLY_SPACED_MAX_MS = 200;
48
+ /**
49
+ * Keep this a multiple of 8 (8, 16, 24, …). Event Timing reports durations in 8 ms steps, so a
50
+ * step that is not a multiple of 8 leaves buckets no interaction can ever land in.
51
+ */
52
+ var EVENLY_SPACED_STEP_MS = 8;
53
+ var BOUNDARIES_PER_DOUBLING = 5;
54
+
55
+ /**
56
+ * The first boundary at or above `latencyMs`, for the range above 200 ms.
57
+ *
58
+ * Five boundaries per doubling is the same as saying the nth boundary sits at `2^(n/5)` ms —
59
+ * boundary 40 at 256 ms, 45 at 512 ms, 50 at 1024 ms. So this turns the latency into a
60
+ * boundary number, rounds that up, and turns it back into milliseconds.
61
+ *
62
+ * Boundaries are floored to whole milliseconds, which keeps each one at or below the exact
63
+ * value it stands for. That is what makes `bucketKeyForMs(boundary) === boundary` hold.
64
+ */
65
+ function firstBoundaryAtOrAbove(latencyMs) {
66
+ // `Math.log2(latencyMs) * 5` is the boundary number: log2 answers how many doublings of
67
+ // 1 ms reach this latency, and five boundaries cover each doubling.
68
+ //
69
+ // A latency sitting on a boundary makes that a whole number, which `Math.ceil` has to
70
+ // keep. ECMA-262 only requires `Math.log2` to be approximate, so 9.0000000000000002 for
71
+ // `log2(512)` would round up to boundary 46 and report 512 ms as 588 ms. EPSILON is
72
+ // larger than such imprecision and far smaller than the gap between two boundaries.
73
+ var EPSILON = 1e-9;
74
+ var boundaryNumber = Math.ceil(BOUNDARIES_PER_DOUBLING * Math.log2(latencyMs) - EPSILON);
75
+
76
+ // One `Math.pow` over the whole exponent, so the result is rounded once. Multiplying the
77
+ // ratio between neighbouring boundaries (2^(1/5) ≈ 1.1487) by itself instead accumulates
78
+ // the rounding of every step, reaching 512.0000000000018 by boundary 45.
79
+ return Math.floor(Math.pow(2, boundaryNumber / BOUNDARIES_PER_DOUBLING));
80
+ }
81
+
82
+ /** The bucket each exact threshold splits — 500 ms splits the one ending at 512 ms. */
83
+ var EXACT_THRESHOLD_BUCKETS = EXACT_THRESHOLDS_MS.map(function (thresholdMs) {
84
+ return {
85
+ thresholdMs: thresholdMs,
86
+ bucketMs: firstBoundaryAtOrAbove(thresholdMs)
87
+ };
88
+ });
89
+
90
+ /**
91
+ * The bucket a latency belongs to, identified by the bucket's upper boundary in whole
92
+ * milliseconds — which is also its key in the reported buckets.
93
+ *
94
+ * Every latency gets a bucket, however slow: the boundaries continue upwards, so there is no
95
+ * overflow bucket. Expects the whole-millisecond durations Event Timing reports; boundaries
96
+ * are floored, so a fractional latency can land in a bucket whose key reads up to a
97
+ * millisecond below it. Non-finite latencies never reach here — `InteractionTracker` drops
98
+ * them as it reads the entry.
99
+ */
100
+ function bucketKeyForMs(latencyMs) {
101
+ if (latencyMs <= REPORTING_THRESHOLD_MS) {
102
+ return REPORTING_THRESHOLD_MS;
103
+ }
104
+ if (latencyMs <= EVENLY_SPACED_MAX_MS) {
105
+ // How many 8 ms steps above the threshold the latency is, rounded up: 17 ms is 0.125
106
+ // steps up and lands on the boundary one step up, 24 ms. Rounding up is what keeps the
107
+ // set of keys fixed when a latency is not a multiple of 8 ms.
108
+ //
109
+ // No EPSILON here, unlike the branch below: subtracting whole numbers gives a whole
110
+ // number, and dividing by a power of two shifts a binary float's exponent without
111
+ // touching its digits, so a latency on a boundary cannot come out just above a whole
112
+ // number of steps.
113
+ var steps = Math.ceil((latencyMs - REPORTING_THRESHOLD_MS) / EVENLY_SPACED_STEP_MS);
114
+ return REPORTING_THRESHOLD_MS + steps * EVENLY_SPACED_STEP_MS;
115
+ }
116
+ var boundary = firstBoundaryAtOrAbove(latencyMs);
117
+
118
+ // A latency in the lower part of a split bucket is reported as the threshold itself, so the
119
+ // threshold is counted exactly.
120
+ var _iterator = _createForOfIteratorHelper(EXACT_THRESHOLD_BUCKETS),
121
+ _step;
122
+ try {
123
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
124
+ var _step$value = _step.value,
125
+ thresholdMs = _step$value.thresholdMs,
126
+ bucketMs = _step$value.bucketMs;
127
+ if (boundary === bucketMs && latencyMs <= thresholdMs) {
128
+ return thresholdMs;
129
+ }
130
+ }
131
+ } catch (err) {
132
+ _iterator.e(err);
133
+ } finally {
134
+ _iterator.f();
135
+ }
136
+ return boundary;
137
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.EditorEventObserver = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _bindEventListener = require("bind-event-listener");
11
+ var _interactionEvents = require("./interaction-events");
12
+ /**
13
+ * Reports the events the interactions with the editor are made of, as the browser dispatched
14
+ * them. What they mean is the tracker's business.
15
+ *
16
+ * Event Timing cannot answer which interactions were with the editor, because it observes the
17
+ * whole document, nor how many there were, because it reports none below 16 ms.
18
+ */
19
+ var EditorEventObserver = exports.EditorEventObserver = /*#__PURE__*/function () {
20
+ function EditorEventObserver(onEvent) {
21
+ (0, _classCallCheck2.default)(this, EditorEventObserver);
22
+ this.onEvent = onEvent;
23
+ }
24
+
25
+ /**
26
+ * Called as the element the editor renders itself into changes: the editor only knows it after
27
+ * its first render, and can replace it.
28
+ */
29
+ return (0, _createClass2.default)(EditorEventObserver, [{
30
+ key: "observe",
31
+ value: function observe(root) {
32
+ var _this = this;
33
+ var next = root !== null && root !== void 0 ? root : undefined;
34
+ if (next === this.root) {
35
+ return;
36
+ }
37
+ this.stop();
38
+ this.root = next;
39
+ if (!next) {
40
+ return;
41
+ }
42
+ this.unbind = (0, _bindEventListener.bindAll)(next, _interactionEvents.INTERACTION_EVENT_TYPES.map(function (type) {
43
+ return {
44
+ type: type,
45
+ listener: _this.onEvent
46
+ };
47
+ }),
48
+ // Capture, so a handler in the editor cannot stop an interaction from being reported.
49
+ {
50
+ capture: true,
51
+ passive: true
52
+ });
53
+ }
54
+ }, {
55
+ key: "stop",
56
+ value: function stop() {
57
+ var _this$unbind;
58
+ (_this$unbind = this.unbind) === null || _this$unbind === void 0 || _this$unbind.call(this);
59
+ this.unbind = undefined;
60
+ this.root = undefined;
61
+ }
62
+ }]);
63
+ }();
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.INTERACTION_EVENT_TYPES = void 0;
7
+ exports.interactionEventKind = interactionEventKind;
8
+ /**
9
+ * The editor group an interaction belongs to, which is also the field the event reports it in.
10
+ *
11
+ * `editorOther` is the remainder — an interaction the browser counts that is neither typing nor
12
+ * pointing — so the three groups together cover whatever the browser calls an interaction.
13
+ */
14
+
15
+ /**
16
+ * The events an interaction is made of, and the group they belong to. Both sides of the collector
17
+ * read this, so counting and grouping cannot disagree.
18
+ *
19
+ * These are the events Event Timing can report with a non-zero `interactionId`, so any of them can
20
+ * be the one an entry measured — and it is usually not the one the interaction is counted on: for a
21
+ * pointer press the slow part is normally the `click` handler.
22
+ *
23
+ * The counted event is at a different end of the interaction in each group, because what can go
24
+ * wrong differs. Typing is counted on `keydown`, since the browser opens a new interaction for
25
+ * every repeat of a held key, and nothing cancels a key press. Pointing is counted on `pointerup`,
26
+ * since a press cannot repeat but can be taken over by a scroll — which the browser does not count
27
+ * either, and in that case `pointerup` never arrives.
28
+ */
29
+ var INTERACTION_EVENTS = {
30
+ keydown: {
31
+ group: 'editorTyping',
32
+ counts: true
33
+ },
34
+ keyup: {
35
+ group: 'editorTyping'
36
+ },
37
+ // Only while an IME composes: the browser counts the text it commits in the interaction of the
38
+ // key that caused it.
39
+ input: {
40
+ group: 'editorTyping'
41
+ },
42
+ pointerdown: {
43
+ group: 'editorPointer'
44
+ },
45
+ pointerup: {
46
+ group: 'editorPointer',
47
+ counts: true
48
+ },
49
+ click: {
50
+ group: 'editorPointer'
51
+ },
52
+ // Ends a pointer press the way `pointerup` does, so the browser counts it as an interaction.
53
+ contextmenu: {
54
+ group: 'editorPointer'
55
+ }
56
+ };
57
+
58
+ /** Derived, so the types listened for cannot drift from the table. */
59
+ var INTERACTION_EVENT_TYPES = exports.INTERACTION_EVENT_TYPES = Object.keys(INTERACTION_EVENTS);
60
+ function interactionEventKind(type) {
61
+ return INTERACTION_EVENTS[type];
62
+ }
@@ -0,0 +1,185 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.InteractionGroup = void 0;
8
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
9
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+ var _bucketBoundaries = require("./bucket-boundaries");
12
+ 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; } } }; }
13
+ 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; } }
14
+ 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; }
15
+ /**
16
+ * Latencies are counted per 8 ms, the resolution Event Timing reports durations at. Every
17
+ * latency is rounded up to this step on the way in, which caps the number of distinct values
18
+ * a group can hold whatever the latency was derived from.
19
+ */
20
+ var RESOLUTION_MS = 8;
21
+
22
+ /** Which percentiles are reported, as quantiles. */
23
+ var REPORTED_QUANTILES = [0.9, 0.98];
24
+
25
+ /**
26
+ * The latencies of one set of interactions — every interaction on the page, or only the ones
27
+ * inside the editor — reported as one object in the event.
28
+ *
29
+ * The whole state is a count per distinct latency, so an interaction only costs a counter
30
+ * whatever its latency was, and everything the event carries — the reported buckets, the
31
+ * count, the sum, the maximum and the percentiles — is derived from that map when a snapshot
32
+ * is taken. Nothing is computed while interactions arrive.
33
+ *
34
+ * The two counters count different populations: `add` takes the interactions Event Timing
35
+ * measured, `countTotal` takes all of them, including the ones below the 16 ms reporting threshold
36
+ * it never delivers. So `totalCount >= observedCount`, and the difference is how many were too
37
+ * fast to be measured.
38
+ */
39
+ var InteractionGroup = exports.InteractionGroup = /*#__PURE__*/function () {
40
+ function InteractionGroup() {
41
+ (0, _classCallCheck2.default)(this, InteractionGroup);
42
+ (0, _defineProperty2.default)(this, "countByLatency", new Map());
43
+ (0, _defineProperty2.default)(this, "totalCount", 0);
44
+ }
45
+ return (0, _createClass2.default)(InteractionGroup, [{
46
+ key: "add",
47
+ value: function add(latencyMs) {
48
+ this.increment(latencyMs);
49
+ }
50
+
51
+ /**
52
+ * Counts an interaction towards the group's total, measured or not. `page` has no use for it:
53
+ * `performance.interactionCount` counts the page's interactions.
54
+ */
55
+ }, {
56
+ key: "countTotal",
57
+ value: function countTotal() {
58
+ this.totalCount += 1;
59
+ }
60
+ }, {
61
+ key: "remeasure",
62
+ value: function remeasure(previousLatencyMs, latencyMs) {
63
+ // Moved rather than counted again: the count belongs to the same interaction.
64
+ this.decrement(previousLatencyMs);
65
+ this.increment(latencyMs);
66
+ }
67
+
68
+ /**
69
+ * @param totalCount every interaction of the group, including those below the Event Timing
70
+ * reporting threshold. Defaults to what `countTotal` was told, which is where an editor
71
+ * group's total comes from; `page` passes `performance.interactionCount` instead.
72
+ */
73
+ }, {
74
+ key: "snapshot",
75
+ value: function snapshot() {
76
+ var _latencies;
77
+ var totalCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.totalCount;
78
+ // Ascending, so the reported buckets come out in order and the last latency is the
79
+ // maximum. Sorted once for everything below.
80
+ var latencies = Array.from(this.countByLatency.keys()).sort(function (a, b) {
81
+ return a - b;
82
+ });
83
+ var observedCount = this.observedCount();
84
+ var percentileRanks = REPORTED_QUANTILES.map(function (quantile) {
85
+ return {
86
+ key: String(Math.round(quantile * 100)),
87
+ rank: Math.max(1, Math.ceil(quantile * observedCount))
88
+ };
89
+ });
90
+ var buckets = {};
91
+ var percentilesMs = {};
92
+ var sumMs = 0;
93
+ var counted = 0;
94
+ var _iterator = _createForOfIteratorHelper(latencies),
95
+ _step;
96
+ try {
97
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
98
+ var _this$countByLatency$, _buckets$bucket;
99
+ var latencyMs = _step.value;
100
+ var count = (_this$countByLatency$ = this.countByLatency.get(latencyMs)) !== null && _this$countByLatency$ !== void 0 ? _this$countByLatency$ : 0;
101
+ sumMs += latencyMs * count;
102
+ var bucket = String((0, _bucketBoundaries.bucketKeyForMs)(latencyMs));
103
+ buckets[bucket] = ((_buckets$bucket = buckets[bucket]) !== null && _buckets$bucket !== void 0 ? _buckets$bucket : 0) + count;
104
+
105
+ // A percentile is the latency the group's interactions reach counting up from the
106
+ // fastest, so it is answered as soon as this many of them have been passed.
107
+ counted += count;
108
+ var _iterator2 = _createForOfIteratorHelper(percentileRanks),
109
+ _step2;
110
+ try {
111
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
112
+ var _step2$value = _step2.value,
113
+ key = _step2$value.key,
114
+ rank = _step2$value.rank;
115
+ if (percentilesMs[key] === undefined && counted >= rank) {
116
+ percentilesMs[key] = latencyMs;
117
+ }
118
+ }
119
+ } catch (err) {
120
+ _iterator2.e(err);
121
+ } finally {
122
+ _iterator2.f();
123
+ }
124
+ }
125
+ } catch (err) {
126
+ _iterator.e(err);
127
+ } finally {
128
+ _iterator.f();
129
+ }
130
+ return {
131
+ // `performance.interactionCount` can lag the entries the observer has delivered.
132
+ totalCount: Math.max(totalCount, observedCount),
133
+ observedCount: observedCount,
134
+ sumMs: sumMs,
135
+ maxMs: (_latencies = latencies[latencies.length - 1]) !== null && _latencies !== void 0 ? _latencies : 0,
136
+ buckets: buckets,
137
+ percentilesMs: percentilesMs
138
+ };
139
+ }
140
+ }, {
141
+ key: "observedCount",
142
+ value: function observedCount() {
143
+ var total = 0;
144
+ var _iterator3 = _createForOfIteratorHelper(this.countByLatency.values()),
145
+ _step3;
146
+ try {
147
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
148
+ var count = _step3.value;
149
+ total += count;
150
+ }
151
+ } catch (err) {
152
+ _iterator3.e(err);
153
+ } finally {
154
+ _iterator3.f();
155
+ }
156
+ return total;
157
+ }
158
+
159
+ /** Rounding lives here so that every count goes through the same step, in or out. */
160
+ }, {
161
+ key: "roundLatencyUp",
162
+ value: function roundLatencyUp(latencyMs) {
163
+ return Math.ceil(latencyMs / RESOLUTION_MS) * RESOLUTION_MS;
164
+ }
165
+ }, {
166
+ key: "increment",
167
+ value: function increment(latencyMs) {
168
+ var _this$countByLatency$2;
169
+ var step = this.roundLatencyUp(latencyMs);
170
+ this.countByLatency.set(step, ((_this$countByLatency$2 = this.countByLatency.get(step)) !== null && _this$countByLatency$2 !== void 0 ? _this$countByLatency$2 : 0) + 1);
171
+ }
172
+ }, {
173
+ key: "decrement",
174
+ value: function decrement(latencyMs) {
175
+ var _this$countByLatency$3;
176
+ var step = this.roundLatencyUp(latencyMs);
177
+ var next = ((_this$countByLatency$3 = this.countByLatency.get(step)) !== null && _this$countByLatency$3 !== void 0 ? _this$countByLatency$3 : 0) - 1;
178
+ if (next > 0) {
179
+ this.countByLatency.set(step, next);
180
+ } else {
181
+ this.countByLatency.delete(step);
182
+ }
183
+ }
184
+ }]);
185
+ }();