Skip to content

Commit

Permalink
fix: task is constantly retrying
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloReszczynski committed Jun 28, 2024
1 parent b5f956d commit d2a35ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 31 deletions.
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
29 changes: 10 additions & 19 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 @@ -77,7 +77,7 @@ export class TopsortBanner extends LitElement {

private task = new Task(this, {
task: this.runAuction,
args: () => [this.buildAuction()],
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;

0 comments on commit d2a35ed

Please sign in to comment.