@elsikora/cladi
Version:
Zero-dependency TypeScript DI toolkit with typed tokens and scoped lifecycles.
154 lines (150 loc) • 6.55 kB
JavaScript
'use strict';
var toError_utility = require('../../../utility/to-error.utility.js');
class ResolutionEngine {
ASSERT_KEY;
ENSURE_ACTIVE;
ENSURE_NOT_DISPOSING;
GET_SCOPE_ID;
NOTIFY_ON_ERROR;
NOTIFY_ON_START;
NOTIFY_ON_SUCCESS;
ON_ASYNC_RESOLUTION_END;
ON_ASYNC_RESOLUTION_START;
RESOLVE_ALL_ASYNC_INTERNAL;
RESOLVE_ALL_SYNC_INTERNAL;
RESOLVE_ASYNC_INTERNAL;
RESOLVE_SYNC_INTERNAL;
constructor(options) {
this.ASSERT_KEY = options.assertKey;
this.ENSURE_ACTIVE = options.ensureActive;
this.ENSURE_NOT_DISPOSING = options.ensureNotDisposing;
this.GET_SCOPE_ID = options.getScopeId;
this.NOTIFY_ON_ERROR = options.notifyOnError;
this.NOTIFY_ON_START = options.notifyOnStart;
this.NOTIFY_ON_SUCCESS = options.notifyOnSuccess;
this.ON_ASYNC_RESOLUTION_END = options.onAsyncResolutionEnd;
this.ON_ASYNC_RESOLUTION_START = options.onAsyncResolutionStart;
this.RESOLVE_ALL_ASYNC_INTERNAL = options.resolveAllAsyncInternal;
this.RESOLVE_ALL_SYNC_INTERNAL = options.resolveAllSyncInternal;
this.RESOLVE_ASYNC_INTERNAL = options.resolveAsyncInternal;
this.RESOLVE_SYNC_INTERNAL = options.resolveSyncInternal;
}
resolve(dependencyKey) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, false, false, false, scopeId);
try {
const resolvedValue = this.RESOLVE_SYNC_INTERNAL(dependencyKeySymbol, [], new Set());
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, false, false, false, scopeId, resolvedValue);
return resolvedValue;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, false, false, false, scopeId, toError_utility.toError(error));
throw error;
}
}
resolveAll(dependencyKey) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, false, true, false, scopeId);
try {
const resolvedValues = this.RESOLVE_ALL_SYNC_INTERNAL(dependencyKeySymbol, [], new Set());
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, false, true, false, scopeId, resolvedValues);
return resolvedValues;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, false, true, false, scopeId, toError_utility.toError(error));
throw error;
}
}
async resolveAllAsync(dependencyKey) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, true, true, false, scopeId);
this.ON_ASYNC_RESOLUTION_START();
try {
const resolvedValues = (await this.RESOLVE_ALL_ASYNC_INTERNAL(dependencyKeySymbol, [], new Set()));
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, true, true, false, scopeId, resolvedValues);
return resolvedValues;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, true, true, false, scopeId, toError_utility.toError(error));
throw error;
}
finally {
this.ON_ASYNC_RESOLUTION_END();
}
}
async resolveAsync(dependencyKey) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, true, false, false, scopeId);
this.ON_ASYNC_RESOLUTION_START();
try {
const resolvedValue = (await this.RESOLVE_ASYNC_INTERNAL(dependencyKeySymbol, [], new Set()));
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, true, false, false, scopeId, resolvedValue);
return resolvedValue;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, true, false, false, scopeId, toError_utility.toError(error));
throw error;
}
finally {
this.ON_ASYNC_RESOLUTION_END();
}
}
resolveOptional(dependencyKey, hasRegistration) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, false, false, true, scopeId);
if (!hasRegistration(dependencyKeySymbol)) {
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, false, false, true, scopeId);
return undefined;
}
try {
const resolvedValue = this.RESOLVE_SYNC_INTERNAL(dependencyKeySymbol, [], new Set());
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, false, false, true, scopeId, resolvedValue);
return resolvedValue;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, false, false, true, scopeId, toError_utility.toError(error));
throw error;
}
}
async resolveOptionalAsync(dependencyKey, hasRegistration) {
this.ENSURE_NOT_DISPOSING();
this.ENSURE_ACTIVE();
const dependencyKeySymbol = this.ASSERT_KEY(dependencyKey);
const scopeId = this.GET_SCOPE_ID();
this.NOTIFY_ON_START(dependencyKeySymbol, true, false, true, scopeId);
if (!hasRegistration(dependencyKeySymbol)) {
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, true, false, true, scopeId);
return undefined;
}
this.ON_ASYNC_RESOLUTION_START();
try {
const resolvedValue = (await this.RESOLVE_ASYNC_INTERNAL(dependencyKeySymbol, [], new Set()));
this.NOTIFY_ON_SUCCESS(dependencyKeySymbol, true, false, true, scopeId, resolvedValue);
return resolvedValue;
}
catch (error) {
this.NOTIFY_ON_ERROR(dependencyKeySymbol, true, false, true, scopeId, toError_utility.toError(error));
throw error;
}
finally {
this.ON_ASYNC_RESOLUTION_END();
}
}
}
exports.ResolutionEngine = ResolutionEngine;
//# sourceMappingURL=resolution.class.js.map