|
| 1 | +/** |
| 2 | + * ProxyPolyfill is a dump wrapper for `handler` |
| 3 | + * where `target` is a map and is always passed as parameter. |
| 4 | + */ |
| 5 | +class MapProxyPolyfill { |
| 6 | + /** |
| 7 | + * Creates ProxyPolyfill and defines methos dynamically. |
| 8 | + * All methods are a dump wrapper to `handler` methods with `target` as first parameter. |
| 9 | + */ |
| 10 | + constructor(target, handler) { |
| 11 | + this.target = target |
| 12 | + for (const item in handler) { |
| 13 | + if (Object.prototype.hasOwnProperty.call(handler, item)) { |
| 14 | + this[item] = (...args) => handler[item](this.target, ...args) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | + // Implements `getOwnPropertyNames` method for wrapped class. |
| 20 | + // This is needed because it is not possible to override `Object.getOwnPropertyNames()` without a `Proxy`. |
| 21 | + // |
| 22 | + // This method is a dump wrapper of `ownKey()` so it must be created only if the handle has `ownKey()` method. |
| 23 | + // (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/ownKeys for more info) |
| 24 | + if (typeof handler.ownKeys === 'function') { |
| 25 | + this.getOwnPropertyNames = () => handler.ownKeys(this.target) |
| 26 | + } |
| 27 | + |
| 28 | + // Implements `assign` method for wrapped class. |
| 29 | + // This is needed because it is not possible to override `Object.assign()` without a `Proxy`. |
| 30 | + if (typeof handler.set === 'function') { |
| 31 | + this.assign = (object) => { |
| 32 | + Object.keys(object).forEach(function(key) { |
| 33 | + handler.set(target, key, object[key]) |
| 34 | + }) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + iterator () { |
| 40 | + // NOTE: this method used to be a generator; it has been converted to a regular |
| 41 | + // method (that mimics the interface of a generator) to avoid having to include |
| 42 | + // generator polyfills in the distribution build. |
| 43 | + // eslint-disable-next-line consistent-this |
| 44 | + const doc = this |
| 45 | + let keys = doc.ownKeys() |
| 46 | + let index = 0 |
| 47 | + return { |
| 48 | + next () { |
| 49 | + let key = keys[index] |
| 50 | + if (!key) return { value: undefined, done: true } |
| 51 | + index = index + 1 |
| 52 | + return {value: [key, doc.get(key)], done: false} |
| 53 | + }, |
| 54 | + [Symbol.iterator]: () => this.iterator(), |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Defines iterator. Iterates the map's key and values |
| 60 | + */ |
| 61 | + [Symbol.iterator] () { |
| 62 | + return this.iterator() |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * To be used by JSON.stringify() function. |
| 67 | + * It returns the wrapped instance. |
| 68 | + * (more info https://javascript.info/json#custom-tojson) |
| 69 | + */ |
| 70 | + toJSON () { |
| 71 | + const { context, objectId } = this.target |
| 72 | + let object = context.getObject(objectId) |
| 73 | + return object |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Implements isArray method for wrapped class. |
| 78 | + * This is needed because it is not possible to override Array.isArray() without a Proxy. |
| 79 | + */ |
| 80 | + isArray () { |
| 81 | + return false |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * ListProxyPolyfill is a dump wrapper for `handler` |
| 87 | + * where `target` is an array and is always passed as parameter. |
| 88 | + */ |
| 89 | +class ListProxyPolyfill { |
| 90 | + /** |
| 91 | + * Creates ListProxyPolyfill and defines methos dynamically. |
| 92 | + * All methods are a dump wrapper to `handler` methods with `target` as first parameter. |
| 93 | + */ |
| 94 | + constructor(target, handler, listMethods) { |
| 95 | + this.target = target |
| 96 | + for (const item in handler) { |
| 97 | + if (Object.prototype.hasOwnProperty.call(handler, item)) { |
| 98 | + this[item] = (...args) => handler[item](this.target, ...args) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Casts `key` to string before calling `handler`s `get` method. |
| 103 | + // This is needed because Proxy does so and the handler is prepared for that. |
| 104 | + this.get = (key) => { |
| 105 | + if (typeof key == 'number') { |
| 106 | + key = key.toString() |
| 107 | + } |
| 108 | + return handler.get(this.target, key) |
| 109 | + } |
| 110 | + |
| 111 | + // Casts `key` to string before calling `handler`s `get` method. |
| 112 | + // This is needed because Proxy does so and the handler is prepared for that. |
| 113 | + this.has = (key) => { |
| 114 | + if (typeof key == 'number') { |
| 115 | + key = key.toString() |
| 116 | + } |
| 117 | + return handler.has(this.target, key) |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + // Implements `objectKeys` method for wrapped class. |
| 122 | + // This is needed because it is not possible to override `Object.keys()` without a `Proxy`. |
| 123 | + // |
| 124 | + // This method returns only enumerable property names. |
| 125 | + // (more info https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) |
| 126 | + if (typeof handler.ownKeys === 'function' && typeof handler.getOwnPropertyDescriptor === 'function') { |
| 127 | + this.objectKeys = () => { |
| 128 | + let keys = [] |
| 129 | + for (let key of handler.ownKeys(this.target)) { |
| 130 | + let description = handler.getOwnPropertyDescriptor(this.target, key) |
| 131 | + if (description.enumerable) { |
| 132 | + keys.push(key) |
| 133 | + } |
| 134 | + } |
| 135 | + return keys |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Implements `getOwnPropertyNames` method for wrapped class. |
| 140 | + // This is needed because it is not possible to override `Object.getOwnPropertyNames()` without a `Proxy`. |
| 141 | + // |
| 142 | + // This method is a dump wrapper of `ownKey()` so it must be created only if the handle has `ownKey()` method. |
| 143 | + // (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/ownKeys for more info) |
| 144 | + if (typeof handler.ownKeys === 'function') { |
| 145 | + this.getOwnPropertyNames = () => handler.ownKeys(this.target) |
| 146 | + } |
| 147 | + |
| 148 | + // Defines same methods as listMethods |
| 149 | + // All methods are a dump wrapper to the ones defined on listMethods. |
| 150 | + const [context, objectId, path] = target |
| 151 | + const _listMethods = listMethods(context, objectId, path) |
| 152 | + for (const methodName in _listMethods) { |
| 153 | + if (Object.prototype.hasOwnProperty.call(_listMethods, methodName)) { |
| 154 | + this[methodName] = (...args) => _listMethods[methodName](...args) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + iterator () { |
| 160 | + // NOTE: this method used to be a generator; it has been converted to a regular |
| 161 | + // method (that mimics the interface of a generator) to avoid having to include |
| 162 | + // generator polyfills in the distribution build. |
| 163 | + // eslint-disable-next-line consistent-this |
| 164 | + let doc = this |
| 165 | + let keysIterator = doc.keys() |
| 166 | + return { |
| 167 | + next () { |
| 168 | + let nextKey = keysIterator.next() |
| 169 | + if (nextKey.done) return nextKey |
| 170 | + return {value: doc.get(nextKey.value), done: false} |
| 171 | + }, |
| 172 | + [Symbol.iterator]: () => this.iterator(), |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Defines iterator. Iterates the array's values |
| 178 | + */ |
| 179 | + [Symbol.iterator] () { |
| 180 | + return this.iterator() |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Implements isArray method for wrapped class. |
| 185 | + * This is needed because it is not possible to override Array.isArray() without a Proxy. |
| 186 | + */ |
| 187 | + isArray () { |
| 188 | + return true |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Implements length method for wrapped class. |
| 193 | + * This is needed because it is not possible to override .length without a Proxy. |
| 194 | + */ |
| 195 | + length () { |
| 196 | + const [context, objectId, /* path */] = this.target |
| 197 | + const object = context.getObject(objectId) |
| 198 | + return object.length |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * To be used by JSON.stringify() function. |
| 203 | + * It returns the wrapped instance. |
| 204 | + * (more info https://javascript.info/json#custom-tojson) |
| 205 | + */ |
| 206 | + toJSON () { |
| 207 | + const [ context, objectId ] = this.target |
| 208 | + let object = context.getObject(objectId) |
| 209 | + return object |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +module.exports = { ListProxyPolyfill, MapProxyPolyfill } |
0 commit comments