Skip to content

Commit

Permalink
feat(decorators): Add a throttle decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Sep 16, 2017
1 parent bf2f00f commit fe59f42
Show file tree
Hide file tree
Showing 15 changed files with 954 additions and 95 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
node_modules
.idea
.idea
dist
11 changes: 0 additions & 11 deletions CHANGELOG.md

This file was deleted.

30 changes: 28 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,40 @@ class Test {
}
```

`debounce` - Add `debounce` functionality to the method
`debounce` - Add `debounce` functionality to the method ([options](https://lodash.com/docs/4.17.4#debounce))
```ts
import { debounce } from 'helpful-decorators';

class Test {
@debounce(1000)
@debounce(1000, options)
method() {
// ...
}
}
```

`throttle` - Add `throttle` functionality to the method ([options](https://lodash.com/docs/4.17.4#throttle))
```ts
import { throttle } from 'helpful-decorators';

class Test {
@throttle(1000, options)
method() {
// ...
}
}
```

### Roadmap

- ~~timeout~~
- ~~debounce~~
- ~~throttle~~
- memoize
- once
- ...

License
----

MIT
29 changes: 26 additions & 3 deletions __tests__/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { timeout, debounce } from '../src';
import { timeout, debounce, throttle } from '../src';
jest.mock('lodash.debounce');
jest.mock('lodash.throttle');
import * as throttleFn from 'lodash.throttle';
import * as debounceFn from 'lodash.debounce';
jest.useFakeTimers();

describe('Decorators', () => {

describe('timeout', function () {
Expand Down Expand Up @@ -34,18 +37,38 @@ describe('Decorators', () => {
debounceFn['mockImplementation'](function () {
return func;
});
class TestA {
class TestDebounce {
@debounce(3000)
method() {
console.log('Debounce Worked!');
}
}
it('should call debounce', function () {
new TestA().method();
new TestDebounce().method();
expect(debounceFn).toBeCalled();
expect(debounceFn['mock'].calls[0][1]).toEqual(3000);
expect(debounceFn['mock'].calls[0][2]).toEqual({});
});
})

describe('throttle', function () {
const func = function () {
return 'called';
}
throttleFn['mockImplementation'](function () {
return func;
});
class TestThrottle {
@throttle(3000)
method() {
console.log('Throttle Worked!');
}
}
it('should call throttle', function () {
new TestThrottle().method();
expect(debounceFn).toBeCalled();
expect(debounceFn['mock'].calls[0][1]).toEqual(3000);
expect(debounceFn['mock'].calls[0][2]).toEqual({});
});
})
});
7 changes: 7 additions & 0 deletions dist/debounce.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
/**
*
* @export
* @param {number} [milliseconds=0]
* @param {any} [options={}]
* @returns
*/
export declare function debounce(milliseconds?: number, options?: {}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
11 changes: 9 additions & 2 deletions dist/debounce.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var lodash_debounce_1 = require("lodash.debounce");
var debounceFn = require("lodash.debounce");
/**
*
* @export
* @param {number} [milliseconds=0]
* @param {any} [options={}]
* @returns
*/
function debounce(milliseconds, options) {
if (milliseconds === void 0) { milliseconds = 0; }
if (options === void 0) { options = {}; }
return function (target, propertyKey, descriptor) {
var originalMethod = descriptor.value;
descriptor.value = lodash_debounce_1.debounce(originalMethod, milliseconds);
descriptor.value = debounceFn(originalMethod, milliseconds, options);
return descriptor;
};
}
Expand Down
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { timeout } from './timeout';
export { debounce } from './debounce';
export { throttle } from './throttle';
2 changes: 2 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ var timeout_1 = require("./timeout");
exports.timeout = timeout_1.timeout;
var debounce_1 = require("./debounce");
exports.debounce = debounce_1.debounce;
var throttle_1 = require("./throttle");
exports.throttle = throttle_1.throttle;
5 changes: 5 additions & 0 deletions dist/timeout.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
/**
*
* @param milliseconds
* @returns {(target:any, propertyKey:string, descriptor:PropertyDescriptor)=>PropertyDescriptor}
*/
export declare function timeout(milliseconds?: number): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
5 changes: 5 additions & 0 deletions dist/timeout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
*
* @param milliseconds
* @returns {(target:any, propertyKey:string, descriptor:PropertyDescriptor)=>PropertyDescriptor}
*/
function timeout(milliseconds) {
if (milliseconds === void 0) { milliseconds = 0; }
return function (target, propertyKey, descriptor) {
Expand Down

0 comments on commit fe59f42

Please sign in to comment.