feat: create a reusable store for objects

This commit is contained in:
Jacob Overgaard
2024-07-08 17:04:09 +02:00
parent 27fd41d8ef
commit 7c3a9e63a5
2 changed files with 61 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
export * from './store-base.js';
export * from './store-object-base.js';
export * from './store.interface.js';
export * from './events/index.js';

View File

@@ -0,0 +1,60 @@
import { UmbStoreUpdateEvent } from './events/index.js';
import { UmbContextBase } from '@umbraco-cms/backoffice/class-api';
import { type Observable, UmbObjectState } from '@umbraco-cms/backoffice/observable-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
import type { UmbApi } from '@umbraco-cms/backoffice/extension-api';
/**
* The base class for a store that holds an object.
*/
export class UmbStoreObjectBase<T> extends UmbContextBase<never> implements UmbApi {
protected _data: UmbObjectState<T>;
constructor(host: UmbControllerHost, storeAlias: string, initialData?: T) {
super(host, storeAlias);
this._data = new UmbObjectState(initialData ?? ({} as T));
}
/**
* Updates the store with the given data
* @param data - The (partial) data to update the store with
* @memberof UmbStoreObjectBase
*/
update(data: Partial<T>) {
this._data.update(data);
this.dispatchEvent(new UmbStoreUpdateEvent([]));
}
/**
* Returns the current state of the store
* @memberof UmbStoreObjectBase
*/
getState(): T {
return this._data.getValue();
}
/**
* Returns an observable of the store
* @memberof UmbStoreObjectBase
*/
all() {
return this._data.asObservable();
}
/**
* Returns an observable of a part of the store
* @param key - The key of the part to return
* @memberof UmbStoreObjectBase
*/
part<Part extends keyof T>(key: Part): Observable<T[Part]> {
return this._data.asObservablePart((data) => data[key]);
}
/**
* Destroys the store
* @memberof UmbStoreObjectBase
*/
override destroy() {
this._data.destroy();
}
}