mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
use realtime feed subcription
This commit is contained in:
parent
03b03b0bfa
commit
94f0219b63
@ -116,30 +116,15 @@ export class PostsListsComponent extends React.Component {
|
|||||||
listRef = React.createRef()
|
listRef = React.createRef()
|
||||||
|
|
||||||
timelineWsEvents = {
|
timelineWsEvents = {
|
||||||
"feed.new": (data) => {
|
"post:new": (data) => {
|
||||||
console.log("New feed => ", data)
|
console.log("[WS] Recived a post >", data)
|
||||||
|
|
||||||
if (!this.state.realtimeUpdates) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
list: [data, ...this.state.list],
|
list: [data, ...this.state.list],
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"post.new": (data) => {
|
"post:delete": (id) => {
|
||||||
console.log("New post => ", data)
|
console.log("[WS] Received a post delete >", id)
|
||||||
|
|
||||||
if (!this.state.realtimeUpdates) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({
|
|
||||||
list: [data, ...this.state.list],
|
|
||||||
})
|
|
||||||
},
|
|
||||||
"post.delete": (id) => {
|
|
||||||
console.log("Deleted post => ", id)
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
list: this.state.list.filter((post) => {
|
list: this.state.list.filter((post) => {
|
||||||
@ -147,6 +132,19 @@ export class PostsListsComponent extends React.Component {
|
|||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
"post:update": (data) => {
|
||||||
|
console.log("[WS] Received a post update >", data)
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
list: this.state.list.map((post) => {
|
||||||
|
if (post._id === data._id) {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
return post
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
handleLoad = async (fn, params = {}) => {
|
handleLoad = async (fn, params = {}) => {
|
||||||
@ -178,8 +176,6 @@ export class PostsListsComponent extends React.Component {
|
|||||||
return null
|
return null
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log("Loaded posts => ", result)
|
|
||||||
|
|
||||||
if (result) {
|
if (result) {
|
||||||
if (result.length === 0) {
|
if (result.length === 0) {
|
||||||
return this.setState({
|
return this.setState({
|
||||||
@ -317,26 +313,17 @@ export class PostsListsComponent extends React.Component {
|
|||||||
initialLoading: false,
|
initialLoading: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (this.props.watchTimeline) {
|
if (this.props.realtime) {
|
||||||
if (!Array.isArray(this.props.watchTimeline)) {
|
for (const [event, handler] of Object.entries(
|
||||||
console.error("watchTimeline prop must be an array")
|
this.timelineWsEvents,
|
||||||
} else {
|
)) {
|
||||||
this.props.watchTimeline.forEach((event) => {
|
app.cores.api.listenEvent(event, handler, "posts")
|
||||||
if (typeof this.timelineWsEvents[event] !== "function") {
|
|
||||||
console.error(
|
|
||||||
`The event "${event}" is not defined in the timelineWsEvents object`,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.cores.api.listenEvent(
|
app.cores.api.joinTopic(
|
||||||
event,
|
|
||||||
this.timelineWsEvents[event],
|
|
||||||
"posts",
|
"posts",
|
||||||
|
this.props.customTopic ?? "realtime:feed",
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
app.cores.api.client().sockets.posts.emit("connect_realtime")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.listRef && this.listRef.current) {
|
if (this.listRef && this.listRef.current) {
|
||||||
@ -347,26 +334,17 @@ export class PostsListsComponent extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount = async () => {
|
componentWillUnmount = async () => {
|
||||||
if (this.props.watchTimeline) {
|
if (this.props.realtime) {
|
||||||
if (!Array.isArray(this.props.watchTimeline)) {
|
for (const [event, handler] of Object.entries(
|
||||||
console.error("watchTimeline prop must be an array")
|
this.timelineWsEvents,
|
||||||
} else {
|
)) {
|
||||||
this.props.watchTimeline.forEach((event) => {
|
app.cores.api.unlistenEvent(event, handler, "posts")
|
||||||
if (typeof this.timelineWsEvents[event] !== "function") {
|
|
||||||
console.error(
|
|
||||||
`The event "${event}" is not defined in the timelineWsEvents object`,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.cores.api.unlistenEvent(
|
app.cores.api.leaveTopic(
|
||||||
event,
|
|
||||||
this.timelineWsEvents[event],
|
|
||||||
"posts",
|
"posts",
|
||||||
|
this.props.customTopic ?? "realtime:feed",
|
||||||
)
|
)
|
||||||
})
|
|
||||||
|
|
||||||
app.cores.api.client().sockets.posts.emit("disconnect_realtime")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.listRef && this.listRef.current) {
|
if (this.listRef && this.listRef.current) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user