[79a0317] | 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 | /** @type {EventSource | undefined} */
|
---|
| 13 | var activeEventSource;
|
---|
| 14 | var activeKeys = new Map();
|
---|
| 15 | var errorHandlers = new Set();
|
---|
| 16 |
|
---|
| 17 | var updateEventSource = function updateEventSource() {
|
---|
| 18 | if (activeEventSource) activeEventSource.close();
|
---|
| 19 | if (activeKeys.size) {
|
---|
| 20 | activeEventSource = new EventSource(
|
---|
| 21 | urlBase + Array.from(activeKeys.keys()).join("@")
|
---|
| 22 | );
|
---|
| 23 | /**
|
---|
| 24 | * @this {EventSource}
|
---|
| 25 | * @param {Event & { message?: string, filename?: string, lineno?: number, colno?: number, error?: Error }} event event
|
---|
| 26 | */
|
---|
| 27 | activeEventSource.onerror = function (event) {
|
---|
| 28 | errorHandlers.forEach(function (onError) {
|
---|
| 29 | onError(
|
---|
| 30 | new Error(
|
---|
| 31 | "Problem communicating active modules to the server: " +
|
---|
| 32 | event.message +
|
---|
| 33 | " " +
|
---|
| 34 | event.filename +
|
---|
| 35 | ":" +
|
---|
| 36 | event.lineno +
|
---|
| 37 | ":" +
|
---|
| 38 | event.colno +
|
---|
| 39 | " " +
|
---|
| 40 | event.error
|
---|
| 41 | )
|
---|
| 42 | );
|
---|
| 43 | });
|
---|
| 44 | };
|
---|
| 45 | } else {
|
---|
| 46 | activeEventSource = undefined;
|
---|
| 47 | }
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * @param {{ data: string, onError: (err: Error) => void, active: boolean, module: module }} options options
|
---|
| 52 | * @returns {() => void} function to destroy response
|
---|
| 53 | */
|
---|
| 54 | exports.keepAlive = function (options) {
|
---|
| 55 | var data = options.data;
|
---|
| 56 | var onError = options.onError;
|
---|
| 57 | var active = options.active;
|
---|
| 58 | var module = options.module;
|
---|
| 59 | errorHandlers.add(onError);
|
---|
| 60 | var value = activeKeys.get(data) || 0;
|
---|
| 61 | activeKeys.set(data, value + 1);
|
---|
| 62 | if (value === 0) {
|
---|
| 63 | updateEventSource();
|
---|
| 64 | }
|
---|
| 65 | if (!active && !module.hot) {
|
---|
| 66 | console.log(
|
---|
| 67 | "Hot Module Replacement is not enabled. Waiting for process restart..."
|
---|
| 68 | );
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | return function () {
|
---|
| 72 | errorHandlers.delete(onError);
|
---|
| 73 | setTimeout(function () {
|
---|
| 74 | var value = activeKeys.get(data);
|
---|
| 75 | if (value === 1) {
|
---|
| 76 | activeKeys.delete(data);
|
---|
| 77 | updateEventSource();
|
---|
| 78 | } else {
|
---|
| 79 | activeKeys.set(data, value - 1);
|
---|
| 80 | }
|
---|
| 81 | }, 1000);
|
---|
| 82 | };
|
---|
| 83 | };
|
---|