Skip to content

Commit

Permalink
feat(decorators): Add once decorator
Browse files Browse the repository at this point in the history
Invoke the decorated function only once
  • Loading branch information
NetanelBasal committed Oct 26, 2017
1 parent b37a692 commit db7a010
Show file tree
Hide file tree
Showing 8 changed files with 5,318 additions and 53 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,25 @@ class Test {
}
```

`once` - Add `once` functionality to the method
```ts
import { once } from 'helpful-decorators';

class Test {
@once
method() {
// This will run only once
}
}
```

### Roadmap

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

License
Expand Down
22 changes: 20 additions & 2 deletions __tests__/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { timeout, debounce, throttle } from '../src';
import { timeout, debounce, throttle, once } 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('Decorators', () => {
describe('timeout', function () {
class Test {
@timeout(1000)
Expand Down Expand Up @@ -70,5 +70,23 @@ describe('Decorators', () => {
expect(debounceFn['mock'].calls[0][1]).toEqual(3000);
expect(debounceFn['mock'].calls[0][2]).toEqual({});
});
});

describe('once', function () {
class TestOnce {
@once
method() {
console.warn('Once Worked!');
}
}
it('should call the method only once', function () {
const instance = new TestOnce();
const consoleSpy = jest.spyOn(console, 'warn');
instance.method();
instance.method();
instance.method();
expect(consoleSpy).toBeCalled();
expect(consoleSpy).toHaveBeenCalledTimes(1);
});
})
});
101 changes: 53 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"keywords": [
"decorators",
"setTimeout decorator",
"debounce decorator"
"debounce decorator",
"once decorator"
],
"devDependencies": {
"@types/jest": "^20.0.8",
Expand All @@ -51,6 +52,7 @@
"dependencies": {
"conventional-changelog-cli": "^1.3.3",
"lodash.debounce": "^4.0.8",
"lodash.once": "^4.1.1",
"lodash.throttle": "^4.1.1"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { timeout } from './timeout';
export { debounce } from './debounce';
export { throttle } from './throttle';
export { throttle } from './throttle';
export { once } from './once';
16 changes: 16 additions & 0 deletions src/once.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as onceFn from 'lodash.once';

/**
*
*
* @export
* @param {*} target
* @param {string} propertyKey
* @param {PropertyDescriptor} descriptor
* @returns
*/
export function once ( target : any, propertyKey : string, descriptor : PropertyDescriptor ) {
const originalMethod = descriptor.value;
descriptor.value = onceFn(originalMethod);
return descriptor;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true,
"outDir": "./dist",
"noImplicitAny": false,
"experimentalDecorators": true,
"strict": true,
"noUnusedLocals": true,
"noEmitOnError": true,
Expand Down

0 comments on commit db7a010

Please sign in to comment.