Skip to content

Commit

Permalink
fix: debug on prod
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Feb 10, 2025
1 parent 0068d34 commit 3269388
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/modules/main/chat-list/persistence/chat-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IDBPDatabase} from "idb";
import {Chat} from "@/modules/main/chat-list/persistence/chat.ts";
import { IDBPDatabase } from "idb";
import { Chat } from "@/modules/main/chat-list/persistence/chat.ts";

export interface ChatStorage {
put(chat: Chat): Promise<void>;
Expand All @@ -11,7 +11,7 @@ export interface ChatStorage {
delete(id: string): Promise<void>;
}

export function createChatObjectStore(db: IDBPDatabase){
export function createChatObjectStore(db: IDBPDatabase) {
db.createObjectStore("chat", {
keyPath: "id",
}).createIndex("lastMessageDate", "lastMessageDate");
Expand All @@ -24,7 +24,7 @@ export function createChatStorage(db: IDBPDatabase): ChatStorage {
},

async get(id: string): Promise<Chat> {
return await db.transaction("chat").store.get(IDBKeyRange.only(id));
return await db.transaction("chat").store.get(IDBKeyRange.only(id)) as Chat;
},

async rename(id: string, title: string): Promise<void> {
Expand Down
1 change: 0 additions & 1 deletion src/modules/main/chat/persistence/message-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function createMessageObjectStore(db: IDBPDatabase) {
export function createMessageStorage(db: IDBPDatabase): MessageStorage {
return {
async lastMessage({ url, queueId }): Promise<ChatMessage | undefined> {
console.log("TEST", url, queueId);
const cursor = await db.transaction("message-v2")
.store.index("queueId")
.openCursor(IDBKeyRange.only([url, queueId]), "prev");
Expand Down
4 changes: 2 additions & 2 deletions src/modules/umbrella/logic/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ function createSeedClient() {
},
});
client.setForeground(true);
window.onfocus = function() {
window.onoffline = function() {
console.log(">> foreground(true)");
client.setForeground(true);
};
window.onblur = function() {
window.ononline = function() {
console.log("<< foreground(false)");
client.setForeground(false);
};
Expand Down
44 changes: 27 additions & 17 deletions src/modules/umbrella/persistence/seed-persistence.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import {
createMessageObjectStore,
createMessageStorage,
Expand Down Expand Up @@ -55,19 +57,23 @@ export async function createPersistence(): Promise<SeedPersistence> {
if (version <= 7) {
const chatStore = transaction.objectStore("chat");

const cursor = await chatStore.openCursor() ?? [];
for await (const { value: chat } of cursor) {
(chat as Chat).unreadCount = 0;
await chatStore.put(chat);
const cursor = await chatStore.openCursor();
if (cursor) {
for await (const { value: chat } of cursor) {
(chat as Chat).unreadCount = 0;
await chatStore.put(chat);
}
}
}
if (version <= 8) {
const chatStore = transaction.objectStore("chat");

const cursor = await chatStore.openCursor() ?? [];
for await (const { value: chat } of cursor) {
(chat as Chat).serverUrl = "https://meetacy.app/seed-go";
await chatStore.put(chat);
const cursor = await chatStore.openCursor();
if (cursor) {
for await (const { value: chat } of cursor) {
(chat as Chat).serverUrl = "https://meetacy.app/seed-go";
await chatStore.put(chat);
}
}
}
if (version <= 9) {
Expand All @@ -76,22 +82,26 @@ export async function createPersistence(): Promise<SeedPersistence> {
const messageStore = transaction.objectStore("message");
const messageV2Store = transaction.objectStore("message-v2");

const cursor = await messageStore.openCursor() ?? [];
for await (const { value } of cursor) {
// eslint-disable-next-line
value.queueId = value.chatId;
await messageV2Store.put(value);
const cursor = await messageStore.openCursor();
if (cursor) {
for await (const { value: message } of cursor) {
message.queueId = message.chatId;
message.url = "wss://meetacy.app/seed-go";
await messageV2Store.put(message);
}
}

db.deleteObjectStore("message");
}
if (version <= 10) {
const chatStore = transaction.objectStore("chat");

const cursor = await chatStore.openCursor() ?? [];
for await (const { value: chat } of cursor) {
(chat as Chat).serverUrl = "wss://meetacy.app/seed-go";
await chatStore.put(chat);
const cursor = await chatStore.openCursor();
if (cursor) {
for await (const { value: chat } of cursor) {
(chat as Chat).serverUrl = "wss://meetacy.app/seed-go";
await chatStore.put(chat);
}
}
}
},
Expand Down
44 changes: 32 additions & 12 deletions src/sdk-v2/seed-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,27 @@ export function createSeedClient(
const events: Observable<SeedClientEvent> = createObservable();

const engine = createSeedEngine(engineOptions.mainUrl);
const subscribeQueueIds: Set<string> = new Set();
const waitingQueues: Map<string, Set<string>> = new Map();
const subscribeQueues: Map<string, Set<string>> = new Map();

setInterval(() => {
void engine.executeOrThrow(
engineOptions.mainUrl,
{
"type": "ping",
},
);
}, 15_000);
function setSubscribeQueue(
url: string,
queueId: string,
subscribed: boolean,
) {
const urlQueues = subscribeQueues.get(url) ?? new Set();
subscribeQueues.set(url, urlQueues);
if (subscribed) {
urlQueues.add(queueId);
} else {
urlQueues.delete(queueId);
}
}
function getSubscribeQueue(url: string, queueId: string) {
const urlQueues = subscribeQueues.get(url);
return urlQueues !== undefined && urlQueues.has(queueId);
}

const waitingQueues: Map<string, Set<string>> = new Map();

function setWaitingQueue(url: string, queueId: string, waiting: boolean) {
const urlQueues = waitingQueues.get(url) ?? new Set();
Expand All @@ -130,6 +140,16 @@ export function createSeedClient(
return urlQueues !== undefined && urlQueues.has(queueId);
}

setInterval(() => {
ensureServer(engineOptions.mainUrl);
void engine.executeOrThrow(
engineOptions.mainUrl,
{
"type": "ping",
},
);
}, 15_000);

engine.events.subscribe(event => {
switch (event.type) {
case "ready":
Expand Down Expand Up @@ -188,11 +208,11 @@ export function createSeedClient(
}

function subscribe(url: string, { queueId, nonce }: SeedClientSubscribeOptions) {
if (subscribeQueueIds.has(url)) {
if (getSubscribeQueue(url, queueId)) {
throw new Error(`Already subscribed to this chat id ${queueId}`);
}
ensureServer(url);
subscribeQueueIds.add(url);
setSubscribeQueue(url, queueId, true);
const request: SubscribeRequest = {
type: "subscribe",
queueId, nonce,
Expand Down
4 changes: 2 additions & 2 deletions src/sdk-v2/seed-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ export function createSeedEngine(mainUrl: string): SeedEngine {
) {
console.log(`>> execute\nServer: ${url}\nRequest:`, payload);
return new Promise((resolve, reject) => {
if (!ready) throw new SeedEngineDisconnected();
if (!ready) reject(new SeedEngineDisconnected());
if (checkConnection && !connectedUrls.has(url)) {
throw new SeedEngineDisconnected();
reject(new SeedEngineDisconnected());
}

requests.push({
Expand Down

0 comments on commit 3269388

Please sign in to comment.