Created
July 22, 2018 06:16
-
-
Save dherges/4a6239607cd56286a13cb32f02db3e28 to your computer and use it in GitHub Desktop.
foo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function ContentChildrenChanges() { | |
return function (target, propertyKey) { | |
if (!isContentChanges(target)) { | |
return; | |
} | |
// Grab reference to toriginal `ngOnDestroy()` hook | |
const proto = target.constructor.prototype; | |
const originalNgAfterContentInit = proto.ngAfterContentInit; | |
const originalNgOnDestroy = proto.ngOnDestroy; | |
const subs: Subscription[] = []; | |
proto.ngAfterContentInit = function () { | |
const propertyValue = this[propertyKey]; | |
if (isQueryList(propertyValue)) { | |
subs.push(propertyValue.changes.subscribe(() => { | |
const change = {}; | |
change[propertyKey] = propertyValue; | |
target.uOnContentChanges(change); | |
})); | |
const change = {}; | |
change[propertyKey] = propertyValue; | |
target.uOnContentChanges(change); | |
} | |
callLifecycleHook(originalNgAfterContentInit, this, arguments); | |
}; | |
proto.ngOnDestroy = function () { | |
// Try to unsubscribe | |
subs.forEach(sub => sub.unsubscribe()); | |
// Call the original `ngOnDestroy()` hook | |
callLifecycleHook(originalNgOnDestroy, this, arguments); | |
}; | |
}; | |
} | |
export function ViewChildrenChanges() { | |
return function (target, propertyKey) { | |
if (!isViewChanges(target)) { | |
return; | |
} | |
// Grab reference to toriginal `ngOnDestroy()` hook | |
const proto = target.constructor.prototype; | |
const originalNgAfterViewInit = proto.ngAfterViewInit; | |
const originalNgOnDestroy = proto.ngOnDestroy; | |
const subs: Subscription[] = []; | |
proto.ngAfterViewInit = function () { | |
const propertyValue = this[propertyKey]; | |
if (isQueryList(propertyValue)) { | |
subs.push(propertyValue.changes.subscribe(() => { | |
const change = {}; | |
change[propertyKey] = propertyValue; | |
target.uOnViewChanges(change); | |
})); | |
const change = {}; | |
change[propertyKey] = propertyValue; | |
target.uOnViewChanges(change); | |
} | |
callLifecycleHook(originalNgAfterViewInit, this, arguments); | |
}; | |
proto.ngOnDestroy = function () { | |
// Try to unsubscribe | |
subs.forEach(sub => sub.unsubscribe()); | |
// Call the original `ngOnDestroy()` hook | |
callLifecycleHook(originalNgOnDestroy, this, arguments); | |
}; | |
}; | |
} | |
function isQueryList(value: any): value is QueryList<any> { | |
return value instanceof QueryList; | |
} | |
function callLifecycleHook(fn: any, target: any, args: any) { | |
if (fn && typeof fn === 'function') { | |
fn.apply(target, args); | |
} | |
} | |
/** | |
* A change reported to a lifecycle hook when a `QueryList` changes. | |
* | |
* See `DynamicQueries()` for more details. | |
* | |
* @stable | |
*/ | |
export interface QueryListChange { | |
[key: string]: QueryList<any>; | |
} | |
/** | |
* Custom lifecycle hook that is invoked when a `QueryList` property, annotated with `@ContentChildren()`, | |
* of the parent component receives changes from dynamic content. | |
* | |
* See `DynamicQueries()` for more details. | |
* | |
* @stable | |
*/ | |
export interface ContentChanges { | |
uOnContentChanges(change: QueryListChange); | |
} | |
/** | |
* Custom lifecycle hook that is invoked when a `QueryList` property, annotated with `@ViewChildren()`, | |
* of the parent component receives changes from dynamic content. | |
* | |
* See `DynamicQueries()` for more details. | |
* | |
* @stable | |
*/ | |
export interface ViewChanges { | |
uOnViewChanges(change: QueryListChange); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment