@talex-touch/utils 1.0.25 → 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/channel/index.ts +22 -1
- package/package.json +1 -1
- package/plugin/channel.ts +23 -0
- package/plugin/index.ts +7 -0
- package/plugin/sdk/examples/storage-onDidChange-example.js +47 -31
- package/plugin/sdk/storage.ts +29 -36
package/channel/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ export enum DataCode {
|
|
|
9
9
|
ERROR = 100
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
export type ChannelCallback = (data: StandardChannelData) => any
|
|
13
|
+
|
|
12
14
|
export interface ITouchChannel {
|
|
13
15
|
|
|
14
16
|
/**
|
|
@@ -18,7 +20,17 @@ export interface ITouchChannel {
|
|
|
18
20
|
* @param eventName {string} The name of event, must be unique in the channel {@link ChannelType}
|
|
19
21
|
* @param callback {Function} The callback function
|
|
20
22
|
*/
|
|
21
|
-
regChannel(type: ChannelType, eventName: string, callback:
|
|
23
|
+
regChannel(type: ChannelType, eventName: string, callback: ChannelCallback): () => void
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Unregister a channel
|
|
27
|
+
* @description Unregister a channel by type, event name and callback
|
|
28
|
+
* @param type {@link ChannelType} The type of channel
|
|
29
|
+
* @param eventName {string} The name of event
|
|
30
|
+
* @param callback {Function} The callback function to unregister
|
|
31
|
+
* @returns {boolean} Returns true if the channel was successfully unregistered, false otherwise
|
|
32
|
+
*/
|
|
33
|
+
unregChannel(type: ChannelType, eventName: string, callback: ChannelCallback): boolean
|
|
22
34
|
|
|
23
35
|
/**
|
|
24
36
|
* @deprecated Use sendMain instead
|
|
@@ -96,6 +108,15 @@ export interface ITouchClientChannel {
|
|
|
96
108
|
*/
|
|
97
109
|
regChannel(eventName: string, callback: (data: StandardChannelData) => any): () => void
|
|
98
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Unregister a channel
|
|
113
|
+
* @description Unregister a channel by event name and callback
|
|
114
|
+
* @param eventName {string} The name of event
|
|
115
|
+
* @param callback {Function} The callback function to unregister
|
|
116
|
+
* @returns {boolean} Returns true if the channel was successfully unregistered, false otherwise
|
|
117
|
+
*/
|
|
118
|
+
unRegChannel(eventName: string, callback: (data: StandardChannelData) => any): boolean
|
|
119
|
+
|
|
99
120
|
/**
|
|
100
121
|
* Send a message to a channel
|
|
101
122
|
* @param eventName {string} The name of event, must be unique in the channel {@link ChannelType}
|
package/package.json
CHANGED
package/plugin/channel.ts
CHANGED
|
@@ -164,6 +164,29 @@ class TouchChannel implements ITouchClientChannel {
|
|
|
164
164
|
};
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
unRegChannel(eventName: string, callback: Function): boolean {
|
|
168
|
+
const listeners = this.channelMap.get(eventName);
|
|
169
|
+
|
|
170
|
+
if (!listeners) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const index = listeners.indexOf(callback);
|
|
175
|
+
|
|
176
|
+
if (index === -1) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
listeners.splice(index, 1);
|
|
181
|
+
|
|
182
|
+
// If no listeners remain for this event, remove the event from the map
|
|
183
|
+
if (listeners.length === 0) {
|
|
184
|
+
this.channelMap.delete(eventName);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
|
|
167
190
|
send(eventName: string, arg: any): Promise<any> {
|
|
168
191
|
const uniqueId = `${new Date().getTime()}#${eventName}@${Math.random().toString(
|
|
169
192
|
12
|
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
|
/**
|
|
@@ -7,12 +7,12 @@
|
|
|
7
7
|
// 获取SDK实例
|
|
8
8
|
const sdk = window.$touchSDK
|
|
9
9
|
|
|
10
|
-
// 示例1:
|
|
10
|
+
// 示例1: 监听特定配置文件的变化
|
|
11
11
|
function setupConfigListener() {
|
|
12
|
-
console.log('[Storage] Setting up listener for
|
|
12
|
+
console.log('[Storage] Setting up listener for config.json changes')
|
|
13
13
|
|
|
14
|
-
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
15
|
-
console.log('[Storage]
|
|
14
|
+
const unsubscribe = sdk.storage.onDidChange('config.json', (newConfig) => {
|
|
15
|
+
console.log('[Storage] config.json changed:')
|
|
16
16
|
console.log(' New config:', newConfig)
|
|
17
17
|
|
|
18
18
|
// 处理特定键的变化
|
|
@@ -25,27 +25,43 @@ function setupConfigListener() {
|
|
|
25
25
|
return unsubscribe
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
// 示例2:
|
|
29
|
-
function
|
|
30
|
-
console.log('[Storage] Setting up
|
|
28
|
+
// 示例2: 监听多个配置文件的变化
|
|
29
|
+
function setupMultipleFileListeners() {
|
|
30
|
+
console.log('[Storage] Setting up multiple file listeners')
|
|
31
31
|
|
|
32
|
-
const
|
|
33
|
-
console.log('[Storage] Config changed:')
|
|
34
|
-
console.log(' New config:', newConfig)
|
|
32
|
+
const unsubscribers = []
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
// 监听 config.json
|
|
35
|
+
const configUnsubscribe = sdk.storage.onDidChange('config.json', (newConfig) => {
|
|
36
|
+
console.log('[Storage] config.json changed:', newConfig)
|
|
37
37
|
if (newConfig.user_preference) {
|
|
38
38
|
handleUserPreferenceChange(newConfig.user_preference)
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
})
|
|
41
|
+
unsubscribers.push(configUnsubscribe)
|
|
42
|
+
|
|
43
|
+
// 监听 settings.json
|
|
44
|
+
const settingsUnsubscribe = sdk.storage.onDidChange('settings.json', (newSettings) => {
|
|
45
|
+
console.log('[Storage] settings.json changed:', newSettings)
|
|
46
|
+
if (newSettings) {
|
|
47
|
+
handleSettingsChange(newSettings)
|
|
42
48
|
}
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
})
|
|
50
|
+
unsubscribers.push(settingsUnsubscribe)
|
|
51
|
+
|
|
52
|
+
// 监听 cache.json
|
|
53
|
+
const cacheUnsubscribe = sdk.storage.onDidChange('cache.json', (newCache) => {
|
|
54
|
+
console.log('[Storage] cache.json changed:', newCache)
|
|
55
|
+
if (newCache) {
|
|
56
|
+
handleCacheChange(newCache)
|
|
45
57
|
}
|
|
46
58
|
})
|
|
59
|
+
unsubscribers.push(cacheUnsubscribe)
|
|
47
60
|
|
|
48
|
-
|
|
61
|
+
// 返回取消所有监听的函数
|
|
62
|
+
return () => {
|
|
63
|
+
unsubscribers.forEach(unsubscribe => unsubscribe())
|
|
64
|
+
}
|
|
49
65
|
}
|
|
50
66
|
|
|
51
67
|
// 示例3: 动态监听
|
|
@@ -53,16 +69,16 @@ function setupDynamicListener() {
|
|
|
53
69
|
let currentListener = null
|
|
54
70
|
|
|
55
71
|
// 开始监听
|
|
56
|
-
function startListening() {
|
|
57
|
-
console.log(
|
|
72
|
+
function startListening(fileName) {
|
|
73
|
+
console.log(`[Storage] Starting to listen for ${fileName} changes`)
|
|
58
74
|
|
|
59
75
|
// 如果已有监听器,先取消
|
|
60
76
|
if (currentListener) {
|
|
61
77
|
currentListener()
|
|
62
78
|
}
|
|
63
79
|
|
|
64
|
-
currentListener = sdk.storage.onDidChange((newConfig) => {
|
|
65
|
-
console.log(
|
|
80
|
+
currentListener = sdk.storage.onDidChange(fileName, (newConfig) => {
|
|
81
|
+
console.log(`[Storage] Dynamic listener for ${fileName} changes:`)
|
|
66
82
|
console.log(' New config:', newConfig)
|
|
67
83
|
})
|
|
68
84
|
}
|
|
@@ -81,9 +97,9 @@ function setupDynamicListener() {
|
|
|
81
97
|
|
|
82
98
|
// 示例4: 条件监听
|
|
83
99
|
function setupConditionalListener() {
|
|
84
|
-
console.log('[Storage] Setting up conditional listener')
|
|
100
|
+
console.log('[Storage] Setting up conditional listener for user preferences')
|
|
85
101
|
|
|
86
|
-
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
102
|
+
const unsubscribe = sdk.storage.onDidChange('config.json', (newConfig) => {
|
|
87
103
|
// 处理用户偏好变化
|
|
88
104
|
if (newConfig.user_preference) {
|
|
89
105
|
const pref = newConfig.user_preference
|
|
@@ -109,17 +125,17 @@ function setupConditionalListener() {
|
|
|
109
125
|
function setupBatchOperationListener() {
|
|
110
126
|
console.log('[Storage] Setting up batch operation listener')
|
|
111
127
|
|
|
112
|
-
const unsubscribe = sdk.storage.onDidChange((newConfig) => {
|
|
113
|
-
console.log('[Storage]
|
|
114
|
-
console.log(' New config
|
|
128
|
+
const unsubscribe = sdk.storage.onDidChange('batch_data.json', (newConfig) => {
|
|
129
|
+
console.log('[Storage] batch_data.json changed:')
|
|
130
|
+
console.log(' New config:', newConfig)
|
|
115
131
|
|
|
116
132
|
// 处理批量数据变化
|
|
117
|
-
if (newConfig
|
|
133
|
+
if (newConfig) {
|
|
118
134
|
console.log('[Storage] Processing batch data changes')
|
|
119
|
-
console.log(' Batch data count:', Object.keys(newConfig
|
|
135
|
+
console.log(' Batch data count:', Object.keys(newConfig).length)
|
|
120
136
|
|
|
121
137
|
// 应用变化
|
|
122
|
-
applyChanges(newConfig
|
|
138
|
+
applyChanges(newConfig)
|
|
123
139
|
}
|
|
124
140
|
})
|
|
125
141
|
|
|
@@ -166,7 +182,7 @@ function applyChanges(changes) {
|
|
|
166
182
|
// 导出函数供外部调用
|
|
167
183
|
window.storageOnDidChangeExample = {
|
|
168
184
|
setupConfigListener,
|
|
169
|
-
|
|
185
|
+
setupMultipleFileListeners,
|
|
170
186
|
setupDynamicListener,
|
|
171
187
|
setupConditionalListener,
|
|
172
188
|
setupBatchOperationListener
|
|
@@ -179,8 +195,8 @@ if (typeof window !== 'undefined' && window.location.hostname === 'localhost') {
|
|
|
179
195
|
// 设置配置监听
|
|
180
196
|
const unsubscribe1 = setupConfigListener()
|
|
181
197
|
|
|
182
|
-
//
|
|
183
|
-
const unsubscribeAll =
|
|
198
|
+
// 设置多文件监听
|
|
199
|
+
const unsubscribeAll = setupMultipleFileListeners()
|
|
184
200
|
|
|
185
201
|
// 设置动态监听
|
|
186
202
|
const { startListening, stopListening } = setupDynamicListener()
|
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,66 +17,60 @@ 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
|
}
|
|
76
69
|
|
|
77
|
-
channel.
|
|
70
|
+
channel.regChannel('plugin:storage:update', listener)
|
|
78
71
|
|
|
79
72
|
return () => {
|
|
80
|
-
channel.
|
|
73
|
+
channel.unRegChannel('plugin:storage:update', listener)
|
|
81
74
|
}
|
|
82
75
|
}
|
|
83
76
|
}
|