Skip to content

Commit

Permalink
scuffed eventapi splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatoleAM committed Aug 15, 2023
1 parent eaf8505 commit cc784f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/worker/worker.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { WorkerPort } from "./worker.port";
export class WorkerDriver extends EventTarget {
http: WorkerHttp;
eventAPI: EventAPI;
eventAPITest?: EventAPI;
db: Dexie7;
log: Logger;

Expand Down Expand Up @@ -37,6 +38,9 @@ export class WorkerDriver extends EventTarget {
this.http = new WorkerHttp(this);
this.eventAPI = new EventAPI(this);

// stress-test
this.eventAPITest = new EventAPI(this, import.meta.env.VITE_APP_API_EVENTS_TEST, true);

db.ready().then(async () => {
// Fetch global emotes & cosmetics
const sets = [] as SevenTV.EmoteSet[];
Expand Down
9 changes: 8 additions & 1 deletion src/worker/worker.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ export class EventAPI {
return "TWITCH";
}

constructor(private driver: WorkerDriver) {}
constructor(
private driver: WorkerDriver,
overrideURL = "",
private test = false,
) {
if (overrideURL) this.url = overrideURL;
}

private getContext(): EventContext {
return {
Expand Down Expand Up @@ -72,6 +78,7 @@ export class EventAPI {
this.onHello(msg as EventAPIMessage<"HELLO">);
break;
case "DISPATCH":
if (this.test) return;
this.onDispatch(msg as EventAPIMessage<"DISPATCH">);
break;
case "ERROR":
Expand Down
17 changes: 13 additions & 4 deletions src/worker/worker.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class WorkerHttp {
if (!ev.port) return;

this.driver.eventAPI.unsubscribeChannel(ev.detail.id, ev.port);
this.driver.eventAPITest?.unsubscribeChannel(ev.detail.id, ev.port);
});
driver.addEventListener("identity_updated", async (ev) => {
const user = await this.API()
Expand Down Expand Up @@ -156,7 +157,9 @@ export class WorkerHttp {

const user = await userPromise;
if (user) {
this.driver.eventAPI.subscribe("user.*", { object_id: user.id }, port, channel.id);
[this.driver.eventAPI, this.driver.eventAPITest].forEach(
(inst) => inst?.subscribe("user.*", { object_id: user.id }, port, channel.id),
);
}

// begin subscriptions to personal events in the channel
Expand All @@ -166,9 +169,15 @@ export class WorkerHttp {
id: channel.id,
};

this.driver.eventAPI.subscribe("entitlement.*", cond, port, channel.id);
this.driver.eventAPI.subscribe("cosmetic.*", cond, port, channel.id);
this.driver.eventAPI.subscribe("emote_set.*", cond, port, channel.id);
[this.driver.eventAPI, this.driver.eventAPITest].forEach(
(inst) => inst?.subscribe("entitlement.*", cond, port, channel.id),
);
[this.driver.eventAPI, this.driver.eventAPITest].forEach(
(inst) => inst?.subscribe("cosmetic.*", cond, port, channel.id),
);
[this.driver.eventAPI, this.driver.eventAPITest].forEach(
(inst) => inst?.subscribe("emote_set.*", cond, port, channel.id),
);

// Send an initial presence so that the current user immediately has their cosmetics
// (sent with "self" property, so that the presence and entitlements are not published)
Expand Down

0 comments on commit cc784f0

Please sign in to comment.