react-native-web-internals
Version:
React Native for Web
143 lines (142 loc) • 5.49 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
var ImageLoader_exports = {};
__export(ImageLoader_exports, {
ImageUriCache: () => ImageUriCache,
default: () => ImageLoader_default
});
module.exports = __toCommonJS(ImageLoader_exports);
function _class_call_check(instance, Constructor) {
if (!(instance instanceof Constructor))
throw new TypeError("Cannot call a class as a function");
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
}
}
function _create_class(Constructor, protoProps, staticProps) {
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor;
}
function _define_property(obj, key, value) {
return key in obj ? Object.defineProperty(obj, key, {
value,
enumerable: !0,
configurable: !0,
writable: !0
}) : obj[key] = value, obj;
}
var dataUriPattern = /^data:/, ImageUriCache = /* @__PURE__ */ function() {
"use strict";
function ImageUriCache2() {
_class_call_check(this, ImageUriCache2);
}
return _create_class(ImageUriCache2, null, [
{
key: "has",
value: function(uri) {
var entries = ImageUriCache2._entries, isDataUri = dataUriPattern.test(uri);
return isDataUri || !!entries[uri];
}
},
{
key: "add",
value: function(uri) {
var entries = ImageUriCache2._entries, lastUsedTimestamp = Date.now();
entries[uri] ? (entries[uri].lastUsedTimestamp = lastUsedTimestamp, entries[uri].refCount += 1) : entries[uri] = {
lastUsedTimestamp,
refCount: 1
};
}
},
{
key: "remove",
value: function(uri) {
var entries = ImageUriCache2._entries;
entries[uri] && (entries[uri].refCount -= 1), ImageUriCache2._cleanUpIfNeeded();
}
},
{
key: "_cleanUpIfNeeded",
value: function() {
var entries = ImageUriCache2._entries, imageUris = Object.keys(entries);
if (imageUris.length + 1 > ImageUriCache2._maximumEntries) {
var leastRecentlyUsedKey, leastRecentlyUsedEntry;
imageUris.forEach(function(uri) {
var entry = entries[uri];
(!leastRecentlyUsedEntry || entry.lastUsedTimestamp < leastRecentlyUsedEntry.lastUsedTimestamp) && entry.refCount === 0 && (leastRecentlyUsedKey = uri, leastRecentlyUsedEntry = entry);
}), leastRecentlyUsedKey && delete entries[leastRecentlyUsedKey];
}
}
}
]), ImageUriCache2;
}();
_define_property(ImageUriCache, "_maximumEntries", 256);
_define_property(ImageUriCache, "_entries", {});
var id = 0, requests = {}, ImageLoader = {
abort(requestId) {
var image = requests[`${requestId}`];
image && (image.onerror = null, image.onload = null, image = null, delete requests[`${requestId}`]);
},
getSize(uri, success, failure) {
var complete = !1, interval = setInterval(callback, 16), requestId = ImageLoader.load(uri, callback, errorCallback);
function callback() {
var image = requests[`${requestId}`];
if (image) {
var { naturalHeight, naturalWidth } = image;
naturalHeight && naturalWidth && (success(naturalWidth, naturalHeight), complete = !0);
}
complete && (ImageLoader.abort(requestId), clearInterval(interval));
}
function errorCallback() {
typeof failure == "function" && failure(), ImageLoader.abort(requestId), clearInterval(interval);
}
},
has(uri) {
return ImageUriCache.has(uri);
},
load(uri, onLoad, onError) {
id += 1;
var image = new window.Image();
return image.onerror = onError, image.onload = function(e) {
var onDecode = function() {
return onLoad({
nativeEvent: e
});
};
typeof image.decode == "function" ? image.decode().then(onDecode, onDecode) : setTimeout(onDecode, 0);
}, image.src = uri, requests[`${id}`] = image, id;
},
prefetch(uri) {
return new Promise(function(resolve, reject) {
ImageLoader.load(uri, function() {
ImageUriCache.add(uri), ImageUriCache.remove(uri), resolve();
}, reject);
});
},
queryCache(uris) {
var result = {};
return uris.forEach(function(u) {
ImageUriCache.has(u) && (result[u] = "disk/memory");
}), Promise.resolve(result);
}
}, ImageLoader_default = ImageLoader;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ImageUriCache
});
//# sourceMappingURL=index.js.map