UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

1 lines 2.89 kB
{"version":3,"sources":["../../../packages/core/base/decorators/debounce.decorators.ts"],"names":[],"mappings":"AAcA,wBAAgB,QAAQ,CAAC,KAAK,GAAE,MAAY,EAAE,kBAAkB,UAAQ,GAAG,eAAe,CAqCzF","file":"debounce.decorators.d.ts","sourcesContent":["/**\r\n * Constant key for the property that we auto-magically inject into decorated class instances to manage timout references.\r\n * the value should be unique enough to never conflict with realistic property names in a class.\r\n */\r\nconst smeDebounceTimeoutRefKey = '__SmeDebounceDecoratorCurrentTimeoutReference__';\r\n/*\r\n * @Debounce() Method Decorator\r\n * Ensures a method is only executed at a maximum of 1 time per every delay interval.\r\n * Note: this only works with functions that return void. The delay makes it impossible to return values in the\r\n * same context as the function was originally executed.\r\n * @param delay The amount of time in milliseconds to debounce the method. Defaults to 200ms\r\n * @param immediateExecution If true, will execute the original method before timeout instead of afterwards. Defaults to false\r\n * @returns A @see MethodDecorator for the method that this is decorating\r\n */\r\nexport function Debounce(delay: number = 200, immediateExecution = false): MethodDecorator {\r\n // eslint-disable-next-line unused-imports/no-unused-vars\r\n return <T>(target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {\r\n // copy the descriptor\r\n const method = { ...descriptor };\r\n // remember the old value\r\n const value = method.value;\r\n // create a new value that debounces calls\r\n method.value = function (...args) {\r\n const instance = this;\r\n let currentTimoutRef = instance[smeDebounceTimeoutRefKey];\r\n\r\n /// if there is a timer then clear it.\r\n if (!MsftSme.isNullOrUndefined(currentTimoutRef)) {\r\n clearTimeout(currentTimoutRef);\r\n } else if (immediateExecution) {\r\n /// if there is a no timer and we were supposed to execute immediately,\r\n // then call the original method before setting a timeout\r\n value.apply(instance, args);\r\n }\r\n\r\n // start a new timeout\r\n instance[smeDebounceTimeoutRefKey] = setTimeout(\r\n () => {\r\n // when the timeout completes, clear our reference to it.\r\n currentTimoutRef = null;\r\n // if we did not start with execution, then execute now by calling the original method\r\n if (!immediateExecution) {\r\n value.apply(instance, args);\r\n }\r\n },\r\n delay\r\n );\r\n };\r\n\r\n return method;\r\n };\r\n}\r\n"]}