Specify return types

This commit is contained in:
Franck Royer 2021-10-05 14:04:12 +11:00
parent b4479ee3a3
commit 8f90c177fb
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 18 additions and 11 deletions

View File

@ -14,7 +14,7 @@ export class Chat {
/** /**
* Create a public chat room. * Create a public chat room.
*/ */
public static async create(id: string) { public static async create(id: string): Promise<Chat> {
const symKey = await createSymKeyFromPassword(id); const symKey = await createSymKeyFromPassword(id);
return new Chat(id, symKey); return new Chat(id, symKey);
@ -34,7 +34,7 @@ export class Chat {
return message; return message;
} }
public handleNewMessage(message: ChatMessage) { public handleNewMessage(message: ChatMessage): void {
this._updateClockFromMessage(message); this._updateClockFromMessage(message);
} }
@ -52,11 +52,18 @@ export class Chat {
} }
private _updateClockFromMessage(message: ChatMessage): void { private _updateClockFromMessage(message: ChatMessage): void {
if (!this.lastMessage || this.lastMessage.clock <= message.clock) { if (
!this.lastMessage ||
!this.lastMessage.clock ||
(message.clock && this.lastMessage.clock <= message.clock)
) {
this.lastMessage = message; this.lastMessage = message;
} }
if (!this.lastClockValue || this.lastClockValue < message.clock) { if (
!this.lastClockValue ||
(message.clock && this.lastClockValue < message.clock)
) {
this.lastClockValue = message.clock; this.lastClockValue = message.clock;
} }
} }

View File

@ -15,7 +15,7 @@ export class ChatMessage {
timestamp: number, timestamp: number,
text: string, text: string,
chatId: string chatId: string
) { ): ChatMessage {
const proto = { const proto = {
clock, // ms? clock, // ms?
timestamp, //ms? timestamp, //ms?
@ -50,7 +50,7 @@ export class ChatMessage {
return proto.ChatMessage.encode(this.proto).finish(); return proto.ChatMessage.encode(this.proto).finish();
} }
public get clock() { public get clock(): number | undefined {
return this.proto.clock; return this.proto.clock;
} }

View File

@ -28,7 +28,7 @@ export class Messenger {
public static async create( public static async create(
identity: Identity, identity: Identity,
wakuOptions?: WakuCreateOptions wakuOptions?: WakuCreateOptions
) { ): Promise<Messenger> {
const _wakuOptions = Object.assign({ bootstrap: true }, wakuOptions); const _wakuOptions = Object.assign({ bootstrap: true }, wakuOptions);
const waku = await Waku.create(_wakuOptions); const waku = await Waku.create(_wakuOptions);
return new Messenger(identity, waku); return new Messenger(identity, waku);
@ -39,7 +39,7 @@ export class Messenger {
* *
* Use `addListener` to get messages received on this chat. * Use `addListener` to get messages received on this chat.
*/ */
public async joinChat(chatId: string) { public async joinChat(chatId: string): Promise<void> {
if (this.chatsById.has(chatId)) if (this.chatsById.has(chatId))
throw `Failed to join chat, it is already joined: ${chatId}`; throw `Failed to join chat, it is already joined: ${chatId}`;
@ -102,7 +102,7 @@ export class Messenger {
public addObserver( public addObserver(
observer: (message: ApplicationMetadataMessage) => void, observer: (message: ApplicationMetadataMessage) => void,
chatId: string chatId: string
) { ): void {
// Not sure this is the best design here. Maybe `addObserver` and `joinChat` should be merged. // Not sure this is the best design here. Maybe `addObserver` and `joinChat` should be merged.
if (!this.chatsById.has(chatId)) if (!this.chatsById.has(chatId))
@ -153,7 +153,7 @@ export class Messenger {
if (!chat) if (!chat)
throw `Failed to retrieve messages, chat is not joined: ${chatId}`; throw `Failed to retrieve messages, chat is not joined: ${chatId}`;
const _callback = (wakuMessages: WakuMessage[]) => { const _callback = (wakuMessages: WakuMessage[]): void => {
const isDefined = ( const isDefined = (
msg: ApplicationMetadataMessage | undefined msg: ApplicationMetadataMessage | undefined
): msg is ApplicationMetadataMessage => { ): msg is ApplicationMetadataMessage => {
@ -187,7 +187,7 @@ export class Messenger {
private _handleNewChatMessage( private _handleNewChatMessage(
chat: Chat, chat: Chat,
message: ApplicationMetadataMessage message: ApplicationMetadataMessage
) { ): void {
if (!message.payload || !message.type || !message.signature) return; if (!message.payload || !message.type || !message.signature) return;
const chatMessage = ChatMessage.decode(message.payload); const chatMessage = ChatMessage.decode(message.payload);