fix: fix basic login - clear store on log out (#1545)

fixes #1544
This commit is contained in:
Nolan Lawson 2019-09-26 09:07:49 -07:00 committed by GitHub
parent 4ddf47f3da
commit 56ba259ccf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 105 additions and 27 deletions

View file

@ -6,6 +6,7 @@ import { goto } from '../../../__sapper__/client'
import { cacheFirstUpdateAfter } from '../_utils/sync' import { cacheFirstUpdateAfter } from '../_utils/sync'
import { getInstanceInfo } from '../_api/instance' import { getInstanceInfo } from '../_api/instance'
import { database } from '../_database/database' import { database } from '../_database/database'
import { importVirtualListStore } from '../_utils/asyncModules'
export function changeTheme (instanceName, newTheme) { export function changeTheme (instanceName, newTheme) {
const { instanceThemes } = store.get() const { instanceThemes } = store.get()
@ -33,29 +34,50 @@ export function switchToInstance (instanceName) {
export async function logOutOfInstance (instanceName, message = `Logged out of ${instanceName}`) { export async function logOutOfInstance (instanceName, message = `Logged out of ${instanceName}`) {
const { const {
loggedInInstances,
instanceThemes,
loggedInInstancesInOrder,
composeData, composeData,
currentInstance currentInstance,
customEmoji,
instanceInfos,
instanceLists,
instanceThemes,
loggedInInstances,
loggedInInstancesInOrder,
verifyCredentials
} = store.get() } = store.get()
loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1) loggedInInstancesInOrder.splice(loggedInInstancesInOrder.indexOf(instanceName), 1)
const newInstance = instanceName === currentInstance const newInstance = instanceName === currentInstance ? loggedInInstancesInOrder[0] : currentInstance
? loggedInInstancesInOrder[0] const objectsToClear = [
: currentInstance composeData,
delete loggedInInstances[instanceName] customEmoji,
delete instanceThemes[instanceName] instanceInfos,
delete composeData[instanceName] instanceLists,
instanceThemes,
loggedInInstances,
verifyCredentials
]
for (const obj of objectsToClear) {
delete obj[instanceName]
}
store.set({ store.set({
loggedInInstances: loggedInInstances, composeData,
instanceThemes: instanceThemes,
loggedInInstancesInOrder: loggedInInstancesInOrder,
currentInstance: newInstance, currentInstance: newInstance,
searchResults: null, customEmoji,
instanceInfos,
instanceLists,
instanceThemes,
loggedInInstances,
loggedInInstancesInOrder,
queryInSearch: '', queryInSearch: '',
composeData: composeData searchResults: null,
timelineInitialized: false,
timelinePreinitialized: false,
verifyCredentials
}) })
store.clearTimelineDataForInstance(instanceName)
store.clearAutosuggestDataForInstance(instanceName)
store.save() store.save()
const { virtualListStore } = await importVirtualListStore()
virtualListStore.clearRealmByPrefix(currentInstance + '/') // TODO: this is a hacky way to clear the vlist cache
toast.say(message) toast.say(message)
const { enableGrayscale } = store.get() const { enableGrayscale } = store.get()
switchToTheme(instanceThemes[newInstance], enableGrayscale) switchToTheme(instanceThemes[newInstance], enableGrayscale)

View file

@ -108,6 +108,7 @@ export async function setupTimeline () {
timelineItemSummariesAreStale, timelineItemSummariesAreStale,
currentTimeline currentTimeline
} = store.get() } = store.get()
console.log({ timelineItemSummaries, timelineItemSummariesAreStale, currentTimeline })
if (!timelineItemSummaries || if (!timelineItemSummaries ||
timelineItemSummariesAreStale || timelineItemSummariesAreStale ||
currentTimeline.startsWith('status/')) { currentTimeline.startsWith('status/')) {

View file

@ -40,7 +40,7 @@
scrollContainer.scrollTop = scrollTop scrollContainer.scrollTop = scrollTop
stop('set scrollTop') stop('set scrollTop')
doubleRAF(() => { doubleRAF(() => {
console.log('initialized VirtualList') console.log('initialized VirtualList (1)')
this.fire('initialized') this.fire('initialized')
}) })
}) })
@ -50,7 +50,7 @@
this.fire('noNeedToScroll') this.fire('noNeedToScroll')
this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => { this.observe('allVisibleItemsHaveHeight', allVisibleItemsHaveHeight => {
if (allVisibleItemsHaveHeight) { if (allVisibleItemsHaveHeight) {
console.log('initialized VirtualList') console.log('initialized VirtualList (2)')
this.fire('initialized') this.fire('initialized')
} }
}) })

View file

@ -8,6 +8,21 @@ class VirtualListStore extends RealmStore {
constructor (state) { constructor (state) {
super(state, /* maxSize */ 10) super(state, /* maxSize */ 10)
} }
// TODO: this is hacky
clearRealmByPrefix (prefix) {
const { realms } = this.get()
if (!realms) {
return
}
for (const key of realms.getAllKeys()) {
if (key.startsWith(prefix)) {
console.log('deleted realm', key)
realms.delete(key)
}
}
this.set({ realms })
}
} }
const virtualListStore = new VirtualListStore() const virtualListStore = new VirtualListStore()
@ -103,9 +118,10 @@ virtualListStore.compute('allVisibleItemsHaveHeight',
return true return true
}) })
if (process.browser && process.env.NODE_ENV !== 'production') { if (process.browser) {
window.virtualListStore = virtualListStore window.__virtualListStore = virtualListStore // for debugging
if (process.env.NODE_ENV !== 'production') { // for extra debugging
virtualListStore.on('state', ({ changed }) => { virtualListStore.on('state', ({ changed }) => {
if (changed.visibleItems) { if (changed.visibleItems) {
window.visibleItemsChangedCount = (window.visibleItemsChangedCount || 0) + 1 window.visibleItemsChangedCount = (window.visibleItemsChangedCount || 0) + 1
@ -115,6 +131,7 @@ if (process.browser && process.env.NODE_ENV !== 'production') {
} }
}) })
} }
}
export { export {
virtualListStore virtualListStore

View file

@ -110,6 +110,7 @@ async function insertStatusThread (instanceName, statusId, statuses) {
} }
export async function insertTimelineItems (instanceName, timeline, timelineItems) { export async function insertTimelineItems (instanceName, timeline, timelineItems) {
console.log('insertTimelineItems', instanceName, timeline, timelineItems)
/* no await */ scheduleCleanup() /* no await */ scheduleCleanup()
if (timeline === 'notifications' || timeline === 'notifications/mentions') { if (timeline === 'notifications' || timeline === 'notifications/mentions') {
return insertTimelineNotifications(instanceName, timeline, timelineItems) return insertTimelineNotifications(instanceName, timeline, timelineItems)

View file

@ -23,4 +23,15 @@ export function autosuggestMixins (Store) {
const { currentInstance, currentComposeRealm } = this.get() const { currentInstance, currentComposeRealm } = this.get()
return get(this.get()[`autosuggestData_${key}`], [currentInstance, currentComposeRealm]) return get(this.get()[`autosuggestData_${key}`], [currentInstance, currentComposeRealm])
} }
Store.prototype.clearAutosuggestDataForInstance = function (instanceName) {
const changes = {}
Object.entries(this.get()).forEach(([key, value]) => {
if (key.startsWith('autosuggestData_') && value) {
delete value[instanceName]
changes[key] = value
}
})
this.set(changes)
}
} }

View file

@ -43,4 +43,15 @@ export function timelineMixins (Store) {
return key.startsWith('status/') return key.startsWith('status/')
}) })
} }
Store.prototype.clearTimelineDataForInstance = function (instanceName) {
const changes = {}
Object.entries(this.get()).forEach(([key, value]) => {
if (key.startsWith('timelineData_') && value) {
delete value[instanceName]
changes[key] = value
}
})
this.set(changes)
}
} }

View file

@ -87,6 +87,17 @@ export class QuickLRU extends EventEmitter {
this._size = 0 this._size = 0
} }
getAllKeys () {
const set = new Set()
for (const key of this.cache.keys()) {
set.add(key)
}
for (const key of this.oldCache.keys()) {
set.add(key)
}
return set
}
// unused // unused
// * keys() { // * keys() {
// for (const [key] of this) { // for (const [key] of this) {

View file

@ -51,3 +51,7 @@ export const importComposeBox = () => import(
export const importTesseractWorker = () => import( export const importTesseractWorker = () => import(
/* webpackChunkName: 'tesseractWorker' */ '../_utils/tesseractWorker.js' /* webpackChunkName: 'tesseractWorker' */ '../_utils/tesseractWorker.js'
).then(getDefault) ).then(getDefault)
export const importVirtualListStore = () => import(
/* webpackChunkName: 'virtualListStore.js' */ '../_components/virtualList/virtualListStore.js'
)