Reorganize state models for clarity (#378)

This commit is contained in:
Paul Frazee 2023-04-03 15:21:17 -05:00 committed by GitHub
parent 9652d994dd
commit 2045c615a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 163 additions and 171 deletions

View file

@ -1,10 +1,10 @@
import {makeAutoObservable} from 'mobx'
import {LRUMap} from 'lru_map'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {AppBskyActorGetProfile as GetProfile} from '@atproto/api'
type CacheValue = Promise<GetProfile.Response> | GetProfile.Response
export class ProfilesViewModel {
export class ProfilesCache {
cache: LRUMap<string, CacheValue> = new LRUMap(100)
constructor(public rootStore: RootStoreModel) {

View file

@ -5,8 +5,8 @@ import {
AppBskyFeedDefs,
RichText,
} from '@atproto/api'
import {AtUri} from '../../third-party/uri'
import {RootStoreModel} from './root-store'
import {AtUri} from '../../../third-party/uri'
import {RootStoreModel} from '../root-store'
import * as apilib from 'lib/api/index'
import {cleanError} from 'lib/strings/errors'
@ -17,7 +17,7 @@ function* reactKeyGenerator(): Generator<string> {
}
}
export class PostThreadViewPostModel {
export class PostThreadItemModel {
// ui state
_reactKey: string = ''
_depth = 0
@ -29,8 +29,8 @@ export class PostThreadViewPostModel {
// data
post: AppBskyFeedDefs.PostView
postRecord?: FeedPost.Record
parent?: PostThreadViewPostModel | AppBskyFeedDefs.NotFoundPost
replies?: (PostThreadViewPostModel | AppBskyFeedDefs.NotFoundPost)[]
parent?: PostThreadItemModel | AppBskyFeedDefs.NotFoundPost
replies?: (PostThreadItemModel | AppBskyFeedDefs.NotFoundPost)[]
richText?: RichText
get uri() {
@ -79,7 +79,7 @@ export class PostThreadViewPostModel {
// parents
if (includeParent && v.parent) {
if (AppBskyFeedDefs.isThreadViewPost(v.parent)) {
const parentModel = new PostThreadViewPostModel(
const parentModel = new PostThreadItemModel(
this.rootStore,
keyGen.next().value,
v.parent,
@ -106,7 +106,7 @@ export class PostThreadViewPostModel {
const replies = []
for (const item of v.replies) {
if (AppBskyFeedDefs.isThreadViewPost(item)) {
const itemModel = new PostThreadViewPostModel(
const itemModel = new PostThreadItemModel(
this.rootStore,
keyGen.next().value,
item,
@ -182,7 +182,7 @@ export class PostThreadViewPostModel {
}
}
export class PostThreadViewModel {
export class PostThreadModel {
// state
isLoading = false
isRefreshing = false
@ -193,7 +193,7 @@ export class PostThreadViewModel {
params: GetPostThread.QueryParams
// data
thread?: PostThreadViewPostModel
thread?: PostThreadItemModel
constructor(
public rootStore: RootStoreModel,
@ -321,7 +321,7 @@ export class PostThreadViewModel {
_replaceAll(res: GetPostThread.Response) {
sortThread(res.data.thread)
const keyGen = reactKeyGenerator()
const thread = new PostThreadViewPostModel(
const thread = new PostThreadItemModel(
this.rootStore,
keyGen.next().value,
res.data.thread as AppBskyFeedDefs.ThreadViewPost,

View file

@ -1,7 +1,7 @@
import {makeAutoObservable} from 'mobx'
import {AppBskyFeedPost as Post} from '@atproto/api'
import {AtUri} from '../../third-party/uri'
import {RootStoreModel} from './root-store'
import {AtUri} from '../../../third-party/uri'
import {RootStoreModel} from '../root-store'
import {cleanError} from 'lib/strings/errors'
type RemoveIndex<T> = {

View file

@ -5,13 +5,13 @@ import {
AppBskyActorProfile,
RichText,
} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import * as apilib from 'lib/api/index'
import {cleanError} from 'lib/strings/errors'
export const ACTOR_TYPE_USER = 'app.bsky.system.actorUser'
export class ProfileViewViewerModel {
export class ProfileViewerModel {
muted?: boolean
following?: string
followedBy?: string
@ -21,7 +21,7 @@ export class ProfileViewViewerModel {
}
}
export class ProfileViewModel {
export class ProfileModel {
// state
isLoading = false
isRefreshing = false
@ -40,7 +40,7 @@ export class ProfileViewModel {
followersCount: number = 0
followsCount: number = 0
postsCount: number = 0
viewer = new ProfileViewViewerModel()
viewer = new ProfileViewerModel()
// added data
descriptionRichText?: RichText = new RichText({text: ''})

View file

@ -1,6 +1,6 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'
import {FeedItemModel} from './feed-view'
import {RootStoreModel} from '../root-store'
import {PostsFeedItemModel} from '../feeds/posts'
import {cleanError} from 'lib/strings/errors'
import {TEAM_HANDLES} from 'lib/constants'
import {
@ -8,14 +8,14 @@ import {
mergePosts,
} from 'lib/api/build-suggested-posts'
export class SuggestedPostsView {
export class SuggestedPostsModel {
// state
isLoading = false
hasLoaded = false
error = ''
// data
posts: FeedItemModel[] = []
posts: PostsFeedItemModel[] = []
constructor(public rootStore: RootStoreModel) {
makeAutoObservable(
@ -57,7 +57,7 @@ export class SuggestedPostsView {
this.posts = finalPosts.map((post, i) => {
// strip the reasons to hide that these are reposts
delete post.reason
return new FeedItemModel(this.rootStore, `post-${i}`, post)
return new PostsFeedItemModel(this.rootStore, `post-${i}`, post)
})
})
this._xIdle()

View file

@ -1,9 +1,9 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AppBskyActorDefs} from '@atproto/api'
import AwaitLock from 'await-lock'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
export class UserAutocompleteViewModel {
export class UserAutocompleteModel {
// state
isLoading = false
isActive = false

View file

@ -9,8 +9,8 @@ import {
} from '@atproto/api'
import AwaitLock from 'await-lock'
import {bundleAsync} from 'lib/async/bundle'
import {RootStoreModel} from './root-store'
import {PostThreadViewModel} from './post-thread-view'
import {RootStoreModel} from '../root-store'
import {PostThreadModel} from '../content/post-thread'
import {cleanError} from 'lib/strings/errors'
const GROUPABLE_REASONS = ['like', 'repost', 'follow']
@ -30,7 +30,7 @@ type SupportedRecord =
| AppBskyFeedLike.Record
| AppBskyGraphFollow.Record
export class NotificationsViewItemModel {
export class NotificationsFeedItemModel {
// ui state
_reactKey: string = ''
@ -47,10 +47,10 @@ export class NotificationsViewItemModel {
record?: SupportedRecord
isRead: boolean = false
indexedAt: string = ''
additional?: NotificationsViewItemModel[]
additional?: NotificationsFeedItemModel[]
// additional data
additionalPost?: PostThreadViewModel
additionalPost?: PostThreadModel
constructor(
public rootStore: RootStoreModel,
@ -75,7 +75,7 @@ export class NotificationsViewItemModel {
this.additional = []
for (const add of v.additional) {
this.additional.push(
new NotificationsViewItemModel(this.rootStore, '', add),
new NotificationsFeedItemModel(this.rootStore, '', add),
)
}
} else if (!preserve) {
@ -171,7 +171,7 @@ export class NotificationsViewItemModel {
postUri = this.subjectUri
}
if (postUri) {
this.additionalPost = new PostThreadViewModel(this.rootStore, {
this.additionalPost = new PostThreadModel(this.rootStore, {
uri: postUri,
depth: 0,
})
@ -185,7 +185,7 @@ export class NotificationsViewItemModel {
}
}
export class NotificationsViewModel {
export class NotificationsFeedModel {
// state
isLoading = false
isRefreshing = false
@ -199,7 +199,7 @@ export class NotificationsViewModel {
lock = new AwaitLock()
// data
notifications: NotificationsViewItemModel[] = []
notifications: NotificationsFeedItemModel[] = []
unreadCount = 0
// this is used to help trigger push notifications
@ -416,7 +416,7 @@ export class NotificationsViewModel {
}
}
async getNewMostRecent(): Promise<NotificationsViewItemModel | undefined> {
async getNewMostRecent(): Promise<NotificationsFeedItemModel | undefined> {
let old = this.mostRecentNotificationUri
const res = await this.rootStore.agent.listNotifications({
limit: 1,
@ -425,7 +425,7 @@ export class NotificationsViewModel {
return
}
this.mostRecentNotificationUri = res.data.notifications[0].uri
const notif = new NotificationsViewItemModel(
const notif = new NotificationsFeedItemModel(
this.rootStore,
'mostRecent',
res.data.notifications[0],
@ -467,9 +467,9 @@ export class NotificationsViewModel {
this.loadMoreCursor = res.data.cursor
this.hasMore = !!this.loadMoreCursor
const promises = []
const itemModels: NotificationsViewItemModel[] = []
const itemModels: NotificationsFeedItemModel[] = []
for (const item of groupNotifications(res.data.notifications)) {
const itemModel = new NotificationsViewItemModel(
const itemModel = new NotificationsFeedItemModel(
this.rootStore,
`item-${_idCounter++}`,
item,
@ -496,7 +496,7 @@ export class NotificationsViewModel {
async _prependAll(res: ListNotifications.Response) {
const promises = []
const itemModels: NotificationsViewItemModel[] = []
const itemModels: NotificationsFeedItemModel[] = []
const dedupedNotifs = res.data.notifications.filter(
n1 =>
!this.notifications.find(
@ -504,7 +504,7 @@ export class NotificationsViewModel {
),
)
for (const item of groupNotifications(dedupedNotifs)) {
const itemModel = new NotificationsViewItemModel(
const itemModel = new NotificationsFeedItemModel(
this.rootStore,
`item-${_idCounter++}`,
item,
@ -565,7 +565,7 @@ function groupNotifications(
return items2
}
type N = ListNotifications.Notification | NotificationsViewItemModel
type N = ListNotifications.Notification | NotificationsFeedItemModel
function isEq(a: N, b: N) {
// this function has a key subtlety- the indexedAt comparison
// the reason for this is reposts: they set the URI of the original post, not of the repost record

View file

@ -10,7 +10,7 @@ import {
import AwaitLock from 'await-lock'
import {bundleAsync} from 'lib/async/bundle'
import sampleSize from 'lodash.samplesize'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {cleanError} from 'lib/strings/errors'
import {SUGGESTED_FOLLOWS} from 'lib/constants'
import {
@ -27,7 +27,7 @@ type PostView = AppBskyFeedDefs.PostView
const PAGE_SIZE = 30
let _idCounter = 0
export class FeedItemModel {
export class PostsFeedItemModel {
// ui state
_reactKey: string = ''
@ -139,12 +139,12 @@ export class FeedItemModel {
}
}
export class FeedSliceModel {
export class PostsFeedSliceModel {
// ui state
_reactKey: string = ''
// data
items: FeedItemModel[] = []
items: PostsFeedItemModel[] = []
constructor(
public rootStore: RootStoreModel,
@ -154,7 +154,7 @@ export class FeedSliceModel {
this._reactKey = reactKey
for (const item of slice.items) {
this.items.push(
new FeedItemModel(rootStore, `item-${_idCounter++}`, item),
new PostsFeedItemModel(rootStore, `item-${_idCounter++}`, item),
)
}
makeAutoObservable(this, {rootStore: false})
@ -206,7 +206,7 @@ export class FeedSliceModel {
}
}
export class FeedModel {
export class PostsFeedModel {
// state
isLoading = false
isRefreshing = false
@ -223,8 +223,8 @@ export class FeedModel {
lock = new AwaitLock()
// data
slices: FeedSliceModel[] = []
nextSlices: FeedSliceModel[] = []
slices: PostsFeedSliceModel[] = []
nextSlices: PostsFeedSliceModel[] = []
constructor(
public rootStore: RootStoreModel,
@ -445,7 +445,11 @@ export class FeedModel {
if (nextSlices[0]?.uri !== this.slices[0]?.uri) {
const nextSlicesModels = nextSlices.map(
slice =>
new FeedSliceModel(this.rootStore, `item-${_idCounter++}`, slice),
new PostsFeedSliceModel(
this.rootStore,
`item-${_idCounter++}`,
slice,
),
)
if (autoPrepend) {
runInAction(() => {
@ -526,9 +530,9 @@ export class FeedModel {
const slices = this.tuner.tune(res.data.feed, this.feedTuners)
const toAppend: FeedSliceModel[] = []
const toAppend: PostsFeedSliceModel[] = []
for (const slice of slices) {
const sliceModel = new FeedSliceModel(
const sliceModel = new PostsFeedSliceModel(
this.rootStore,
`item-${_idCounter++}`,
slice,

View file

@ -1,7 +1,7 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AtUri} from '../../third-party/uri'
import {AtUri} from '../../../third-party/uri'
import {AppBskyFeedGetLikes as GetLikes} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {cleanError} from 'lib/strings/errors'
import {bundleAsync} from 'lib/async/bundle'
import * as apilib from 'lib/api/index'
@ -10,7 +10,7 @@ const PAGE_SIZE = 30
export type LikeItem = GetLikes.Like
export class LikesViewModel {
export class LikesModel {
// state
isLoading = false
isRefreshing = false

View file

@ -1,10 +1,10 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {AtUri} from '../../third-party/uri'
import {AtUri} from '../../../third-party/uri'
import {
AppBskyFeedGetRepostedBy as GetRepostedBy,
AppBskyActorDefs,
} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {bundleAsync} from 'lib/async/bundle'
import {cleanError} from 'lib/strings/errors'
import * as apilib from 'lib/api/index'
@ -13,7 +13,7 @@ const PAGE_SIZE = 30
export type RepostedByItem = AppBskyActorDefs.ProfileViewBasic
export class RepostedByViewModel {
export class RepostedByModel {
// state
isLoading = false
isRefreshing = false

View file

@ -3,7 +3,7 @@ import {
AppBskyGraphGetFollowers as GetFollowers,
AppBskyActorDefs as ActorDefs,
} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {cleanError} from 'lib/strings/errors'
import {bundleAsync} from 'lib/async/bundle'
@ -11,7 +11,7 @@ const PAGE_SIZE = 30
export type FollowerItem = ActorDefs.ProfileViewBasic
export class UserFollowersViewModel {
export class UserFollowersModel {
// state
isLoading = false
isRefreshing = false

View file

@ -3,7 +3,7 @@ import {
AppBskyGraphGetFollows as GetFollows,
AppBskyActorDefs as ActorDefs,
} from '@atproto/api'
import {RootStoreModel} from './root-store'
import {RootStoreModel} from '../root-store'
import {cleanError} from 'lib/strings/errors'
import {bundleAsync} from 'lib/async/bundle'
@ -11,7 +11,7 @@ const PAGE_SIZE = 30
export type FollowItem = ActorDefs.ProfileViewBasic
export class UserFollowsViewModel {
export class UserFollowsModel {
// state
isLoading = false
isRefreshing = false

View file

@ -1,7 +1,7 @@
import {makeAutoObservable, runInAction} from 'mobx'
import {RootStoreModel} from './root-store'
import {FeedModel} from './feed-view'
import {NotificationsViewModel} from './notifications-view'
import {PostsFeedModel} from './feeds/posts'
import {NotificationsFeedModel} from './feeds/notifications'
import {MyFollowsCache} from './cache/my-follows'
import {isObj, hasProp} from 'lib/type-guards'
@ -13,8 +13,8 @@ export class MeModel {
avatar: string = ''
followsCount: number | undefined
followersCount: number | undefined
mainFeed: FeedModel
notifications: NotificationsViewModel
mainFeed: PostsFeedModel
notifications: NotificationsFeedModel
follows: MyFollowsCache
constructor(public rootStore: RootStoreModel) {
@ -23,10 +23,10 @@ export class MeModel {
{rootStore: false, serialize: false, hydrate: false},
{autoBind: true},
)
this.mainFeed = new FeedModel(this.rootStore, 'home', {
this.mainFeed = new PostsFeedModel(this.rootStore, 'home', {
algorithm: 'reverse-chronological',
})
this.notifications = new NotificationsViewModel(this.rootStore, {})
this.notifications = new NotificationsFeedModel(this.rootStore, {})
this.follows = new MyFollowsCache(this.rootStore)
}

View file

@ -12,9 +12,9 @@ import {isObj, hasProp} from 'lib/type-guards'
import {LogModel} from './log'
import {SessionModel} from './session'
import {ShellUiModel} from './ui/shell'
import {ProfilesViewModel} from './profiles-view'
import {ProfilesCache} from './cache/profiles-view'
import {LinkMetasCache} from './cache/link-metas'
import {NotificationsViewItemModel} from './notifications-view'
import {NotificationsFeedItemModel} from './feeds/notifications'
import {MeModel} from './me'
import {PreferencesModel} from './ui/preferences'
import {resetToTab} from '../../Navigation'
@ -36,7 +36,7 @@ export class RootStoreModel {
shell = new ShellUiModel(this)
preferences = new PreferencesModel()
me = new MeModel(this)
profiles = new ProfilesViewModel(this)
profiles = new ProfilesCache(this)
linkMetas = new LinkMetasCache(this)
imageSizes = new ImageSizesCache()
@ -205,11 +205,11 @@ export class RootStoreModel {
// a notification has been queued for push
onPushNotification(
handler: (notif: NotificationsViewItemModel) => void,
handler: (notif: NotificationsFeedItemModel) => void,
): EmitterSubscription {
return DeviceEventEmitter.addListener('push-notification', handler)
}
emitPushNotification(notif: NotificationsViewItemModel) {
emitPushNotification(notif: NotificationsFeedItemModel) {
DeviceEventEmitter.emit('push-notification', notif)
}

View file

@ -1,7 +1,7 @@
import {makeAutoObservable} from 'mobx'
import {RootStoreModel} from '../root-store'
import {ProfileViewModel} from '../profile-view'
import {FeedModel} from '../feed-view'
import {ProfileModel} from '../content/profile'
import {PostsFeedModel} from '../feeds/posts'
export enum Sections {
Posts = 'Posts',
@ -20,8 +20,8 @@ export class ProfileUiModel {
static EMPTY_ITEM = {_reactKey: '__empty__'}
// data
profile: ProfileViewModel
feed: FeedModel
profile: ProfileModel
feed: PostsFeedModel
// ui state
selectedViewIndex = 0
@ -38,14 +38,14 @@ export class ProfileUiModel {
},
{autoBind: true},
)
this.profile = new ProfileViewModel(rootStore, {actor: params.user})
this.feed = new FeedModel(rootStore, 'author', {
this.profile = new ProfileModel(rootStore, {actor: params.user})
this.feed = new PostsFeedModel(rootStore, 'author', {
actor: params.user,
limit: 10,
})
}
get currentView(): FeedModel {
get currentView(): PostsFeedModel {
if (
this.selectedView === Sections.Posts ||
this.selectedView === Sections.PostsWithReplies
@ -137,7 +137,7 @@ export class ProfileUiModel {
async update() {
const view = this.currentView
if (view instanceof FeedModel) {
if (view instanceof PostsFeedModel) {
await view.update()
}
}

View file

@ -1,7 +1,7 @@
import {AppBskyEmbedRecord} from '@atproto/api'
import {RootStoreModel} from '../root-store'
import {makeAutoObservable} from 'mobx'
import {ProfileViewModel} from '../profile-view'
import {ProfileModel} from '../content/profile'
import {isObj, hasProp} from 'lib/type-guards'
import {PickedMedia} from 'lib/media/types'
@ -14,7 +14,7 @@ export interface ConfirmModal {
export interface EditProfileModal {
name: 'edit-profile'
profileView: ProfileViewModel
profileView: ProfileModel
onUpdate?: () => void
}
@ -77,7 +77,7 @@ interface LightboxModel {}
export class ProfileImageLightbox implements LightboxModel {
name = 'profile-image'
constructor(public profileView: ProfileViewModel) {
constructor(public profileView: ProfileModel) {
makeAutoObservable(this)
}
}