8
8
const nextTick = require ( "process" ) . nextTick ;
9
9
10
10
/** @typedef {import("./Resolver").FileSystem } FileSystem */
11
+ /** @typedef {import("./Resolver").PathLike } PathLike */
12
+ /** @typedef {import("./Resolver").PathOrFileDescriptor } PathOrFileDescriptor */
11
13
/** @typedef {import("./Resolver").SyncFileSystem } SyncFileSystem */
12
- /** @typedef {any } BaseFileSystem */
14
+ /** @typedef {FileSystem & SyncFileSystem } BaseFileSystem */
13
15
14
16
/**
15
17
* @template T
@@ -58,8 +60,8 @@ const runCallbacks = (callbacks, err, result) => {
58
60
59
61
class OperationMergerBackend {
60
62
/**
61
- * @param {function } provider async method in filesystem
62
- * @param {function } syncProvider sync method in filesystem
63
+ * @param {Function | undefined } provider async method in filesystem
64
+ * @param {Function | undefined } syncProvider sync method in filesystem
63
65
* @param {BaseFileSystem } providerContext call context for the provider methods
64
66
*/
65
67
constructor ( provider , syncProvider , providerContext ) {
@@ -81,7 +83,7 @@ class OperationMergerBackend {
81
83
options = undefined ;
82
84
}
83
85
if ( options ) {
84
- return this . _provider . call (
86
+ return /** @type { Function } */ ( this . _provider ) . call (
85
87
this . _providerContext ,
86
88
path ,
87
89
options ,
@@ -98,7 +100,8 @@ class OperationMergerBackend {
98
100
return ;
99
101
}
100
102
this . _activeAsyncOperations . set ( path , ( callbacks = [ callback ] ) ) ;
101
- provider (
103
+ /** @type {Function } */
104
+ ( provider ) (
102
105
path ,
103
106
/**
104
107
* @param {Error } err error
@@ -118,7 +121,11 @@ class OperationMergerBackend {
118
121
* @returns {any } result
119
122
*/
120
123
( path , options ) => {
121
- return this . _syncProvider . call ( this . _providerContext , path , options ) ;
124
+ return /** @type {Function } */ ( this . _syncProvider ) . call (
125
+ this . _providerContext ,
126
+ path ,
127
+ options
128
+ ) ;
122
129
}
123
130
: null ;
124
131
}
@@ -151,11 +158,19 @@ const STORAGE_MODE_IDLE = 0;
151
158
const STORAGE_MODE_SYNC = 1 ;
152
159
const STORAGE_MODE_ASYNC = 2 ;
153
160
161
+ /**
162
+ * @callback Provide
163
+ * @param {PathLike | PathOrFileDescriptor } path path
164
+ * @param {any } options options
165
+ * @param {FileSystemCallback<any> } callback callback
166
+ * @returns {void }
167
+ */
168
+
154
169
class CacheBackend {
155
170
/**
156
171
* @param {number } duration max cache duration of items
157
- * @param {function } provider async method
158
- * @param {function } syncProvider sync method
172
+ * @param {function | undefined } provider async method
173
+ * @param {function | undefined } syncProvider sync method
159
174
* @param {BaseFileSystem } providerContext call context for the provider methods
160
175
*/
161
176
constructor ( duration , provider , syncProvider , providerContext ) {
@@ -188,7 +203,7 @@ class CacheBackend {
188
203
}
189
204
190
205
/**
191
- * @param {string } path path
206
+ * @param {PathLike | PathOrFileDescriptor } path path
192
207
* @param {any } options options
193
208
* @param {FileSystemCallback<any> } callback callback
194
209
* @returns {void }
@@ -203,7 +218,7 @@ class CacheBackend {
203
218
return ;
204
219
}
205
220
if ( options ) {
206
- return this . _provider . call (
221
+ return /** @type { Function } */ ( this . _provider ) . call (
207
222
this . _providerContext ,
208
223
path ,
209
224
options ,
@@ -232,7 +247,8 @@ class CacheBackend {
232
247
this . _activeAsyncOperations . set ( path , ( callbacks = [ callback ] ) ) ;
233
248
234
249
// Run the operation
235
- this . _provider . call (
250
+ /** @type {Function } */
251
+ ( this . _provider ) . call (
236
252
this . _providerContext ,
237
253
path ,
238
254
/**
@@ -256,7 +272,7 @@ class CacheBackend {
256
272
}
257
273
258
274
/**
259
- * @param {string } path path
275
+ * @param {PathLike | PathOrFileDescriptor } path path
260
276
* @param {any } options options
261
277
* @returns {any } result
262
278
*/
@@ -265,7 +281,11 @@ class CacheBackend {
265
281
throw new TypeError ( "path must be a string" ) ;
266
282
}
267
283
if ( options ) {
268
- return this . _syncProvider . call ( this . _providerContext , path , options ) ;
284
+ return /** @type {Function } */ ( this . _syncProvider ) . call (
285
+ this . _providerContext ,
286
+ path ,
287
+ options
288
+ ) ;
269
289
}
270
290
271
291
// In sync mode we may have to decay some cache items
@@ -289,7 +309,10 @@ class CacheBackend {
289
309
// When in idle mode, we will enter sync mode
290
310
let result ;
291
311
try {
292
- result = this . _syncProvider . call ( this . _providerContext , path ) ;
312
+ result = /** @type {Function } */ ( this . _syncProvider ) . call (
313
+ this . _providerContext ,
314
+ path
315
+ ) ;
293
316
} catch ( err ) {
294
317
this . _storeResult ( path , /** @type {Error } */ ( err ) , undefined ) ;
295
318
this . _enterSyncModeWhenIdle ( ) ;
@@ -449,9 +472,9 @@ class CacheBackend {
449
472
* @template {function} AsyncProvider
450
473
* @template FileSystem
451
474
* @param {number } duration duration in ms files are cached
452
- * @param {Provider } provider provider
453
- * @param {AsyncProvider } syncProvider sync provider
454
- * @param {FileSystem } providerContext provider context
475
+ * @param {Provider | undefined } provider provider
476
+ * @param {AsyncProvider | undefined } syncProvider sync provider
477
+ * @param {BaseFileSystem } providerContext provider context
455
478
* @returns {OperationMergerBackend | CacheBackend } backend
456
479
*/
457
480
const createBackend = ( duration , provider , syncProvider , providerContext ) => {
0 commit comments