-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
50 lines (42 loc) · 1.14 KB
/
index.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
/* eslint-env browser */
'use strict';
import {
accountListName,
callbackQueueName,
trackerInstanceName,
trackerVersionName
} from './constants';
function ymProxy(id, methodName, ...args) {
try {
window[trackerInstanceName(id)][methodName](...args);
} catch (ex) {
console.warn(ex);
}
}
function accountIdList() {
return typeof window !== 'undefined' ? window[accountListName] : [];
}
function ymAsyncProxy(ids) {
return function (...args) {
ids.forEach(id => {
let trackerVersion = window[trackerVersionName(id)];
let callbackQueue = window[callbackQueueName(trackerVersion)];
if (callbackQueue) {
callbackQueue.push(() => ymProxy(id, ...args));
} else {
ymProxy(id, ...args);
}
});
};
}
function ym(...args) {
return ymAsyncProxy(accountIdList())(...args);
}
export function withId(counterId) {
return withFilter(id => counterId === id);
}
export function withFilter(f) {
return ymAsyncProxy(accountIdList().filter(f));
}
export default ym;
export { YMInitializer } from './component';