@@ -103,106 +103,90 @@ p3xr.ng.component('p3xrMainKey', {
103
103
104
104
this . loading = false
105
105
const loadKey = async ( options = { } ) => {
106
- $interval . cancel ( interval )
107
- let { withoutParent} = options
108
- if ( withoutParent === undefined ) {
109
- withoutParent = false
110
- }
111
-
112
-
113
- let hadError = undefined
106
+ const { withoutParent = false } = options ;
107
+
114
108
try {
115
-
116
- // it can throw an error, when we switch the database
117
- //p3xr.ui.overlay.show({
118
- // message: p3xr.strings.intention.getKey
119
- //})
120
- this . loading = true
121
-
122
- setTimeout ( ( ) => {
123
- $scope . $digest ( ) ;
124
- } )
125
-
126
- //const type = p3xr.state.keysInfo[$stateParams.key].type
127
- //console.warn('$stateParams.key', $stateParams.key)
109
+ this . loading = true ;
110
+
128
111
const response = await p3xrSocket . request ( {
129
112
action : 'key-get' ,
130
- payload : {
131
- key : $stateParams . key ,
132
- //type: type,
133
- }
134
- } )
135
- this . response = response
136
-
137
- //const type = response.type
113
+ payload : { key : $stateParams . key } ,
114
+ } ) ;
115
+
138
116
if ( response . ttl === - 2 ) {
139
- checkTtl ( )
117
+ checkTtl ( ) ;
140
118
return ;
141
119
}
142
- response . size = 0
143
-
144
- if ( response . type !== 'stream' ) {
145
- if ( typeof response . valueBuffer === 'object' && response . length > 0 ) {
146
- for ( let keys of Object . keys ( response . valueBuffer ) ) {
147
- response . size += response . valueBuffer [ keys ] . byteLength
148
- }
149
- } else if ( Array . isArray ( response . valueBuffer ) ) {
150
- for ( let i = 0 ; i < response . valueBuffer . length ; i ++ ) {
151
- response . size += response . valueBuffer [ i ] . byteLength
152
- }
153
- } else {
154
- response . size = response . valueBuffer . byteLength
155
- }
156
- } else {
157
- //console.log('response', response)
158
- function sumMaxByteLength ( arr ) {
159
- let total = 0 ;
120
+
121
+ const { type, valueBuffer } = response ;
122
+
123
+ switch ( type ) {
124
+ case 'string' :
125
+ response . value = new TextDecoder ( ) . decode ( valueBuffer ) ;
126
+ break ;
127
+
128
+ case 'list' :
129
+ case 'set' :
130
+ response . value = valueBuffer . map ( ( buf ) => new TextDecoder ( ) . decode ( buf ) ) ;
131
+ break ;
132
+
133
+ case 'hash' :
134
+ response . value = { } ;
135
+ Object . entries ( valueBuffer ) . forEach ( ( [ key , buf ] ) => {
136
+ response . value [ key ] = new TextDecoder ( ) . decode ( buf ) ;
137
+ } ) ;
138
+ break ;
139
+
140
+ case 'zset' :
141
+ response . value = [ ] ;
142
+ for ( let i = 0 ; i < valueBuffer . length ; i += 2 ) {
143
+ // Ensure proper decoding of score and value
144
+ const value = new TextDecoder ( ) . decode ( valueBuffer [ i ] ) ;
145
+ const score = new TextDecoder ( ) . decode ( valueBuffer [ i + 1 ] ) ;
160
146
161
- function processElement ( element ) {
162
- if ( ArrayBuffer . isView ( element ) || element instanceof ArrayBuffer ) {
163
- total += element . byteLength ;
164
- } else if ( Array . isArray ( element ) ) {
165
- element . forEach ( processElement ) ;
166
- }
147
+ response . value . push ( value ) ;
148
+ response . value . push ( score ) ;
149
+ }
150
+ break ;
151
+
152
+ case 'stream' :
153
+ function decodeStreamEntry ( entry ) {
154
+ // Recursively decode stream entry
155
+ return entry . map ( ( item ) => {
156
+ if ( Array . isArray ( item ) ) {
157
+ // If the item is an array, recursively process it
158
+ return decodeStreamEntry ( item ) ;
159
+ } else if ( ArrayBuffer . isView ( item ) || item instanceof ArrayBuffer ) {
160
+ // Decode binary data if it's an ArrayBuffer
161
+ return new TextDecoder ( ) . decode ( item ) ;
162
+ } else {
163
+ // Return strings or any other values as-is
164
+ return item ;
165
+ }
166
+ } ) ;
167
167
}
168
168
169
- arr . forEach ( processElement ) ;
170
- return total ;
171
- }
172
- response . size = sumMaxByteLength ( response . valueBuffer )
169
+ // Process the valueBuffer
170
+ response . value = valueBuffer . map ( ( entry ) => decodeStreamEntry ( entry ) ) ;
171
+ break ;
173
172
}
173
+
174
+ //console.log('response', response);
174
175
175
- if ( response . ttl > - 1 ) {
176
- wasExpiring = true
177
- }
178
- loadTtl ( )
179
- $scope . $digest ( )
180
-
176
+ this . response = response ;
177
+ loadTtl ( ) ;
181
178
} catch ( e ) {
182
- hadError = e
183
- console . error ( e )
179
+ console . error ( e ) ;
184
180
if ( ! p3xr . settings . handleConnectionIsClosed ( e , $rootScope ) ) {
185
- p3xrCommon . alert ( p3xr . strings . label . unableToLoadKey ( { key : $stateParams . key } ) )
186
- } else {
187
- p3xrCommon . alert ( e . message )
181
+ p3xrCommon . alert ( p3xr . strings . label . unableToLoadKey ( { key : $stateParams . key } ) ) ;
182
+ } else {
183
+ p3xrCommon . alert ( e . message ) ;
188
184
}
189
- //p3xrCommon.generalHandleError(e)
190
185
} finally {
191
- //p3xr.ui.overlay.hide()
192
- if ( hadError !== undefined ) {
193
- $state . go ( 'main.statistics' ) ;
194
- } else if ( ! withoutParent && $stateParams . resize !== null ) {
195
- $stateParams . resize ( )
196
- }
197
-
198
- $timeout ( ( ) => {
199
- this . loading = false
200
- $scope . $digest ( )
201
- } , p3xr . settings . debounce )
202
-
186
+ this . loading = false ;
203
187
}
204
-
205
- }
188
+ } ;
189
+
206
190
207
191
const generateHighlight = ( ) => {
208
192
$ ( '#p3xr-theme-styles-tree-key' ) . remove ( )
0 commit comments