projects/i-components/src/lib/utils/notify-collection-changed-handler.ts
Provides a helper class that listens for changes a collection that implements {@INotifyCollectionChanged}. Helper is responsible for keeping track of the registered handler so it can be removed later. Usage is very simple:
Properties |
|
Methods |
constructor(handler: (e: any,args: CollectionChangeInfo) => void)
|
||||||||
Creates an instance of NotifyCollectionChangedHandler. content changes
Parameters :
|
Private collection |
Type : INotifyCollectionChanged
|
Private collectionHandler |
Type : any
|
cleanup |
cleanup()
|
Cleans up this instance by removing current attached handler from tracked collection.
Returns :
void
|
Private isINotifyCollection | ||||||
isINotifyCollection(value: any)
|
||||||
Parameters :
Returns :
any
|
trackCollection | ||||||
trackCollection(newCollection: any)
|
||||||
Tracks the given collection, once called this method the changes on the new collection will be notified. If a previous collection was being tracked then it gets clean up.
Parameters :
Returns :
void
|
import {
CollectionChangeInfo,
INotifyCollectionChanged,
ObservableCollection,
ReflectionHelper,
} from '@mobilize/wms-framework';
/*****************************************************************************
* Copyright (C) Mobilize.Net <info@mobilize.net> - All Rights Reserved
*
* This file is part of the Mobilize Frameworks, which is
* proprietary and confidential.
*
* NOTICE: All information contained herein is, and remains
* the property of Mobilize.Net Corporation.
* The intellectual and technical concepts contained herein are
* proprietary to Mobilize.Net Corporation and may be covered
* by U.S. Patents, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Mobilize.Net Corporation.
******************************************************************************/
/**
* Provides a helper class that listens for changes a collection that implements {@INotifyCollectionChanged}. Helper
* is responsible for keeping track of the registered handler so it can be removed later.
* Usage is very simple:
* <ul>
* <li>Create a new instance of {@link NotifyCollectionChangedHandler} and set the handler to take action when
* collection changes </li>
* <li>Call the {@link NotifyCollectionChangedHandler#trackCollection} method with the collection to listen
* changes for.</li>
* <li>Call the {@link NotifyCollectionChangedHandler#cleaup} method when finishing (on ngDestroy for instance)
* </ul>
*
*/
export class NotifyCollectionChangedHandler {
private collectionHandler: any;
private collection: INotifyCollectionChanged;
/**
* Creates an instance of NotifyCollectionChangedHandler.
* @param {(e: any, args: CollectionChangeInfo) => void} handler the handler to be called when the collection
* content changes
* @memberof NotifyCollectionChangedHandler
*/
constructor(private handler: (e: any, args: CollectionChangeInfo) => void) {}
/**
* Cleans up this instance by removing current attached handler from tracked collection.
*
* @memberof NotifyCollectionChangedHandler
*/
cleanup() {
if (this.collection && this.collectionHandler) {
this.collection.CollectionChanged.removeHandler(this.collectionHandler);
this.collectionHandler = undefined;
}
}
/**
* Tracks the given collection, once called this method the changes on the new collection will be
* notified.<p>
* If a previous collection was being tracked then it gets clean up.
*
* @param {*} newCollection
* @memberof NotifyCollectionChangedHandler
*/
trackCollection(newCollection: any) {
this.cleanup();
/* istanbul ignore else */
if (this.isINotifyCollection(newCollection)) {
this.collection = newCollection;
this.collectionHandler = this.collection.CollectionChanged.addHandler(
this.handler
);
}
}
private isINotifyCollection(value: any) {
return (
value &&
(value instanceof ObservableCollection ||
ReflectionHelper.isInterfaceIsSupported(
value,
'System.Collections.Specialized.INotifyCollectionChanged'
))
);
}
}