Skip to content

Commit

Permalink
Implement types for Vuex docs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmyersdev committed Aug 27, 2024
1 parent 201eecc commit f469882
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { debouncer } from '#root/src/common/debouncer'
import { storage } from '#helpers/storage'
import { pack, unpack } from '#root/src/models/doc'

import { type Plugin, type Store } from 'vuex'
import { storage } from '/helpers/storage'
import { debouncer } from '/src/common/debouncer'
import { type Doc, type PackedDoc, pack, unpack } from '/src/models/doc'
import {
ADD_DOCUMENT,
DISCARD_DOCUMENT,
Expand All @@ -14,18 +14,22 @@ import {
SHARE_DOCUMENT,
TOUCH_DOCUMENT,
} from '/src/store/actions'
import { SETTINGS_LOADED } from '/src/store/modules/settings'

import { SETTINGS_LOADED } from '#root/src/store/modules/settings'
type LegacyPackedDoc = PackedDoc & {
clientId?: string,
dataKey?: string,
}

const cache = storage().instance({ name: 'firebase/documents' })

const { debounce } = debouncer(100)

const find = (state, id) => {
return state.documents.all.find(doc => doc.id === id)
const find = (state: Store<any>['state'], id: string) => {
return state.documents.all.find((doc: Doc) => doc.id === id)
}

export default (store) => {
const docsPlugin: Plugin<any> = (store) => {
store.subscribe(({ type, payload }, state) => {
switch (type) {
case ADD_DOCUMENT:
Expand All @@ -36,6 +40,7 @@ export default (store) => {
case RESTRICT_DOCUMENT:
case SHARE_DOCUMENT:
case TOUCH_DOCUMENT:
// eslint-disable-next-line no-case-declarations
const found = find(state, payload.id)

if (found) {
Expand All @@ -50,12 +55,12 @@ export default (store) => {
case SETTINGS_LOADED:
// load all documents from the cache after settings are loaded
cache.keys()
.then(ids => Promise.all(ids.map(id => cache.getItem(id))))
.then(ids => Promise.all(ids.map(id => cache.getItem<LegacyPackedDoc>(id))))
.then((docs) => {
// unpack cached data
return Promise.all(
docs.map((doc) => {
const packed = Object.assign({}, doc, { id: (doc.id || doc.clientId), textKey: (doc.textKey || doc.dataKey) })
const packed = Object.assign({}, doc, { id: (doc!.id || doc!.clientId), textKey: (doc!.textKey || doc!.dataKey) })

return unpack(packed, { privateKey: state.settings.crypto.privateKey })
}),
Expand All @@ -69,3 +74,5 @@ export default (store) => {
}
})
}

export default docsPlugin

0 comments on commit f469882

Please sign in to comment.