@modern-js/runtime-utils
Version:
A Progressive React Framework for modern web development.
228 lines (227 loc) • 7.27 kB
JavaScript
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _call_super } from "@swc/helpers/_/_call_super";
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
import { _ as _create_class } from "@swc/helpers/_/_create_class";
import { _ as _define_property } from "@swc/helpers/_/_define_property";
import { _ as _inherits } from "@swc/helpers/_/_inherits";
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _type_of } from "@swc/helpers/_/_type_of";
import { _ as _wrap_native_super } from "@swc/helpers/_/_wrap_native_super";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
function invariant(value, message) {
if (value === false || value === null || typeof value === "undefined") {
throw new Error(message);
}
}
var AbortedDeferredError = /* @__PURE__ */ function(Error1) {
"use strict";
_inherits(AbortedDeferredError2, Error1);
function AbortedDeferredError2() {
_class_call_check(this, AbortedDeferredError2);
return _call_super(this, AbortedDeferredError2, arguments);
}
return AbortedDeferredError2;
}(_wrap_native_super(Error));
function isTrackedPromise(value) {
return _instanceof(value, Promise) && value._tracked === true;
}
function unwrapTrackedPromise(value) {
if (!isTrackedPromise(value)) {
return value;
}
if (value._error) {
throw value._error;
}
return value._data;
}
var DeferredData = /* @__PURE__ */ function() {
"use strict";
function DeferredData2(data, responseInit) {
var _this = this;
_class_call_check(this, DeferredData2);
this.pendingKeysSet = /* @__PURE__ */ new Set();
this.subscribers = /* @__PURE__ */ new Set();
this.__modern_deferred = true;
this.deferredKeys = [];
invariant(data && (typeof data === "undefined" ? "undefined" : _type_of(data)) === "object" && !Array.isArray(data), "defer() only accepts plain objects");
var reject;
this.abortPromise = new Promise(function(_, r) {
return reject = r;
});
this.controller = new AbortController();
var onAbort = function() {
return reject(new AbortedDeferredError("Deferred data aborted"));
};
this.unlistenAbortSignal = function() {
return _this.controller.signal.removeEventListener("abort", onAbort);
};
this.controller.signal.addEventListener("abort", onAbort);
this.data = Object.entries(data).reduce(function(acc, param) {
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
return Object.assign(acc, _define_property({}, key, _this.trackPromise(key, value)));
}, {});
if (this.done) {
this.unlistenAbortSignal();
}
this.init = responseInit;
}
var _proto = DeferredData2.prototype;
_proto.trackPromise = function trackPromise(key, value) {
var _this = this;
if (!_instanceof(value, Promise)) {
return value;
}
this.deferredKeys.push(key);
this.pendingKeysSet.add(key);
var promise = Promise.race([
value,
this.abortPromise
]).then(function(data) {
return _this.onSettle(promise, key, void 0, data);
}, function(error) {
return _this.onSettle(promise, key, error);
});
promise.catch(function() {
});
Object.defineProperty(promise, "_tracked", {
get: function() {
return true;
}
});
return promise;
};
_proto.onSettle = function onSettle(promise, key, error, data) {
if (this.controller.signal.aborted && _instanceof(error, AbortedDeferredError)) {
this.unlistenAbortSignal();
Object.defineProperty(promise, "_error", {
get: function() {
return error;
}
});
return Promise.reject(error);
}
this.pendingKeysSet.delete(key);
if (this.done) {
this.unlistenAbortSignal();
}
if (error === void 0 && data === void 0) {
var undefinedError = new Error('Deferred data for key "'.concat(key, '" resolved/rejected with `undefined`, you must resolve/reject with a value or `null`.'));
Object.defineProperty(promise, "_error", {
get: function() {
return undefinedError;
}
});
this.emit(false, key);
return Promise.reject(undefinedError);
}
if (data === void 0) {
Object.defineProperty(promise, "_error", {
get: function() {
return error;
}
});
this.emit(false, key);
return Promise.reject(error);
}
Object.defineProperty(promise, "_data", {
get: function() {
return data;
}
});
this.emit(false, key);
return data;
};
_proto.emit = function emit(aborted, settledKey) {
this.subscribers.forEach(function(subscriber) {
return subscriber(aborted, settledKey);
});
};
_proto.subscribe = function subscribe(fn) {
var _this = this;
this.subscribers.add(fn);
return function() {
return _this.subscribers.delete(fn);
};
};
_proto.cancel = function cancel() {
var _this = this;
this.controller.abort();
this.pendingKeysSet.forEach(function(v, k) {
return _this.pendingKeysSet.delete(k);
});
this.emit(true);
};
_proto.resolveData = function resolveData(signal) {
var _this = this;
return _async_to_generator(function() {
var aborted, onAbort;
return _ts_generator(this, function(_state) {
switch (_state.label) {
case 0:
aborted = false;
if (!!_this.done)
return [
3,
2
];
onAbort = function() {
return _this.cancel();
};
signal.addEventListener("abort", onAbort);
return [
4,
new Promise(function(resolve) {
_this.subscribe(function(aborted2) {
signal.removeEventListener("abort", onAbort);
if (aborted2 || _this.done) {
resolve(aborted2);
}
});
})
];
case 1:
aborted = _state.sent();
_state.label = 2;
case 2:
return [
2,
aborted
];
}
});
})();
};
_create_class(DeferredData2, [
{
key: "done",
get: function get() {
return this.pendingKeysSet.size === 0;
}
},
{
key: "unwrappedData",
get: function get() {
invariant(this.data !== null && this.done, "Can only unwrap data on initialized and settled deferreds");
return Object.entries(this.data).reduce(function(acc, param) {
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
return Object.assign(acc, _define_property({}, key, unwrapTrackedPromise(value)));
}, {});
}
},
{
key: "pendingKeys",
get: function get() {
return Array.from(this.pendingKeysSet);
}
}
]);
return DeferredData2;
}();
var activeDeferreds = /* @__PURE__ */ new Map();
export {
AbortedDeferredError,
DeferredData,
activeDeferreds,
invariant
};