Skip to content

feat: data loader#2237

Draft
andogq wants to merge 2 commits intocanonical:mainfrom
andogq:feat/data-loader
Draft

feat: data loader#2237
andogq wants to merge 2 commits intocanonical:mainfrom
andogq:feat/data-loader

Conversation

@andogq
Copy link
Member

@andogq andogq commented Feb 26, 2026

Done

  • Create Source<T> definition (WD-33878)
  • Implement createSource<T> to handle basic source house keeping (WD-33879)

@webteam-app
Copy link

@github-actions
Copy link

github-actions bot commented Feb 26, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 97.44% (🎯 95%) 17083 / 17531
🔵 Statements 97.44% (🎯 95%) 17083 / 17531
🔵 Functions 98.16% (🎯 95%) 695 / 708
🔵 Branches 92% (🎯 90%) 3384 / 3678
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/data/source.ts 0% 0% 0% 0% 1-70
src/data/sourceBase.ts 98.19% 95.23% 100% 98.19% 68-70
Generated in workflow #2600 for commit d9de123 by the Vitest Coverage Report Action

Comment on lines +67 to +70
} catch (error) {
// Ignore any errors thrown by the callback function.
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this should use the logger to inform of unhandled errors in dev, otherwise this will just swallow the error.

}

// This load is the latest one to complete successfully.
if (loadId >= latestSuccessfulLoad) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would we ever care about the response if it's not the very latest call? If not could it abort the other calls that are in progress?

Or another way to look at this might be: do we ever want to allow another call to be initiated if there's already a fetch in progress?

Copy link
Member Author

Choose a reason for hiding this comment

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

We mightn't care about the data that's returned, which is why this check is here. This lets us temporarily use data from an old request whilst waiting for the result of a newer request.

The initiator of the load can also pass an AbortSignal to the load function, so they're capable of cancelling their own requests if something superseding it comes in. I don't think it makes much sense for the load to automatically be cancelled if a new one starts, as the thing that triggers a new load has full control over the previous load too.

See https://github.com/canonical/juju-dashboard/pull/2237/changes#diff-c68dd5398fe4e6af6d1b13b0273295f7979c5d6ffc4fbb1d17cc3af184f5ba92R103 for an example.

function handleError(error: unknown): NonNullable<Source<unknown>["error"]> {
// Extract the message of the error.
let message = "An unknown error occurred";
if (error instanceof Error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

})
.finally(() => {
// Track the latest load to complete, favouring the latest one.
latestCompletedLoad = Math.max(loadId, latestCompletedLoad);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this needs to check if it was aborted as this will always run so an abort would cause this to update the id (if I've read this correctly).

Copy link
Member Author

Choose a reason for hiding this comment

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

You've read it correctly, however we do want to track whenever a load finishes, whether it finishes successfully or not. That's why latestCompletedLoad is tracked in the finally, whilst the latestSuccessfulLoad is only tracked in the then branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants