6666
6767--- Save the current stack to persistent storage with optional name
6868--- @param name ? string
69- --- @param opts ? { scope ?: string , root_winid ?: integer , silent ?: boolean , on_done ?: fun ( success : boolean ) }
69+ --- @param opts ? { scope ?: string , root_winid ?: integer , silent ?: boolean , sync ?: boolean , on_done ?: fun ( success : boolean ) }
7070function M .save_current (name , opts )
7171 local silent = opts and opts .silent or false
72+ local sync = opts and opts .sync or false
7273 local on_done = opts and opts .on_done or nil
7374 local function finish (success )
7475 if on_done then
@@ -77,6 +78,7 @@ function M.save_current(name, opts)
7778 end
7879
7980 if not ensure_enabled (silent ) then
81+ finish (false )
8082 return
8183 end
8284
@@ -100,42 +102,62 @@ function M.save_current(name, opts)
100102 data_items = vim .list_slice (data_items , # data_items - max_items + 1 , # data_items )
101103 end
102104
103- store .read (scope , {
104- on_done = function (read_data )
105- local data = migrate .ensure (read_data )
106- local now = os.time ()
105+ --- @param data PeekstackStoreData
106+ --- @return PeekstackStoreData
107+ local function upsert_session (data )
108+ local now = os.time ()
109+ if data .sessions [resolved_name ] then
110+ data .sessions [resolved_name ].items = data_items
111+ data .sessions [resolved_name ].meta .updated_at = now
112+ else
113+ data .sessions [resolved_name ] = {
114+ items = data_items ,
115+ meta = {
116+ created_at = now ,
117+ updated_at = now ,
118+ },
119+ }
120+ end
121+ return data
122+ end
107123
108- if data .sessions [resolved_name ] then
109- data .sessions [resolved_name ].items = data_items
110- data .sessions [resolved_name ].meta .updated_at = now
124+ local function notify_save_result (success )
125+ if not silent then
126+ if success then
127+ vim .notify (" Session saved: " .. resolved_name , vim .log .levels .INFO )
111128 else
112- data .sessions [resolved_name ] = {
113- items = data_items ,
114- meta = {
115- created_at = now ,
116- updated_at = now ,
117- },
118- }
129+ vim .notify (" Failed to save session: " .. resolved_name , vim .log .levels .WARN )
119130 end
131+ end
132+ if success then
133+ user_events .emit (" PeekstackSave" , {
134+ session = resolved_name ,
135+ item_count = # data_items ,
136+ })
137+ end
138+ end
139+
140+ if sync then
141+ local data = upsert_session (migrate .ensure (store .read_sync (scope )))
142+ local success = store .write_sync (scope , data )
143+ if success then
144+ update_cache (data )
145+ end
146+ notify_save_result (success )
147+ finish (success )
148+ return
149+ end
150+
151+ store .read (scope , {
152+ on_done = function (read_data )
153+ local data = upsert_session (migrate .ensure (read_data ))
120154
121155 store .write (scope , data , {
122156 on_done = function (success )
123157 if success then
124158 update_cache (data )
125159 end
126- if not silent then
127- if success then
128- vim .notify (" Session saved: " .. resolved_name , vim .log .levels .INFO )
129- else
130- vim .notify (" Failed to save session: " .. resolved_name , vim .log .levels .WARN )
131- end
132- end
133- if success then
134- user_events .emit (" PeekstackSave" , {
135- session = resolved_name ,
136- item_count = # data_items ,
137- })
138- end
160+ notify_save_result (success )
139161 finish (success )
140162 end ,
141163 })
@@ -156,6 +178,7 @@ function M.restore(name, opts)
156178 end
157179
158180 if not ensure_enabled (silent ) then
181+ finish (false )
159182 return
160183 end
161184
0 commit comments