-
Notifications
You must be signed in to change notification settings - Fork 291
/
index.ts
208 lines (190 loc) · 4.48 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { invoke } from "@tauri-apps/api/core";
interface ChangePayload<T> {
path: string;
key: string;
value: T | null;
}
/**
* A key-value store persisted by the backend layer.
*/
export class Store {
path: string;
constructor(path: string) {
this.path = path;
}
/**
* Inserts a key-value pair into the store.
*
* @param key
* @param value
* @returns
*/
async set(key: string, value: unknown): Promise<void> {
await invoke("plugin:store|set", {
path: this.path,
key,
value,
});
}
/**
* Returns the value for the given `key` or `null` the key does not exist.
*
* @param key
* @returns
*/
async get<T>(key: string): Promise<T | null> {
return await invoke("plugin:store|get", {
path: this.path,
key,
});
}
/**
* Returns `true` if the given `key` exists in the store.
*
* @param key
* @returns
*/
async has(key: string): Promise<boolean> {
return await invoke("plugin:store|has", {
path: this.path,
key,
});
}
/**
* Removes a key-value pair from the store.
*
* @param key
* @returns
*/
async delete(key: string): Promise<boolean> {
return await invoke("plugin:store|delete", {
path: this.path,
key,
});
}
/**
* Clears the store, removing all key-value pairs.
*
* Note: To clear the storage and reset it to it's `default` value, use `reset` instead.
* @returns
*/
async clear(): Promise<void> {
await invoke("plugin:store|clear", {
path: this.path,
});
}
/**
* Resets the store to it's `default` value.
*
* If no default value has been set, this method behaves identical to `clear`.
* @returns
*/
async reset(): Promise<void> {
await invoke("plugin:store|reset", {
path: this.path,
});
}
/**
* Returns a list of all key in the store.
*
* @returns
*/
async keys(): Promise<string[]> {
return await invoke("plugin:store|keys", {
path: this.path,
});
}
/**
* Returns a list of all values in the store.
*
* @returns
*/
async values<T>(): Promise<T[]> {
return await invoke("plugin:store|values", {
path: this.path,
});
}
/**
* Returns a list of all entries in the store.
*
* @returns
*/
async entries<T>(): Promise<Array<[key: string, value: T]>> {
return await invoke("plugin:store|entries", {
path: this.path,
});
}
/**
* Returns the number of key-value pairs in the store.
*
* @returns
*/
async length(): Promise<number> {
return await invoke("plugin:store|length", {
path: this.path,
});
}
/**
* Attempts to load the on-disk state at the stores `path` into memory.
*
* This method is useful if the on-disk state was edited by the user and you want to synchronize the changes.
*
* Note: This method does not emit change events.
* @returns
*/
async load(): Promise<void> {
await invoke("plugin:store|load", {
path: this.path,
});
}
/**
* Saves the store to disk at the stores `path`.
*
* As the store is only persisted to disk before the apps exit, changes might be lost in a crash.
* This method lets you persist the store to disk whenever you deem necessary.
* @returns
*/
async save(): Promise<void> {
await invoke("plugin:store|save", {
path: this.path,
});
}
/**
* Listen to changes on a store key.
* @param key
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
async onKeyChange<T>(
key: string,
cb: (value: T | null) => void,
): Promise<UnlistenFn> {
return await listen<ChangePayload<T>>("store://change", (event) => {
if (event.payload.path === this.path && event.payload.key === key) {
cb(event.payload.value);
}
});
}
/**
* Listen to changes on the store.
* @param cb
* @returns A promise resolving to a function to unlisten to the event.
*
* @since 2.0.0
*/
async onChange<T>(
cb: (key: string, value: T | null) => void,
): Promise<UnlistenFn> {
return await listen<ChangePayload<T>>("store://change", (event) => {
if (event.payload.path === this.path) {
cb(event.payload.key, event.payload.value);
}
});
}
}