Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Migrate TaskQueue to TypeScript (#5658)
This is a simple module but took a bit of work because: * It wraps a Promise that can return basically anything. In a pure TS codebase we'd solve these with generics, so you could do `new TaskQueue<T>` where `T` will be what's returned from the queue, but because we're calling that from JS we can't yet. I've left a TODO and once we migrate the call sites to TS we can do a much better job than the `void | any` type I've gone with for now. * It was used in typedefs via `Puppeteer.TaskQueue`. I've removed that entry from `externs.d.ts` in favour of importing it and using the type directly. This does mean that we have imports that ESLint doesn't realiase are actually used but I think this is better than maintaining `externs.d.ts`.
- Loading branch information
1 parent
ef3befa
commit 1a57ba2
Showing
6 changed files
with
50 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2020 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* TODO(jacktfranklin@): once we are calling this from TS files we can | ||
* avoid the horrible void | any type and instead make use of generics | ||
* to make this into TaskQueue<T> and let the caller tell us what types | ||
* the promise in the queue should return. | ||
*/ | ||
class TaskQueue { | ||
_chain: Promise<void | any>; | ||
|
||
constructor() { | ||
this._chain = Promise.resolve(); | ||
} | ||
|
||
public postTask(task: () => any): Promise<void | any> { | ||
const result = this._chain.then(task); | ||
this._chain = result.catch(() => {}); | ||
return result; | ||
} | ||
} | ||
|
||
export = { TaskQueue }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters