1
1
import type { Detail } from "jsr:@vim-fall/core@^0.3.0/item" ;
2
- import { brotli } from "jsr:@deno-library/compress@^0.5.6" ;
3
2
4
3
import type { PickerContext } from "./picker.ts" ;
5
4
6
5
/**
7
- * In-memory storage for compressed picker sessions.
6
+ * In-memory storage for picker sessions.
8
7
* Sessions are stored in chronological order (oldest first).
9
8
*/
10
9
// deno-lint-ignore no-explicit-any
11
- const sessions : PickerSessionCompressed < any > [ ] = [ ] ;
10
+ const sessions : PickerSession < any > [ ] = [ ] ;
12
11
13
12
/**
14
13
* Maximum number of sessions to keep in memory.
@@ -28,88 +27,24 @@ export type PickerSession<T extends Detail> = {
28
27
readonly context : PickerContext < T > ;
29
28
} ;
30
29
31
- /**
32
- * Compressed version of PickerSession where the context is stored as binary data.
33
- * This reduces memory usage when storing multiple sessions.
34
- * @template T - The type of item detail in the picker
35
- */
36
- export type PickerSessionCompressed < T extends Detail > =
37
- & Omit < PickerSession < T > , "context" >
38
- & {
39
- /** Brotli-compressed binary representation of the context */
40
- context : Uint8Array ;
41
- } ;
42
-
43
- /**
44
- * Compresses a picker session by converting its context to brotli-compressed binary data.
45
- * This is used internally to reduce memory usage when storing sessions.
46
- * @template T - The type of item detail in the picker
47
- * @param session - The session to compress
48
- * @returns A promise that resolves to the compressed session
49
- */
50
- async function compressPickerSession < T extends Detail > (
51
- session : PickerSession < T > ,
52
- ) : Promise < PickerSessionCompressed < T > > {
53
- const encoder = new TextEncoder ( ) ;
54
- // Convert Set to Array for JSON serialization
55
- const contextForSerialization = {
56
- ...session . context ,
57
- selection : Array . from ( session . context . selection ) ,
58
- } ;
59
- return {
60
- ...session ,
61
- context : await brotli . compress (
62
- encoder . encode ( JSON . stringify ( contextForSerialization ) ) ,
63
- ) ,
64
- } ;
65
- }
66
-
67
- /**
68
- * Decompresses a picker session by converting its binary context back to structured data.
69
- * @template T - The type of item detail in the picker
70
- * @param compressed - The compressed session to decompress
71
- * @returns A promise that resolves to the decompressed session
72
- */
73
- export async function decompressPickerSession < T extends Detail > (
74
- compressed : PickerSessionCompressed < T > ,
75
- ) : Promise < PickerSession < T > > {
76
- const decoder = new TextDecoder ( ) ;
77
- const decompressedContext = JSON . parse (
78
- decoder . decode ( await brotli . uncompress ( compressed . context ) ) ,
79
- ) ;
80
- // Convert selection array back to Set
81
- return {
82
- ...compressed ,
83
- context : {
84
- ...decompressedContext ,
85
- selection : new Set ( decompressedContext . selection ) ,
86
- } ,
87
- } ;
88
- }
89
-
90
30
/**
91
31
* Lists all stored picker sessions in reverse chronological order (newest first).
92
- * @returns A readonly array of compressed sessions
32
+ * @returns A readonly array of sessions
93
33
*/
94
- export function listPickerSessions ( ) : readonly PickerSessionCompressed <
95
- Detail
96
- > [ ] {
34
+ export function listPickerSessions ( ) : readonly PickerSession < Detail > [ ] {
97
35
return sessions . slice ( ) . reverse ( ) ; // Return a copy in reverse order
98
36
}
99
37
100
38
/**
101
39
* Saves a picker session to the in-memory storage.
102
- * The session is compressed before storage to reduce memory usage.
103
40
* If the storage exceeds MAX_SESSION_COUNT, the oldest session is removed.
104
41
* @template T - The type of item detail in the picker
105
42
* @param session - The session to save
106
- * @returns A promise that resolves when the session is saved
107
43
*/
108
- export async function savePickerSession < T extends Detail > (
44
+ export function savePickerSession < T extends Detail > (
109
45
session : PickerSession < T > ,
110
- ) : Promise < void > {
111
- const compressed = await compressPickerSession ( session ) ;
112
- sessions . push ( compressed ) ;
46
+ ) : void {
47
+ sessions . push ( session ) ;
113
48
if ( sessions . length > MAX_SESSION_COUNT ) {
114
49
sessions . shift ( ) ; // Keep only the last MAX_SESSION_COUNT sessions
115
50
}
@@ -130,29 +65,25 @@ export type LoadPickerSessionOptions = {
130
65
* @template T - The type of item detail in the picker
131
66
* @param indexFromLatest - The index from the latest session (0 = most recent, 1 = second most recent, etc.)
132
67
* @param options - Options to filter sessions
133
- * @returns A promise that resolves to the decompressed session, or undefined if not found
68
+ * @returns The session, or undefined if not found
134
69
* @example
135
70
* ```ts
136
71
* // Load the most recent session
137
- * const session1 = await loadPickerSession();
72
+ * const session1 = loadPickerSession();
138
73
*
139
74
* // Load the second most recent session
140
- * const session2 = await loadPickerSession({ number: 2 });
75
+ * const session2 = loadPickerSession({ number: 2 });
141
76
*
142
77
* // Load the most recent session with name "file"
143
- * const session3 = await loadPickerSession({ name: "file", number: 1 });
78
+ * const session3 = loadPickerSession({ name: "file", number: 1 });
144
79
* ```
145
80
*/
146
- export async function loadPickerSession < T extends Detail > (
81
+ export function loadPickerSession < T extends Detail > (
147
82
{ name, number : indexFromLatest } : LoadPickerSessionOptions = { } ,
148
- ) : Promise < PickerSession < T > | undefined > {
83
+ ) : PickerSession < T > | undefined {
149
84
const filteredSessions = name
150
85
? sessions . filter ( ( s ) => s . name === name )
151
86
: sessions ;
152
87
const index = filteredSessions . length - ( indexFromLatest ?? 1 ) ;
153
- const compressed = filteredSessions . at ( index ) ;
154
- if ( ! compressed ) {
155
- return undefined ;
156
- }
157
- return await decompressPickerSession ( compressed ) ;
88
+ return filteredSessions . at ( index ) as PickerSession < T > | undefined ;
158
89
}
0 commit comments