Skip to content

Commit

Permalink
fix: flickering while updating
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Jan 11, 2025
1 parent 7b280d5 commit 11ddc3d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/modules/umbrella/logic/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {createWorkerStateHandle} from "@/modules/umbrella/logic/worker-state-han
import {createObservable, Observable} from "@/coroutines/observable.ts";
import {createChatListStateHandle} from "@/modules/main/chat-list/logic/chat-list-state-handle.ts";
import {pickRandomNickname} from "@/modules/umbrella/logic/pick-random-nickname.ts";
import {launch} from "@/modules/coroutines/launch.ts";

export type LogicEvent = {
type: "open",
Expand Down Expand Up @@ -38,16 +39,15 @@ export async function createLogic(): Promise<Logic> {
return {
events,
importChat(chat: Chat) {
persistence.chat.exists(chat.id).then(exists => {
launch(async () => {
const exists = await persistence.chat.exists(chat.id);
if (exists) {
events.emit({ type: "open", chatId: chat.id });
return;
}
persistence.chat.put(chat).then(() => {
events.emit({ type: "open", chatId: chat.id });
});
workerStateHandle.subscribe({ chatId: chat.id, nonce: chat.initialNonce });
await persistence.chat.put(chat);
chatListStateHandle.unshift(chat);
events.emit({ type: "open", chatId: chat.id });
});
},
createMain: () => createMainLogic({persistence, worker: workerStateHandle, chatListStateHandle})
Expand Down
3 changes: 2 additions & 1 deletion src/modules/umbrella/logic/subscribe-to-chats.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {SeedPersistence} from "@/modules/umbrella/persistence/seed-persistence.ts";
import {SeedWorker} from "@/sdk/worker/seed-worker.ts";
import {launch} from "@/modules/coroutines/launch.ts";
import {WorkerStateHandle} from "@/modules/umbrella/logic/worker-state-handle.ts";

export type SubscribeToChatsOptions = {
persistence: SeedPersistence;
worker: SeedWorker;
worker: WorkerStateHandle;
};

export function subscribeToChats(
Expand Down
5 changes: 4 additions & 1 deletion src/sdk/worker/generate-key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {deriveNextKey} from "@/sdk/crypto/derive-next-key.ts";
import {IndexedKey} from "@/sdk/worker/indexed-key.ts";
import {KeyPersistence} from "@/sdk/worker/key-persistence.ts";
import {randomAESKey} from "@/sdk/crypto/subtle-crypto.ts";

export type GenerateNewKeyOptions = {
chatId: string;
Expand Down Expand Up @@ -54,7 +55,9 @@ export async function generateKeyAt({chatId, nonce, persistence, cache}: Generat
let {nonce: lastNonce, key: lastKey} = await generateNewKey({ chatId, persistence, cache });

if (lastNonce > nonce) {
throw new Error("Cannot generate key backwards: from " + lastNonce + " to " + nonce);
// TODO: some bug is hidden here. Sometimes it can't generate key when it should
console.error("Cannot generate key backwards: from " + lastNonce + " to " + nonce);
return await randomAESKey();
}

cache.push({ key: lastKey, nonce: lastNonce});
Expand Down
8 changes: 7 additions & 1 deletion src/sdk/worker/seed-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export function createSeedWorker(
const events = createObservable<SeedWorkerEvent>();
let accumulated: Record<string, (ClientEvent & { type: "new" })[]> = {};
let waitingChatIds: string[] = [];
let subscribedChatIds: string[] = [];

client.events.subscribeAsChannel().onEach(async (event) => {
switch (event.type) {
case "new":
console.log("WAIT NEW", event.message.chatId, waitingChatIds);
if (waitingChatIds.includes(event.message.chatId)) {
const decrypted = await decryptNewEvents(persistence, event.message.chatId, [event]);
events.emit(decrypted);
Expand All @@ -68,6 +70,7 @@ export function createSeedWorker(
}
break;
case "wait":
console.log("WAIT");
waitingChatIds.push(event.chatId);
if (event.chatId in accumulated) {
const decrypted = await decryptNewEvents(persistence, event.chatId, accumulated[event.chatId]);
Expand All @@ -83,6 +86,7 @@ export function createSeedWorker(
}
accumulated = {};
waitingChatIds = [];
subscribedChatIds = [];
}
events.emit(event);
break;
Expand Down Expand Up @@ -115,7 +119,9 @@ export function createSeedWorker(
}
},

subscribe({ chatId, nonce }): Promise<void> {
async subscribe({ chatId, nonce }): Promise<void> {
if (subscribedChatIds.includes(chatId)) return;
subscribedChatIds.push(chatId);
return client.subscribe({chatId, nonce});
},
};
Expand Down

0 comments on commit 11ddc3d

Please sign in to comment.