@talex-touch/utils 1.0.25 → 1.0.26
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
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
|
|
@@ -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
|
@@ -74,10 +74,10 @@ export function usePluginStorage() {
|
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
channel.
|
|
77
|
+
channel.regChannel('plugin:storage:update', listener)
|
|
78
78
|
|
|
79
79
|
return () => {
|
|
80
|
-
channel.
|
|
80
|
+
channel.unRegChannel('plugin:storage:update', listener)
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
}
|