mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-30 01:43:36 +08:00
This CL adds the eslint-plugin-jsdoc plugin and enables just one rule, `jsdoc/check-alignment`, which ensures that all the lines in a comment are correctly aligned. For example: ``` /** * foo * bar */ ``` Would fail the check and be corrected to: ``` /** * foo * bar */ ``` DISABLE_THIRD_PARTY_CHECK=deliberate install + use of eslint-plugin-jsdoc node_module Bug: none Change-Id: I969983cd550653654a334ede3409b8c850467245 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4110906 Commit-Queue: Andres Olivares <andoli@chromium.org> Reviewed-by: Andres Olivares <andoli@chromium.org> Auto-Submit: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Jack Franklin <jacktfranklin@chromium.org>
91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
// Copyright 2021 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
interface PromiseInfo<T> {
|
|
promise: Promise<T>;
|
|
resolve: (obj: T) => void;
|
|
reject: (error: Error) => void;
|
|
}
|
|
|
|
/**
|
|
* A class that facilitates resolving a id to an object of type T. If the id does not yet resolve, a promise
|
|
* is created that gets resolved once `onResolve` is called with the corresponding id.
|
|
*
|
|
* This class enables clients to control the duration of the wait and the lifetime of the associated
|
|
* promises by using the `clear` method on this class.
|
|
*/
|
|
export abstract class ResolverBase<Id, T> {
|
|
#unresolvedIds: Map<Id, PromiseInfo<T>> = new Map();
|
|
|
|
protected abstract getForId(id: Id): T|null;
|
|
protected abstract startListening(): void;
|
|
protected abstract stopListening(): void;
|
|
|
|
/**
|
|
* Returns a promise that resolves once the `id` can be resolved to an object.
|
|
*/
|
|
async waitFor(id: Id): Promise<T> {
|
|
const obj = this.getForId(id);
|
|
if (!obj) {
|
|
return this.getOrCreatePromise(id);
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Resolve the `id`. Returns the object immediatelly if it can be resolved,
|
|
* and otherwise waits for the object to appear and calls `callback` once
|
|
* it is resolved.
|
|
*/
|
|
tryGet(id: Id, callback: (t: T) => void): T|null {
|
|
const obj = this.getForId(id);
|
|
if (!obj) {
|
|
const swallowTheError = (): void => {};
|
|
void this.getOrCreatePromise(id).catch(swallowTheError).then(obj => {
|
|
if (obj) {
|
|
callback(obj);
|
|
}
|
|
});
|
|
return null;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
/**
|
|
* Aborts all waiting and rejects all unresolved promises.
|
|
*/
|
|
clear(): void {
|
|
this.stopListening();
|
|
for (const [id, {reject}] of this.#unresolvedIds.entries()) {
|
|
reject(new Error(`Object with ${id} never resolved.`));
|
|
}
|
|
this.#unresolvedIds.clear();
|
|
}
|
|
|
|
private getOrCreatePromise(id: Id): Promise<T> {
|
|
const promiseInfo = this.#unresolvedIds.get(id);
|
|
if (promiseInfo) {
|
|
return promiseInfo.promise;
|
|
}
|
|
let resolve: (value: T) => void = () => {};
|
|
let reject: (error: Error) => void = () => {};
|
|
const promise = new Promise<T>((res, rej) => {
|
|
resolve = res;
|
|
reject = rej;
|
|
});
|
|
this.#unresolvedIds.set(id, {promise, resolve, reject});
|
|
this.startListening();
|
|
return promise;
|
|
}
|
|
|
|
protected onResolve(id: Id, t: T): void {
|
|
const promiseInfo = this.#unresolvedIds.get(id);
|
|
this.#unresolvedIds.delete(id);
|
|
if (this.#unresolvedIds.size === 0) {
|
|
this.stopListening();
|
|
}
|
|
promiseInfo?.resolve(t);
|
|
}
|
|
}
|