1 | /* global __resourceQuery */
|
---|
2 |
|
---|
3 | "use strict";
|
---|
4 |
|
---|
5 | if (typeof EventSource !== "function") {
|
---|
6 | throw new Error(
|
---|
7 | "Environment doesn't support lazy compilation (requires EventSource)"
|
---|
8 | );
|
---|
9 | }
|
---|
10 |
|
---|
11 | var urlBase = decodeURIComponent(__resourceQuery.slice(1));
|
---|
12 | var activeEventSource;
|
---|
13 | var activeKeys = new Map();
|
---|
14 | var errorHandlers = new Set();
|
---|
15 |
|
---|
16 | var updateEventSource = function updateEventSource() {
|
---|
17 | if (activeEventSource) activeEventSource.close();
|
---|
18 | if (activeKeys.size) {
|
---|
19 | activeEventSource = new EventSource(
|
---|
20 | urlBase + Array.from(activeKeys.keys()).join("@")
|
---|
21 | );
|
---|
22 | activeEventSource.onerror = function (event) {
|
---|
23 | errorHandlers.forEach(function (onError) {
|
---|
24 | onError(
|
---|
25 | new Error(
|
---|
26 | "Problem communicating active modules to the server: " +
|
---|
27 | event.message +
|
---|
28 | " " +
|
---|
29 | event.filename +
|
---|
30 | ":" +
|
---|
31 | event.lineno +
|
---|
32 | ":" +
|
---|
33 | event.colno +
|
---|
34 | " " +
|
---|
35 | event.error
|
---|
36 | )
|
---|
37 | );
|
---|
38 | });
|
---|
39 | };
|
---|
40 | } else {
|
---|
41 | activeEventSource = undefined;
|
---|
42 | }
|
---|
43 | };
|
---|
44 |
|
---|
45 | exports.keepAlive = function (options) {
|
---|
46 | var data = options.data;
|
---|
47 | var onError = options.onError;
|
---|
48 | var active = options.active;
|
---|
49 | var module = options.module;
|
---|
50 | errorHandlers.add(onError);
|
---|
51 | var value = activeKeys.get(data) || 0;
|
---|
52 | activeKeys.set(data, value + 1);
|
---|
53 | if (value === 0) {
|
---|
54 | updateEventSource();
|
---|
55 | }
|
---|
56 | if (!active && !module.hot) {
|
---|
57 | console.log(
|
---|
58 | "Hot Module Replacement is not enabled. Waiting for process restart..."
|
---|
59 | );
|
---|
60 | }
|
---|
61 |
|
---|
62 | return function () {
|
---|
63 | errorHandlers.delete(onError);
|
---|
64 | setTimeout(function () {
|
---|
65 | var value = activeKeys.get(data);
|
---|
66 | if (value === 1) {
|
---|
67 | activeKeys.delete(data);
|
---|
68 | updateEventSource();
|
---|
69 | } else {
|
---|
70 | activeKeys.set(data, value - 1);
|
---|
71 | }
|
---|
72 | }, 1000);
|
---|
73 | };
|
---|
74 | };
|
---|