Merge remote-tracking branch 'origin/main' into feature/implement-type-specific-subjects
# Conflicts: # .storybook/preview.js # libs/store/store-base.ts # libs/store/store.ts # src/backoffice/backoffice.element.ts # src/backoffice/documents/document-blueprints/document-blueprint.store.ts # src/backoffice/documents/document-types/document-type.store.ts # src/backoffice/documents/documents/document.store.ts # src/backoffice/media/media-types/media-type.store.ts # src/backoffice/media/media/media.store.ts # src/backoffice/members/member-groups/member-group.store.ts # src/backoffice/members/member-types/member-type.store.ts # src/backoffice/settings/data-types/data-type.store.ts # src/backoffice/shared/collection/collection.context.ts # src/backoffice/shared/components/input-document-picker/input-document-picker.element.ts # src/backoffice/shared/components/tree/tree-item.element.ts # src/backoffice/shared/components/tree/tree.element.ts # src/backoffice/shared/components/variant-selector/variant-selector.element.ts # src/backoffice/shared/components/workspace/workspace-content/workspace-content.context.ts # src/backoffice/translation/dictionary/dictionary.store.ts # src/backoffice/users/user-groups/user-group.store.ts # src/backoffice/users/users/user.store.ts # src/core/modal/layouts/picker-user-group/picker-layout-user-group.element.ts
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
import { UniqueBehaviorSubject } from "./unique-behavior-subject";
|
||||
import { appendToFrozenArray } from "./append-to-frozen-array.method";
|
||||
|
||||
/**
|
||||
* @export
|
||||
* @class UniqueObjectBehaviorSubject
|
||||
* @extends {UniqueBehaviorSubject<T>}
|
||||
* @description - A RxJS UniqueObjectBehaviorSubject 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 UniqueObjectBehaviorSubject provides methods to append data when the data is an Object.
|
||||
*/
|
||||
|
||||
export class UniqueArrayBehaviorSubject<T> extends UniqueBehaviorSubject<T[]> {
|
||||
|
||||
|
||||
constructor(initialData: T[], private _getUnique?: (entry: T) => unknown) {
|
||||
super(initialData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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'}
|
||||
* ];
|
||||
* const mySubject = new UniqueArrayBehaviorSubject(data, (x) => x.key);
|
||||
* mySubject.remove([1]);
|
||||
*/
|
||||
remove(uniques: unknown[]) {
|
||||
const unFrozenDataSet = [...this.getValue()];
|
||||
if (this._getUnique) {
|
||||
uniques.forEach( unique =>
|
||||
unFrozenDataSet.filter(x => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
return this._getUnique(x) !== unique;
|
||||
})
|
||||
);
|
||||
|
||||
this.next(unFrozenDataSet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @method append
|
||||
* @param {Partial<T>} partialData - A object containing some of the data for this Subject.
|
||||
* @description - Append some new data to this Subject.
|
||||
* @example <caption>Example append some data.</caption>
|
||||
* const data = [
|
||||
* { key: 1, value: 'foo'},
|
||||
* { key: 2, value: 'bar'}
|
||||
* ];
|
||||
* const mySubject = new UniqueArrayBehaviorSubject(data);
|
||||
* mySubject.append({ key: 1, value: 'replaced-foo'});
|
||||
*/
|
||||
appendOne(entry: T) {
|
||||
this.next(appendToFrozenArray(this.getValue(), entry, this._getUnique))
|
||||
}
|
||||
|
||||
/**
|
||||
* @method append
|
||||
* @param {T[]} entries - A array of new data to be added in this Subject.
|
||||
* @description - Append some new data to this Subject, if it compares to existing data it will replace it.
|
||||
* @example <caption>Example append some data.</caption>
|
||||
* const data = [
|
||||
* { key: 1, value: 'foo'},
|
||||
* { key: 2, value: 'bar'}
|
||||
* ];
|
||||
* const mySubject = new UniqueArrayBehaviorSubject(data);
|
||||
* mySubject.append([
|
||||
* { key: 1, value: 'replaced-foo'},
|
||||
* { key: 3, value: 'another-bla'}
|
||||
* ]);
|
||||
*/
|
||||
append(entries: T[]) {
|
||||
// TODO: stop calling appendOne for each but make sure to handle this in one.
|
||||
entries.forEach(x => this.appendOne(x))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user