Skip to content

Commit

Permalink
fix: serialization updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanenkoStud committed Nov 24, 2023
1 parent c90f1f3 commit 7d852b1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
31 changes: 19 additions & 12 deletions packages/kite-chat/src/kite-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
getMessages,
addMessage,
modifyMessage,
deleteMessage
deleteMessage,
messageById
} from './kite-storage';

export type KiteChatOptions = {
Expand Down Expand Up @@ -142,6 +143,7 @@ export class KiteChat {
return;
}
addMessage(msg, this.db);
this.element?.appendMsg(msg);
}

private update(messageId: string, updatedMsg: ContentMsg) {
Expand All @@ -155,10 +157,13 @@ export class KiteChat {
if(!this.db) {
return;
}
const msgElement = document.querySelector(
`${KiteMsgElement.TAG}[messageId="${messageId}"]`
) as KiteFileElement | undefined;
msgElement?.remove();
deleteMessage(messageId, this.db);
}


private restore() {
if(!this.db) {
return;
Expand Down Expand Up @@ -209,7 +214,7 @@ export class KiteChat {
throw new Error('Not connected');
}
console.debug('outgoing', outgoing);
this.save(outgoing);
this.db && addMessage(outgoing, this.db);
this.kiteWorker.port.postMessage(outgoing);
}

Expand Down Expand Up @@ -268,8 +273,16 @@ export class KiteChat {

protected onContentMessage(incoming: ContentMsg) {
console.debug('onContentMessage', incoming.messageId, incoming.timestamp);
this.save(incoming);
this.element?.appendMsg(incoming);
if(!this.db) {
return;
}
messageById(incoming.messageId, this.db).then(message => {
if(!message) {
this.db && this.save(incoming);
} else {
this.db && this.update(incoming.messageId, incoming);
}
})
}

protected onConnected(payload: Connected) {
Expand Down Expand Up @@ -305,13 +318,7 @@ export class KiteChat {
this.update(e.messageId, {
file: e.file,
} as ContentMsg);
e.zippedIds.forEach(id => {
const msgElement = document.querySelector(
`${KiteMsgElement.TAG}[messageId="${id}"]`
) as KiteFileElement | undefined;
msgElement?.remove();
this.delete(id);
})
e.zippedIds.forEach(id => this.delete(id));
}

protected onFailedMessage(e: FailedMsg) {
Expand Down
1 change: 1 addition & 0 deletions packages/kite-chat/src/kite-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export type BinaryMsg = {
fileType?: string;
fileSize?: number;
timestamp: Date;
status?: MsgStatus;
};

export type Connected = {
Expand Down
18 changes: 18 additions & 0 deletions packages/kite-chat/src/serialization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ describe('serialization', () => {

expect(decoded).toStrictEqual(plaintext);
});

it('should be able to encode kite message with status', () => {
const plaintext: PlaintextMsg = {
type: MsgType.PLAINTEXT,
messageId: 'ui4bJAV0Jo',
text: 'hello',
timestamp: new Date('2023-11-23T13:19:07.255Z'),
status: 2,
};

const encoded = JSON.stringify(["TXT","ui4bJAV0Jo","hello","2023-11-23T13:19:07.255Z",2]);
console.log(encoded);

const decoded = decodeKiteMsg(encoded);
console.log(decoded);

expect(decoded).toStrictEqual(plaintext);
});
});
14 changes: 12 additions & 2 deletions packages/kite-chat/src/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ const BINARY_MESSAGE_FIELDS: Array<keyof BinaryMsg> = [
'timestamp',
];

const INCOMING_PLAINTEXT_MESSAGE_FIELDS: Array<keyof PlaintextMsg> = [
...PLAINTEXT_MESSAGE_FIELDS,
'status',
];

const INCOMING_BINARY_MESSAGE_FIELDS: Array<keyof BinaryMsg> = [
...BINARY_MESSAGE_FIELDS,
'status',
];

const UPLOAD_REQUEST_FIELDS: Array<keyof UploadRequest> = [
'type',
'messageId',
Expand All @@ -104,9 +114,9 @@ const KITE_MSG_DECODERS: Partial<Record<MsgType, Decoder>> = {
[MsgType.ACK]: decoderFactory(MESSAGE_ACK_FIELDS),
[MsgType.OK]: decoderFactory(OK_FIELDS),
[MsgType.ERROR]: decoderFactory(ERROR_RESPONSE_FIELDS),
[MsgType.PLAINTEXT]: decoderFactory(PLAINTEXT_MESSAGE_FIELDS),
[MsgType.PLAINTEXT]: decoderFactory(INCOMING_PLAINTEXT_MESSAGE_FIELDS),
[MsgType.UPLOAD]: decoderFactory(UPLOAD_RESPONSE_FIELDS),
[MsgType.BIN]: decoderFactory(BINARY_MESSAGE_FIELDS),
[MsgType.BIN]: decoderFactory(INCOMING_BINARY_MESSAGE_FIELDS),
[MsgType.PONG]: decoderFactory(PONG_FIELDS),
};

Expand Down

0 comments on commit 7d852b1

Please sign in to comment.