Skip to content

Commit

Permalink
Merge pull request #1022 from umbraco/chore/observable-api-restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
nielslyngsoe authored Nov 27, 2023
2 parents e150238 + 57d4dfd commit ad35ba2
Show file tree
Hide file tree
Showing 27 changed files with 52 additions and 45 deletions.
16 changes: 3 additions & 13 deletions src/libs/observable-api/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
export * from './observer.controller.js';
export * from './observer.js';
export * from './basic-state.js';
export * from './boolean-state.js';
export * from './number-state.js';
export * from './string-state.js';
export * from './class-state.js';
export * from './deep-state.js';
export * from './array-state.js';
export * from './object-state.js';
export * from './create-observable-part.function.js';
export * from './append-to-frozen-array.function.js';
export * from './filter-frozen-array.function.js';
export * from './partial-update-frozen-array.function.js';
export * from './mapping-function.js';
export * from './states/index.js';
export * from './types/index.js';
export * from './utils/index.js';
2 changes: 1 addition & 1 deletion src/libs/observable-api/observer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from '@open-wc/testing';
import { UmbObjectState } from './object-state.js';
import { UmbObjectState } from './states/object-state.js';
import { UmbObserver } from './observer.js';

describe('UmbObserver', () => {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { partialUpdateFrozenArray } from '../utils/partial-update-frozen-array.function.js';
import { pushToUniqueArray } from '../utils/push-to-unique-array.function.js';
import { UmbDeepState } from './deep-state.js';
import { partialUpdateFrozenArray } from './partial-update-frozen-array.function.js';
import { pushToUniqueArray } from './push-to-unique-array.function.js';

/**
* @export
Expand All @@ -12,12 +12,12 @@ import { pushToUniqueArray } from './push-to-unique-array.function.js';
* The ArrayState provides methods to append data when the data is an Object.
*/
export class UmbArrayState<T> extends UmbDeepState<T[]> {
readonly getUnique: (entry: T) => unknown;
readonly getUniqueMethod: (entry: T) => unknown;
#sortMethod?: (a: T, b: T) => number;

constructor(initialData: T[], getUniqueMethod: (entry: T) => unknown) {
constructor(initialData: T[], getUniqueOfEntryMethod: (entry: T) => unknown) {
super(initialData);
this.getUnique = getUniqueMethod;
this.getUniqueMethod = getUniqueOfEntryMethod;
}

/**
Expand Down Expand Up @@ -60,12 +60,12 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
remove(uniques: unknown[]) {
let next = this.getValue();
if (this.getUnique) {
if (this.getUniqueMethod) {
uniques.forEach((unique) => {
next = next.filter((x) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.getUnique(x) !== unique;
return this.getUniqueMethod(x) !== unique;
});
});

Expand All @@ -89,11 +89,11 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
removeOne(unique: unknown) {
let next = this.getValue();
if (this.getUnique) {
if (this.getUniqueMethod) {
next = next.filter((x) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return this.getUnique(x) !== unique;
return this.getUniqueMethod(x) !== unique;
});

this.next(next);
Expand Down Expand Up @@ -142,8 +142,8 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
*/
appendOne(entry: T) {
const next = [...this.getValue()];
if (this.getUnique) {
pushToUniqueArray(next, entry, this.getUnique);
if (this.getUniqueMethod) {
pushToUniqueArray(next, entry, this.getUniqueMethod);
} else {
next.push(entry);
}
Expand All @@ -168,10 +168,10 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
* ]);
*/
append(entries: T[]) {
if (this.getUnique) {
if (this.getUniqueMethod) {
const next = [...this.getValue()];
entries.forEach((entry) => {
pushToUniqueArray(next, entry, this.getUnique!);
pushToUniqueArray(next, entry, this.getUniqueMethod!);
});
this.next(next);
} else {
Expand All @@ -195,10 +195,10 @@ export class UmbArrayState<T> extends UmbDeepState<T[]> {
* myState.updateOne(2, {value: 'updated-bar'});
*/
updateOne(unique: unknown, entry: Partial<T>) {
if (!this.getUnique) {
if (!this.getUniqueMethod) {
throw new Error("Can't partial update an ArrayState without a getUnique method provided when constructed.");
}
this.next(partialUpdateFrozenArray(this.getValue(), entry, (x) => unique === this.getUnique!(x)));
this.next(partialUpdateFrozenArray(this.getValue(), entry, (x) => unique === this.getUniqueMethod!(x)));
return this;
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { UmbBasicState } from './basic-state.js';
import { createObservablePart } from './create-observable-part.function.js';
import { deepFreeze } from './deep-freeze.function.js';
import type { MappingFunction } from './mapping-function.js';
import type { MemoizationFunction } from './memoization-function.js';
import { naiveObjectComparison } from './naive-object-comparison.js';
import { createObservablePart } from '../utils/create-observable-part.function.js';
import { deepFreeze } from '../utils/deep-freeze.function.js';
import type { MappingFunction } from '../types/mapping-function.type.js';
import type { MemoizationFunction } from '../types/memoization-function.type.js';
import { naiveObjectComparison } from '../utils/naive-object-comparison.function.js';

/**
* @export
Expand All @@ -13,14 +13,13 @@ import { naiveObjectComparison } from './naive-object-comparison.js';
* Additionally the Subject ensures the data is unique, not updating any Observes unless there is an actual change of the content.
*/
export class UmbDeepState<T> extends UmbBasicState<T> {

constructor(initialData: T) {
super(deepFreeze(initialData));
}

asObservablePart<ReturnType>(
mappingFunction: MappingFunction<T, ReturnType>,
memoizationFunction?: MemoizationFunction<ReturnType>
memoizationFunction?: MemoizationFunction<ReturnType>,
) {
return createObservablePart(this._subject, mappingFunction, memoizationFunction);
}
Expand Down
8 changes: 8 additions & 0 deletions src/libs/observable-api/states/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './basic-state.js';
export * from './boolean-state.js';
export * from './number-state.js';
export * from './string-state.js';
export * from './class-state.js';
export * from './deep-state.js';
export * from './array-state.js';
export * from './object-state.js';
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/libs/observable-api/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './mapping-function.type.js';
export * from './memoization-function.type.js';
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MemoizationFunction } from './memoization-function.js';
import { MappingFunction } from './mapping-function.js';
import { defaultMemoization } from './default-memoization.js';
import { MemoizationFunction } from '../types/memoization-function.type.js';
import { MappingFunction } from '../types/mapping-function.type.js';
import { defaultMemoization } from './default-memoization.function.js';
import { distinctUntilChanged, map, Observable, shareReplay } from '@umbraco-cms/backoffice/external/rxjs';

/**
Expand All @@ -17,11 +17,11 @@ import { distinctUntilChanged, map, Observable, shareReplay } from '@umbraco-cms
export function createObservablePart<R, T>(
source: Observable<T>,
mappingFunction: MappingFunction<T, R>,
memoizationFunction?: MemoizationFunction<R>
memoizationFunction?: MemoizationFunction<R>,
): Observable<R> {
return source.pipe(
map(mappingFunction),
distinctUntilChanged(memoizationFunction || defaultMemoization),
shareReplay(1)
shareReplay(1),
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { naiveObjectComparison } from './naive-object-comparison.js';
import { naiveObjectComparison } from './naive-object-comparison.function.js';

export function defaultMemoization(previousValue: any, currentValue: any): boolean {
if (typeof previousValue === 'object' && typeof currentValue === 'object') {
Expand Down
8 changes: 8 additions & 0 deletions src/libs/observable-api/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './append-to-frozen-array.function.js';
export * from './create-observable-part.function.js';
export * from './deep-freeze.function.js';
export * from './default-memoization.function.js';
export * from './filter-frozen-array.function.js';
export * from './naive-object-comparison.function.js';
export * from './partial-update-frozen-array.function.js';
export * from './push-to-unique-array.function.js';
6 changes: 3 additions & 3 deletions src/packages/core/store/store-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
*/
append(item: StoreItemType) {
this._data.append([item]);
const unique = this._data.getUnique(item) as string;
const unique = this._data.getUniqueMethod(item) as string;
this.dispatchEvent(new UmbStoreAppendEvent([unique]));
}

Expand All @@ -42,7 +42,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
*/
appendItems(items: Array<StoreItemType>) {
this._data.append(items);
const uniques = items.map((item) => this._data.getUnique(item)) as Array<string>;
const uniques = items.map((item) => this._data.getUniqueMethod(item)) as Array<string>;
this.dispatchEvent(new UmbStoreAppendEvent(uniques));
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export class UmbStoreBase<StoreItemType = any> extends EventTarget implements Um
* @memberof UmbStoreBase
*/
getItems(uniques: Array<string>) {
return this._data.getValue().filter((item) => uniques.includes(this._data.getUnique(item) as string));
return this._data.getValue().filter((item) => uniques.includes(this._data.getUniqueMethod(item) as string));
}

/**
Expand Down

0 comments on commit ad35ba2

Please sign in to comment.