Skip to content

Commit

Permalink
feat(core): add return value to flow commands
Browse files Browse the repository at this point in the history
  • Loading branch information
blakebyrnes committed Mar 2, 2022
1 parent dcd8cad commit 029e676
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
8 changes: 4 additions & 4 deletions client/lib/CoreTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,18 @@ export default class CoreTab implements IJsPathEventTarget {
await this.commandQueue.runOutOfBand('Tab.registerFlowHandler', name, id, callsitePath);
}

public async runFlowCommand(
commandFn: () => Promise<void>,
public async runFlowCommand<T>(
commandFn: () => Promise<T>,
exitState: IDomState | DomState | IDomStateAllFn,
callsitePath: ISourceCodeLocation[],
options?: IFlowCommandOptions
): Promise<void> {
): Promise<T> {
if (typeof exitState === 'function') {
exitState = { all: exitState };
}
const flowCommand = await this.flowCommands.create(commandFn, exitState, callsitePath, options);

await flowCommand.run();
return await flowCommand.run();
}

public async shouldRetryFlowHandlers(
Expand Down
15 changes: 9 additions & 6 deletions client/lib/FlowCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ISourceCodeLocation from '@ulixee/commons/interfaces/ISourceCodeLocation'
import IDomState from '@ulixee/hero-interfaces/IDomState';
import IFlowCommandOptions from '@ulixee/hero-interfaces/IFlowCommandOptions';

export default class FlowCommand implements IFlowCommand {
export default class FlowCommand<T= void> implements IFlowCommand {
public retryNumber = 0;

public get isComplete(): Promise<boolean> {
Expand All @@ -29,10 +29,11 @@ export default class FlowCommand implements IFlowCommand {
public isFlowStateChanged = false;

private readonly exitHandler: DomStateHandler;
private lastResult: T;

constructor(
private readonly coreTab: CoreTab,
private runCommandsFn: () => Promise<void>,
private runCommandsFn: () => Promise<T>,
exitState: IDomState,
readonly id: number,
readonly parent: FlowCommand,
Expand All @@ -47,9 +48,11 @@ export default class FlowCommand implements IFlowCommand {
}
}

async run(): Promise<void> {
async run(): Promise<T> {
// if we have previously tried this and it's still valid, break out
if (this.retryNumber > 0 && !!this.exitHandler && (await this.isComplete)) return;
if (this.retryNumber > 0 && !!this.exitHandler && (await this.isComplete)) {
return this.lastResult;
}

// Retry until isComplete is satisfied, or we have retried a max number of times
for (let count = 0; count < this.options.maxRetries; count += 1) {
Expand All @@ -58,9 +61,9 @@ export default class FlowCommand implements IFlowCommand {
this.isFlowStateChanged = false; // clear out any flow state changes
this.retryNumber += count; // add to retry count because we might be nested
this.setCommandState();
await this.runCommandsFn();
this.lastResult = await this.runCommandsFn();

if (await this.isComplete) return;
if (await this.isComplete) return this.lastResult;

if (this.isFlowStateChanged) continue;
// if not complete, trigger flow handlers to retry (catch will trigger on its own)
Expand Down
12 changes: 6 additions & 6 deletions client/lib/FlowCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import IDomState from '@ulixee/hero-interfaces/IDomState';
import IFlowCommandOptions from '@ulixee/hero-interfaces/IFlowCommandOptions';

export default class FlowCommands {
private readonly flowCommands: FlowCommand[] = [];
private readonly flowCommands: FlowCommand<any>[] = [];

public get runningFlowCommand(): FlowCommand {
return this.flowCommands.find(x => x.isRunning);
Expand All @@ -17,21 +17,21 @@ export default class FlowCommands {

constructor(private readonly coreTab: CoreTab) {}

public async create(
commandFn: () => Promise<void>,
public async create<T>(
commandFn: () => Promise<T>,
exitState: IDomState,
callsitePath: ISourceCodeLocation[],
options: IFlowCommandOptions,
): Promise<FlowCommand> {
): Promise<FlowCommand<T>> {
const id = this.flowCommands.length + 1;
const parentFlow = this.runningFlowCommand;

let flowCommand: FlowCommand;
let flowCommand: FlowCommand<T>;
if (parentFlow && parentFlow.retryNumber > 0) {
const callsiteJson = JSON.stringify(callsitePath);
flowCommand = this.flowCommands.find(
x => x.parentId === parentFlow.id && callsiteJson === JSON.stringify(x.callsitePath),
);
) as any;
flowCommand.retryNumber += 1;
return flowCommand;
} else {
Expand Down
18 changes: 9 additions & 9 deletions client/lib/Tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,20 +281,20 @@ export default class Tab extends AwaitedEventTarget<IEventType> {
await coreTab.registerFlowHandler(name, state, handlerFn, callsitePath);
}

public async flowCommand(
commandFn: () => Promise<void>,
public async triggerFlowHandlers(): Promise<void> {
const coreTab = await this.#coreTabPromise;
await coreTab.triggerFlowHandlers();
}

public async flowCommand<T = void>(
commandFn: () => Promise<T>,
exitState?: IDomState | DomState | IDomStateAllFn,
options?: IFlowCommandOptions
): Promise<void> {
): Promise<T> {
const callsitePath = scriptInstance.getScriptCallsite();

const coreTab = await this.#coreTabPromise;
await coreTab.runFlowCommand(commandFn, exitState, callsitePath, options);
}

public async triggerFlowHandlers(): Promise<void> {
const coreTab = await this.#coreTabPromise;
await coreTab.triggerFlowHandlers();
return await coreTab.runFlowCommand(commandFn, exitState, callsitePath, options);
}

public waitForResource(
Expand Down

0 comments on commit 029e676

Please sign in to comment.