Instantiate all cahts with community
This commit is contained in:
parent
047efcbb79
commit
61284731ee
|
@ -15,7 +15,11 @@ export class Messenger {
|
||||||
chatsById: Map<string, Chat>;
|
chatsById: Map<string, Chat>;
|
||||||
observers: {
|
observers: {
|
||||||
[chatId: string]: Set<
|
[chatId: string]: Set<
|
||||||
(message: ApplicationMetadataMessage, timestamp: Date) => void
|
(
|
||||||
|
message: ApplicationMetadataMessage,
|
||||||
|
timestamp: Date,
|
||||||
|
chatId: string
|
||||||
|
) => void
|
||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
identity: Identity;
|
identity: Identity;
|
||||||
|
@ -49,6 +53,19 @@ export class Messenger {
|
||||||
await this.joinChat(chat);
|
await this.joinChat(chat);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Joins several of public chats.
|
||||||
|
*
|
||||||
|
* Use `addListener` to get messages received on these chats.
|
||||||
|
*/
|
||||||
|
public async joinChats(chats: Iterable<Chat>): Promise<void> {
|
||||||
|
await Promise.all(
|
||||||
|
Array.from(chats).map((chat) => {
|
||||||
|
return this.joinChat(chat);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joins a public chat.
|
* Joins a public chat.
|
||||||
*
|
*
|
||||||
|
@ -110,18 +127,30 @@ export class Messenger {
|
||||||
* @throws string If the chat has not been joined first using [joinChat].
|
* @throws string If the chat has not been joined first using [joinChat].
|
||||||
*/
|
*/
|
||||||
public addObserver(
|
public addObserver(
|
||||||
observer: (message: ApplicationMetadataMessage, timestamp: Date) => void,
|
observer: (
|
||||||
|
message: ApplicationMetadataMessage,
|
||||||
|
timestamp: Date,
|
||||||
chatId: string
|
chatId: string
|
||||||
|
) => void,
|
||||||
|
chatId: string | string[]
|
||||||
): void {
|
): void {
|
||||||
// Not sure this is the best design here. Maybe `addObserver` and `joinChat` should be merged.
|
let chats = [];
|
||||||
|
|
||||||
if (!this.chatsById.has(chatId))
|
if (typeof chatId === "string") {
|
||||||
throw "Cannot add observer on a chat that is not joined.";
|
chats.push(chatId);
|
||||||
if (!this.observers[chatId]) {
|
} else {
|
||||||
this.observers[chatId] = new Set();
|
chats = [...chatId];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.observers[chatId].add(observer);
|
chats.forEach((id) => {
|
||||||
|
if (!this.chatsById.has(id))
|
||||||
|
throw "Cannot add observer on a chat that is not joined.";
|
||||||
|
if (!this.observers[id]) {
|
||||||
|
this.observers[id] = new Set();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.observers[id].add(observer);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -211,7 +240,7 @@ export class Messenger {
|
||||||
|
|
||||||
if (this.observers[chat.id]) {
|
if (this.observers[chat.id]) {
|
||||||
this.observers[chat.id].forEach((observer) => {
|
this.observers[chat.id].forEach((observer) => {
|
||||||
observer(message, timestamp);
|
observer(message, timestamp, chat.id);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue