Skip to content

Commit

Permalink
0.7.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pilsy committed Sep 16, 2024
1 parent 945d413 commit c15be5c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 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.2",
"version": "0.7.5",
"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
2 changes: 2 additions & 0 deletions src/SchemaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class SchemaManager extends EventEmitter {
public static getInstance(workflowId?: string): SchemaManager {
if (!this.instance) {
this.instance = new SchemaManager(workflowId);
} else {
this.instance.workflowId = String(workflowId);
}
return this.instance;
}
Expand Down
7 changes: 5 additions & 2 deletions src/workflows/StatefulWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ManagedPath = {
path?: string;
workflowType?: string;
idAttribute?: string | string[];
includeParentId?: boolean;
autoStartChildren?: boolean;
cancellationType?: workflow.ChildWorkflowCancellationType;
parentClosePolicy?: workflow.ParentClosePolicy;
Expand Down Expand Up @@ -561,6 +562,7 @@ export abstract class StatefulWorkflow<
workflowType,
entityName,
idAttribute,
includeParentId,
cancellationType = workflow.ChildWorkflowCancellationType.WAIT_CANCELLATION_COMPLETED,
parentClosePolicy = workflow.ParentClosePolicy.PARENT_CLOSE_POLICY_REQUEST_CANCEL,
autoStartChildren
Expand All @@ -578,7 +580,7 @@ export abstract class StatefulWorkflow<
const data = typeof config.processData === 'function' ? config.processData(rawData, this) : rawData;
const { [idAttribute as string]: id, ...rest } = state;
const compositeId = Array.isArray(idAttribute) ? getCompositeKey(data, idAttribute) : id;
const workflowId = `${entityName}-${compositeId}`;
const workflowId = includeParentId ? `${entityName}-${compositeId}-${this.id}` : `${entityName}-${compositeId}`;

if (this.ancestorWorkflowIds.includes(workflowId)) {
this.log.warn(
Expand Down Expand Up @@ -651,6 +653,7 @@ export abstract class StatefulWorkflow<
workflowType,
entityName,
idAttribute,
includeParentId,
cancellationType = workflow.ChildWorkflowCancellationType.WAIT_CANCELLATION_COMPLETED,
parentClosePolicy = workflow.ParentClosePolicy.PARENT_CLOSE_POLICY_REQUEST_CANCEL,
autoStartChildren
Expand All @@ -668,7 +671,7 @@ export abstract class StatefulWorkflow<
const data = typeof config.processData === 'function' ? config.processData(rawData, this) : rawData;
const { [idAttribute as string]: id } = state;
const compositeId = Array.isArray(config.idAttribute) ? getCompositeKey(data, config.idAttribute) : state[config.idAttribute as string];
const workflowId = `${entityName}-${compositeId}`;
const workflowId = includeParentId ? `${entityName}-${compositeId}-${this.id}` : `${entityName}-${compositeId}`;

if (this.ancestorWorkflowIds.includes(workflowId)) {
this.log.warn(
Expand Down
32 changes: 20 additions & 12 deletions src/workflows/Workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@ export abstract class Workflow<P = unknown, O = unknown> extends EventEmitter {

// @ts-ignore
const hooks: { before: { [name: string]: string[] }; after: { [name: string]: string[] } } = this.collectHookMetadata(
HOOKS_METADATA_KEY,
this.constructor.prototype
);

Expand Down Expand Up @@ -598,22 +597,31 @@ export abstract class Workflow<P = unknown, O = unknown> extends EventEmitter {
return collectedMetadata;
}

private collectHookMetadata(metadataKey: Symbol, target: any): Record<string, HookMetadata> {
const collectedMetadata: Record<string, HookMetadata> = {};
private collectHookMetadata(target: any): { [key: string]: { before: string[]; after: string[] } } {
const collectedMetadata: { [key: string]: { before: string[]; after: string[] } } = {};

const protoChain: any[] = [];
let currentProto = target;

// Build the prototype chain from base to derived
while (currentProto && currentProto !== Workflow.prototype) {
const metadata = Reflect.getOwnMetadata(metadataKey, currentProto);
if (metadata) {
for (const key in metadata) {
if (metadata.hasOwnProperty(key)) {
collectedMetadata[key] = collectedMetadata[key] || { before: [], after: [] };
collectedMetadata[key].before.push(...(metadata[key].before || []));
collectedMetadata[key].after.push(...(metadata[key].after || []));
}
protoChain.unshift(currentProto); // Push at the start to reverse the order
currentProto = Object.getPrototypeOf(currentProto);
}

// Collect metadata from each prototype in the chain in order
for (const proto of protoChain) {
const metadata = Reflect.getOwnMetadata(HOOKS_METADATA_KEY, proto) || {};

for (const key of Object.keys(metadata)) {
// Ensure that collectedMetadata has the structure defined
if (!collectedMetadata[key]) {
collectedMetadata[key] = { before: [], after: [] };
}

collectedMetadata[key].before.push(...(metadata[key].before || []));
collectedMetadata[key].after.push(...(metadata[key].after || []));
}
currentProto = Object.getPrototypeOf(currentProto);
}

return collectedMetadata;
Expand Down

0 comments on commit c15be5c

Please sign in to comment.