Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad hoc/fix thread messages stream updates on #159

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
---
name: pubnub-js-chat
version: v0.5.1
version: v0.5.2
scm: github.com/pubnub/js-chat
schema: 1
files:
- lib/dist/index.js
changelog:
- date: 2024-01-16
version: v0.5.2
changes:
- type: feature
text: "Make ThreadMessage.streamUpdatesOn return ThreadMessage[] instead of Message[]."
- date: 2023-12-18
version: v0.5.1
changes:
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pubnub/chat",
"version": "0.5.1",
"version": "0.5.2",
"description": "PubNub JavaScript Chat SDK",
"author": "PubNub <[email protected]>",
"license": "SEE LICENSE IN LICENSE",
Expand Down
6 changes: 3 additions & 3 deletions lib/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@ export class Message {
}

/** @internal */
private clone(params: Partial<MessageFields>) {
protected clone(params: Partial<MessageFields>) {
const { timetoken, content, channelId, userId, actions, meta } = this
const data = Object.assign({}, { timetoken, content, channelId, userId, actions, meta }, params)
return new Message(this.chat, data)
}

/** @internal */
private assignAction(action: PubNub.MessageAction) {
protected assignAction(action: PubNub.MessageAction) {
const { actionTimetoken, type, value, uuid } = action
const newActions = this.actions || {}
newActions[type] ||= {}
Expand All @@ -131,7 +131,7 @@ export class Message {
}

/** @internal */
private filterAction(action: PubNub.MessageAction) {
protected filterAction(action: PubNub.MessageAction) {
const { actionTimetoken, type, value, uuid } = action
const newActions = this.actions || {}
newActions[type] ||= {}
Expand Down
46 changes: 46 additions & 0 deletions lib/src/entities/thread-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Chat } from "./chat"
import { ThreadMessageDTOParams } from "../types"
import { Channel } from "./channel"
import { getErrorProxiedEntity } from "../error-logging"
import PubNub from "pubnub"

export class ThreadMessage extends Message {
readonly parentChannelId: string
Expand All @@ -29,6 +30,51 @@ export class ThreadMessage extends Message {
return getErrorProxiedEntity(new ThreadMessage(chat, data), chat.errorLogger)
}

/** @internal */
protected clone(params: Partial<MessageFields>) {
const { timetoken, content, channelId, userId, actions, meta, parentChannelId } = this
const data = Object.assign(
{},
{ parentChannelId, timetoken, content, channelId, userId, actions, meta },
params
)
return new ThreadMessage(this.chat, data)
}

static streamUpdatesOn(
threadMessages: ThreadMessage[],
callback: (threadMessages: ThreadMessage[]) => unknown
) {
if (!threadMessages.length) throw "Cannot stream message updates on an empty list"
const listener = {
messageAction: (event: PubNub.MessageActionEvent) => {
const threadMessage = threadMessages.find(
(msg) => msg.timetoken === event.data.messageTimetoken
)
if (!threadMessage) return
if (threadMessage.channelId !== event.channel) return
let actions
if (event.event === "added") actions = threadMessage.assignAction(event.data)
if (event.event === "removed") actions = threadMessage.filterAction(event.data)
const newMessage = threadMessage.clone({ actions })
const newMessages = threadMessages.map((msg) =>
msg.timetoken === newMessage.timetoken ? newMessage : msg
)
callback(newMessages)
},
}
const { chat } = threadMessages[0]
const removeListener = chat.addListener(listener)
const subscriptions = threadMessages
.filter((m1, i) => threadMessages.findIndex((m2) => m1.channelId === m2.channelId) === i)
.map((message) => chat.subscribe(message.channelId))

return () => {
removeListener()
subscriptions.map((unsub) => unsub())
}
}

async pinToParentChannel() {
const parentChannel = await this.chat.getChannel(this.parentChannelId)
if (!parentChannel) {
Expand Down
Loading