@@ -102,7 +102,12 @@ function isAuthentifiedFunction(methodName) {
102
102
'stat' , 'batch' ] . includes ( methodName ) ;
103
103
}
104
104
105
- const HOOKS = [ 'onRead' , 'onWrite' ] ;
105
+ const HOOKS = {
106
+ ON_READ : 'onRead' ,
107
+ ON_WRITE : 'onWrite'
108
+ } ;
109
+
110
+ const applyPlugins = ( plugins , content ) => plugins . reduce ( ( data , action ) => action ( data ) , content ) ;
106
111
107
112
const connectors = Symbol ( 'connectors' ) ;
108
113
@@ -121,8 +126,8 @@ class Unifile {
121
126
*/
122
127
constructor ( ) {
123
128
this [ connectors ] = new Map ( ) ;
124
- this . plugins = HOOKS . reduce ( ( memo , hook ) => {
125
- memo [ hook ] = [ ] ;
129
+ this . plugins = Object . keys ( HOOKS ) . reduce ( ( memo , key ) => {
130
+ memo [ HOOKS [ key ] ] = [ ] ;
126
131
return memo ;
127
132
} , { } ) ;
128
133
}
@@ -144,7 +149,7 @@ class Unifile {
144
149
* @param {Plugin } plugin - A plugin with at least one hook implemented
145
150
*/
146
151
ext ( plugin ) {
147
- HOOKS . forEach ( ( hook ) => {
152
+ Object . keys ( HOOKS ) . map ( ( key ) => HOOKS [ key ] ) . forEach ( ( hook ) => {
148
153
if ( plugin . hasOwnProperty ( hook ) ) this . plugins [ hook ] . push ( plugin [ hook ] ) ;
149
154
} ) ;
150
155
}
@@ -267,10 +272,7 @@ class Unifile {
267
272
* @return {external:Promise<null> } an empty promise.
268
273
*/
269
274
writeFile ( session , connectorName , path , content ) {
270
- let input = content ;
271
- this . plugins . onWrite . forEach ( ( action ) => {
272
- input = action ( input ) ;
273
- } ) ;
275
+ const input = applyPlugins ( this . plugins [ HOOKS . ON_WRITE ] , content ) ;
274
276
return this . callMethod ( connectorName , session , 'writeFile' , path , input ) ;
275
277
}
276
278
@@ -294,13 +296,7 @@ class Unifile {
294
296
*/
295
297
readFile ( session , connectorName , path ) {
296
298
return this . callMethod ( connectorName , session , 'readFile' , path )
297
- . then ( ( content ) => {
298
- let output = content ;
299
- this . plugins . onRead . forEach ( ( action ) => {
300
- output = action ( output ) ;
301
- } ) ;
302
- return output ;
303
- } ) ;
299
+ . then ( ( content ) => applyPlugins ( this . plugins [ HOOKS . ON_READ ] , content ) ) ;
304
300
}
305
301
306
302
/**
0 commit comments