@jsenv/core 27.3.3 → 27.5.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.
- package/dist/js/autoreload.js +359 -0
- package/dist/js/execute_using_dynamic_import.js +1 -1
- package/dist/js/html_supervisor_installer.js +524 -147
- package/dist/js/html_supervisor_setup.js +3 -4
- package/dist/js/new_stylesheet.js +26 -58
- package/dist/js/server_events_client.js +307 -0
- package/dist/main.js +7699 -7307
- package/package.json +15 -15
- package/{README.md → readme.md} +18 -7
- package/src/build/build.js +16 -18
- package/src/build/start_build_server.js +24 -28
- package/src/dev/start_dev_server.js +30 -94
- package/src/execute/execute.js +17 -35
- package/src/execute/run.js +2 -0
- package/src/omega/errors.js +43 -9
- package/src/omega/kitchen.js +42 -25
- package/src/omega/omega_server.js +96 -74
- package/src/omega/server/file_service.js +256 -28
- package/src/omega/url_graph.js +39 -20
- package/src/plugins/autoreload/client/autoreload.js +201 -0
- package/src/plugins/autoreload/{dev_sse/client → client}/autoreload_preference.js +0 -0
- package/src/plugins/autoreload/{dev_sse/client → client}/reload.js +29 -10
- package/src/plugins/autoreload/{dev_sse/client → client}/url_helpers.js +0 -0
- package/src/plugins/autoreload/jsenv_plugin_autoreload.js +4 -8
- package/src/plugins/autoreload/{dev_sse/jsenv_plugin_dev_sse_client.js → jsenv_plugin_autoreload_client.js} +8 -8
- package/src/plugins/autoreload/jsenv_plugin_autoreload_server.js +196 -0
- package/src/{dev/plugins → plugins}/explorer/client/explorer.html +0 -0
- package/src/{dev/plugins → plugins}/explorer/client/jsenv.png +0 -0
- package/src/{dev/plugins → plugins}/explorer/jsenv_plugin_explorer.js +1 -3
- package/src/plugins/html_supervisor/client/error_overlay.js +401 -0
- package/src/plugins/html_supervisor/client/html_supervisor_installer.js +138 -23
- package/src/plugins/html_supervisor/client/html_supervisor_setup.js +3 -4
- package/src/plugins/html_supervisor/jsenv_plugin_html_supervisor.js +55 -23
- package/src/plugins/inline/jsenv_plugin_html_inline_content.js +97 -117
- package/src/plugins/node_esm_resolution/jsenv_plugin_node_esm_resolution.js +66 -58
- package/src/plugins/plugin_controller.js +102 -67
- package/src/plugins/plugins.js +10 -10
- package/src/{helpers/event_source/event_source.js → plugins/server_events/client/event_source_connection.js} +125 -33
- package/src/plugins/server_events/client/server_events_client.js +17 -0
- package/src/plugins/server_events/jsenv_plugin_server_events_client_injection.js +48 -0
- package/src/plugins/server_events/server_events_dispatcher.js +69 -0
- package/src/{dev/plugins → plugins}/toolbar/client/animation/toolbar_animation.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/eventsource/eventsource.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/eventsource/toolbar_eventsource.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/execution/execution.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/execution/toolbar_execution.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/focus/focus.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/focus/toolbar_focus.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/jsenv_logo.svg +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/notification/toolbar_notification.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/responsive/overflow_menu.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/responsive/toolbar_responsive.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/settings/settings.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/settings/toolbar_settings.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/theme/jsenv_theme.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/theme/light_theme.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/theme/toolbar_theme.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/toolbar.html +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/toolbar_injector.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/toolbar_main.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/toolbar_main.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/tooltip/tooltip.css +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/tooltip/tooltip.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/animation.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/dom.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/fetch_using_xhr.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/fetching.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/iframe_to_parent_href.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/jsenv_logger.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/preferences.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/responsive.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/util/util.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/client/variant/variant.js +0 -0
- package/src/{dev/plugins → plugins}/toolbar/jsenv_plugin_toolbar.js +0 -0
- package/src/plugins/transpilation/as_js_classic/jsenv_plugin_as_js_classic_html.js +4 -3
- package/src/plugins/transpilation/babel/new_stylesheet/client/new_stylesheet.js +25 -55
- package/src/plugins/transpilation/import_assertions/jsenv_plugin_import_assertions.js +44 -24
- package/src/plugins/transpilation/jsenv_plugin_transpilation.js +6 -1
- package/src/plugins/url_analysis/html/html_urls.js +8 -8
- package/src/plugins/url_analysis/jsenv_plugin_url_analysis.js +3 -1
- package/src/test/execute_plan.js +36 -54
- package/src/test/execute_test_plan.js +2 -2
- package/src/test/logs_file_execution.js +60 -27
- package/src/test/logs_file_execution.test.mjs +41 -0
- package/dist/js/event_source_client.js +0 -528
- package/src/helpers/event_source/sse_service.js +0 -53
- package/src/plugins/autoreload/dev_sse/client/event_source_client.js +0 -193
- package/src/plugins/autoreload/dev_sse/jsenv_plugin_dev_sse_server.js +0 -203
- package/src/plugins/html_supervisor/client/error_in_document.js +0 -198
|
@@ -14,7 +14,7 @@ window.__html_supervisor__ = {
|
|
|
14
14
|
type: "js_classic",
|
|
15
15
|
isInline,
|
|
16
16
|
currentScript: document.currentScript,
|
|
17
|
-
execute: () => {
|
|
17
|
+
execute: (url) => {
|
|
18
18
|
return new Promise((resolve, reject) => {
|
|
19
19
|
const script = document.createElement("script")
|
|
20
20
|
if (crossorigin) {
|
|
@@ -23,8 +23,7 @@ window.__html_supervisor__ = {
|
|
|
23
23
|
if (integrity) {
|
|
24
24
|
script.integrity = integrity
|
|
25
25
|
}
|
|
26
|
-
script.src =
|
|
27
|
-
const scriptUrl = new URL(src, window.location).href
|
|
26
|
+
script.src = url
|
|
28
27
|
let lastWindowErrorUrl
|
|
29
28
|
let lastWindowError
|
|
30
29
|
const windowErrorCallback = (e) => {
|
|
@@ -45,7 +44,7 @@ window.__html_supervisor__ = {
|
|
|
45
44
|
})
|
|
46
45
|
script.addEventListener("load", () => {
|
|
47
46
|
cleanup()
|
|
48
|
-
if (lastWindowErrorUrl ===
|
|
47
|
+
if (lastWindowErrorUrl === url) {
|
|
49
48
|
reject(lastWindowError)
|
|
50
49
|
} else {
|
|
51
50
|
resolve()
|
|
@@ -14,30 +14,6 @@
|
|
|
14
14
|
|
|
15
15
|
var defineProperty = Object.defineProperty;
|
|
16
16
|
var forEach = Array.prototype.forEach;
|
|
17
|
-
|
|
18
|
-
var hasBrokenRules = function () {
|
|
19
|
-
var style = bootstrapper.createElement("style");
|
|
20
|
-
style.textContent = '.x{content:"y"}';
|
|
21
|
-
bootstrapper.body.appendChild(style);
|
|
22
|
-
return style.sheet.cssRules[0].style.content !== '"y"';
|
|
23
|
-
}();
|
|
24
|
-
|
|
25
|
-
var brokenRulePatterns = [/content:\s*["']/gm];
|
|
26
|
-
|
|
27
|
-
function fixBrokenRules(content) {
|
|
28
|
-
return brokenRulePatterns.reduce(function (acc, pattern) {
|
|
29
|
-
return acc.replace(pattern, "$&%%%");
|
|
30
|
-
}, content);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
var placeholderPatterns = [/(content:\s*["'])%%%/gm];
|
|
34
|
-
var getCssText = hasBrokenRules ? function (rule) {
|
|
35
|
-
return placeholderPatterns.reduce(function (acc, pattern) {
|
|
36
|
-
return acc.replace(pattern, "$1");
|
|
37
|
-
}, rule.cssText);
|
|
38
|
-
} : function (rule) {
|
|
39
|
-
return rule.cssText;
|
|
40
|
-
};
|
|
41
17
|
var importPattern = /@import.+?;?$/gm;
|
|
42
18
|
|
|
43
19
|
function rejectImports(contents) {
|
|
@@ -50,18 +26,6 @@
|
|
|
50
26
|
return _contents.trim();
|
|
51
27
|
}
|
|
52
28
|
|
|
53
|
-
function clearRules(sheet) {
|
|
54
|
-
for (var i = 0; i < sheet.cssRules.length; i++) {
|
|
55
|
-
sheet.deleteRule(0);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function insertAllRules(from, to) {
|
|
60
|
-
forEach.call(from.cssRules, function (rule, i) {
|
|
61
|
-
to.insertRule(getCssText(rule), i);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
29
|
function isElementConnected(element) {
|
|
66
30
|
return "isConnected" in element ? element.isConnected : document.contains(element);
|
|
67
31
|
}
|
|
@@ -106,9 +70,10 @@
|
|
|
106
70
|
return typeof instance === "object" ? nonConstructedProto.isPrototypeOf(instance) : false;
|
|
107
71
|
}
|
|
108
72
|
|
|
109
|
-
var $
|
|
73
|
+
var $basicStyleElement = new WeakMap();
|
|
110
74
|
var $locations = new WeakMap();
|
|
111
75
|
var $adoptersByLocation = new WeakMap();
|
|
76
|
+
var $appliedMethods = new WeakMap();
|
|
112
77
|
|
|
113
78
|
function addAdopterLocation(sheet, location) {
|
|
114
79
|
var adopter = document.createElement("style");
|
|
@@ -130,13 +95,15 @@
|
|
|
130
95
|
|
|
131
96
|
function restyleAdopter(sheet, adopter) {
|
|
132
97
|
requestAnimationFrame(function () {
|
|
133
|
-
|
|
134
|
-
|
|
98
|
+
adopter.textContent = $basicStyleElement.get(sheet).textContent;
|
|
99
|
+
$appliedMethods.get(sheet).forEach(function (command) {
|
|
100
|
+
return adopter.sheet[command.method].apply(adopter.sheet, command.args);
|
|
101
|
+
});
|
|
135
102
|
});
|
|
136
103
|
}
|
|
137
104
|
|
|
138
105
|
function checkInvocationCorrectness(self) {
|
|
139
|
-
if (!$
|
|
106
|
+
if (!$basicStyleElement.has(self)) {
|
|
140
107
|
throw new TypeError("Illegal invocation");
|
|
141
108
|
}
|
|
142
109
|
}
|
|
@@ -145,9 +112,10 @@
|
|
|
145
112
|
var self = this;
|
|
146
113
|
var style = document.createElement("style");
|
|
147
114
|
bootstrapper.body.appendChild(style);
|
|
148
|
-
$
|
|
115
|
+
$basicStyleElement.set(self, style);
|
|
149
116
|
$locations.set(self, []);
|
|
150
117
|
$adoptersByLocation.set(self, new WeakMap());
|
|
118
|
+
$appliedMethods.set(self, []);
|
|
151
119
|
}
|
|
152
120
|
|
|
153
121
|
var proto$1 = ConstructedStyleSheet.prototype;
|
|
@@ -166,9 +134,8 @@
|
|
|
166
134
|
|
|
167
135
|
if (typeof contents === "string") {
|
|
168
136
|
var self_1 = this;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
$basicStyleSheet.set(self_1, style.sheet);
|
|
137
|
+
$basicStyleElement.get(self_1).textContent = rejectImports(contents);
|
|
138
|
+
$appliedMethods.set(self_1, []);
|
|
172
139
|
$locations.get(self_1).forEach(function (location) {
|
|
173
140
|
if (location.isConnected()) {
|
|
174
141
|
restyleAdopter(self_1, getAdopterByLocation(self_1, location));
|
|
@@ -182,7 +149,15 @@
|
|
|
182
149
|
enumerable: true,
|
|
183
150
|
get: function cssRules() {
|
|
184
151
|
checkInvocationCorrectness(this);
|
|
185
|
-
return $
|
|
152
|
+
return $basicStyleElement.get(this).sheet.cssRules;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
defineProperty(proto$1, "media", {
|
|
156
|
+
configurable: true,
|
|
157
|
+
enumerable: true,
|
|
158
|
+
get: function media() {
|
|
159
|
+
checkInvocationCorrectness(this);
|
|
160
|
+
return $basicStyleElement.get(this).sheet.media;
|
|
186
161
|
}
|
|
187
162
|
});
|
|
188
163
|
cssStyleSheetMethods.forEach(function (method) {
|
|
@@ -190,25 +165,18 @@
|
|
|
190
165
|
var self = this;
|
|
191
166
|
checkInvocationCorrectness(self);
|
|
192
167
|
var args = arguments;
|
|
168
|
+
$appliedMethods.get(self).push({
|
|
169
|
+
method: method,
|
|
170
|
+
args: args
|
|
171
|
+
});
|
|
193
172
|
$locations.get(self).forEach(function (location) {
|
|
194
173
|
if (location.isConnected()) {
|
|
195
174
|
var sheet = getAdopterByLocation(self, location).sheet;
|
|
196
175
|
sheet[method].apply(sheet, args);
|
|
197
176
|
}
|
|
198
177
|
});
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (method === "insertRule") {
|
|
202
|
-
args[0] = fixBrokenRules(args[0]);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (method === "addRule") {
|
|
206
|
-
args[1] = fixBrokenRules(args[1]);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
var basic = $basicStyleSheet.get(self);
|
|
211
|
-
return basic[method].apply(basic, args);
|
|
178
|
+
var basicSheet = $basicStyleElement.get(self).sheet;
|
|
179
|
+
return basicSheet[method].apply(basicSheet, args);
|
|
212
180
|
};
|
|
213
181
|
});
|
|
214
182
|
defineProperty(ConstructedStyleSheet, Symbol.hasInstance, {
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
const STATUSES = {
|
|
2
|
+
CONNECTING: "connecting",
|
|
3
|
+
CONNECTED: "connected",
|
|
4
|
+
DISCONNECTED: "disconnected"
|
|
5
|
+
};
|
|
6
|
+
const createEventSourceConnection = (eventSourceUrl, {
|
|
7
|
+
retryMaxAttempt = Infinity,
|
|
8
|
+
retryAllocatedMs = Infinity,
|
|
9
|
+
lastEventId,
|
|
10
|
+
useEventsToManageConnection = true
|
|
11
|
+
} = {}) => {
|
|
12
|
+
const {
|
|
13
|
+
EventSource
|
|
14
|
+
} = window;
|
|
15
|
+
|
|
16
|
+
if (typeof EventSource !== "function") {
|
|
17
|
+
return () => {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let eventSource;
|
|
21
|
+
const listenersMap = new Map();
|
|
22
|
+
const callbacksMap = new Map();
|
|
23
|
+
const eventSourceOrigin = new URL(eventSourceUrl).origin;
|
|
24
|
+
|
|
25
|
+
const addEventCallbacks = namedCallbacks => {
|
|
26
|
+
let listenersMapSize = listenersMap.size;
|
|
27
|
+
Object.keys(namedCallbacks).forEach(eventName => {
|
|
28
|
+
const callback = namedCallbacks[eventName];
|
|
29
|
+
const existingCallbacks = callbacksMap.get(eventName);
|
|
30
|
+
let callbacks;
|
|
31
|
+
|
|
32
|
+
if (existingCallbacks) {
|
|
33
|
+
callbacks = existingCallbacks;
|
|
34
|
+
} else {
|
|
35
|
+
callbacks = [];
|
|
36
|
+
callbacksMap.set(eventName, callbacks);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (callbacks.length === 0) {
|
|
40
|
+
const eventListener = e => {
|
|
41
|
+
if (e.origin === eventSourceOrigin) {
|
|
42
|
+
if (e.lastEventId) {
|
|
43
|
+
lastEventId = e.lastEventId;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
callbacks.forEach(eventCallback => {
|
|
47
|
+
eventCallback(e);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
listenersMap.set(eventName, eventListener);
|
|
53
|
+
|
|
54
|
+
if (eventSource) {
|
|
55
|
+
eventSource.addEventListener(eventName, eventListener);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
callbacks.push(callback);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (useEventsToManageConnection && listenersMapSize === 0 && listenersMap.size > 0 && status.value !== STATUSES.CONNECTING && status.value !== STATUSES.CONNECTED) {
|
|
63
|
+
_connect();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let removed = false;
|
|
67
|
+
return () => {
|
|
68
|
+
if (removed) return;
|
|
69
|
+
removed = true;
|
|
70
|
+
listenersMapSize = listenersMap.size;
|
|
71
|
+
Object.keys(namedCallbacks).forEach(eventName => {
|
|
72
|
+
const callback = namedCallbacks[eventName];
|
|
73
|
+
const callbacks = callbacksMap.get(eventName);
|
|
74
|
+
|
|
75
|
+
if (callbacks) {
|
|
76
|
+
const index = callbacks.indexOf(callback);
|
|
77
|
+
|
|
78
|
+
if (index > -1) {
|
|
79
|
+
callbacks.splice(index, 1);
|
|
80
|
+
|
|
81
|
+
if (callbacks.length === 0) {
|
|
82
|
+
const listener = listenersMap.get(eventName);
|
|
83
|
+
|
|
84
|
+
if (listener) {
|
|
85
|
+
listenersMap.delete(listener);
|
|
86
|
+
|
|
87
|
+
if (eventSource) {
|
|
88
|
+
eventSource.removeEventListener(eventName, listener);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
namedCallbacks = null; // allow garbage collect
|
|
96
|
+
|
|
97
|
+
if (useEventsToManageConnection && listenersMapSize > 0 && listenersMap.size === 0 && (status.value === STATUSES.CONNECTING || status.value === STATUSES.CONNECTED)) {
|
|
98
|
+
_disconnect();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const status = {
|
|
104
|
+
value: "default",
|
|
105
|
+
goTo: value => {
|
|
106
|
+
if (value === status.value) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
status.value = value;
|
|
111
|
+
status.onchange();
|
|
112
|
+
},
|
|
113
|
+
onchange: () => {}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
let _disconnect = () => {};
|
|
117
|
+
|
|
118
|
+
const attemptConnection = url => {
|
|
119
|
+
if (status.value === STATUSES.CONNECTING || status.value === STATUSES.CONNECTED) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
eventSource = new EventSource(url, {
|
|
124
|
+
withCredentials: true
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
_disconnect = () => {
|
|
128
|
+
if (status.value !== STATUSES.CONNECTING && status.value !== STATUSES.CONNECTED) {
|
|
129
|
+
console.warn(`disconnect() ignored because connection is ${status.value}`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (eventSource) {
|
|
134
|
+
eventSource.onerror = undefined;
|
|
135
|
+
eventSource.close();
|
|
136
|
+
listenersMap.forEach((listener, eventName) => {
|
|
137
|
+
eventSource.removeEventListener(eventName, listener);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
eventSource = null;
|
|
142
|
+
status.goTo(STATUSES.DISCONNECTED);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
let retryCount = 0;
|
|
146
|
+
let firstRetryMs = Date.now();
|
|
147
|
+
|
|
148
|
+
eventSource.onerror = errorEvent => {
|
|
149
|
+
if (errorEvent.target.readyState === EventSource.CONNECTING) {
|
|
150
|
+
if (retryCount > retryMaxAttempt) {
|
|
151
|
+
console.info(`could not connect after ${retryMaxAttempt} attempt`);
|
|
152
|
+
|
|
153
|
+
_disconnect();
|
|
154
|
+
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (retryCount === 0) {
|
|
159
|
+
firstRetryMs = Date.now();
|
|
160
|
+
} else {
|
|
161
|
+
const allRetryDuration = Date.now() - firstRetryMs;
|
|
162
|
+
|
|
163
|
+
if (retryAllocatedMs && allRetryDuration > retryAllocatedMs) {
|
|
164
|
+
console.info(`could not connect in less than ${retryAllocatedMs} ms`);
|
|
165
|
+
|
|
166
|
+
_disconnect();
|
|
167
|
+
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
retryCount++;
|
|
173
|
+
status.goTo(STATUSES.CONNECTING);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (errorEvent.target.readyState === EventSource.CLOSED) {
|
|
178
|
+
_disconnect();
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
eventSource.onopen = () => {
|
|
185
|
+
status.goTo(STATUSES.CONNECTED);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
listenersMap.forEach((listener, eventName) => {
|
|
189
|
+
eventSource.addEventListener(eventName, listener);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
if (!listenersMap.has("welcome")) {
|
|
193
|
+
addEventCallbacks({
|
|
194
|
+
welcome: () => {} // to update lastEventId
|
|
195
|
+
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
status.goTo(STATUSES.CONNECTING);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
let _connect = () => {
|
|
203
|
+
attemptConnection(eventSourceUrl);
|
|
204
|
+
|
|
205
|
+
_connect = () => {
|
|
206
|
+
attemptConnection(lastEventId ? addLastEventIdIntoUrlSearchParams(eventSourceUrl, lastEventId) : eventSourceUrl);
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const removePageUnloadListener = listenPageUnload(() => {
|
|
211
|
+
if (status.value === STATUSES.CONNECTING || status.value === STATUSES.CONNECTED) {
|
|
212
|
+
_disconnect();
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const destroy = () => {
|
|
217
|
+
removePageUnloadListener();
|
|
218
|
+
|
|
219
|
+
_disconnect();
|
|
220
|
+
|
|
221
|
+
listenersMap.clear();
|
|
222
|
+
callbacksMap.clear();
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
status,
|
|
227
|
+
connect: () => _connect(),
|
|
228
|
+
addEventCallbacks,
|
|
229
|
+
disconnect: () => _disconnect(),
|
|
230
|
+
destroy
|
|
231
|
+
};
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const addLastEventIdIntoUrlSearchParams = (url, lastEventId) => {
|
|
235
|
+
if (url.indexOf("?") === -1) {
|
|
236
|
+
url += "?";
|
|
237
|
+
} else {
|
|
238
|
+
url += "&";
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return `${url}last-event-id=${encodeURIComponent(lastEventId)}`;
|
|
242
|
+
}; // const listenPageMightFreeze = (callback) => {
|
|
243
|
+
// const removePageHideListener = listenEvent(window, "pagehide", (pageHideEvent) => {
|
|
244
|
+
// if (pageHideEvent.persisted === true) {
|
|
245
|
+
// callback(pageHideEvent)
|
|
246
|
+
// }
|
|
247
|
+
// })
|
|
248
|
+
// return removePageHideListener
|
|
249
|
+
// }
|
|
250
|
+
// const listenPageFreeze = (callback) => {
|
|
251
|
+
// const removeFreezeListener = listenEvent(document, "freeze", (freezeEvent) => {
|
|
252
|
+
// callback(freezeEvent)
|
|
253
|
+
// })
|
|
254
|
+
// return removeFreezeListener
|
|
255
|
+
// }
|
|
256
|
+
// const listenPageIsRestored = (callback) => {
|
|
257
|
+
// const removeResumeListener = listenEvent(document, "resume", (resumeEvent) => {
|
|
258
|
+
// removePageshowListener()
|
|
259
|
+
// callback(resumeEvent)
|
|
260
|
+
// })
|
|
261
|
+
// const removePageshowListener = listenEvent(window, "pageshow", (pageshowEvent) => {
|
|
262
|
+
// if (pageshowEvent.persisted === true) {
|
|
263
|
+
// removePageshowListener()
|
|
264
|
+
// removeResumeListener()
|
|
265
|
+
// callback(pageshowEvent)
|
|
266
|
+
// }
|
|
267
|
+
// })
|
|
268
|
+
// return () => {
|
|
269
|
+
// removeResumeListener()
|
|
270
|
+
// removePageshowListener()
|
|
271
|
+
// }
|
|
272
|
+
// }
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
const listenPageUnload = callback => {
|
|
276
|
+
const removePageHideListener = listenEvent(window, "pagehide", pageHideEvent => {
|
|
277
|
+
if (pageHideEvent.persisted !== true) {
|
|
278
|
+
callback(pageHideEvent);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
return removePageHideListener;
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const listenEvent = (emitter, event, callback) => {
|
|
285
|
+
emitter.addEventListener(event, callback);
|
|
286
|
+
return () => {
|
|
287
|
+
emitter.removeEventListener(event, callback);
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
const eventsourceConnection = createEventSourceConnection(document.location.href, {
|
|
292
|
+
retryMaxAttempt: Infinity,
|
|
293
|
+
retryAllocatedMs: 20 * 1000
|
|
294
|
+
});
|
|
295
|
+
const {
|
|
296
|
+
status,
|
|
297
|
+
connect,
|
|
298
|
+
addEventCallbacks,
|
|
299
|
+
disconnect
|
|
300
|
+
} = eventsourceConnection;
|
|
301
|
+
window.__server_events__ = {
|
|
302
|
+
addEventCallbacks,
|
|
303
|
+
status,
|
|
304
|
+
connect,
|
|
305
|
+
disconnect
|
|
306
|
+
};
|
|
307
|
+
connect();
|