| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | /* global __resourceQuery */
|
|---|
| 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 |
|
|---|
| 58 | errorHandlers.add(onError);
|
|---|
| 59 |
|
|---|
| 60 | var value = activeKeys.get(data) || 0;
|
|---|
| 61 |
|
|---|
| 62 | activeKeys.set(data, value + 1);
|
|---|
| 63 |
|
|---|
| 64 | if (value === 0) {
|
|---|
| 65 | updateEventSource();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | if (!options.active && !options.module.hot) {
|
|---|
| 69 | console.log(
|
|---|
| 70 | "Hot Module Replacement is not enabled. Waiting for process restart..."
|
|---|
| 71 | );
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return function () {
|
|---|
| 75 | errorHandlers.delete(onError);
|
|---|
| 76 | setTimeout(function () {
|
|---|
| 77 | var value = activeKeys.get(data);
|
|---|
| 78 | if (value === 1) {
|
|---|
| 79 | activeKeys.delete(data);
|
|---|
| 80 | updateEventSource();
|
|---|
| 81 | } else {
|
|---|
| 82 | activeKeys.set(data, value - 1);
|
|---|
| 83 | }
|
|---|
| 84 | }, 1000);
|
|---|
| 85 | };
|
|---|
| 86 | };
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * @param {string} value new url value
|
|---|
| 90 | */
|
|---|
| 91 | exports.setUrl = function (value) {
|
|---|
| 92 | urlBase = value;
|
|---|
| 93 | };
|
|---|