From ebc2033d186af6cf81f9533082857ee4a8fa6822 Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 18 Jan 2023 16:45:48 -0600 Subject: [PATCH] Handle post deletions by removing from active views (close #37) --- src/state/models/feed-view.ts | 25 ++++++++++++++++++++- src/state/models/post-thread-view.ts | 16 +++++++++++++ src/state/models/root-store.ts | 10 +++++++++ src/view/com/post-thread/PostThreadItem.tsx | 2 +- src/view/screens/Home.tsx | 2 ++ src/view/screens/PostThread.tsx | 4 +++- src/view/screens/Profile.tsx | 4 +++- 7 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/state/models/feed-view.ts b/src/state/models/feed-view.ts index 5697108a..2b3fba64 100644 --- a/src/state/models/feed-view.ts +++ b/src/state/models/feed-view.ts @@ -166,6 +166,7 @@ export class FeedItemModel { did: this.post.author.did, rkey: new AtUri(this.post.uri).rkey, }) + this.rootStore.emitPostDeleted(this.post.uri) } } @@ -255,6 +256,14 @@ export class FeedModel { this._loadPromise = undefined } + /** + * Register any event listeners. Returns a cleanup function. + */ + registerListeners() { + const sub = this.rootStore.onPostDeleted(this.onPostDeleted.bind(this)) + return () => sub.remove() + } + /** * Reset and load */ @@ -303,7 +312,7 @@ export class FeedModel { } /** - * Check if new postrs are available + * Check if new posts are available */ async checkForLatest() { if (this.hasNewLatest) { @@ -322,6 +331,20 @@ export class FeedModel { this.setHasNewLatest(hasNewLatest) } + /** + * Removes posts from the feed upon deletion. + */ + onPostDeleted(uri: string) { + console.log('hit', uri) + let i + do { + i = this.feed.findIndex(item => item.post.uri === uri) + if (i !== -1) { + this.feed.splice(i, 1) + } + } while (i !== -1) + } + // state transitions // = diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts index 1fc18872..584658e1 100644 --- a/src/state/models/post-thread-view.ts +++ b/src/state/models/post-thread-view.ts @@ -173,6 +173,7 @@ export class PostThreadViewPostModel { did: this.post.author.did, rkey: new AtUri(this.post.uri).rkey, }) + this.rootStore.emitPostDeleted(this.post.uri) } } @@ -229,6 +230,14 @@ export class PostThreadViewModel { } } + /** + * Register any event listeners. Returns a cleanup function. + */ + registerListeners() { + const sub = this.rootStore.onPostDeleted(this.onPostDeleted.bind(this)) + return () => sub.remove() + } + /** * Reset and load */ @@ -246,6 +255,13 @@ export class PostThreadViewModel { this._load() } + /** + * Refreshes when posts are deleted + */ + onPostDeleted(_uri: string) { + this.refresh() + } + // state transitions // = diff --git a/src/state/models/root-store.ts b/src/state/models/root-store.ts index 5646157c..73f1c452 100644 --- a/src/state/models/root-store.ts +++ b/src/state/models/root-store.ts @@ -5,6 +5,7 @@ import {makeAutoObservable} from 'mobx' import {sessionClient as AtpApi, SessionServiceClient} from '@atproto/api' import {createContext, useContext} from 'react' +import {DeviceEventEmitter, EmitterSubscription} from 'react-native' import {isObj, hasProp} from '../lib/type-guards' import {LogModel} from './log' import {SessionModel} from './session' @@ -102,6 +103,15 @@ export class RootStoreModel { this.nav.clear() this.me.clear() } + + onPostDeleted(handler: (uri: string) => void): EmitterSubscription { + return DeviceEventEmitter.addListener('post-deleted', handler) + } + + emitPostDeleted(uri: string) { + console.log('emit') + DeviceEventEmitter.emit('post-deleted', uri) + } } const throwawayInst = new RootStoreModel(AtpApi.service('http://localhost')) // this will be replaced by the loader, we just need to supply a value at init diff --git a/src/view/com/post-thread/PostThreadItem.tsx b/src/view/com/post-thread/PostThreadItem.tsx index cbd6f86b..46512e75 100644 --- a/src/view/com/post-thread/PostThreadItem.tsx +++ b/src/view/com/post-thread/PostThreadItem.tsx @@ -102,7 +102,7 @@ export const PostThreadItem = observer(function PostThreadItem({ if (deleted) { return ( - + { + const feedCleanup = store.me.mainFeed.registerListeners() const pollInterval = setInterval(() => doPoll(), 15e3) const cleanup = () => { clearInterval(pollInterval) + feedCleanup() } if (!visible) { diff --git a/src/view/screens/PostThread.tsx b/src/view/screens/PostThread.tsx index eb8ac9eb..c14c93af 100644 --- a/src/view/screens/PostThread.tsx +++ b/src/view/screens/PostThread.tsx @@ -25,8 +25,9 @@ export const PostThread = ({navIdx, visible, params}: ScreenParams) => { } useEffect(() => { let aborted = false + const threadCleanup = view.registerListeners() if (!visible) { - return + return threadCleanup } setTitle() store.shell.setMinimalShellMode(false) @@ -44,6 +45,7 @@ export const PostThread = ({navIdx, visible, params}: ScreenParams) => { } return () => { aborted = true + threadCleanup() } }, [visible, store.nav, store.log, name]) diff --git a/src/view/screens/Profile.tsx b/src/view/screens/Profile.tsx index a3e3ecd3..d701fba0 100644 --- a/src/view/screens/Profile.tsx +++ b/src/view/screens/Profile.tsx @@ -31,8 +31,9 @@ export const Profile = observer(({navIdx, visible, params}: ScreenParams) => { useEffect(() => { let aborted = false + const feedCleanup = uiState.feed.registerListeners() if (!visible) { - return + return feedCleanup } if (hasSetup) { uiState.update() @@ -45,6 +46,7 @@ export const Profile = observer(({navIdx, visible, params}: ScreenParams) => { } return () => { aborted = true + feedCleanup() } }, [visible, params.name, store])