@@ -22,6 +22,10 @@ const { updateSnippetContent, updateSnippet } = useSnippets()
2222
2323const updateQueue = ref < Map < string , UpdateQueueItem > > ( new Map ( ) )
2424const updateContentQueue = ref < Map < string , UpdateContentQueueItem > > ( new Map ( ) )
25+ const contentUpdateTimers = ref < Map < string , ReturnType < typeof setTimeout > > > (
26+ new Map ( ) ,
27+ )
28+ const inFlightContentKeys = ref < Set < string > > ( new Set ( ) )
2529
2630const updateDebounced = useDebounceFn ( ( snippetId : number ) => {
2731 const key = `${ snippetId } `
@@ -33,18 +37,47 @@ const updateDebounced = useDebounceFn((snippetId: number) => {
3337 }
3438} , UPDATE_DEBOUNCE_TIME )
3539
36- const updateContentDebounced = useDebounceFn (
37- ( snippetId : number , contentId : number ) => {
38- const key = `${ snippetId } -${ contentId } `
39- const update = updateContentQueue . value . get ( key )
40+ function getContentUpdateKey ( snippetId : number , contentId : number ) {
41+ return `${ snippetId } -${ contentId } `
42+ }
43+
44+ async function flushContentUpdate ( key : string ) {
45+ const update = updateContentQueue . value . get ( key )
46+ if ( ! update ) {
47+ return
48+ }
4049
41- if ( update ) {
42- updateSnippetContent ( update . snippetId , update . contentId , update . data )
43- updateContentQueue . value . delete ( key )
50+ updateContentQueue . value . delete ( key )
51+ inFlightContentKeys . value . add ( key )
52+
53+ try {
54+ await updateSnippetContent ( update . snippetId , update . contentId , update . data )
55+ }
56+ catch ( error ) {
57+ console . error ( error )
58+ }
59+ finally {
60+ inFlightContentKeys . value . delete ( key )
61+
62+ if ( updateContentQueue . value . has ( key ) ) {
63+ scheduleContentUpdate ( key )
4464 }
45- } ,
46- UPDATE_DEBOUNCE_TIME ,
47- )
65+ }
66+ }
67+
68+ function scheduleContentUpdate ( key : string ) {
69+ const pendingTimer = contentUpdateTimers . value . get ( key )
70+ if ( pendingTimer ) {
71+ clearTimeout ( pendingTimer )
72+ }
73+
74+ const timer = setTimeout ( ( ) => {
75+ contentUpdateTimers . value . delete ( key )
76+ void flushContentUpdate ( key )
77+ } , UPDATE_DEBOUNCE_TIME )
78+
79+ contentUpdateTimers . value . set ( key , timer )
80+ }
4881
4982function addToUpdateQueue ( snippetId : number , data : SnippetsUpdate ) {
5083 const key = `${ snippetId } `
@@ -57,14 +90,40 @@ function addToUpdateContentQueue(
5790 contentId : number ,
5891 data : SnippetContentsAdd ,
5992) {
60- const key = ` ${ snippetId } - ${ contentId } `
93+ const key = getContentUpdateKey ( snippetId , contentId )
6194 updateContentQueue . value . set ( key , { snippetId, contentId, data } )
62- updateContentDebounced ( snippetId , contentId )
95+
96+ if ( inFlightContentKeys . value . has ( key ) ) {
97+ return
98+ }
99+
100+ scheduleContentUpdate ( key )
101+ }
102+
103+ function getPendingContentUpdate ( snippetId : number , contentId : number ) {
104+ const key = getContentUpdateKey ( snippetId , contentId )
105+ return updateContentQueue . value . get ( key ) ?. data
106+ }
107+
108+ function isContentUpdateBusy ( snippetId : number , contentId : number ) {
109+ const key = getContentUpdateKey ( snippetId , contentId )
110+ return (
111+ updateContentQueue . value . has ( key ) || inFlightContentKeys . value . has ( key )
112+ )
113+ }
114+
115+ function hasBusyContentUpdates ( ) {
116+ return (
117+ updateContentQueue . value . size > 0 || inFlightContentKeys . value . size > 0
118+ )
63119}
64120
65121export function useSnippetUpdate ( ) {
66122 return {
67123 addToUpdateContentQueue,
68124 addToUpdateQueue,
125+ getPendingContentUpdate,
126+ hasBusyContentUpdates,
127+ isContentUpdateBusy,
69128 }
70129}
0 commit comments