From 59b460391592978339d9860941a3d51fcf799caa Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Tue, 7 Mar 2023 02:23:56 +0000 Subject: [PATCH] reimplement `watchTimeline` prop --- .../app/src/components/PostsList/index.jsx | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/app/src/components/PostsList/index.jsx b/packages/app/src/components/PostsList/index.jsx index e26bcefd..3a545b63 100755 --- a/packages/app/src/components/PostsList/index.jsx +++ b/packages/app/src/components/PostsList/index.jsx @@ -39,6 +39,17 @@ export class PostsListsComponent extends React.Component { viewRef = this.props.innerRef timelineWsEvents = { + "feed.new": (data) => { + console.log("New feed => ", data) + + if (!this.state.realtimeUpdates) { + return + } + + this.setState({ + list: [data, ...this.state.list], + }) + }, "post.new": (data) => { console.log("New post => ", data) @@ -160,9 +171,17 @@ export class PostsListsComponent extends React.Component { }) if (this.props.watchTimeline) { - Object.entries(this.timelineWsEvents).forEach(([event, callback]) => { - app.cores.api.listenEvent(event, callback) - }) + if (!Array.isArray(this.props.watchTimeline)) { + console.error("watchTimeline prop must be an array") + } else { + this.props.watchTimeline.forEach((event) => { + if (typeof this.timelineWsEvents[event] !== "function") { + console.error(`The event "${event}" is not defined in the timelineWsEvents object`) + } + + app.cores.api.listenEvent(event, this.timelineWsEvents[event]) + }) + } } //console.log("PostsList mounted", this.viewRef) @@ -177,9 +196,17 @@ export class PostsListsComponent extends React.Component { componentWillUnmount = async () => { if (this.props.watchTimeline) { - Object.entries(this.timelineWsEvents).forEach(([event, callback]) => { - app.cores.api.unlistenEvent(event, callback) - }) + if (!Array.isArray(this.props.watchTimeline)) { + console.error("watchTimeline prop must be an array") + } else { + this.props.watchTimeline.forEach((event) => { + if (typeof this.timelineWsEvents[event] !== "function") { + console.error(`The event "${event}" is not defined in the timelineWsEvents object`) + } + + app.cores.api.unlistenEvent(event, this.timelineWsEvents[event]) + }) + } } if (this.viewRef) {