ngx-auto-unsubscribe
Advanced tools
Comparing version 2.4.1 to 3.0.0
@@ -1,14 +0,12 @@ | ||
import { AutoUnsubscribe } from '../src/auto-unsubscribe'; | ||
import { AutoUnsubscribe } from "../src/auto-unsubscribe"; | ||
const mockSubscription = { | ||
unsubscribe: jest.fn() | ||
} | ||
}; | ||
const mockSubscription2 = { | ||
unsubscribe: jest.fn() | ||
} | ||
}; | ||
describe('@AutoUnsubscribe', () => { | ||
describe("@AutoUnsubscribe", () => { | ||
afterEach(() => { | ||
@@ -18,56 +16,9 @@ jest.resetAllMocks(); | ||
it('should warn when not implement on destroy', () => { | ||
const consoleSpy = jest.spyOn(console, 'warn'); | ||
it("should call unsubscribe on destroy", () => { | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
} | ||
new TodsComponent()['ngOnDestroy'](); | ||
expect(consoleSpy).toHaveBeenCalled(); | ||
}); | ||
it('should not warn when implement on destroy', () => { | ||
const consoleSpy = jest.spyOn(console, 'warn'); | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
sub = mockSubscription; | ||
ngOnDestroy() {} | ||
} | ||
new TodsComponent()['ngOnDestroy'](); | ||
expect(consoleSpy).not.toHaveBeenCalled(); | ||
}); | ||
it('should not warn when disable AOT mode', () => { | ||
window['disableAutoUnsubscribeAot'] = true; | ||
const consoleSpy = jest.spyOn(console, 'warn'); | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
} | ||
new TodsComponent()['ngOnDestroy'](); | ||
expect(consoleSpy).not.toHaveBeenCalled(); | ||
}); | ||
it('should not warn when disable AOT mode with typo', () => { | ||
window['disableAuthUnsubscribeAot'] = true; | ||
const consoleSpy = jest.spyOn(console, 'warn'); | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
} | ||
new TodsComponent()['ngOnDestroy'](); | ||
expect(consoleSpy).not.toHaveBeenCalled(); | ||
}); | ||
it('should call unsubscribe on destroy', () => { | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
ngOnDestroy() {} | ||
} | ||
new TodsComponent().ngOnDestroy(); | ||
@@ -77,6 +28,6 @@ expect(mockSubscription.unsubscribe.mock.calls.length).toBe(1); | ||
it('should call unsubscribe on custom event callback', () => { | ||
@AutoUnsubscribe({event: 'ionViewDidLeave'}) | ||
it("should call unsubscribe on custom event callback", () => { | ||
@AutoUnsubscribe({ event: "ionViewDidLeave" }) | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
sub = mockSubscription; | ||
ngOnDestroy() {} | ||
@@ -95,7 +46,7 @@ ionViewDidLeave() {} | ||
it('should work with multiple observables', () => { | ||
it("should work with multiple observables", () => { | ||
@AutoUnsubscribe() | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
sub2 = mockSubscription2; | ||
sub = mockSubscription; | ||
sub2 = mockSubscription2; | ||
ngOnDestroy() {} | ||
@@ -109,7 +60,7 @@ } | ||
it('should NOT unsubscribe if property is in blacklist', () => { | ||
@AutoUnsubscribe({blackList: ['sub']}) | ||
it("should NOT unsubscribe if property is in blacklist", () => { | ||
@AutoUnsubscribe({ blackList: ["sub"] }) | ||
class TodsComponent { | ||
sub = mockSubscription; | ||
sub2 = mockSubscription2; | ||
sub = mockSubscription; | ||
sub2 = mockSubscription2; | ||
ngOnDestroy() {} | ||
@@ -123,38 +74,24 @@ } | ||
describe('includeArrays', () => { | ||
it('should unsubscribe an array of subscriptions', () => { | ||
@AutoUnsubscribe({ includeArrays: true }) | ||
describe("includeArrays", () => { | ||
it("should unsubscribe an array of subscriptions", () => { | ||
@AutoUnsubscribe({ arrayName: "subs" }) | ||
class TodsComponent { | ||
subs = Array(3).fill(mockSubscription); | ||
ngOnDestroy() { } | ||
ngOnDestroy() {} | ||
} | ||
new TodsComponent().ngOnDestroy(); | ||
expect(mockSubscription.unsubscribe.mock.calls.length).toBe(3); | ||
}); | ||
it('should NOT unsubscribe if array is in blacklist', () => { | ||
@AutoUnsubscribe({ includeArrays: true, blackList: ['subs'] }) | ||
class TodsComponent { | ||
subs = Array(3).fill(mockSubscription); | ||
subs2 = Array(3).fill(mockSubscription2); | ||
ngOnDestroy() {} | ||
} | ||
new TodsComponent().ngOnDestroy(); | ||
expect(mockSubscription.unsubscribe.mock.calls.length).toBe(0); | ||
expect(mockSubscription2.unsubscribe.mock.calls.length).toBe(3); | ||
}); | ||
}); | ||
describe('arrayName', () => { | ||
describe("arrayName", () => { | ||
beforeEach(() => { | ||
@AutoUnsubscribe({ arrayName: 'subscriptions' }) | ||
@AutoUnsubscribe({ arrayName: "subscriptions" }) | ||
class TodsComponent { | ||
subscriptions = Array(3).fill(mockSubscription); | ||
subs = Array(3).fill(mockSubscription2); | ||
ngOnDestroy() { } | ||
ngOnDestroy() {} | ||
} | ||
new TodsComponent().ngOnDestroy(); | ||
@@ -170,4 +107,3 @@ }); | ||
}); | ||
}); | ||
}); |
@@ -1,6 +0,5 @@ | ||
export declare function AutoUnsubscribe({blackList, includeArrays, arrayName, event}?: { | ||
export declare function AutoUnsubscribe({blackList, arrayName, event}?: { | ||
blackList?: any[]; | ||
includeArrays?: boolean; | ||
arrayName?: string; | ||
event?: string; | ||
}): (constructor: Function) => void; |
@@ -1,18 +0,23 @@ | ||
var isFunction = function (fn) { return typeof fn === 'function'; }; | ||
var isFunction = function (fn) { return typeof fn === "function"; }; | ||
var doUnsubscribe = function (subscription) { | ||
subscription && isFunction(subscription.unsubscribe) && subscription.unsubscribe(); | ||
subscription && | ||
isFunction(subscription.unsubscribe) && | ||
subscription.unsubscribe(); | ||
}; | ||
var doUnsubscribeIfArray = function (subscriptionsArray) { | ||
Array.isArray(subscriptionsArray) && subscriptionsArray.forEach(doUnsubscribe); | ||
Array.isArray(subscriptionsArray) && | ||
subscriptionsArray.forEach(doUnsubscribe); | ||
}; | ||
export function AutoUnsubscribe(_a) { | ||
var _b = _a === void 0 ? {} : _a, _c = _b.blackList, blackList = _c === void 0 ? [] : _c, _d = _b.includeArrays, includeArrays = _d === void 0 ? false : _d, _e = _b.arrayName, arrayName = _e === void 0 ? '' : _e, _f = _b.event, event = _f === void 0 ? 'ngOnDestroy' : _f; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.blackList, blackList = _c === void 0 ? [] : _c, _d = _b.arrayName, arrayName = _d === void 0 ? "" : _d, _e = _b.event, event = _e === void 0 ? "ngOnDestroy" : _e; | ||
return function (constructor) { | ||
var original = constructor.prototype[event]; | ||
if (!isFunction(original) && !disableAutoUnsubscribeAot()) { | ||
console.warn(constructor.name + " is using @AutoUnsubscribe but does not implement OnDestroy"); | ||
if (!isFunction(original)) { | ||
throw new Error(constructor.name + " is using @AutoUnsubscribe but does not implement OnDestroy"); | ||
} | ||
constructor.prototype[event] = function () { | ||
if (arrayName) { | ||
return doUnsubscribeIfArray(this[arrayName]); | ||
doUnsubscribeIfArray(this[arrayName]); | ||
isFunction(original) && original.apply(this, arguments); | ||
return; | ||
} | ||
@@ -24,3 +29,2 @@ for (var propName in this) { | ||
doUnsubscribe(property); | ||
doUnsubscribeIfArray(property); | ||
} | ||
@@ -30,6 +34,3 @@ isFunction(original) && original.apply(this, arguments); | ||
}; | ||
function disableAutoUnsubscribeAot() { | ||
return window && window['disableAutoUnsubscribeAot'] || window['disableAuthUnsubscribeAot']; | ||
} | ||
} | ||
//# sourceMappingURL=auto-unsubscribe.js.map |
{ | ||
"name": "ngx-auto-unsubscribe", | ||
"version": "2.4.1", | ||
"version": "3.0.0", | ||
"main": "dist/index.js", | ||
@@ -41,2 +41,2 @@ "description": "Class decorator that automatically unsubscribes from observables and events", | ||
} | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
### Angular - Auto Unsubscribe For Pros | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors) | ||
### Angular - Auto Unsubscribe For Pros | ||
[![npm](https://img.shields.io/npm/dt/ngx-auto-unsubscribe.svg)]() | ||
@@ -35,7 +35,5 @@ [![Build Status](https://semaphoreci.com/api/v1/netanel7799/ngx-auto-unsubscribe/branches/master/badge.svg)](https://semaphoreci.com/netanel7799/ngx-auto-unsubscribe) | ||
// If you work with AOT this method must be present, even if empty! | ||
// Otherwise 'ng build --prod' will optimize away any calls to ngOnDestroy, | ||
// even if the method is added by the @AutoUnsubscribe decorator | ||
// This method must be present, even if empty. | ||
ngOnDestroy() { | ||
// You can also do whatever you need here | ||
// We'll throw an error if it doesn't | ||
} | ||
@@ -47,8 +45,7 @@ } | ||
| Option | Description | Default Value | | ||
| ----------------- | ------------------------------------------------------------- | ---------------- | | ||
| `includeArrays` | unsubscribe from arrays of subscriptions | `false` | | ||
| `arrayName` | unsubscribe from subscriptions only in specified array | `''` | | ||
| `blackList` | an array of properties to exclude | `[]` | | ||
| `event` | a name of event callback to execute on | `ngOnDestroy` | | ||
| Option | Description | Default Value | | ||
| ----------- | ------------------------------------------------------ | ------------- | | ||
| `arrayName` | unsubscribe from subscriptions only in specified array | `''` | | ||
| `blackList` | an array of properties to exclude | `[]` | | ||
| `event` | a name of event callback to execute on | `ngOnDestroy` | | ||
@@ -60,13 +57,1 @@ Note: `blackList` is ignored if `arrayName` is specified. | ||
You can also use https://github.com/NetanelBasal/ngx-take-until-destroy. | ||
## Contributors | ||
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
<!-- prettier-ignore --> | ||
| [<img src="https://avatars2.githubusercontent.com/u/2690948?v=4" width="100px;"/><br /><sub><b>misaizdaleka</b></sub>](https://github.com/misaizdaleka)<br />[💻](https://github.com/Netanel Basal/ngx-auto-unsubscribe/commits?author=misaizdaleka "Code") | | ||
| :---: | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13243
146
55