Skip to content

Commit

Permalink
Fix issue with limitRecursion and its usage converting circular struc…
Browse files Browse the repository at this point in the history
…tures
  • Loading branch information
pilsy committed Sep 18, 2024
1 parent 69f1285 commit 69a9211
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrono-forge",
"version": "0.7.14",
"version": "0.7.15",
"description": "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 control over distributed systems.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
19 changes: 11 additions & 8 deletions src/utils/limitRecursion.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import isObject from 'lodash.isobject';
import { isObject } from 'lodash';

export const limitRecursion = (ob: Record<string, any>, root: Record<string, any>) => {
const seen = new WeakSet();

const recurse = (obj: Record<string, any>, rootEntities: Record<string, any>) => {
if (!obj || typeof obj !== 'object') return obj;

// Check if we have already processed this object
if (seen.has(obj)) {
return obj?.id || null; // Return the ID or null to stop recursion
const idAttribute = rootEntities[rootEntities.length - 1].idAttribute || 'id';
return obj?.[idAttribute] || null;
}
seen.add(obj); // Mark this object as processed
seen.add(obj);

const rootEntity = rootEntities[rootEntities.length - 1];

return Object.entries(obj).reduce((acc, [key, value]): any => {
if (rootEntity.schema[key]) {
const subEntity = rootEntity.schema[key] instanceof Array ? rootEntity.schema[key][0] : rootEntity.schema[key];
const subEntities = rootEntity.schema[key] instanceof Array ? obj[key] : obj[key];
const subEntity = Array.isArray(rootEntity.schema[key]) ? rootEntity.schema[key][0] : rootEntity.schema[key];
const subEntities = Array.isArray(rootEntity.schema[key]) ? obj[key] : obj[key];

if (rootEntities[0] !== subEntity) {
rootEntities.push(subEntity);
if (!subEntities || subEntities === null) {
return acc;
}

return {
...acc,
[key]: Array.isArray(subEntities)
Expand All @@ -33,14 +34,16 @@ export const limitRecursion = (ob: Record<string, any>, root: Record<string, any
: recurse(subEntities, rootEntities)
};
}

const idAttribute = rootEntities[0].idAttribute || 'id';
return {
...acc,
[key]: Array.isArray(subEntities)
? // @ts-ignore
subEntities.map((s: any): any => (isObject(s) ? s?.id : s))
subEntities.map((s: any): any => (isObject(s) ? s?.[idAttribute] : s))
: isObject(subEntities)
? // @ts-ignore
subEntities?.id
subEntities?.[idAttribute]
: subEntities
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/workflows/StatefulWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ export abstract class StatefulWorkflow<
}

const entitySchema = SchemaManager.getInstance().getSchema(entityName as string);
const rawData = limitRecursion(denormalize(state, entitySchema, newState), entitySchema);
const data = typeof config.processData === 'function' ? config.processData(rawData, this) : rawData;
const rawData = denormalize(state, entitySchema, newState);
const data = limitRecursion(typeof config.processData === 'function' ? config.processData(rawData, this) : rawData, entitySchema);
const { [idAttribute as string]: id, ...rest } = state;
const compositeId = Array.isArray(idAttribute) ? getCompositeKey(data, idAttribute) : id;
const workflowId = includeParentId ? `${entityName}-${compositeId}-${this.id}` : `${entityName}-${compositeId}`;
Expand Down Expand Up @@ -706,8 +706,8 @@ export abstract class StatefulWorkflow<
}

const entitySchema = SchemaManager.getInstance().getSchema(entityName as string);
const rawData = limitRecursion(denormalize(state, entitySchema, newState), entitySchema);
const data = typeof config.processData === 'function' ? config.processData(rawData, this) : rawData;
const rawData = denormalize(state, entitySchema, newState);
const data = limitRecursion(typeof config.processData === 'function' ? config.processData(rawData, this) : rawData, entitySchema);
const { [idAttribute as string]: id } = state;
const compositeId = Array.isArray(config.idAttribute) ? getCompositeKey(data, config.idAttribute) : state[config.idAttribute as string];
const workflowId = includeParentId ? `${entityName}-${compositeId}-${this.id}` : `${entityName}-${compositeId}`;
Expand Down

0 comments on commit 69a9211

Please sign in to comment.