1- export function debounce ( func : Function , wait : number , context : any ) {
2- let timer : any = null ;
1+ /*---------------------------------------------------------------------------------------------
2+ * Copyright (c) Microsoft Corporation. All rights reserved.
3+ * Licensed under the MIT License. See License.txt in the project root for license information.
4+ *--------------------------------------------------------------------------------------------*/
35
4- return function ( ...args : any [ ] ) {
5- clearTimeout ( timer ) ;
6+ 'use strict' ;
67
7- timer = setTimeout ( ( ) => {
8- func . apply ( context , ...args ) ;
9- } , wait ) ;
8+ import { done } from './util' ;
9+
10+ function decorate ( decorator : ( fn : Function , key : string ) => Function ) : Function {
11+ return ( target : any , key : string , descriptor : any ) => {
12+ let fnKey : string | null = null ;
13+ let fn : Function | null = null ;
14+
15+ if ( typeof descriptor . value === 'function' ) {
16+ fnKey = 'value' ;
17+ fn = descriptor . value ;
18+ } else if ( typeof descriptor . get === 'function' ) {
19+ fnKey = 'get' ;
20+ fn = descriptor . get ;
21+ }
22+
23+ if ( ! fn || ! fnKey ) {
24+ throw new Error ( 'not supported' ) ;
25+ }
26+
27+ descriptor [ fnKey ] = decorator ( fn , key ) ;
28+ } ;
29+ }
30+
31+ function _memoize ( fn : Function , key : string ) : Function {
32+ const memoizeKey = `$memoize$${ key } ` ;
33+
34+ return function ( ...args : any [ ] ) {
35+ if ( ! this . hasOwnProperty ( memoizeKey ) ) {
36+ Object . defineProperty ( this , memoizeKey , {
37+ configurable : false ,
38+ enumerable : false ,
39+ writable : false ,
40+ value : fn . apply ( this , args )
41+ } ) ;
42+ }
43+
44+ return this [ memoizeKey ] ;
1045 } ;
1146}
1247
13- export function throttleAsync ( fn : Function , key : string , context : any ) {
14- // const currentKey = `$throttle$current$${key}`;
15- // const nextKey = `$throttle$next$${key}`;
16- // const trigger = function(...args: any[]) {
17- // if (this[nextKey]) {
18- // return this[nextKey];
19- // }
20- // if (this[nextKey]) {
21- // done(this[currentKey]).then(() => {
22- // this[nextKey] = false;
23- // return trigger.apply(context, ...args);
24- // });
25- // return this[nextKey];
26- // }
27- // this[currentKey] = fn.apply(context, args);
28- // this[currentKey].then(() => {
29- // this[currentKey] = false;
30- // });
31- // return this[currentKey];
32- // };
33- // return trigger;
48+ export const memoize = decorate ( _memoize ) ;
49+
50+ function _throttle < T > ( fn : Function , key : string ) : Function {
51+ const currentKey = `$throttle$current$${ key } ` ;
52+ const nextKey = `$throttle$next$${ key } ` ;
53+
54+ const trigger = function ( ...args : any [ ] ) {
55+ if ( this [ nextKey ] ) {
56+ return this [ nextKey ] ;
57+ }
58+
59+ if ( this [ currentKey ] ) {
60+ this [ nextKey ] = done ( this [ currentKey ] ) . then ( ( ) => {
61+ this [ nextKey ] = undefined ;
62+ return trigger . apply ( this , args ) ;
63+ } ) ;
64+
65+ return this [ nextKey ] ;
66+ }
67+
68+ this [ currentKey ] = fn . apply ( this , args ) as Promise < T > ;
69+
70+ const clear = ( ) => this [ currentKey ] = undefined ;
71+ done ( this [ currentKey ] ) . then ( clear , clear ) ;
72+
73+ return this [ currentKey ] ;
74+ } ;
75+
76+ return trigger ;
3477}
3578
36- function done ( promise : Promise < void > ) {
37- return promise . then ( ( ) => void 0 ) ;
79+ export const throttle = decorate ( _throttle ) ;
80+
81+ function _sequentialize < T > ( fn : Function , key : string ) : Function {
82+ const currentKey = `__$sequence$${ key } ` ;
83+
84+ return function ( ...args : any [ ] ) {
85+ const currentPromise = this [ currentKey ] as Promise < any > || Promise . resolve ( null ) ;
86+ const run = async ( ) => await fn . apply ( this , args ) ;
87+ this [ currentKey ] = currentPromise . then ( run , run ) ;
88+ return this [ currentKey ] ;
89+ } ;
3890}
91+
92+ export const sequentialize = decorate ( _sequentialize ) ;
93+
94+ export function debounce ( delay : number ) : Function {
95+ return decorate ( ( fn , key ) => {
96+ const timerKey = `$debounce$${ key } ` ;
97+
98+ return function ( ...args : any [ ] ) {
99+ clearTimeout ( this [ timerKey ] ) ;
100+ this [ timerKey ] = setTimeout ( ( ) => fn . apply ( this , args ) , delay ) ;
101+ } ;
102+ } ) ;
103+ }
0 commit comments