Exemplo n.º 1
0
export default simpleMemoize(() => {
  const storageAreas = {
    local: initLocalStorage(),
    sync: initSyncStorage(),
    session: initSessionStorage(),
  }
  const listeners = []

  const createListener = storageAreaName => changes => {
    for (const [ key, { oldValue, newValue } ] of Object.entries(changes)) {
      safelyInvokeFns({
        fns: listeners,
        args: [ {
          key,
          storageAreaName,
          oldValue,
          newValue,
        } ],
      })
    }
  }

  // sessionStorage 不需要监听改动
  for (const storageAreaName of [ 'local', 'sync' ]) {
    storageAreas[storageAreaName].listen(createListener(storageAreaName))
  }

  return {
    ...storageAreas,
    listen: fn => arrayUniquePush(listeners, fn),
    unlisten: fn => arrayRemove(listeners, fn),
  }
})
Exemplo n.º 2
0
// 是否为当前登录用户的个人页面(消息页面,不含收藏、相册等)
export function isLoggedInUserProfilePage() {
  const userId = getLoggedInUserId()

  return (
    userId &&
    window.location.pathname.split('/')[1] === userId
  )
}

// 是否为当前登录用户用户好友页面
export const isLoggedInUserFriendsListPage = simpleMemoize(async () => {
  if (!isFriendsListPage()) return false

  await safeElementReady('#stream')

  const urlA = select('.tabs .crumb').href
  const urlB = getLoggedInUserProfilePageUrl()

  return urlA === urlB
})

// 是否为当前登录用户用户粉丝页面
export const isLoggedInUserFollowersListPage = simpleMemoize(async () => {
  if (!isFollowersListPage()) return false

  await safeElementReady('#stream')

  const urlA = select('.tabs .crumb').href
  const urlB = getLoggedInUserProfilePageUrl()

  return urlA === urlB
Exemplo n.º 3
0
import simpleMemoize from 'just-once'
import keepRetry from '@libs/keepRetry'

export default simpleMemoize(() => keepRetry({
  checker: () => document.head,
  delay: 0, // 这里必须是 0,否则会影响到主样式的加载速度,进而出现样式抖动现象
}))
Exemplo n.º 4
0
import cookies from 'js-cookie'
import simpleMemoize from 'just-once'

// 默认 cookies 在页面加载后不发生变化
export default simpleMemoize(() => cookies.get('u'))