|
| 1 | +/* Copyright 2012 Mozilla Foundation |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +const INITIAL_DATA = Symbol("INITIAL_DATA"); |
| 17 | + |
| 18 | +/** |
| 19 | + * A PDF document and page is built of many objects. E.g. there are objects for |
| 20 | + * fonts, images, rendering code, etc. These objects may get processed inside of |
| 21 | + * a worker. This class implements some basic methods to manage these objects. |
| 22 | + */ |
| 23 | +class PDFObjects { |
| 24 | + #objs = Object.create(null); |
| 25 | + |
| 26 | + /** |
| 27 | + * Ensures there is an object defined for `objId`. |
| 28 | + * |
| 29 | + * @param {string} objId |
| 30 | + * @returns {Object} |
| 31 | + */ |
| 32 | + #ensureObj(objId) { |
| 33 | + return (this.#objs[objId] ||= { |
| 34 | + ...Promise.withResolvers(), |
| 35 | + data: INITIAL_DATA, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * If called *without* callback, this returns the data of `objId` but the |
| 41 | + * object needs to be resolved. If it isn't, this method throws. |
| 42 | + * |
| 43 | + * If called *with* a callback, the callback is called with the data of the |
| 44 | + * object once the object is resolved. That means, if you call this method |
| 45 | + * and the object is already resolved, the callback gets called right away. |
| 46 | + * |
| 47 | + * @param {string} objId |
| 48 | + * @param {function} [callback] |
| 49 | + * @returns {any} |
| 50 | + */ |
| 51 | + get(objId, callback = null) { |
| 52 | + // If there is a callback, then the get can be async and the object is |
| 53 | + // not required to be resolved right now. |
| 54 | + if (callback) { |
| 55 | + const obj = this.#ensureObj(objId); |
| 56 | + obj.promise.then(() => callback(obj.data)); |
| 57 | + return null; |
| 58 | + } |
| 59 | + // If there isn't a callback, the user expects to get the resolved data |
| 60 | + // directly. |
| 61 | + const obj = this.#objs[objId]; |
| 62 | + // If there isn't an object yet or the object isn't resolved, then the |
| 63 | + // data isn't ready yet! |
| 64 | + if (!obj || obj.data === INITIAL_DATA) { |
| 65 | + throw new Error(`Requesting object that isn't resolved yet ${objId}.`); |
| 66 | + } |
| 67 | + return obj.data; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param {string} objId |
| 72 | + * @returns {boolean} |
| 73 | + */ |
| 74 | + has(objId) { |
| 75 | + const obj = this.#objs[objId]; |
| 76 | + return !!obj && obj.data !== INITIAL_DATA; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param {string} objId |
| 81 | + * @returns {boolean} |
| 82 | + */ |
| 83 | + delete(objId) { |
| 84 | + const obj = this.#objs[objId]; |
| 85 | + if (!obj || obj.data === INITIAL_DATA) { |
| 86 | + // Only allow removing the object *after* it's been resolved. |
| 87 | + return false; |
| 88 | + } |
| 89 | + delete this.#objs[objId]; |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Resolves the object `objId` with optional `data`. |
| 95 | + * |
| 96 | + * @param {string} objId |
| 97 | + * @param {any} [data] |
| 98 | + */ |
| 99 | + resolve(objId, data = null) { |
| 100 | + const obj = this.#ensureObj(objId); |
| 101 | + obj.data = data; |
| 102 | + obj.resolve(); |
| 103 | + } |
| 104 | + |
| 105 | + clear() { |
| 106 | + for (const objId in this.#objs) { |
| 107 | + const { data } = this.#objs[objId]; |
| 108 | + data?.bitmap?.close(); // Release any `ImageBitmap` data. |
| 109 | + } |
| 110 | + this.#objs = Object.create(null); |
| 111 | + } |
| 112 | + |
| 113 | + *[Symbol.iterator]() { |
| 114 | + for (const objId in this.#objs) { |
| 115 | + const { data } = this.#objs[objId]; |
| 116 | + |
| 117 | + if (data === INITIAL_DATA) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + yield [objId, data]; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export { PDFObjects }; |
0 commit comments