Chat Message Service¶
This service is meant to handle persistence of conversation.
Foot gun
The current implementation is a "foot gun" implementation that was easy to put together and work for the limited use cases that were laid out. However, there are inheirent drawbacks that might induce silent failures in the expected behavior.
Currently, only one chat history (defined by Profile.id
) can be tracked at any one point of time. Calling ChatMessageService.load()
will drop the reference to previously loaded chat histories. This means that previous BehaviorSubject
s tracking the conversation will be orphaned and will not recieve any updates upon changes.
How to fix
This can be improved by memoizing the loaded chat history into a Record<string, BehaviorSubject<Message[]>
, so all BehaviorSubject
s can be tracked and updated as necessary. Calling ChatMessageService.load()
can then return a reference to the memoized BehaviorSubject
. This will allow multiple conversations to be loaded and updated simultaniously.
service
ChatMessageService¶
Attributes¶
private
attr
$messages¶
BehaviorSubject<Message[]>
tracking the currently active chat history.
Important
See the note above.
private
attr
$currentProfileId¶
BehaviorSubject<string>
to keep track of the currently loaded conversation. Mainly used to print a warning in console.
Methods¶
meth
Load Messages¶
async load(profileId: string): Promise<BehaviorSubject<Message[]>>
- Description
- Method to load the conversation with a given profile into memory to be tracked for updates.
- Parameters
profileId
(string
): Profile ID of the conversation to load.- Returns
Promise<BehaviorSubject<Message[]>>
Important
See the note above.
meth
Static Load Messages¶
async staticLoad(profileId: string): Promise<Message[]>
- Description
- Method to fetch the current existing messages with a given profile. This does not induce any side effects.
- Parameters
profileId
(string
): ID of the profile to fetch for.- Returns
Promise<Message[]>
meth
Insert Message¶
insert(message: Message): Promise<void>
- Description
- Method to persist a message in
IndexedDb
. - Parameters
message
(Message
): Message to be persisted- Returns
Promise<void>
meth
Upsert Message¶
upsert(message: Message): Promise<void>
- Description
- Method to update or create new message for persistence.
- Parameters
message
(Message
): Message to be updated or created.- Returns
Promise<void>