-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD4C.ts
571 lines (516 loc) · 15.6 KB
/
D4C.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import { Queue } from './Queue'
type Task = {
unlock: (value?: unknown) => void
preError?: Error
inheritPreErr?: boolean
}
type TaskQueue = {
queue: Queue<Task>
/** isRunning will be removed since runningTask can cover its effect */
isRunning: boolean
concurrency: number
runningTask: number
}
type UnwrapPromise<T> = T extends Promise<infer U>
? U
: T extends (...args: any) => Promise<infer U>
? U
: T extends (...args: any) => infer U
? U
: T
type QueueTag = string | symbol
type TaskQueuesType = Map<string | symbol, TaskQueue>
type IAnyFn = (...args: any[]) => Promise<any> | any
type MethodDecoratorType = (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) => void
export enum ErrMsg {
InstanceInvalidTag = 'instanceInvalidTag: it should be string/symbol/undefined',
InvalidDecoratorOption = 'not valid option when using decorators',
InvalidQueueConcurrency = 'invalidQueueConcurrency',
InvalidQueueTag = 'invalidQueueTag',
InvalidClassDecoratorParameter = 'invalidClassDecoratorParameter',
TwoDecoratorsIncompatible = 'TwoDecoratorsInCompatible',
ClassAndMethodDecoratorsIncompatible = 'ClassAndMethodDecoratorsIncompatible',
MissingThisDueBindIssue = 'missingThisDueBindIssue',
QueueIsFull = 'QueueIsFull',
}
const queueSymbol = Symbol('d4cQueues') // subQueue system
const concurrentSymbol = Symbol('concurrent') // record the concurrency of each instance method decorator's tag
const isConcurrentSymbol = Symbol('isConcurrent') // record isConcurrent of each instance method decorator's tag
const defaultTag = Symbol('D4C')
const DEFAULT_CONCURRENCY = 1
export class PreviousTaskError extends Error {
constructor(message) {
super(message)
this.name = 'PreviousError'
}
}
function checkIfClassConcurrencyApplyOnSynchronizedMethod(
target,
usedTag: string | symbol
) {
// true means isConcurrent, false means sync, undefined means no static method decorator on this tag
if (target[isConcurrentSymbol][usedTag] === undefined) {
return
} else if (target[isConcurrentSymbol][usedTag] === false) {
throw new Error(ErrMsg.ClassAndMethodDecoratorsIncompatible)
}
}
/**
* Class decorator to setup concurrency for queues
* @param queuesParam a array of each queue parameter
*/
export function QConcurrency(
queuesParam: Array<{
limit: number
tag?: string | symbol
isStatic?: boolean
}>
): ClassDecorator {
if (!Array.isArray(queuesParam)) {
throw new Error(ErrMsg.InvalidClassDecoratorParameter)
}
/** target is constructor */
return (target) => {
queuesParam.forEach((queueParam) => {
if (!queueParam) {
return
}
const { tag, limit, isStatic } = queueParam
if (
!checkTag(tag) ||
typeof limit !== 'number' ||
(isStatic !== undefined && typeof isStatic !== 'boolean')
) {
throw new Error(ErrMsg.InvalidClassDecoratorParameter)
}
const usedTag = tag ?? defaultTag
/** TODO: refactor below as they are use similar code */
if (isStatic) {
// check if at least one static method is using @synchronized/@concurrent
if (!target[queueSymbol]) {
return
}
checkIfClassConcurrencyApplyOnSynchronizedMethod(target, usedTag)
/** inject concurrency info for each tag in static method case */
if (target[concurrentSymbol]?.[usedTag]) {
target[concurrentSymbol][usedTag] = limit
}
} else {
// check if at least one instance method is using @synchronized/@concurrent
if (target.prototype[queueSymbol] !== null) {
return
}
checkIfClassConcurrencyApplyOnSynchronizedMethod(
target.prototype,
usedTag
)
/** inject concurrency info for each tag in instance method case */
if (target.prototype[concurrentSymbol]?.[usedTag]) {
target.prototype[concurrentSymbol][usedTag] = limit
}
}
})
}
}
function checkTag(tag) {
if (tag === undefined || typeof tag === 'string' || typeof tag === 'symbol') {
return true
}
return false
}
function checkIfTwoDecoratorsHaveSameConcurrentValue(
target,
tag: QueueTag,
isConcurrent: boolean
) {
// init
if (!target[isConcurrentSymbol]) {
target[isConcurrentSymbol] = {}
}
// check if two decorators for same queue have same isConcurrency value
if (target[isConcurrentSymbol][tag] === undefined) {
target[isConcurrentSymbol][tag] = isConcurrent
} else if (target[isConcurrentSymbol][tag] !== isConcurrent) {
throw new Error(ErrMsg.TwoDecoratorsIncompatible)
}
/** set default concurrency is infinity for @concurrent on instance/static methods*/
if (isConcurrent) {
if (!target[concurrentSymbol]) {
target[concurrentSymbol] = {}
}
target[concurrentSymbol][tag] = Infinity
}
}
function injectQueue(
constructorOrPrototype,
tag: string | symbol,
isConcurrent: boolean
) {
if (constructorOrPrototype.prototype) {
// constructor, means static method
if (!constructorOrPrototype[queueSymbol]) {
constructorOrPrototype[queueSymbol] = new Map<
string | symbol,
TaskQueue
>()
}
} else {
// prototype, means instance method
if (constructorOrPrototype[queueSymbol] !== null) {
constructorOrPrototype[queueSymbol] = null
}
}
checkIfTwoDecoratorsHaveSameConcurrentValue(
constructorOrPrototype,
tag,
isConcurrent
)
}
/** if class has a static member call inheritPreErr, even no using parentheses,
* targetOrOption will have targetOrOption property but its type is function */
function checkIfDecoratorOptionObject(obj: any): boolean {
/** still count valid argument, e.g. @synchronized(null) */
if (obj === undefined || obj === null) {
return true
}
/**
* hasOwnProperty should be false since it is a literal object
*/
if (
typeof obj === 'object' &&
//eslint-disable-next-line
!obj.hasOwnProperty('constructor') &&
(typeof obj.inheritPreErr === 'boolean' ||
obj.inheritPreErr === undefined) &&
(typeof obj.noBlockCurr === 'boolean' || obj.noBlockCurr === undefined) &&
(typeof obj.dropWhenReachLimit === 'boolean' ||
obj.dropWhenReachLimit === undefined) &&
checkTag(obj.tag)
) {
return true
}
return false
}
/**
* Static and instance method decorator. Default concurrency = Infinity.
* usage example:
* ```typescript
* @concurrent
* async fetchData() {}
* // or
* @concurrent({ tag: 'world', inheritPreErr: true, noBlockCurr: true })
* static async fetchData(url: string) {}
* ```
* */
export function concurrent(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
): void
export function concurrent(option?: {
tag?: string | symbol
inheritPreErr?: boolean
noBlockCurr?: boolean
dropWhenReachLimit?: boolean
}): MethodDecoratorType
export function concurrent(
targetOrOption?: any,
propertyKey?: string,
descriptor?: PropertyDescriptor
): void | MethodDecoratorType {
return _methodDecorator(targetOrOption, propertyKey, descriptor, true)
}
/**
* Static and instance method decorator. Default concurrency = 1 for lock.
* usage example:
* ```typescript
* @synchronize
* async connect() {}
* // or
* @synchronized({ tag: 'world', inheritPreErr: true, noBlockCurr: true })
* static async staticMethod(text: string) {}
* ```
* */
export function synchronized(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
): void
export function synchronized(option?: {
tag?: string | symbol
inheritPreErr?: boolean
noBlockCurr?: boolean
}): MethodDecoratorType
export function synchronized(
targetOrOption?: any,
propertyKey?: string,
descriptor?: PropertyDescriptor
): void | MethodDecoratorType {
return _methodDecorator(targetOrOption, propertyKey, descriptor, false)
}
function _methodDecorator(
targetOrOption: any,
propertyKey: string,
descriptor: PropertyDescriptor,
isConcurrent: boolean
): void | MethodDecoratorType {
if (checkIfDecoratorOptionObject(targetOrOption)) {
/** parentheses case containing option (=targetOrOption) */
return function (
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
injectQueue(target, targetOrOption?.tag ?? defaultTag, isConcurrent)
const originalMethod = descriptor.value
const newFunc = _q(null, originalMethod, targetOrOption)
descriptor.value = newFunc
}
} else {
/** no parentheses case */
const type = typeof targetOrOption
/**
* static method decorator case: target type is constructor function. use target.prototype
* method decorator case: target is a prototype object, not literally object. use target
*/
if (
(type === 'function' || targetOrOption.hasOwnProperty('constructor')) && // eslint-disable-line
typeof propertyKey === 'string' &&
typeof descriptor === 'object' &&
typeof descriptor.value === 'function'
) {
injectQueue(targetOrOption, defaultTag, isConcurrent)
const originalMethod = descriptor.value
const newFunc = _q(null, originalMethod, {})
descriptor.value = newFunc
} else {
throw new Error(ErrMsg.InvalidDecoratorOption)
}
}
}
function _q<T extends IAnyFn>(
d4cObj: { queues: TaskQueuesType; defaultConcurrency: number },
func: T,
option?: {
tag?: QueueTag
inheritPreErr?: boolean
noBlockCurr?: boolean
dropWhenReachLimit?: boolean
}
): (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>> {
return async function (...args: any[]): Promise<any> {
/** Detect tag */
let tag: QueueTag
if (option?.tag !== undefined) {
tag = option.tag
} else {
tag = defaultTag
}
let decoratorConcurrencyLimit: number
/** Assign queues */
let taskQueue: TaskQueue
let currTaskQueues: TaskQueuesType
if (d4cObj) {
/** D4C instance case */
currTaskQueues = d4cObj.queues
} else if (this && (this[queueSymbol] || this[queueSymbol] === null)) {
if (this[queueSymbol] === null) {
/** instance method decorator first time case, using injected queues in user defined objects*/
this[queueSymbol] = new Map<string | symbol, TaskQueue>()
}
currTaskQueues = this[queueSymbol]
decoratorConcurrencyLimit = this[concurrentSymbol]?.[tag]
} else {
throw new Error(ErrMsg.MissingThisDueBindIssue)
}
/** Get sub-queue */
taskQueue = currTaskQueues.get(tag)
if (!taskQueue) {
taskQueue = {
queue: new Queue<Task>(),
isRunning: false,
runningTask: 0,
/** D4C instance usage ?? (Decorator usage - specified limit ?? decorator - unspecified case) */
concurrency:
d4cObj?.defaultConcurrency ??
decoratorConcurrencyLimit ??
DEFAULT_CONCURRENCY,
}
currTaskQueues.set(tag, taskQueue)
}
/** Detect if the queue is running or not, use promise to wait it if it is running */
let result
let err: Error
let task: Task
if (taskQueue.runningTask === taskQueue.concurrency) {
if (!option?.dropWhenReachLimit) {
const promise = new Promise(function (resolve) {
task = {
unlock: resolve,
preError: null,
inheritPreErr: option?.inheritPreErr,
}
})
taskQueue.queue.push(task)
await promise
taskQueue.runningTask += 1
} else {
// drop this time, throttle mechanism
throw new Error(ErrMsg.QueueIsFull)
}
} else if (option?.noBlockCurr) {
taskQueue.runningTask += 1
await Promise.resolve()
} else {
taskQueue.runningTask += 1
}
/** Run the task */
if (task?.preError) {
err = new PreviousTaskError(task.preError.message ?? task.preError)
} else {
try {
/** this will be constructor function for static method case */
const value = func.apply(this, args)
/** Detect if it is a async/promise function or not */
if (value && typeof value.then === 'function') {
result = await value
} else {
result = value
}
} catch (error) {
err = error
}
}
taskQueue.runningTask -= 1
/** After the task is executed, check the following tasks */
if (taskQueue.queue.length > 0) {
const nextTask: Task = taskQueue.queue.shift()
/** Pass error to next task */
if (err && nextTask.inheritPreErr) {
nextTask.preError = err
}
nextTask.unlock()
}
if (err) {
throw err
}
return result
} as (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>>
}
export class D4C {
private queues: TaskQueuesType
private defaultConcurrency = DEFAULT_CONCURRENCY
/**
* Default concurrency is 1. Omitting tag means it is for default queue.
* If you specify concurrency limit for some tag queue,
* this instance will not use that tag queue by default.
*/
constructor(
queuesParam?: Array<{
concurrency: { tag?: string | symbol; limit?: number }
}>
) {
this.queues = new Map<string | symbol, TaskQueue>()
if (Array.isArray(queuesParam)) {
queuesParam.forEach((option) => {
if (option?.concurrency?.limit > 0) {
this._setConcurrency(option.concurrency)
}
})
}
}
/**
* @param option tag is optional for specific queue. omitting is for default queue
* @param option.limit is limit of concurrency and should be >= 1
*/
setConcurrency(
queuesParam: Array<{
tag?: string | symbol
limit: number
}>
) {
if (Array.isArray(queuesParam)) {
queuesParam.forEach((option) => {
this._setConcurrency(option)
})
}
}
private _setConcurrency(concurrency?: {
tag?: string | symbol
limit?: number
}) {
if (
concurrency?.limit === undefined ||
typeof concurrency.limit !== 'number'
) {
throw new Error(ErrMsg.InvalidQueueConcurrency)
}
const { tag, limit } = concurrency
if (limit < 1) {
throw new Error(ErrMsg.InvalidQueueConcurrency)
}
if (!checkTag(tag)) {
throw new Error(ErrMsg.InvalidQueueTag)
}
// TODO: refactor this, _q has similar code */
let usedTag: string | symbol
if (tag !== undefined) {
usedTag = tag
} else {
usedTag = defaultTag
}
// TODO: refactor, other places have similar code
let taskQueue = this.queues.get(usedTag)
if (!taskQueue) {
taskQueue = {
queue: new Queue<Task>(),
isRunning: false,
runningTask: 0,
concurrency: limit,
}
} else {
taskQueue.concurrency = limit
}
this.queues.set(usedTag, taskQueue)
}
/** It wraps original function for queue ready and executes it*/
apply<T extends IAnyFn>(
func: T,
option?: {
tag?: string | symbol
inheritPreErr?: boolean
noBlockCurr?: boolean
dropWhenReachLimit?: boolean
args?: Parameters<typeof func>
}
): Promise<UnwrapPromise<typeof func>> {
const resp = this.wrap(func, option).apply(null, option?.args)
return resp
}
/** It wraps original function for queue ready */
wrap<T extends IAnyFn>(
func: T,
option?: {
tag?: string | symbol
inheritPreErr?: boolean
noBlockCurr?: boolean
dropWhenReachLimit?: boolean
}
): (...args: Parameters<typeof func>) => Promise<UnwrapPromise<typeof func>> {
if (!option || checkTag(option.tag)) {
return _q(
{
queues: this.queues,
defaultConcurrency: this.defaultConcurrency,
},
func,
option
)
}
throw new Error(ErrMsg.InstanceInvalidTag)
}
}