diff --git a/src/processes/adapter/pause.ts b/src/processes/adapter/pause.ts new file mode 100644 index 00000000..8a4518a7 --- /dev/null +++ b/src/processes/adapter/pause.ts @@ -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 + }); + } + +} diff --git a/src/processes/index.ts b/src/processes/index.ts index 953cce43..9bc0ebb3 100644 --- a/src/processes/index.ts +++ b/src/processes/index.ts @@ -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'; @@ -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, }; diff --git a/src/workflow-transducer.ts b/src/workflow-transducer.ts index 4bfa0060..660c61de 100644 --- a/src/workflow-transducer.ts +++ b/src/workflow-transducer.ts @@ -13,6 +13,7 @@ import { Insert, Replace, Update, + Pause, Fix, Start, PreFetch, @@ -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); diff --git a/src/workflow.ts b/src/workflow.ts index 0e5388f3..46d76586 100644 --- a/src/workflow.ts +++ b/src/workflow.ts @@ -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, @@ -100,6 +100,11 @@ export class Workflow { 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); }