@talex-touch/utils 1.0.26 → 1.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/plugin/index.ts +7 -0
- package/plugin/sdk/storage.ts +27 -34
package/package.json
CHANGED
package/plugin/index.ts
CHANGED
|
@@ -225,6 +225,13 @@ export interface IFeatureLifeCycle {
|
|
|
225
225
|
/** Activation data if shouldActivate is true */
|
|
226
226
|
activation?: any
|
|
227
227
|
} | void>
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Called when plugin storage changes.
|
|
231
|
+
* @param key - The storage key that changed
|
|
232
|
+
* @param value - The new value (undefined if key was removed)
|
|
233
|
+
*/
|
|
234
|
+
onStorageChange?(key: string, value: any): void
|
|
228
235
|
}
|
|
229
236
|
|
|
230
237
|
/**
|
package/plugin/sdk/storage.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Get the storage for the current plugin.
|
|
3
|
-
* It provides
|
|
4
|
-
*
|
|
5
|
-
* Each plugin has its own separate storage file.
|
|
3
|
+
* It provides file-based storage that is persisted across application launches.
|
|
4
|
+
* Each plugin can have multiple storage files in its own directory.
|
|
6
5
|
*
|
|
7
6
|
* @returns An object with methods to interact with the storage.
|
|
8
7
|
*/
|
|
@@ -18,58 +17,52 @@ export function usePluginStorage() {
|
|
|
18
17
|
|
|
19
18
|
return {
|
|
20
19
|
/**
|
|
21
|
-
* Retrieves
|
|
22
|
-
* @param
|
|
23
|
-
* @returns A promise that resolves with the
|
|
20
|
+
* Retrieves the content of a storage file.
|
|
21
|
+
* @param fileName The name of the file to retrieve.
|
|
22
|
+
* @returns A promise that resolves with the file content, or null if the file does not exist.
|
|
24
23
|
*/
|
|
25
|
-
getItem: async (
|
|
26
|
-
return channel.send('plugin:storage:get-
|
|
24
|
+
getItem: async (fileName: string): Promise<any> => {
|
|
25
|
+
return channel.send('plugin:storage:get-file', { pluginName, fileName })
|
|
27
26
|
},
|
|
28
27
|
|
|
29
28
|
/**
|
|
30
|
-
* Stores
|
|
31
|
-
* @param
|
|
32
|
-
* @param
|
|
33
|
-
* @returns A promise that resolves when the
|
|
29
|
+
* Stores content to a storage file.
|
|
30
|
+
* @param fileName The name of the file to store.
|
|
31
|
+
* @param content The content to store in the file.
|
|
32
|
+
* @returns A promise that resolves when the file has been stored.
|
|
34
33
|
*/
|
|
35
|
-
setItem: async (
|
|
36
|
-
return channel.send('plugin:storage:
|
|
34
|
+
setItem: async (fileName: string, content: any): Promise<{ success: boolean, error?: string }> => {
|
|
35
|
+
return channel.send('plugin:storage:save-file', { pluginName, fileName, content: JSON.parse(JSON.stringify(content)) })
|
|
37
36
|
},
|
|
38
37
|
|
|
39
38
|
/**
|
|
40
|
-
* Removes
|
|
41
|
-
* @param
|
|
42
|
-
* @returns A promise that resolves when the
|
|
39
|
+
* Removes a storage file.
|
|
40
|
+
* @param fileName The name of the file to remove.
|
|
41
|
+
* @returns A promise that resolves when the file has been removed.
|
|
43
42
|
*/
|
|
44
|
-
removeItem: async (
|
|
45
|
-
return channel.send('plugin:storage:remove-
|
|
43
|
+
removeItem: async (fileName: string): Promise<{ success: boolean, error?: string }> => {
|
|
44
|
+
return channel.send('plugin:storage:remove-file', { pluginName, fileName })
|
|
46
45
|
},
|
|
47
46
|
|
|
48
47
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @returns A promise that resolves
|
|
48
|
+
* Lists all storage files for the current plugin.
|
|
49
|
+
* @returns A promise that resolves with an array of file names.
|
|
51
50
|
*/
|
|
52
|
-
|
|
53
|
-
return channel.send('plugin:storage:
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Retrieves all items from the storage for the current plugin.
|
|
58
|
-
* @returns A promise that resolves with an object containing all items.
|
|
59
|
-
*/
|
|
60
|
-
getAllItems: async (): Promise<Record<string, any>> => {
|
|
61
|
-
return channel.send('plugin:storage:get-all', { pluginName })
|
|
51
|
+
getAllItems: async (): Promise<string[]> => {
|
|
52
|
+
return channel.send('plugin:storage:list-files', { pluginName })
|
|
62
53
|
},
|
|
63
54
|
|
|
64
55
|
/**
|
|
65
56
|
* Listens for changes to the storage.
|
|
66
57
|
* When `clear()` is called, the key will be `__clear__`.
|
|
58
|
+
* @param fileName The file name to listen for changes
|
|
67
59
|
* @param callback The function to call when the storage changes for the current plugin.
|
|
68
60
|
* @returns A function to unsubscribe from the listener.
|
|
69
61
|
*/
|
|
70
|
-
onDidChange: (
|
|
71
|
-
const listener = (data: { name: string, key?: string }) => {
|
|
72
|
-
if (data.name === pluginName
|
|
62
|
+
onDidChange: (fileName: string, callback: (newConfig: any) => void) => {
|
|
63
|
+
const listener = (data: { name: string, fileName?: string, key?: string }) => {
|
|
64
|
+
if (data.name === pluginName &&
|
|
65
|
+
(data.fileName === fileName || data.fileName === undefined)) {
|
|
73
66
|
callback(data)
|
|
74
67
|
}
|
|
75
68
|
}
|