xshell 1.2.14 → 1.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -133,3 +133,7 @@ export declare class Timer {
133
133
  export declare function lowercase_first_letter(str: string): string;
134
134
  /** 大于 n 的最小的 2 的幂次 */
135
135
  export declare function ceil2(n: number): number;
136
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
137
+ export declare function throttle(duration: number, func: Function, delay_first?: boolean): (this: any, ...args: any[]) => void;
138
+ /** 防抖,间隔一段时间不再触发时调用 */
139
+ export declare function debounce(duration: number, func: Function): (this: any, ...args: any[]) => void;
package/utils.browser.js CHANGED
@@ -416,4 +416,49 @@ export function ceil2(n) {
416
416
  ;
417
417
  return power;
418
418
  }
419
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
420
+ export function throttle(duration, func, delay_first = false) {
421
+ let timeout = 0;
422
+ let last = 0;
423
+ let saved_this = null;
424
+ let saved_args = null;
425
+ return function throttled(...args) {
426
+ // 当前时间间隔已预定执行,本次调用仅更新调用参数
427
+ if (timeout) {
428
+ saved_this = this;
429
+ saved_args = args;
430
+ return;
431
+ }
432
+ const now = Date.now();
433
+ if (last === 0 && delay_first)
434
+ last = now;
435
+ const ellapsed = now - last;
436
+ // 过了时间间隔末尾,直接执行
437
+ if (ellapsed >= duration) {
438
+ last = now;
439
+ func.apply(this, args);
440
+ }
441
+ else { // 预定在间隔末尾执行
442
+ saved_this = this;
443
+ saved_args = args;
444
+ timeout = setTimeout(() => {
445
+ timeout = null;
446
+ last = Date.now();
447
+ func.apply(saved_this, saved_args);
448
+ saved_this = null;
449
+ saved_args = null;
450
+ }, duration - ellapsed);
451
+ }
452
+ };
453
+ }
454
+ /** 防抖,间隔一段时间不再触发时调用 */
455
+ export function debounce(duration, func) {
456
+ let timeout = null;
457
+ return function debounced(...args) {
458
+ clearTimeout(timeout);
459
+ timeout = setTimeout(() => {
460
+ func.apply(this, args);
461
+ }, duration);
462
+ };
463
+ }
419
464
  //# sourceMappingURL=utils.browser.js.map
package/utils.d.ts CHANGED
@@ -210,3 +210,7 @@ export declare function consume_stream(stream: Readable, ignore_error?: boolean)
210
210
  export declare function range_to_numbers(range: string, reverse?: boolean): Generator<number, void, unknown>;
211
211
  /** 大于 n 的最小的 2 的幂次 */
212
212
  export declare function ceil2(n: number): number;
213
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
214
+ export declare function throttle(duration: number, func: Function, delay_first?: boolean): (this: any, ...args: any[]) => void;
215
+ /** 防抖,间隔一段时间不再触发时调用 */
216
+ export declare function debounce(duration: number, func: Function): (this: any, ...args: any[]) => void;
package/utils.js CHANGED
@@ -692,4 +692,49 @@ export function ceil2(n) {
692
692
  ;
693
693
  return power;
694
694
  }
695
+ /** 节流,最多只在时间间隔末尾调用一次,首次调用也延后 */
696
+ export function throttle(duration, func, delay_first = false) {
697
+ let timeout = null;
698
+ let last = 0;
699
+ let saved_this = null;
700
+ let saved_args = null;
701
+ return function throttled(...args) {
702
+ // 当前时间间隔已预定执行,本次调用仅更新调用参数
703
+ if (timeout) {
704
+ saved_this = this;
705
+ saved_args = args;
706
+ return;
707
+ }
708
+ const now = Date.now();
709
+ if (last === 0 && delay_first)
710
+ last = now;
711
+ const ellapsed = now - last;
712
+ // 过了时间间隔末尾,直接执行
713
+ if (ellapsed >= duration) {
714
+ last = now;
715
+ func.apply(this, args);
716
+ }
717
+ else { // 预定在间隔末尾执行
718
+ saved_this = this;
719
+ saved_args = args;
720
+ timeout = setTimeout(() => {
721
+ timeout = null;
722
+ last = Date.now();
723
+ func.apply(saved_this, saved_args);
724
+ saved_this = null;
725
+ saved_args = null;
726
+ }, duration - ellapsed);
727
+ }
728
+ };
729
+ }
730
+ /** 防抖,间隔一段时间不再触发时调用 */
731
+ export function debounce(duration, func) {
732
+ let timeout = null;
733
+ return function debounced(...args) {
734
+ clearTimeout(timeout);
735
+ timeout = setTimeout(() => {
736
+ func.apply(this, args);
737
+ }, duration);
738
+ };
739
+ }
695
740
  //# sourceMappingURL=utils.js.map