-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Migrate custom store and queue to TypeScript #25389
Conversation
This migrates the custom store and the queue to typescript.
takeFirstN: function (n, cb) { | ||
const lockId = uuid++ | ||
takeFirstN: function (n, cb): void { | ||
const lockId = `` + uuid++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better-queue
states that the lockId
should be a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think I would prefer String(uuid++)
for clarity.
import { ProgressActivityTracker } from "../.." | ||
|
||
export type Task = any | ||
type TaskResult = any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't locate a better type, but at least we can just change these lines if one exists.
if (jobs.length === 0) { | ||
return Promise.resolve() | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
let taskFinishCallback | ||
|
||
const gc = (): void => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gc
depends on taskFailedCallback
and taskFailedCallback
depends on gc
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Next time, could you please use Array<>
rather than []
for array typing? This isn't formalized yet but we are likely to move into that direction soon (consistency with Map
, Set
, Promise
, etc).
I'm a little surprised TS is not asking you to type the cb
functions, but if it doesn't I guess that's okay.
Most of my comments are nits. Can you at least apply the String(uuid++)
cases? And maybe look at the others. This looks good otherwise, thanks! :)
oldPriorityTasks.splice(oldPriorityTasks.indexOf(taskId), 1) | ||
|
||
if ( | ||
addTaskWithPriority(taskId, priority) || | ||
oldPriority.length === 0 | ||
oldPriorityTasks.length === 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this a bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe so. The oldPriority
value previously used was a number (or null) so it has no length
field and TS complained. It seems logical to me that the intention was to check the lenght of the old tasks instead. Annoyingly, no test failures.
I think what it's saying is if calling code attempts to add the same task twice at different priorities, and removing the older task results in an empty set of tasks for the original priority, then the priority keys need updating. I can try and write a test to cover that.
takeFirstN: function (n, cb) { | ||
const lockId = uuid++ | ||
takeFirstN: function (n, cb): void { | ||
const lockId = `` + uuid++ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think I would prefer String(uuid++)
for clarity.
This is an update of two halves (well, commits). The first deals with the nitpicks and style updates, the second those untyped callbacks. By changing the invocation of Queue to use a full Options object, we can get the types from that and the callbacks all check out. I was a little wary, especially given it's not explicitly tested, but I checked the source for better-queue and I think it's fine. We were initially passing a |
Rather than leaking the extra function to the outside world, we'll keep it internal. The tests do not cause an issue as they're written in a way that erases types.
Having slept on it, I pushed up a very small change. It avoids leaking the additional method that's been added to the Store interface further into the codebase. At the moment, the tests are not typesafe as the Store object is run through |
Merged up to date with master, and included the recent changes to the queue file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Do you want to update the last comment I made or leave it? :)
let lockId | ||
let tasks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: you're folding up the declaration with the initialization above (correctly, I think), why not here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just missed it, I'm afraid. Pushing up a commit for it now.
Awesome! :D Thanks 💜 |
* WIP * chore: Migrate custom store to TypeScript This migrates the custom store and the queue to typescript. * Test types * Remove warning * chore: Pass full config object * Do not leak interface extension details Rather than leaking the extra function to the outside world, we'll keep it internal. The tests do not cause an issue as they're written in a way that erases types. * Bring in queue changes * Initialize and declare initial values
Description
This migrates three files to TypeScript:
Related Issues
Related to #21995