Skip to content

Commit

Permalink
Change Workflow construction methods
Browse files Browse the repository at this point in the history
- constructor is now private
- create and restore are static functions that return a Workflow instance
- getSnapshot is also static now
- updated parameters of the modified functions
  • Loading branch information
FyreByrd committed Sep 30, 2024
1 parent 0d2f588 commit 47c0e0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
49 changes: 23 additions & 26 deletions source/SIL.AppBuilder.Portal/common/workflow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,54 @@ export class Workflow {
private adminLevel: RequiredAdminLevel;
private productType: ProductType;

constructor(productId: string) {
private constructor(productId: string, input: WorkflowInput) {
this.productId = productId;
this.currentState = null;
this.flow = null;
this.adminLevel = input.adminLevel;
this.productType = input.productType;
}

/* PUBLIC METHODS */
/** Create a new workflow instance and populate the database tables. */
public async create(input: WorkflowInput, productId?: string): Promise<void> {
this.flow?.stop();
this.currentState = null;
this.productId = productId ?? this.productId;
this.adminLevel = input.adminLevel;
this.productType = input.productType;

public static async create(productId: string, input: WorkflowInput): Promise<Workflow> {
DatabaseWrites.workflowInstances.upsert({
where: {
ProductId: this.productId
ProductId: productId
},
update: {},
create: {
Snapshot: '',
ProductId: this.productId
ProductId: productId
}
});

this.flow = createActor(DefaultWorkflow, {
const flow = new Workflow(productId, input);

flow.flow = createActor(DefaultWorkflow, {
inspect: (e) => {
if (e.type === '@xstate.snapshot') this.inspect(e);
if (e.type === '@xstate.snapshot') flow.inspect(e);
},
input: input
});

this.flow.start();
flow.flow.start();

return flow;
}
/** Restore from a snapshot in the database. */
public async restore(): Promise<void> {
this.flow?.stop();
this.currentState = null;
const snap = await this.getSnapshot();
this.flow = createActor(DefaultWorkflow, {
public static async restore(productId: string): Promise<Workflow> {
const snap = await Workflow.getSnapshot(productId);
const flow = new Workflow(productId, snap.input);
flow.flow = createActor(DefaultWorkflow, {
snapshot: snap ? DefaultWorkflow.resolveState(snap) : undefined,
inspect: (e) => {
if (e.type === '@xstate.snapshot') this.inspect(e);
if (e.type === '@xstate.snapshot') flow.inspect(e);
},
input: snap.input
});

this.flow.start();
flow.flow.start();

return flow;
}

/** Send a transition event to the workflow. */
Expand All @@ -99,21 +98,19 @@ export class Workflow {
}

/** Retrieves the workflow's snapshot from the database and sets the `WorkflowAdminLevel` and `ProductType` */
public async getSnapshot(): Promise<Snapshot> {
public static async getSnapshot(productId: string): Promise<Snapshot> {
const snap = JSON.parse(
(
await prisma.workflowInstances.findUnique({
where: {
ProductId: this.productId
ProductId: productId
},
select: {
Snapshot: true
}
})
)?.Snapshot || 'null'
) as Snapshot | null;
this.adminLevel = snap?.input.adminLevel;
this.productType = snap?.input.productType;
return snap;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export const load: PageServerLoad = async ({ params, url, locals }) => {
}
});

const flow = new Workflow(params.product_id);
const flow = await Workflow.restore(params.product_id);

const snap = await flow.getSnapshot();
const snap = await Workflow.getSnapshot(params.product_id);

return {
instance: instance,
Expand All @@ -68,8 +68,7 @@ export const actions = {
const form = await superValidate(request, valibot(jumpStateSchema));
if (!form.valid) return fail(400, { form, ok: false });

const flow = new Workflow(params.product_id);
await flow.restore();
const flow = await Workflow.restore(params.product_id);

flow.send({
type: 'Jump',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ type Fields = {
export const load = (async ({ params, url, locals }) => {
const session = await locals.auth();
// TODO: permission check
const flow = new Workflow(params.product_id);
await flow.restore();

const snap = await flow.getSnapshot();
const flow = await Workflow.restore(params.product_id);
const snap = await Workflow.getSnapshot(params.product_id);

const product = await prisma.products.findUnique({
where: {
Expand Down Expand Up @@ -176,8 +174,7 @@ export const actions = {
const form = await superValidate(request, valibot(sendActionSchema));
if (!form.valid) return fail(400, { form, ok: false });

const flow = new Workflow(params.product_id);
await flow.restore();
const flow = await Workflow.restore(params.product_id);

//double check that state matches current snapshot
if (form.data.state === flow.state()) {
Expand Down

0 comments on commit 47c0e0f

Please sign in to comment.