forked from googleanalytics/autotrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker-queue.js
74 lines (65 loc) · 2.33 KB
/
tracker-queue.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
/**
* Copyright 2018 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {IdleQueue} from 'idlize/IdleQueue.mjs';
const instances = {};
/**
* A class that enforces a unique IdleQueue per tracking ID.
*/
export default class TrackerQueue extends IdleQueue {
/**
* Gets an existing instance for the passed tracking ID or creates a new
* instance if one doesn't exist.
* @param {string} trackingId An analytics.js tracking ID.
* @return {!TrackerQueue}
*/
static getOrCreate(trackingId) {
// Don't create multiple instances for the same tracking ID.
if (!(trackingId in instances)) {
instances[trackingId] = {
references: 0,
value: new TrackerQueue(trackingId),
};
}
++instances[trackingId].references;
return instances[trackingId].value;
}
/**
* @param {string} trackingId
*/
constructor(trackingId) {
// If an idle callback is being run in between frame rendering, it'll
// have an initial `timeRemaining()` value <= 16ms. If it's run when
// no frames are being rendered, it'll have an initial
// `timeRemaining()` <= 50ms. Since all the tasks queued by autotrack
// are non-critial and non-UI-related, we do not want our tasks to
// interfere with frame rendering, and therefore by default we pick a
// `defaultMinTaskTime` value > 16ms, so tasks are always processed
// outside of frame rendering.
super({defaultMinTaskTime: 25, ensureTasksRun: true});
this.trackingId_ = trackingId;
}
/**
* Removes a reference from the instances map. If no more references exist
* for this instance, destroy it.
*/
destroy() {
--instances[this.trackingId_].references;
if (instances[this.trackingId_].references === 0) {
super.destroy();
delete instances[this.trackingId_];
}
}
}