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

feat: use a single web worker #1035

Merged
merged 3 commits into from
Jan 9, 2025
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
2 changes: 1 addition & 1 deletion src/frontend/src/lib/services/worker.auth.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { authRemainingTimeStore, type AuthStoreData } from '$lib/stores/auth.sto
import type { PostMessage, PostMessageDataResponseAuth } from '$lib/types/post-message';

export const initAuthWorker = async () => {
const AuthWorker = await import('$lib/workers/auth.worker?worker');
const AuthWorker = await import('$lib/workers/workers?worker');
const authWorker: Worker = new AuthWorker.default();

authWorker.onmessage = async ({
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/lib/services/worker.cycles.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface CyclesWorker {
}

export const initCyclesWorker = async (): Promise<CyclesWorker> => {
const CyclesWorker = await import('$lib/workers/cycles.worker?worker');
const CyclesWorker = await import('$lib/workers/workers?worker');
const cyclesWorker: Worker = new CyclesWorker.default();

let cyclesCallback: CyclesCallback | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/lib/services/worker.hosting.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PostMessage, PostMessageDataResponse } from '$lib/types/post-messa
export type HostingCallback = (data: PostMessageDataResponse) => void;

export const initHostingWorker = async () => {
const HostingWorker = await import('$lib/workers/hosting.worker?worker');
const HostingWorker = await import('$lib/workers/workers?worker');
const hostingWorker: Worker = new HostingWorker.default();

let hostingCallback: HostingCallback | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface MonitoringWorker {
}

export const initStatusesWorker = async (): Promise<MonitoringWorker> => {
const MonitoringWorker = await import('$lib/workers/monitoring.worker?worker');
const MonitoringWorker = await import('$lib/workers/workers?worker');
const monitoringWorker: Worker = new MonitoringWorker.default();

let monitoringCallback: MonitoringCallback | undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/lib/services/worker.wallet.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface WalletWorker {
}

export const initWalletWorker = async (): Promise<WalletWorker> => {
const WalletWorker = await import('$lib/workers/wallet.worker?worker');
const WalletWorker = await import('$lib/workers/workers?worker');
const worker: Worker = new WalletWorker.default();

let walletCallback: WalletCallback | undefined;
Expand Down
5 changes: 4 additions & 1 deletion src/frontend/src/lib/workers/auth.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { createAuthClient } from '$lib/utils/auth.utils';
import { IdbStorage, KEY_STORAGE_DELEGATION, type AuthClient } from '@dfinity/auth-client';
import { DelegationChain, isDelegationValid } from '@dfinity/identity';

onmessage = ({ data }: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
export const onAuthMessage = async ({
data
// eslint-disable-next-line require-await
}: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
const { msg } = data;

switch (msg) {
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/lib/workers/cycles.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import type { Identity } from '@dfinity/agent';
import { isNullish, nonNullish } from '@dfinity/utils';
import { set } from 'idb-keyval';

onmessage = async ({ data: dataMsg }: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
export const onCyclesMessage = async ({
data: dataMsg
}: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
const { msg, data } = dataMsg;

switch (msg) {
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/lib/workers/hosting.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import type { CustomDomainRegistrationState } from '$lib/types/custom-domain';
import type { PostMessage, PostMessageDataRequest } from '$lib/types/post-message';
import { fromNullable, nonNullish } from '@dfinity/utils';

onmessage = async ({ data: dataMsg }: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
export const onHostingMessage = async ({
data: dataMsg
}: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
const { msg, data } = dataMsg;

switch (msg) {
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/lib/workers/monitoring.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import { fromNullable, isNullish, nonNullish } from '@dfinity/utils';
import { addDays, endOfDay, format, startOfDay } from 'date-fns';
import { get, set } from 'idb-keyval';

onmessage = async ({ data: dataMsg }: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
export const onMonitoringMessage = async ({
data: dataMsg
}: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
const { msg, data } = dataMsg;

switch (msg) {
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/lib/workers/wallet.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import type {
import { Principal } from '@dfinity/principal';
import { isNullish, jsonReplacer } from '@dfinity/utils';

onmessage = async ({ data: dataMsg }: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
export const onWalletMessage = async ({
data: dataMsg
}: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
const { msg, data } = dataMsg;

switch (msg) {
Expand Down
16 changes: 16 additions & 0 deletions src/frontend/src/lib/workers/workers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { PostMessage, PostMessageDataRequest } from '$lib/types/post-message';
import { onAuthMessage } from '$lib/workers/auth.worker';
import { onCyclesMessage } from '$lib/workers/cycles.worker';
import { onHostingMessage } from '$lib/workers/hosting.worker';
import { onMonitoringMessage } from '$lib/workers/monitoring.worker';
import { onWalletMessage } from '$lib/workers/wallet.worker';

onmessage = async (msg: MessageEvent<PostMessage<PostMessageDataRequest>>) => {
await Promise.allSettled([
onAuthMessage(msg),
onCyclesMessage(msg),
onHostingMessage(msg),
onMonitoringMessage(msg),
onWalletMessage(msg)
]);
};
Loading