mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix: simplify EventData typings, drop NotifyData
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
import { Observable } from '../data/observable';
|
||||
|
||||
// Known Limitation
|
||||
@@ -6,75 +5,71 @@ import { Observable } from '../data/observable';
|
||||
// to make assignable our `AbortSignal` into that.
|
||||
// https://github.com/Microsoft/TSJS-lib-generator/pull/623
|
||||
type Events = {
|
||||
abort: any // Event & Type<"abort">
|
||||
}
|
||||
abort: any; // Event & Type<"abort">
|
||||
};
|
||||
type EventAttributes = {
|
||||
onabort: any // Event & Type<"abort">
|
||||
}
|
||||
onabort: any; // Event & Type<"abort">
|
||||
};
|
||||
|
||||
/**
|
||||
* The signal class.
|
||||
* @see https://dom.spec.whatwg.org/#abortsignal
|
||||
*/
|
||||
export default class AbortSignal extends Observable {
|
||||
/**
|
||||
* AbortSignal cannot be constructed directly.
|
||||
*/
|
||||
public constructor() {
|
||||
super()
|
||||
}
|
||||
/**
|
||||
* AbortSignal cannot be constructed directly.
|
||||
*/
|
||||
public constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
|
||||
*/
|
||||
public get aborted(): boolean {
|
||||
const aborted = abortedFlags.get(this)
|
||||
if (typeof aborted !== "boolean") {
|
||||
throw new TypeError(
|
||||
`Expected 'this' to be an 'AbortSignal' object, but got ${
|
||||
this === null ? "null" : typeof this
|
||||
}`,
|
||||
)
|
||||
}
|
||||
return aborted
|
||||
}
|
||||
/**
|
||||
* Returns `true` if this `AbortSignal`'s `AbortController` has signaled to abort, and `false` otherwise.
|
||||
*/
|
||||
public get aborted(): boolean {
|
||||
const aborted = abortedFlags.get(this);
|
||||
if (typeof aborted !== 'boolean') {
|
||||
throw new TypeError(`Expected 'this' to be an 'AbortSignal' object, but got ${this === null ? 'null' : typeof this}`);
|
||||
}
|
||||
return aborted;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an AbortSignal object.
|
||||
*/
|
||||
export function createAbortSignal(): AbortSignal {
|
||||
const signal = new AbortSignal();
|
||||
abortedFlags.set(signal, false)
|
||||
return signal
|
||||
const signal = new AbortSignal();
|
||||
abortedFlags.set(signal, false);
|
||||
return signal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Abort a given signal.
|
||||
*/
|
||||
export function abortSignal(signal: AbortSignal): void {
|
||||
if (abortedFlags.get(signal) !== false) {
|
||||
return
|
||||
}
|
||||
if (abortedFlags.get(signal) !== false) {
|
||||
return;
|
||||
}
|
||||
|
||||
abortedFlags.set(signal, true)
|
||||
signal.notify({ eventName: "abort", type: "abort" })
|
||||
abortedFlags.set(signal, true);
|
||||
signal.notify({ eventName: 'abort', type: 'abort', object: this });
|
||||
}
|
||||
|
||||
/**
|
||||
* Aborted flag for each instances.
|
||||
*/
|
||||
const abortedFlags = new WeakMap<AbortSignal, boolean>()
|
||||
const abortedFlags = new WeakMap<AbortSignal, boolean>();
|
||||
|
||||
// Properties should be enumerable.
|
||||
Object.defineProperties(AbortSignal.prototype, {
|
||||
aborted: { enumerable: true },
|
||||
})
|
||||
aborted: { enumerable: true },
|
||||
});
|
||||
|
||||
// `toString()` should return `"[object AbortSignal]"`
|
||||
if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") {
|
||||
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
|
||||
configurable: true,
|
||||
value: "AbortSignal",
|
||||
})
|
||||
}
|
||||
if (typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol') {
|
||||
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, {
|
||||
configurable: true,
|
||||
value: 'AbortSignal',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user