chrono-forge
Version:
A comprehensive framework for building resilient Temporal workflows, advanced state management, and real-time streaming activities in TypeScript. Designed for a seamless developer experience with powerful abstractions, dynamic orchestration, and full cont
115 lines (114 loc) • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEntityName = exports.limitRecursion = void 0;
const normalizr_1 = require("normalizr");
const SchemaManager_1 = require("../store/SchemaManager");
const StateManager_1 = require("../store/StateManager");
const limitRecursion = (entityId, entityName, entities, stateManager = false, visited = new Map(), depth = 0) => {
const schema = SchemaManager_1.SchemaManager.schemas[entityName];
const entity = entities[entityName]?.[entityId];
if (!entity) {
return entityId;
}
const entityKey = `${entityName}::${entityId}`;
const visitedDepth = visited.get(entityKey);
if (visitedDepth !== undefined && visitedDepth <= depth) {
return entityId;
}
visited.set(entityKey, depth);
if (stateManager instanceof StateManager_1.StateManager && depth === 0 && stateManager.cache.has(entityKey)) {
return stateManager.cache.get(entityKey);
}
const result = Object.create({});
const schemaRelations = schema.schema;
for (const key of Object.keys(entity)) {
const value = entity[key];
const relation = schemaRelations[key];
if (relation) {
const relationName = (0, exports.getEntityName)(relation);
if (Array.isArray(value)) {
const lazyArray = [...value];
value.forEach((childId, index) => {
Object.defineProperty(lazyArray, index, {
enumerable: true,
get: function () {
let valueId;
if (typeof childId === 'string' || typeof childId === 'number') {
valueId = childId;
}
else if ('idAttribute' in relation && typeof relation.idAttribute === 'function') {
valueId = childId[relation.idAttribute(childId)];
}
else {
valueId = childId.id;
}
const resolvedValue = (0, exports.limitRecursion)(valueId, relationName, entities, stateManager, new Map(visited), depth + 1);
Object.defineProperty(lazyArray, index, {
enumerable: true,
value: resolvedValue,
writable: true,
configurable: true
});
return resolvedValue;
},
configurable: true
});
});
result[key] = lazyArray;
}
else {
Object.defineProperty(result, key, {
enumerable: true,
get: function () {
let valueId;
if (typeof value === 'string' || typeof value === 'number') {
valueId = value;
}
else if ('idAttribute' in relation && typeof relation.idAttribute === 'function') {
valueId = value[relation.idAttribute(value)];
}
else {
valueId = value.id;
}
const resolvedValue = (0, exports.limitRecursion)(valueId, relationName, entities, stateManager, new Map(visited), depth + 1);
Object.defineProperty(result, key, {
enumerable: true,
value: resolvedValue,
writable: true,
configurable: true
});
return resolvedValue;
},
configurable: true
});
}
}
else {
result[key] = value;
}
}
if (stateManager instanceof StateManager_1.StateManager && depth === 0) {
stateManager.cache.set(entityKey, result);
}
return result;
};
exports.limitRecursion = limitRecursion;
const getEntityName = (relation) => {
if (Array.isArray(relation)) {
return (0, exports.getEntityName)(relation[0]);
}
else if (relation instanceof normalizr_1.schema.Entity) {
return relation.key;
}
else if (typeof relation === 'string') {
return relation;
}
else if (relation && typeof relation === 'object' && 'relatedEntityName' in relation) {
return relation.relatedEntityName;
}
else {
console.error(relation);
throw new Error('Unknown schema relation type');
}
};
exports.getEntityName = getEntityName;