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

84 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-24 13:40:25 +01:00
import { DeepState } from "./deep-state";
2023-01-23 14:42:24 +01:00
import { appendToFrozenArray } from "./append-to-frozen-array.method";
2023-01-13 08:37:57 +01:00
/**
* @export
2023-01-24 13:40:25 +01:00
* @class ArrayState
* @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 ArrayState 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 ArrayState<T> extends DeepState<T[]> {
2023-01-13 08:37:57 +01:00
2023-01-23 14:42:24 +01:00
constructor(initialData: T[], private _getUnique?: (entry: T) => unknown) {
2023-01-13 08:37:57 +01:00
super(initialData);
}
2023-01-23 14:42:24 +01:00
/**
* @method append
* @param {unknown} unique - The unique value to remove.
* @description - Remove some new data of this Subject.
* @example <caption>Example remove entry with key '1'</caption>
* const data = [
* { key: 1, value: 'foo'},
* { key: 2, value: 'bar'}
* ];
2023-01-24 13:40:25 +01:00
* const mySubject = new ArrayState(data, (x) => x.key);
2023-01-23 15:47:46 +01:00
* mySubject.remove([1]);
2023-01-23 14:42:24 +01:00
*/
2023-01-23 15:47:46 +01:00
remove(uniques: unknown[]) {
2023-01-23 14:42:24 +01:00
const unFrozenDataSet = [...this.getValue()];
if (this._getUnique) {
2023-01-23 15:47:46 +01:00
uniques.forEach( unique =>
unFrozenDataSet.filter(x => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this._getUnique(x) !== unique;
})
);
2023-01-23 14:42:24 +01:00
this.next(unFrozenDataSet);
}
}
2023-01-13 08:37:57 +01:00
/**
* @method append
* @param {Partial<T>} partialData - A object containing some of the data for this Subject.
2023-01-23 14:42:24 +01:00
* @description - Append some new data to this Subject.
2023-01-13 08:37:57 +01:00
* @example <caption>Example append some data.</caption>
* const data = [
* { key: 1, value: 'foo'},
* { key: 2, value: 'bar'}
* ];
2023-01-24 13:40:25 +01:00
* const mySubject = new ArrayState(data);
2023-01-13 08:37:57 +01:00
* mySubject.append({ key: 1, value: 'replaced-foo'});
*/
appendOne(entry: T) {
2023-01-23 14:42:24 +01:00
this.next(appendToFrozenArray(this.getValue(), entry, this._getUnique))
2023-01-13 08:37:57 +01:00
}
/**
* @method append
* @param {T[]} entries - A array of new data to be added in this Subject.
2023-01-23 14:42:24 +01:00
* @description - Append some new data to this Subject, if it compares to existing data it will replace it.
2023-01-13 08:37:57 +01:00
* @example <caption>Example append some data.</caption>
* const data = [
* { key: 1, value: 'foo'},
* { key: 2, value: 'bar'}
* ];
2023-01-24 13:40:25 +01:00
* const mySubject = new ArrayState(data);
2023-01-13 08:37:57 +01:00
* mySubject.append([
* { key: 1, value: 'replaced-foo'},
* { key: 3, value: 'another-bla'}
* ]);
*/
append(entries: T[]) {
2023-01-23 13:59:42 +01:00
// TODO: stop calling appendOne for each but make sure to handle this in one.
2023-01-13 08:37:57 +01:00
entries.forEach(x => this.appendOne(x))
}
}