about summary refs log tree commit diff
path: root/src/state/cache/profile-shadow.ts
blob: 6ebd3913278249ff636da5c5d33abd813c515bde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import {useEffect, useState, useMemo, useCallback} from 'react'
import EventEmitter from 'eventemitter3'
import {AppBskyActorDefs} from '@atproto/api'
import {batchedUpdates} from '#/lib/batchedUpdates'
import {Shadow, castAsShadow} from './types'
export type {Shadow} from './types'

const emitter = new EventEmitter()

export interface ProfileShadow {
  followingUri: string | undefined
  muted: boolean | undefined
  blockingUri: string | undefined
}

interface CacheEntry {
  ts: number
  value: ProfileShadow
}

type ProfileView =
  | AppBskyActorDefs.ProfileView
  | AppBskyActorDefs.ProfileViewBasic
  | AppBskyActorDefs.ProfileViewDetailed

const firstSeenMap = new WeakMap<ProfileView, number>()
function getFirstSeenTS(profile: ProfileView): number {
  let timeStamp = firstSeenMap.get(profile)
  if (timeStamp !== undefined) {
    return timeStamp
  }
  timeStamp = Date.now()
  firstSeenMap.set(profile, timeStamp)
  return timeStamp
}

export function useProfileShadow(profile: ProfileView): Shadow<ProfileView> {
  const profileSeenTS = getFirstSeenTS(profile)
  const [state, setState] = useState<CacheEntry>(() => ({
    ts: profileSeenTS,
    value: fromProfile(profile),
  }))

  const [prevProfile, setPrevProfile] = useState(profile)
  if (profile !== prevProfile) {
    // if we got a new prop, assume it's fresher
    // than whatever shadow state we accumulated
    setPrevProfile(profile)
    setState({
      ts: profileSeenTS,
      value: fromProfile(profile),
    })
  }

  const onUpdate = useCallback(
    (value: Partial<ProfileShadow>) => {
      setState(s => ({ts: Date.now(), value: {...s.value, ...value}}))
    },
    [setState],
  )

  // react to shadow updates
  useEffect(() => {
    emitter.addListener(profile.did, onUpdate)
    return () => {
      emitter.removeListener(profile.did, onUpdate)
    }
  }, [profile.did, onUpdate])

  return useMemo(() => {
    return state.ts > profileSeenTS
      ? mergeShadow(profile, state.value)
      : castAsShadow(profile)
  }, [profile, state, profileSeenTS])
}

export function updateProfileShadow(
  uri: string,
  value: Partial<ProfileShadow>,
) {
  batchedUpdates(() => {
    emitter.emit(uri, value)
  })
}

function fromProfile(profile: ProfileView): ProfileShadow {
  return {
    followingUri: profile.viewer?.following,
    muted: profile.viewer?.muted,
    blockingUri: profile.viewer?.blocking,
  }
}

function mergeShadow(
  profile: ProfileView,
  shadow: ProfileShadow,
): Shadow<ProfileView> {
  return castAsShadow({
    ...profile,
    viewer: {
      ...(profile.viewer || {}),
      following: shadow.followingUri,
      muted: shadow.muted,
      blocking: shadow.blockingUri,
    },
  })
}