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

fix: task is constantly retrying #43

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
// Setup @topsort/banners.js
window.TS_BANNERS = {
getLoadingElement() {
return document.createElement("nb-skeleton");
const div = document.createElement("div");
div.innerText = "Loading Banner...";
},
getErrorElement(error) {
const div = document.createElement("div");
Expand Down
31 changes: 11 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Task } from "@lit/task";
import { LitElement, type TemplateResult, css, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { TopsortConfigurationError, TopsortRequestError } from "./errors";
import type { Auction, Banner, BannerState } from "./types";
import type { Auction, Banner } from "./types";

/* Set up global environment for TS_BANNERS */

Expand Down Expand Up @@ -76,8 +76,8 @@ export class TopsortBanner extends LitElement {
readonly location?: string;

private task = new Task(this, {
task: this.runAuction,
args: () => [this.buildAuction()],
task: (...args) => this.runAuction(...args),
args: () => [],
});

private getLink(banner: Banner): string {
Expand Down Expand Up @@ -182,10 +182,9 @@ export class TopsortBanner extends LitElement {
return auction;
}

private async runAuction(
[auction]: Auction[],
{ signal }: { signal: AbortSignal },
): Promise<BannerState> {
private async runAuction(_: never[], { signal }: { signal: AbortSignal }): Promise<Banner[]> {
const auction = this.buildAuction();
console.debug("Running auction", auction);
const device = getDeviceType();
const token = window.TS.token;
const url = window.TS.url || "https://api.topsort.com";
Expand Down Expand Up @@ -214,15 +213,7 @@ export class TopsortBanner extends LitElement {
logError(result.error);
throw new Error(result.error);
}
if (result.winners.length) {
return {
status: "ready",
banners: result.winners,
};
}
return {
status: "nowinners",
};
return result.winners;
}

protected render() {
Expand All @@ -231,12 +222,12 @@ export class TopsortBanner extends LitElement {
}
return this.task.render({
pending: () => this.getLoadingElement(),
complete: (value) => {
this.emitEvent(value.status);
if (value.status === "nowinners") {
complete: (banners) => {
this.emitEvent(banners.length ? "ready" : "nowinners");
if (!banners.length) {
return this.getNoWinnersElement();
}
return this.getBannerElement(value.banners[0]);
return this.getBannerElement(banners[0]);
},
error: (error) => this.getErrorElement(error),
});
Expand Down
11 changes: 0 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
export interface NoWinners {
status: "nowinners";
}

export interface Ready {
status: "ready";
banners: Banner[];
}

export interface Auction {
type: "banners";
slots: 1;
Expand All @@ -30,5 +21,3 @@ export interface Banner {
resolvedBidId: string;
asset: [{ url: string }];
}

export type BannerState = NoWinners | Ready;