Line data Source code
1 : import { useDebounceFn } from '@vueuse/core'
2 : import { defineStore } from 'pinia'
3 : import { onScopeDispose, ref, shallowReactive, watch } from 'vue'
4 :
5 : import type {
6 : AssetItem,
7 : AssetUserMetadata
8 : } from '@/platform/assets/schemas/assetSchema'
9 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
10 : import { useAssetsStore } from '@/stores/assetsStore'
11 :
12 0 : function unconfirmedUpdates(
13 : pending: AssetUserMetadata,
14 : submitted: AssetUserMetadata
15 : ): AssetUserMetadata {
16 0 : const confirmed = new Map(Object.entries(submitted))
17 0 : return Object.fromEntries(
18 : Object.entries(pending).filter(
19 : ([key, value]) => !confirmed.has(key) || value !== confirmed.get(key)
20 : )
21 : )
22 : }
23 :
24 2355 : export const useAssetMetadataStore = defineStore('assetMetadata', () => {
25 0 : const assetsStore = useAssetsStore()
26 0 : const workspaceStore = useTeamWorkspaceStore()
27 0 : const drafts = shallowReactive(
28 : new Map<string, ReturnType<typeof createDraft>>()
29 : )
30 :
31 0 : watch(
32 : [() => workspaceStore.workspaceId, () => workspaceStore.identityGeneration],
33 : () => drafts.clear(),
34 : { flush: 'sync' }
35 : )
36 0 : onScopeDispose(() => drafts.clear())
37 :
38 0 : function createDraft(initialAsset: AssetItem, initialCacheKey?: string) {
39 0 : const updates = ref<AssetUserMetadata>({})
40 0 : let targetAsset = initialAsset
41 0 : let targetCacheKey = initialCacheKey
42 0 : let saving = false
43 0 : const draft = { updates, queue }
44 :
45 0 : const flush = useDebounceFn(async () => {
46 0 : if (
47 : drafts.get(initialAsset.id) !== draft ||
48 : saving ||
49 : targetAsset.is_immutable !== false
50 : )
51 0 : return
52 0 : const submitted = updates.value
53 0 : if (Object.keys(submitted).length === 0) return
54 0 : const metadata = { ...targetAsset.user_metadata, ...submitted }
55 0 : saving = true
56 0 : const saved = await assetsStore.updateAssetMetadata(
57 : targetAsset,
58 : metadata,
59 : targetCacheKey
60 : )
61 0 : saving = false
62 0 : finishSave(submitted, metadata, saved)
63 : }, 500)
64 :
65 0 : function finishSave(
66 : submitted: AssetUserMetadata,
67 : metadata: AssetItem['user_metadata'],
68 : saved: boolean
69 : ) {
70 0 : if (drafts.get(initialAsset.id) !== draft) return
71 0 : const hasNewerEdits = updates.value !== submitted
72 0 : if (saved) {
73 0 : targetAsset = { ...targetAsset, user_metadata: metadata }
74 0 : updates.value = unconfirmedUpdates(updates.value, submitted)
75 : }
76 0 : if (Object.keys(updates.value).length === 0)
77 0 : drafts.delete(initialAsset.id)
78 0 : else if (saved || hasNewerEdits) void flush()
79 : }
80 :
81 0 : function queue(
82 : newUpdates: AssetUserMetadata,
83 : currentAsset: AssetItem,
84 : currentCacheKey?: string
85 : ) {
86 0 : targetAsset = currentAsset
87 0 : targetCacheKey = currentCacheKey
88 0 : updates.value = { ...updates.value, ...newUpdates }
89 0 : void flush()
90 : }
91 :
92 0 : return draft
93 : }
94 :
95 0 : function queueUpdate(
96 : asset: AssetItem,
97 : updates: AssetUserMetadata,
98 : cacheKey?: string
99 : ) {
100 0 : if (asset.is_immutable !== false) return
101 0 : let draft = drafts.get(asset.id)
102 0 : if (!draft) {
103 0 : draft = createDraft(asset, cacheKey)
104 0 : drafts.set(asset.id, draft)
105 : }
106 0 : draft.queue(updates, asset, cacheKey)
107 : }
108 :
109 0 : function getPendingUpdates(assetId: string): AssetUserMetadata {
110 0 : return drafts.get(assetId)?.updates.value ?? {}
111 : }
112 :
113 0 : return { queueUpdate, getPendingUpdates }
114 : })
|