|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const conversions = require("webidl-conversions"); |
| 4 | +const utils = require("./utils.js"); |
| 5 | + |
| 6 | +const impl = utils.implSymbol; |
| 7 | +const EventTarget = require("./EventTarget.js"); |
| 8 | + |
| 9 | +module.exports = { |
| 10 | + createInterface: function(defaultPrivateData = {}) { |
| 11 | + function AbortSignal() { |
| 12 | + throw new TypeError("Illegal constructor"); |
| 13 | + } |
| 14 | + |
| 15 | + Object.setPrototypeOf(AbortSignal.prototype, EventTarget.interface.prototype); |
| 16 | + Object.setPrototypeOf(AbortSignal, EventTarget.interface); |
| 17 | + |
| 18 | + Object.defineProperty(AbortSignal, "prototype", { |
| 19 | + value: AbortSignal.prototype, |
| 20 | + writable: false, |
| 21 | + enumerable: false, |
| 22 | + configurable: false |
| 23 | + }); |
| 24 | + |
| 25 | + Object.defineProperty(AbortSignal.prototype, "aborted", { |
| 26 | + get() { |
| 27 | + if (!this || !module.exports.is(this)) { |
| 28 | + throw new TypeError("Illegal invocation"); |
| 29 | + } |
| 30 | + |
| 31 | + return this[impl]["aborted"]; |
| 32 | + }, |
| 33 | + |
| 34 | + enumerable: true, |
| 35 | + configurable: true |
| 36 | + }); |
| 37 | + |
| 38 | + Object.defineProperty(AbortSignal.prototype, "onabort", { |
| 39 | + get() { |
| 40 | + if (!this || !module.exports.is(this)) { |
| 41 | + throw new TypeError("Illegal invocation"); |
| 42 | + } |
| 43 | + |
| 44 | + return utils.tryWrapperForImpl(this[impl]["onabort"]); |
| 45 | + }, |
| 46 | + |
| 47 | + set(V) { |
| 48 | + if (!this || !module.exports.is(this)) { |
| 49 | + throw new TypeError("Illegal invocation"); |
| 50 | + } |
| 51 | + |
| 52 | + V = utils.tryImplForWrapper(V); |
| 53 | + |
| 54 | + this[impl]["onabort"] = V; |
| 55 | + }, |
| 56 | + |
| 57 | + enumerable: true, |
| 58 | + configurable: true |
| 59 | + }); |
| 60 | + |
| 61 | + Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, { |
| 62 | + value: "AbortSignal", |
| 63 | + writable: false, |
| 64 | + enumerable: false, |
| 65 | + configurable: true |
| 66 | + }); |
| 67 | + |
| 68 | + const iface = { |
| 69 | + create(constructorArgs, privateData) { |
| 70 | + let obj = Object.create(AbortSignal.prototype); |
| 71 | + obj = this.setup(obj, constructorArgs, privateData); |
| 72 | + return obj; |
| 73 | + }, |
| 74 | + createImpl(constructorArgs, privateData) { |
| 75 | + let obj = Object.create(AbortSignal.prototype); |
| 76 | + obj = this.setup(obj, constructorArgs, privateData); |
| 77 | + return utils.implForWrapper(obj); |
| 78 | + }, |
| 79 | + _internalSetup(obj) { |
| 80 | + EventTarget._internalSetup(obj); |
| 81 | + }, |
| 82 | + setup(obj, constructorArgs, privateData) { |
| 83 | + if (!privateData) privateData = {}; |
| 84 | + |
| 85 | + for (var prop in defaultPrivateData) { |
| 86 | + if (!(prop in privateData)) { |
| 87 | + privateData[prop] = defaultPrivateData[prop]; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + privateData.wrapper = obj; |
| 92 | + |
| 93 | + this._internalSetup(obj); |
| 94 | + Object.defineProperty(obj, impl, { |
| 95 | + value: new Impl.implementation(constructorArgs, privateData), |
| 96 | + writable: false, |
| 97 | + enumerable: false, |
| 98 | + configurable: true |
| 99 | + }); |
| 100 | + |
| 101 | + obj[impl][utils.wrapperSymbol] = obj; |
| 102 | + if (Impl.init) { |
| 103 | + Impl.init(obj[impl], privateData); |
| 104 | + } |
| 105 | + return obj; |
| 106 | + }, |
| 107 | + interface: AbortSignal, |
| 108 | + expose: { |
| 109 | + Window: { AbortSignal }, |
| 110 | + Worker: { AbortSignal } |
| 111 | + } |
| 112 | + }; // iface |
| 113 | + return iface; |
| 114 | + }, // createInterface |
| 115 | + |
| 116 | + // When an interface-module that implements this interface as a mixin is loaded, it will append its own `.is()` |
| 117 | + // method into this array. It allows objects that directly implements *those* interfaces to be recognized as |
| 118 | + // implementing this mixin interface. |
| 119 | + _mixedIntoPredicates: [], |
| 120 | + is(obj) { |
| 121 | + if (obj) { |
| 122 | + if (utils.hasOwn(obj, impl) && obj[impl] instanceof Impl.implementation) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + for (const isMixedInto of module.exports._mixedIntoPredicates) { |
| 126 | + if (isMixedInto(obj)) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return false; |
| 132 | + }, |
| 133 | + isImpl(obj) { |
| 134 | + if (obj) { |
| 135 | + if (obj instanceof Impl.implementation) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + const wrapper = utils.wrapperForImpl(obj); |
| 140 | + for (const isMixedInto of module.exports._mixedIntoPredicates) { |
| 141 | + if (isMixedInto(wrapper)) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + return false; |
| 147 | + }, |
| 148 | + convert(obj, { context = "The provided value" } = {}) { |
| 149 | + if (module.exports.is(obj)) { |
| 150 | + return utils.implForWrapper(obj); |
| 151 | + } |
| 152 | + throw new TypeError(`${context} is not of type 'AbortSignal'.`); |
| 153 | + } |
| 154 | +}; // module.exports |
| 155 | + |
| 156 | +const Impl = require("../aborting/AbortSignal-impl.js"); |
0 commit comments