Skip to content

Commit

Permalink
Handle reset as deleted instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Excellify committed Nov 11, 2024
1 parent 947407f commit d2907a6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 34 deletions.
15 changes: 0 additions & 15 deletions src/composable/useCosmetics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,6 @@ db.ready().then(async () => {
target.addEventListener("entitlement_deleted", (ev) => {
setEntitlement(ev.detail, "-");
});
target.addEventListener("entitlement_reset", async (ev) => {
const userID = ev.detail.id;
await until(firstFlush).toBe(true);

const entries = db.entitlements
.where("user_id")
.equals(userID)
.and((e) => e.kind !== "EMOTE_SET");

await entries.each((ent) => {
setEntitlement(ent, "-");
});

entries.delete();
});

// Assign stored entitlements
db.entitlements
Expand Down
8 changes: 0 additions & 8 deletions src/composable/useWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ function useHandlers(mp: MessagePort) {
events.emit("entitlement_deleted", entitlement);
break;
}
case "ENTITLEMENT_RESET": {
const entitlement = data as TypedWorkerMessage<"ENTITLEMENT_RESET">;

events.emit("entitlement_reset", entitlement);
break;
}
case "STATIC_COSMETICS_FETCHED": {
const { badges, paints } = data as TypedWorkerMessage<"STATIC_COSMETICS_FETCHED">;

Expand Down Expand Up @@ -255,7 +249,6 @@ export type WorkletEventName =
| "cosmetic_created"
| "entitlement_created"
| "entitlement_deleted"
| "entitlement_reset"
| "static_cosmetics_fetched"
| "twitch_emote_set_data"
| "emote_set_updated"
Expand All @@ -270,7 +263,6 @@ type WorkletTypedEvent<EVN extends WorkletEventName> = {
cosmetic_created: Pick<SevenTV.Cosmetic, "id" | "data" | "kind">;
entitlement_created: Pick<SevenTV.Entitlement, "id" | "kind" | "ref_id" | "user_id" | "platform_id">;
entitlement_deleted: Pick<SevenTV.Entitlement, "id" | "kind" | "ref_id" | "user_id" | "platform_id">;
entitlement_reset: Pick<SevenTV.Entitlement, "id">;
static_cosmetics_fetched: Pick<TypedWorkerMessage<"STATIC_COSMETICS_FETCHED">, "badges" | "paints">;
twitch_emote_set_data: SevenTV.EmoteSet;
emote_set_updated: TypedWorkerMessage<"EMOTE_SET_UPDATED">;
Expand Down
15 changes: 6 additions & 9 deletions src/worker/event-handlers/entitlement.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ export async function onEntitlementDelete(ctx: EventContext, cm: ChangeMap<Seven
}

export async function onEntitlementReset(ctx: EventContext, obj: Pick<SevenTV.User, "id">) {
const removals = ctx.db.entitlements.filter((e) => e.user_id === obj.id);
const arr = await removals.toArray();
for (const port of ctx.driver.ports.values()) {
const platform = port.platform;
if (!platform) return; // no platform set
if (!port.platform) return; // no platform set

const o: typeof obj = structuredClone(obj);
if (!o.id) return;

port.postMessage("ENTITLEMENT_RESET", {
id: o.id,
arr.forEach((r) => {
port.postMessage("ENTITLEMENT_DELETED", r);
});

ctx.db.entitlements.filter((e) => e.user_id === o.id).delete();
}
await removals.delete();
}
2 changes: 0 additions & 2 deletions src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export enum workerMessageType {
COSMETIC_CREATED,
ENTITLEMENT_CREATED,
ENTITLEMENT_DELETED,
ENTITLEMENT_RESET,
INIT,
LOG,
STATE,
Expand Down Expand Up @@ -65,7 +64,6 @@ export type TypedWorkerMessage<T extends WorkerMessageType> = {
COSMETIC_CREATED: SevenTV.Cosmetic<"BADGE" | "PAINT" | "AVATAR">;
ENTITLEMENT_CREATED: Pick<SevenTV.Entitlement, "id" | "kind" | "ref_id" | "user_id" | "platform_id">;
ENTITLEMENT_DELETED: Pick<SevenTV.Entitlement, "id" | "kind" | "ref_id" | "user_id" | "platform_id">;
ENTITLEMENT_RESET: Pick<SevenTV.User, "id">;
INIT: object;
LOG: {
type: LogType;
Expand Down

0 comments on commit d2907a6

Please sign in to comment.