Files
Umbraco-CMS/src/Umbraco.Web.UI.Client/libs/observable-api/object-state.ts

28 lines
1.0 KiB
TypeScript
Raw Normal View History

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