Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/object-state.ts
Niels Lyngsø c65d0506e1 lint and style
2023-03-16 21:13:58 +01:00

28 lines
1.0 KiB
TypeScript

import { DeepState } from './deep-state';
/**
* @export
* @class ObjectState
* @extends {DeepState<T>}
* @description - A RxJS BehaviorSubject which deepFreezes the object-data to ensure its not manipulated from any implementations.
* Additionally the Subject ensures the data is unique, not updating any Observes unless there is an actual change of the content.
*
* The ObjectState provides methods to append data when the data is an Object.
*/
export class ObjectState<T> extends DeepState<T> {
/**
* @method update
* @param {Partial<T>} partialData - A object containing some of the data to update in this Subject.
* @description - Append some new data to this Object.
* @return {ObjectState<T>} Reference to it self.
* @example <caption>Example append some data.</caption>
* const data = {key: 'myKey', value: 'myInitialValue'};
* const myState = new ObjectState(data);
* myState.update({value: 'myNewValue'});
*/
update(partialData: Partial<T>) {
this.next({ ...this.getValue(), ...partialData });
return this;
}
}