Skip to content
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

Merged
merged 12 commits into from
Jul 13, 2020

Conversation

chooban
Copy link
Contributor

@chooban chooban commented Jun 29, 2020

Description

This migrates three files to TypeScript:

  • gatsby/src/query/queue.js
  • gatsby/src/query/better-queue-custom-store.js (and its test)

Related Issues

Related to #21995

chooban added 3 commits June 29, 2020 17:21
This migrates the custom store and the queue to typescript.
@chooban chooban requested a review from a team as a code owner June 29, 2020 17:15
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 29, 2020
@chooban chooban changed the title Ts migrate/better queue store chore: Migrate custom store and queue to TypeScript Jun 29, 2020
takeFirstN: function (n, cb) {
const lockId = uuid++
takeFirstN: function (n, cb): void {
const lockId = `` + uuid++
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ So this has changed. The interface from better-queue states that the lockId should be a string.

Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

@pvdz pvdz added status: needs core review Currently awaiting review from Core team member and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jun 30, 2020
Copy link
Contributor

@pvdz pvdz left a 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! :)

packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
oldPriorityTasks.splice(oldPriorityTasks.indexOf(taskId), 1)

if (
addTaskWithPriority(taskId, priority) ||
oldPriority.length === 0
oldPriorityTasks.length === 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this a bug?

Copy link
Contributor Author

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++
Copy link
Contributor

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.

packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
packages/gatsby/src/query/better-queue-custom-store.ts Outdated Show resolved Hide resolved
@chooban
Copy link
Contributor Author

chooban commented Jun 30, 2020

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 handler value as the first argument, and that's then assigned to the process field on options. Instead, we put it as a the process option ourself and pass just that config object.

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.
@chooban
Copy link
Contributor Author

chooban commented Jul 1, 2020

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 pify first. It should be straightforward enough to refactor that as well, possibly removing pify, if required.

@chooban chooban requested a review from pvdz July 13, 2020 11:13
@chooban
Copy link
Contributor Author

chooban commented Jul 13, 2020

Merged up to date with master, and included the recent changes to the queue file.

pvdz
pvdz previously approved these changes Jul 13, 2020
Copy link
Contributor

@pvdz pvdz left a 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? :)

Comment on lines 81 to 82
let lockId
let tasks
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@pvdz pvdz merged commit 79348f9 into gatsbyjs:master Jul 13, 2020
@pvdz
Copy link
Contributor

pvdz commented Jul 13, 2020

Awesome! :D Thanks 💜

@chooban chooban deleted the ts-migrate/better-queue-store branch July 13, 2020 14:10
pragmaticpat pushed a commit to pragmaticpat/gatsby that referenced this pull request Apr 28, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs core review Currently awaiting review from Core team member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants