| 862 | } |
| 863 | |
| 864 | abort(error: any, trySendAbortMessage: boolean = true) { |
| 865 | // Don't double-abort. |
| 866 | if (this.abortReason !== undefined) return; |
| 867 | |
| 868 | this.cancelReadLoop?.(error); |
| 869 | this.cancelReadLoop = undefined; |
| 870 | |
| 871 | if (trySendAbortMessage) { |
| 872 | try { |
| 873 | let abortMsg = ["abort", Devaluator.devaluate(error, undefined, this, undefined, this.encodingLevel)]; |
| 874 | if (this.encodingLevel === "string") { |
| 875 | let sent = (this.transport as RpcTransport) |
| 876 | .send(JSON.stringify(abortMsg)) as Promise<void> | undefined; |
| 877 | if (sent !== undefined && typeof sent.catch === "function") { |
| 878 | sent.catch(err => {}); |
| 879 | } |
| 880 | } else { |
| 881 | let result = (this.transport as RpcTransportWithCustomEncoding).send(abortMsg) as unknown; |
| 882 | if (result && typeof (result as PromiseLike<unknown>).then === "function") { |
| 883 | Promise.resolve(result).catch(err => {}); |
| 884 | } |
| 885 | } |
| 886 | } catch (err) { |
| 887 | // ignore, probably the whole reason we're aborting is because the transport is broken |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | if (error === undefined) { |
| 892 | // Shouldn't happen, but if it does, avoid setting `abortReason` to `undefined`. |
| 893 | error = "undefined"; |
| 894 | } |
| 895 | |
| 896 | this.abortReason = error; |
| 897 | if (this.onBatchDone) { |
| 898 | this.onBatchDone.reject(error); |
| 899 | } |
| 900 | |
| 901 | if (this.transport.abort) { |
| 902 | // Call transport's abort handler, but guard against buggy app code. |
| 903 | try { |
| 904 | this.transport.abort(error); |
| 905 | } catch (err) { |
| 906 | // Treat as unhandled rejection. |
| 907 | Promise.resolve(err); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // WATCH OUT: these are sparse arrays. `for/let/of` will iterate only positive indexes |
| 912 | // including deleted indexes -- bad. We need to use `for/let/in` instead. |
| 913 | for (let i in this.onBrokenCallbacks) { |
| 914 | try { |
| 915 | this.onBrokenCallbacks[i](error); |
| 916 | } catch (err) { |
| 917 | // Treat as unhandled rejection. |
| 918 | Promise.resolve(err); |
| 919 | } |
| 920 | } |
| 921 | for (let i in this.imports) { |