-
Notifications
You must be signed in to change notification settings - Fork 26
/
loadClients.js
62 lines (56 loc) · 1.29 KB
/
loadClients.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
/*
* Treasure Client Loader
*/
// Modules
var _ = require('./utils/lodash')
var window = require('global/window')
// Helpers
function applyToClient (client, method) {
var _method = '_' + method
if (client[_method]) {
var arr = client[_method] || []
while (arr.length) {
client[method].apply(client, arr.shift())
}
delete client[_method]
}
}
// Constants
var TREASURE_KEYS = [
'init',
'set',
'blockEvents',
'unblockEvents',
'setSignedMode',
'setAnonymousMode',
'resetUUID',
'addRecord',
'fetchGlobalID',
'trackPageview',
'trackEvent',
'trackClicks',
'fetchUserSegments',
'fetchServerCookie',
'ready'
]
/*
* Load clients
*/
module.exports = function loadClients (Treasure, name) {
if (_.isObject(window[name])) {
var snippet = window[name]
var clients = snippet.clients
// Copy over Treasure.prototype functions over to snippet's prototype
// This allows already-instanciated clients to work
_.forIn(Treasure.prototype, function (value, key) {
snippet.prototype[key] = value
})
// Iterate over each client instance
_.forEach(clients, function (client) {
// Call each key and with any stored values
_.forEach(TREASURE_KEYS, function (value) {
applyToClient(client, value)
})
})
}
}