polyapi 0.11.0 → 0.11.1
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.
|
@@ -15,6 +15,8 @@ const nodeEnv = process.env.NODE_ENV;
|
|
|
15
15
|
const isDevEnv = nodeEnv === 'development';
|
|
16
16
|
|
|
17
17
|
let socket = null;
|
|
18
|
+
let listenersCount = 0;
|
|
19
|
+
|
|
18
20
|
const getSocket = () => {
|
|
19
21
|
let apiBaseUrl = '{{apiBaseUrl}}';
|
|
20
22
|
if (!isDevEnv) {
|
|
@@ -26,7 +28,35 @@ const getSocket = () => {
|
|
|
26
28
|
transports: ['websocket']
|
|
27
29
|
});
|
|
28
30
|
}
|
|
29
|
-
|
|
31
|
+
|
|
32
|
+
return new Proxy(socket, {
|
|
33
|
+
get(target, property, receiver) {
|
|
34
|
+
const value = target[property];
|
|
35
|
+
|
|
36
|
+
if (property === 'emit') {
|
|
37
|
+
return function emit(...arguments) {
|
|
38
|
+
const [event] = arguments;
|
|
39
|
+
|
|
40
|
+
if (event.match(/^register/)) {
|
|
41
|
+
listenersCount+=1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (event.match(/^unregister*/) && listenersCount > 0) {
|
|
45
|
+
listenersCount = listenersCount - 1;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (listenersCount === 0) {
|
|
49
|
+
socket.disconnect();
|
|
50
|
+
socket = null;
|
|
51
|
+
} else {
|
|
52
|
+
return value.apply(this === receiver ? target : this, arguments);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return Reflect.get(...arguments);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
30
60
|
};
|
|
31
61
|
const getApiKey = () => polyCustom.executionApiKey || '{{apiKey}}';
|
|
32
62
|
const poly = {};
|