@furystack/core
Version:
Core FuryStack package
65 lines • 2.63 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Injectable } from '@furystack/inject';
import { isAsyncDisposable, isDisposable } from '@furystack/utils';
import { AggregatedError } from './errors/aggregated-error.js';
/**
* Manager class for store instances
*/
let StoreManager = class StoreManager {
/**
* Disposes the StoreManager and all store instances
*/
async [Symbol.asyncDispose]() {
const result = await Promise.allSettled([...this.stores.entries()].map(async ([_model, store]) => {
if (isDisposable(store)) {
store[Symbol.dispose]();
}
if (isAsyncDisposable(store)) {
await store[Symbol.asyncDispose]();
}
}));
const fails = result.filter((r) => r.status === 'rejected');
if (fails && fails.length) {
const error = new AggregatedError(`There was an error during disposing ${fails.length} stores: ${fails.map((f) => f.reason).join(', ')}`, fails);
throw error;
}
}
stores = new Map();
/**
* Returns a store model for a constructable object.
* Throws error if no store is registered
* @param model The Constructable object
* @param primaryKey The Primary Key field
* @throws if the Store is not registered
* @returns a Store object
*/
getStoreFor(model, primaryKey) {
const instance = this.stores.get(model);
if (!instance) {
throw Error(`Store not found for '${model.name}'`);
}
if (primaryKey !== instance.primaryKey) {
throw Error('Primary keys not match');
}
return instance;
}
/**
* Adds a store instance to the StoreManager class
* @param store The store to add
* @returns the StoreManager instance for chaining
*/
addStore(store) {
this.stores.set(store.model, store);
return this;
}
};
StoreManager = __decorate([
Injectable({ lifetime: 'singleton' })
], StoreManager);
export { StoreManager };
//# sourceMappingURL=store-manager.js.map