-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.html
1963 lines (1639 loc) · 58.1 KB
/
index.html
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Utils-Decorators</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="./site/tailwind.css" rel="stylesheet">
<link href="./site/highlight.css" rel="stylesheet">
<style>
*, ::after, ::before {
box-sizing: border-box;
}
body {
margin: 0;
height: 100vh;
display: flex;
flex-direction: column;
}
.ud-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
height: 4rem;
padding: 0 1.2rem;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
z-index: 10;
}
.ud-toolbar .ud-header {
font-size: 1.5rem;
color: #5F5F5F;
}
.ud-toolbar .ud-actions {
display: flex;
align-items: center;
gap: 10px;
}
.ud-toolbar .ud-actions > span {
position: relative;
top: 5px;
}
.ud-menu-btn {
display: none;
position: relative;
top: 2px;
border: 0;
background: transparent;
outline: 0;
}
@media (max-width: 599px) {
.ud-menu-btn {
display: block;
}
}
.ud-page-container {
flex: 1;
display: flex;
}
.ud-sidenav {
width: 14rem;
height: calc(100vh - 4rem);
overflow: auto;
background-color: white;
border-right: 1px solid #F3F4F6;
z-index: 5;
}
.ud-overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: none;
background: rgba(0, 0, 0, 0.6);
z-index: 1;
}
@media (max-width: 599px) {
.ud-sidenav {
position: fixed;
transform: translateX(-100%);
}
.ud-nav-open .ud-sidenav {
transform: translateX(0);
transition: all 500ms;
}
.ud-nav-open .ud-overlay {
display: block;
}
}
.ud-sidenav li {
height: 3.5rem;
width: 100%;
display: flex;
align-items: center;
}
.ud-nav {
border-bottom: 1px solid #F3F4F6;
}
.ud-nav:hover {
background-color: #F3F4F6;
}
.ud-sidenav li > a,
.ud-sidenav li > div {
height: 100%;
width: 100%;
padding: 1rem;
}
.ud-sidenav li > a {
padding-left: 1.5rem;
}
.ud-group {
font-weight: bold;
}
.ud-main {
position: relative;
flex: 1;
display: flex;
flex-direction: column;
height: calc(100vh - 4rem);
overflow: auto;
}
.ud-section-title {
position: sticky;
top: 0;
display: flex;
align-items: center;
background-color: #E5E7EB;
color: #5F5F5F;
font-size: 1.3rem;
padding: 1.5rem;
}
.ud-section .ud-subtitle {
font-weight: bold;
}
.ud-section-content {
padding: 1.5rem;
}
.ud-description {
margin-bottom: 1.5rem;
}
.ud-code-wrapper {
display: flex;
flex-wrap: wrap;
margin-bottom: 1.5rem;
gap: 1.5rem;
}
@media (max-width: 599px) {
.ud-code-wrapper {
flex-direction: column;
}
}
.ud-code-wrapper .ud-code-part {
flex: 1;
display: flex;
flex-direction: column;
max-width: 100%;
overflow: auto;
}
</style>
</head>
<body>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ZL8PJHMK5L"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-ZL8PJHMK5L');
</script>
<div class="ud-toolbar">
<span class="ud-header">utils-decorators</span>
<div class="ud-actions">
<a class="github-button" href="https://github.com/vlio20/utils-decorators" data-icon="octicon-star"
data-show-count="true" aria-label="Star vlio20/utils-decorators on GitHub">Star</a>
<button class="ud-menu-btn" onclick="toggleMenu()">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="black" width="24px" height="24px">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z"/>
</svg>
</button>
</div>
</div>
<div class="ud-page-container">
<div class="ud-sidenav">
<ul>
<li class="ud-group">
<div>General</div>
</li>
<li class="ud-nav">
<a href="#install" onclick="logNavEvent('install')">Installation</a>
</li>
<li class="ud-group">
<div>Decorators</div>
</li>
<li class="ud-nav">
<a href="#after" onclick="logNavEvent('after')">after</a>
</li>
<li class="ud-nav">
<a href="#before" onclick="logNavEvent('before')">before</a>
</li>
<li class="ud-nav">
<a href="#cancelPrevious" onclick="logNavEvent('cancelPrevious')">cancel-previous</a>
</li>
<li class="ud-nav">
<a href="#debounce" onclick="logNavEvent('debounce')">debounce</a>
</li>
<li class="ud-nav">
<a href="#delay" onclick="logNavEvent('delay')">delay</a>
</li>
<li class="ud-nav">
<a href="#delegate" onclick="logNavEvent('delegate')">delegate</a>
</li>
<li class="ud-nav">
<a href="#execTime" onclick="logNavEvent('execTime')">execTime</a>
</li>
<li class="ud-nav">
<a href="#memoize" onclick="logNavEvent('memoize')">memoize</a>
</li>
<li class="ud-nav">
<a href="#memoizeAsync" onclick="logNavEvent('memoizeAsync')">memoizeAsync</a>
</li>
<li class="ud-nav">
<a href="#multiDispatch" onclick="logNavEvent('multiDispatch')">multiDispatch</a>
</li>
<li class="ud-nav">
<a href="#onError" onclick="logNavEvent('onError')">onError</a>
</li>
<li class="ud-nav">
<a href="#rateLimit" onclick="logNavEvent('rateLimit')">rateLimit</a>
</li>
<li class="ud-nav">
<a href="#readonly" onclick="logNavEvent('readonly')">readonly</a>
</li>
<li class="ud-nav">
<a href="#refreshable" onclick="logNavEvent('refreshable')">refreshable</a>
</li>
<li class="ud-nav">
<a href="#retry" onclick="logNavEvent('retry')">retry</a>
</li>
<li class="ud-nav">
<a href="#throttle" onclick="logNavEvent('throttle')">throttle</a>
</li>
<li class="ud-nav">
<a href="#throttleAsync" onclick="logNavEvent('throttleAsync')">throttleAsync</a>
</li>
<li class="ud-nav">
<a href="#timeout" onclick="logNavEvent('timeout')">timeout</a>
</li>
</ul>
</div>
<div class="ud-main">
<div class="ud-overlay" onclick="toggleMenu()"></div>
<section class="ud-section">
<div id="install"></div>
<div class="ud-section-title">Installation</div>
<div class="ud-section-content">
<p class="ud-description">
This library was highly inspired by lodash but uses decorators to implement its util methods.
The lib can be used both in node and in web application, it is built to be <b>tree-shakable so you can use it
even if you need a specific decorator</b>.
To install the lib via npm please follow the command bellow:
</p>
<pre class="ud-code">
npm i utils-decorators</pre>
</div>
</section>
<section class="ud-section">
<div id="after"></div>
<div class="ud-section-title">After</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Invocation of the decorated method will cause execution of the provided function method after the invocation
of the decorated method.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
interface AfterConfig<T = any, D = any> {
// the function (or the method name within the context) to invoke after the invocation of the decorated method
func: keyof T | (args: ...any[]) => <D> ;
// in case of a returned promise of the decorated method, should the after function wait for it to be resolved
wait?: boolean;
}</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Arguments to provided method:</div>
<pre class="ud-code">
interface AfterParams<D = any> {
// the arguments that were provided to the decorated method
args: any[];
// the response data of the decorated method
response: D;
}</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { after } from 'utils-decorators';
class Example1 {
@after({
func: 'after',
})
foo(x: number): number {
return 42;
}
after(respData: AfterParams<number>): void {
console.log(respData.args, respData.response);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { afterify } from 'utils-decorators';
function foo = () => {
console.log('original method called');
}
const fooWithAfter = afterify(foo, {
func: (respData: AfterParams<number>) => {
console.log(respData.args, respData.response);
};
});</pre>
</div>
</div>
<div class="ud-subtitle">Decorator Example 2:</div>
<pre class="ud-code">
import { after } from 'utils-decorators';
class Example2 {
@after({
func: (respData: AfterParams<number>) => console.log(respData.args, respData.response),
wait: true,
})
foo(x: number): Promise<number> {
return this.numberProvider.getNumber(x);
}
}</pre>
</div>
</section>
<section class="ud-section">
<div id="before"></div>
<div class="ud-section-title">Before</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Invocation of the decorated method will cause an execution of the provided function method before the
invocation
of the decorated method.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
interface BeforeConfig<T = any, D = any> {
// the function (or the method name within the context) to invoke before the invocation of the decorated method
func: keyof T | (args: ...any[]) => <D> ;
// in case of a returned promise of the decorated method, should the before function wait for it to be resolved
wait?: boolean;
}</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { before } from 'utils-decorators';
class Example1 {
@before({
func: 'before',
})
foo(x: number): number {
return 42;
}
before(): void {
console.log('this will be invoked before');
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { beforify } from 'utils-decorators';
function foo = () => {
console.log('original method called');
}
const fooWithBefore = beforify(foo, {
func: () => {
console.log('this will be invoked before');
};
});</pre>
</div>
</div>
<div class="ud-subtitle">Decorator Example 2:</div>
<pre class="ud-code">
import { before } from 'utils-decorators';
class Example2 {
@after({
func: () => console.log('this will be invoked before');,
wait: true,
})
foo(x: number): Promise<number> {
return this.numberProvider.getNumber(x);
}
}</pre>
</div>
</section>
<section class="ud-section">
<div id="cancelPrevious"></div>
<div class="ud-section-title">Cancel Previous</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Invocation of the decorated method will cause the a rejection of the previous invocation with an error of
<i>CancelPromise</i> type.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { cancelPrevious } from 'utils-decorators';
import { DataProvider, DataDto } from './data-provider.service';
class Example1 {
constructor(private readonly dataProvider: DataProvider) { }
@cancelPrevious()
getData(): Promise<DataDto> {
return this.dataProvider.getData();
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { cancelPreviousify } from 'utils-decorators';
const dataProvider = cancelPreviousify(() => {
return fetch(...);
}));</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="debounce"></div>
<div class="ud-section-title">Debounce</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Causes a delay in the invocation of the decorated method by given time (in ms), if during the delay another
invocation will happen, the delay will be restarted.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
// the time (in milliseconds) to wait before invoke
delay: number;
</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { debounce } from 'utils-decorators';
class Example1 {
@debounce(1000)
foo(x: number): number {
return 42;
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { debouncify } from 'utils-decorators';
function foo(): void {
console.log('will be debounced')
}
const debouncedFoo = debouncify(foo, 1000);</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="delay"></div>
<div class="ud-section-title">Delay</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Causes a delay in the invocation of the decorated method by given time (in ms).
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
// the time (in milliseconds) to wait before invoke
delay: number;
</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { delay } from 'utils-decorators';
class Example1 {
@delay(1000)
foo(x: number): number {
return 42;
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { delayfy } from 'utils-decorators';
function foo(): void {
console.log('will be delayed')
}
const delayedFoo = delayfy(foo, 1000);</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="delegate"></div>
<div class="ud-section-title">Delegate</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
For a given input, if within the time period of the resolving of the promise of the first invocation the
decorated method was invoked multiple times (with the same input) the response would be the promise that was
generated by the first invocation. This way this decorator reduces the amount of calls to the implementation
of the decorated method, for example accessing the database.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
// an optional function (or a function name within the context) that maps the arguments to a specific key.
// The default mapping function is JSON.stringify(args).
keyResolver?: string | (...args: any[]) => string;
</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { delegate } from 'utils-decorators';
import { DataProvider, DataDto } from './data-provider.service';
class Example1 {
constructor(private readonly dataProvider: DataProvider) { }
@delegate()
getData(): Promise<DataDto> {
return this.dataProvider.getData();
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { delegatify } from 'utils-decorators';
function getData(): Promise<DataDto> {
return dataProvider.getData();
}
const delegatedFoo = delegatify(getData);</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 2 (with custom mapper):</div>
<pre class="ud-code">
import { delegate } from 'utils-decorators';
import { DataProvider, DataDto } from './data-provider.service';
class Example2 {
constructor(private readonly dataProvider: DataProvider) { }
@delegate((x, y) => `${x}_${y}`)
getData(x, y): Promise<DataDto> {
return this.dataProvider.getData(x, y);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 2 (with custom mapper):</div>
<pre class="ud-code">
import { delegatify } from 'utils-decorators';
function getData(): Promise<DataDto> {
return dataProvider.getData();
}
const delegatedFoo = delegatify(getData, (x, y) => `${x}_${y}`);</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="execTime"></div>
<div class="ud-section-title">Exec-Time</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Measures the time that takes for the decorated method to response.
By default will log the result using <i>console.info()</i> but this can be changed by providing your own
reporter
function.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Arguments to the reporter function:</div>
<pre class="ud-code">
interface ExactTimeReportData = {
args: any[];
result: any;
execTime: number;
};</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
// an optional function (or a function name within the context) that receives <i>ExactTimeReportData</i>
// (see details above) as an argument. If the decorated method returns a Promise,
// the decorator will wait for it to be resolved/rejected.
// The default report function is (data: ExactTimeReportData) => console.info(data.execTime)
reporter?: string | (data: ExactTimeReportData) => any;
</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1:</div>
<pre class="ud-code">
import { execTime } from 'utils-decorators';
import { DataProvider, DataDto } from './data-provider.service';
class Example1 {
constructor(private readonly dataProvider: DataProvider) { }
@execTime()
getData(): Promise<DataDto> {
return this.dataProvider.getData();
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { execTimify } from 'utils-decorators';
function getData(): Promise<DataDto> {
return dataProvider.getData();
}
const measuredFoo = execTimify(getData);</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 2 (with custom reporter):</div>
<pre class="ud-code">
import { execTime } from 'utils-decorators';
import { DataProvider, DataDto } from './data-provider.service';
class Example2 {
constructor(private readonly dataProvider: DataProvider) { }
@execTime((data: ExactTimeReportData) => {
console.log(data.args, data.result, data.execTime);
})
getData(x, y): Promise<DataDto> {
return this.dataProvider.getData(x, y);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 2 (with custom reporter):</div>
<pre class="ud-code">
import { delegatify } from 'utils-decorators';
function getData(): Promise<DataDto> {
return dataProvider.getData();
}
const measuredFoo = execTimify(getData, (data: ExactTimeReportData) => {
console.log(data.args, data.result, data.execTime);
});</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="memoize"></div>
<div class="ud-section-title">Memoize</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Memoizes the response that is being returned by the decorated method.
Be default the key of the cached value will be the serialized <i>JSON.stringify</i> value of the provided
arguments. You can supply your own key resolver.
Also, you can provide your own cache, it has to implement the <i>Cache<D></i> interface (see bellow), by
default
the decorator is using a simple <i>Map<string, D></i>.
</p>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Configuration:</div>
<pre class="ud-code">
interface MemoizeConfig<T, D> {
// an optional cache object which should implement the Cache interface.
// Internally the decorator is using Map<string, D>
cache?: Cache<D>;
// an optional function (or a function name within the context) that maps the arguments to a specific key.
// The default mapping function is JSON.stringify(args).
keyResolver?: string | (...args: any[]) => string;
// An optional TTL (time to leave in milliseconds) for the entry to be removed from the cache.
// If not provided the cache will be never cleared.
expirationTimeMs?: number;
}</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 1 (cache with no ttl):</div>
<pre class="ud-code">
import { memoize } from 'utils-decorators';
class Example1 {
@memoize()
fibo(n: number): number; {
if (n < 2) return 1;
return this.fibo(n - 1) + this.fibo(n - 2);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1 (cache with no ttl):</div>
<pre class="ud-code">
import { memoizify } from 'utils-decorators';
function fibo(n: number): number {
if (n < 2) return 1;
return fibo(n - 1) + fibo(n - 2);
);
const memoizedFibo = memoizify(fibo);</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 2 (with a ttl only):</div>
<pre class="ud-code">
import { memoize, Cache } from 'utils-decorators';
class Example2 {
// a one hour ttl
@memoize(1000 * 60 * 60)
fibo(n: number): number; {
if (n < 2) return 1;
return this.fibo(n - 1) + this.fibo(n - 2);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 2 (with a ttl only):</div>
<pre class="ud-code">
import { memoizify } from 'utils-decorators';
function fibo(n: number): number {
if (n < 2) return 1;
return fibo(n - 1) + fibo(n - 2);
);
// a one hour ttl
const memoizedFibo = memoizeify(fibo, 1000 * 60 * 60);</pre>
</div>
</div>
<div class="ud-code-wrapper">
<div class="ud-code-part">
<div class="ud-subtitle">Decorator Example 3 (with a custom cache):</div>
<pre class="ud-code">
import { memoize, Cache } from 'utils-decorators';
class CustomCache implements Cache<number> {
set: (key: string, value: number) => {...};
get: (key: string) => number | null => {...}
delete: (key: string) => void => {...}
has: (key: string) => boolean => {...}
}
const customCache = new CustomCache();
class Example3 {
@memoize({
cache: customCache
})
fibo(n: number): number; {
if (n < 2) return 1;
return this.fibo(n - 1) + this.fibo(n - 2);
}
}</pre>
</div>
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 3 (with a custom cache):</div>
<pre class="ud-code">
import { memoizify } from 'utils-decorators';
const customCache: Cache<number> = {
set: (key: string, value: number) => {...};
get: (key: string) => number | null => {...}
delete: (key: string) => void => {...}
has: (key: string) => boolean => {...}
}
function fibo(n: number): number {
if (n < 2) return 1;
return fibo(n - 1) + fibo(n - 2);
);
const memoizedFibo = memoizeify(fibo, {
cache: customCache,
});</pre>
</div>
</div>
</div>
</section>
<section class="ud-section">
<div id="memoizeAsync"></div>
<div class="ud-section-title">Memoize Async</div>
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-description">
Memoizes the promise that being returned by the decorated method.
If the promise would be rejected, the promise won't be memoized.
Another great feature of this decorator is that it delegates requests, for example if the same method has been
called more than one time after the promise was resolved,
only one invocation of the decorated method will be invoked.
Be default the key of the cached value will be the serialized <i>JSON.stringify</i> value of the provided
arguments.
You can supply your own key resolver.
Also, you can provide your own cache, it has to implement the <i>GetterSetter<D></i> interface, by
default the decorator is using a simple <i>Map<string, Promise<D>></i>.
For further readying please read <a
href="https://medium.com/swlh/caching-in-node-nestjs-web-applications-was-never-easier-1bb560c7cd62">this</a>