mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-06 08:08:38 +01:00
Merge pull request #795 from mvanhorn/fix/662-map-upsert-polyfill
fix: polyfill Map.prototype.getOrInsertComputed to stop PDF.js render crash
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import './utils/map-upsert-polyfill.js';
|
||||
import { categories } from './config/tools.js';
|
||||
import { dom, switchView, hideAlert } from './ui.js';
|
||||
import { ShortcutsManager } from './logic/shortcuts.js';
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
type MapPrototypeWithUpsert = typeof Map.prototype & {
|
||||
getOrInsert?: (key: unknown, value: unknown) => unknown;
|
||||
getOrInsertComputed?: (
|
||||
key: unknown,
|
||||
callback: (key: unknown) => unknown
|
||||
) => unknown;
|
||||
};
|
||||
|
||||
const mapPrototype = Map.prototype as MapPrototypeWithUpsert;
|
||||
const mapHas = Map.prototype.has;
|
||||
const mapGet = Map.prototype.get;
|
||||
const mapSet = Map.prototype.set;
|
||||
|
||||
if (typeof mapPrototype.getOrInsert !== 'function') {
|
||||
Object.defineProperty(mapPrototype, 'getOrInsert', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: function getOrInsert(key: unknown, value: unknown) {
|
||||
if (mapHas.call(this, key)) return mapGet.call(this, key);
|
||||
mapSet.call(this, key, value);
|
||||
return value;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof mapPrototype.getOrInsertComputed !== 'function') {
|
||||
Object.defineProperty(mapPrototype, 'getOrInsertComputed', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: function getOrInsertComputed(
|
||||
key: unknown,
|
||||
callback: (key: unknown) => unknown
|
||||
) {
|
||||
const hasKey = mapHas.call(this, key);
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('callback must be a function');
|
||||
}
|
||||
if (hasKey) return mapGet.call(this, key);
|
||||
|
||||
const value = callback(key);
|
||||
mapSet.call(this, key, value);
|
||||
return value;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export {};
|
||||
Reference in New Issue
Block a user