Skip to content

Commit 047c023

Browse files
committed
Added JDocs for changeable-helper classes, Renamed functions for clarity
1 parent 107c37c commit 047c023

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

libs/fabric/src/lib/components/contextual-menu/directives/contextual-menu-item.directive.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,10 @@ export class ContextualMenuItemDirective extends ChangeableItemDirective<IContex
117117
this.subMenuProps.items = items;
118118
}
119119
});
120-
this.changeableItemsHelper.afterContentInit();
121120
}
122121

123122
ngOnDestroy() {
124-
this.changeableItemsHelper.onDestroy();
123+
this.changeableItemsHelper.destroy();
125124
}
126125

127126
private _directiveToContextualMenuItem(directive: ContextualMenuItemDirective): IContextualMenuItem {

libs/fabric/src/lib/components/core/shared/changeable-helper.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ export class ChangeableItemHelper<TItem> {
1616

1717
constructor(private readonly key: string) {}
1818

19-
onChanges(changes: TypedChanges<TItem>) {
19+
/**
20+
* Action to be called by user of class when change is detected
21+
* (Typically called in ngOnChanges)
22+
* @param changes TypedChanges that are to be emitted
23+
*/
24+
emitChanges(changes: TypedChanges<TItem>) {
2025
this.onItemChanged.emit({ key: this.key, changes });
2126
}
2227
}
@@ -31,22 +36,32 @@ export class ChangeableItemsHelper<TItem> {
3136
private readonly _subscriptionsMap: { [key: string]: Subscription } = {};
3237
private _changeSubscription: Subscription;
3338

39+
/**
40+
* Initialize subscriptions to watch for changes to children ChangeableItemDirectives
41+
* (Typically called in ngAfterContentInit after @ContentChildren are initialized)
42+
* @param directiveItems List of children to watch for
43+
* @param self Reference to component using this helper. This component gets filtered out in case
44+
* it appears in the list of children (i.e. when a component has children of its own type)
45+
* @param nonSelfHandler Callback to handle filtered list of children when updated
46+
*/
3447
constructor(
3548
private directiveItems: QueryList<ChangeableItemDirective<TItem>>,
3649
private self?: IChangeableItemsContainer<TItem>,
3750
private nonSelfHandler?: (nonSelfDirectives: ChangeableItemDirective<TItem>[]) => void
38-
) {}
39-
40-
afterContentInit() {
51+
) {
4152
this._subscribeNewDirectives();
4253
this._changeSubscription = this.directiveItems.changes.subscribe(newValues => {
4354
this.onItemsChanged.emit(newValues);
4455
this._subscribeNewDirectives();
4556
});
4657
}
4758

48-
onDestroy() {
49-
Object.keys(this._subscriptionsMap).forEach(key => this._subscriptionsMap[key].unsubscribe());
59+
/**
60+
* Action to be called by user of class when directive/component is destroyed
61+
* (Typically called in ngOnDestroy)
62+
*/
63+
destroy() {
64+
Object.values(this._subscriptionsMap).forEach(value => value.unsubscribe());
5065
this._changeSubscription.unsubscribe();
5166
}
5267

libs/fabric/src/lib/components/core/shared/changeable-item.directive.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { EventEmitter, Input, Output } from '@angular/core';
4+
import { EventEmitter, Input, OnInit, Output } from '@angular/core';
55

66
import { OnChanges, TypedChanges } from '../../../declarations/angular/typed-changes';
77
import { ItemChangedPayload } from '../../core/declarative/item-changed.payload';
@@ -10,7 +10,7 @@ import { ChangeableItemHelper } from './changeable-helper';
1010
/**
1111
* Parent class for wrapper directive for single item with OnChanges
1212
*/
13-
export abstract class ChangeableItemDirective<TItem> implements OnChanges<ChangeableItemDirective<TItem>> {
13+
export abstract class ChangeableItemDirective<TItem> implements OnChanges<ChangeableItemDirective<TItem>>, OnInit {
1414
@Input()
1515
key: string;
1616

@@ -27,7 +27,7 @@ export abstract class ChangeableItemDirective<TItem> implements OnChanges<Change
2727

2828
ngOnChanges(changes: TypedChanges<TItem>) {
2929
if (this.changeableItemHelper) {
30-
this.changeableItemHelper.onChanges(changes);
30+
this.changeableItemHelper.emitChanges(changes);
3131
}
3232
}
3333
}

libs/fabric/src/lib/components/core/shared/changeable-items.directive.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ export abstract class ChangeableItemsDirective<TItem>
3030

3131
ngAfterContentInit() {
3232
this.changeableItemsHelper = new ChangeableItemsHelper(this.directiveItems);
33-
this.changeableItemsHelper.afterContentInit();
3433
}
3534

3635
ngOnDestroy() {
37-
this.changeableItemsHelper.onDestroy();
36+
this.changeableItemsHelper.destroy();
3837
}
3938
}

libs/fabric/src/lib/components/group/directives/group-item.directive.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ChangeableItemDirective } from '../../core/shared/changeable-item.direc
1919

2020
@Directive({ selector: 'fab-group-item' })
2121
export class GroupItemDirective extends ChangeableItemDirective<IGroup>
22-
implements AfterContentInit, IChangeableItemsContainer<IGroup>, OnDestroy {
22+
implements AfterContentInit, IChangeableItemsContainer<IGroup>, IGroup, OnDestroy {
2323
@ContentChildren(GroupItemDirective)
2424
readonly groupItemsDirectives: QueryList<GroupItemDirective>;
2525

@@ -61,10 +61,9 @@ export class GroupItemDirective extends ChangeableItemDirective<IGroup>
6161
this.changeableItemsHelper = new ChangeableItemsHelper(this.groupItemsDirectives, this, nonSelf => {
6262
this.children = nonSelf as any;
6363
});
64-
this.changeableItemsHelper.afterContentInit();
6564
}
6665

6766
ngOnDestroy() {
68-
this.changeableItemsHelper.onDestroy();
67+
this.changeableItemsHelper.destroy();
6968
}
7069
}

0 commit comments

Comments
 (0)