Skip to content

Commit

Permalink
issue-56 pause process implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilt committed Jan 14, 2024
1 parent 095064f commit d9df49a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/processes/adapter/pause.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Scroller } from '../../scroller';
import { BaseAdapterProcessFactory, AdapterProcess, ProcessStatus } from '../misc/index';

export default class Pause extends BaseAdapterProcessFactory(AdapterProcess.pause) {

static run(scroller: Scroller, options?: { resume: boolean }): void {
const resume = !!options?.resume;

if (!resume) {
if (!scroller.state.paused.get()) {
scroller.state.paused.set(true);
scroller.logger.log('pause scroller');
} else {
scroller.logger.log('pause scroller (cancelled)');
}
return;
}

if (!scroller.state.paused.get()) {
scroller.logger.log('resume scroller (cancelled)');
return;
}

scroller.state.paused.set(false);
scroller.logger.log('resume scroller');

scroller.workflow.call({
process: AdapterProcess.pause,
status: ProcessStatus.done
});
}

}
3 changes: 2 additions & 1 deletion src/processes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import UserClip from './adapter/clip';
import Insert from './adapter/insert';
import Replace from './adapter/replace';
import Update from './adapter/update';
import Pause from './adapter/pause';
import Fix from './adapter/fix';
import Start from './start';
import PreFetch from './preFetch';
Expand All @@ -24,7 +25,7 @@ import { CommonProcess, AdapterProcess, ProcessStatus } from './misc/enums';

export {
Init, Scroll,
Reset, Reload, Append, Check, Remove, UserClip, Insert, Replace, Update, Fix,
Reset, Reload, Append, Check, Remove, UserClip, Insert, Replace, Update, Pause, Fix,
Start, PreFetch, Fetch, PostFetch, Render, PreClip, Clip, Adjust, End,
CommonProcess, AdapterProcess, ProcessStatus,
};
9 changes: 9 additions & 0 deletions src/workflow-transducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Insert,
Replace,
Update,
Pause,
Fix,
Start,
PreFetch,
Expand Down Expand Up @@ -131,6 +132,14 @@ export const runStateMachine = ({
run(Init)(process);
}
break;
case AdapterProcess.pause:
if (status === Status.start) {
run(Pause)(options);
}
if (status === Status.done) {
run(Init)(process);
}
break;
case AdapterProcess.fix:
if (status === Status.start) {
run(Fix)(options);
Expand Down
7 changes: 6 additions & 1 deletion src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Scroller } from './scroller';
import { runStateMachine } from './workflow-transducer';
import { Reactive } from './classes/reactive';
import { Item } from './classes/item';
import { CommonProcess, ProcessStatus as Status, } from './processes/index';
import { AdapterProcess, CommonProcess, ProcessStatus as Status, } from './processes/index';
import { WORKFLOW, validate } from './inputs/index';
import {
WorkflowParams,
Expand Down Expand Up @@ -100,6 +100,11 @@ export class Workflow<ItemData = unknown> {
return;
}
const { process, status } = processSubject;
// if the scroller is paused, any process other than "pause" and "reset" should be blocked
if (this.scroller.state.paused.get() && process !== AdapterProcess.pause && process !== AdapterProcess.reset) {
this.scroller.logger.log('scroller is paused: ' + process + ' process is ignored');
return;
}
if (process && process.startsWith('adapter') && status !== Status.next) {
this.adapterRun$.set(processSubject);
}
Expand Down

0 comments on commit d9df49a

Please sign in to comment.