0.2.1 • Published 2 years ago

@agoyu/procaser v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Procaser

To combine processing into a single callback function which can be handled by a switch-case statement.

Installation

npm i @agoyu/procaser

Usage

import {Procaser} from '@agoyu/procaser';

  new Procaser(null, (proc, step, props) => {
    switch (step) {
      case proc.BOOT:
        proc.next('MyStep');
        break;
      case 'start@MyStep':
        // write your process management
        // ...
      case proc.EXIT:
        break;
    }
  });

Example

processJob(willSuccess) {
  const myProps = {
    name: 'Potesuke',
    email: 'pote@example.com'
  };
  
  const caser = new Procaser(
    myProps,
    (proc, step, props) => {
      console.log('step:' + step);
      switch (step) {
        case proc.BOOT:
          proc.next('Process');
          break;

        case 'start@Process':
          this.showProgressPopup({
            title: 'Processing',
            caption: 'Please Wait',
          });
          this.requestProcess(proc, willSuccess);
          break;
        case 'end@Process':
          this.showProgressPopup(false);
          break;
        case 'success@Process':
          proc.next('Complete');
          break;
        case 'failed@Process':
          proc.next('Failed');
          break;

        case 'start@Complete':
          this.showConfirmPopup(
            {
              title: 'Process succeeded',
              caption: 'You made it!',
              yes: 'OK',
              no: false,
            },
            proc);
          break;
        case 'end@Complete':
          this.showConfirmPopup(false);
          break;
        case 'confirm@Complete':
          proc.exit();
          break;

        case 'start@Failed':
          this.showConfirmPopup(
            {
              title: 'Process failed',
              caption: 'Try again!',
              yes: 'OK',
              no: false,
            },
            proc);
          break;
        case 'end@Failed':
          this.showConfirmPopup(false);
          break;
        case 'confirm@Failed':
          proc.exit();
          break;
        
        case proc.EXIT:
          break;
      }
    }
  );
}

/**
 * Send the signal to the Procaser after 2 seconds
 * @param {Procaser} proc 
 * @param {boolean} willSuccess 
 */
 requestProcess(proc, willSuccess) {
  setTimeout(() => {
    if (willSuccess) {
      proc.signal('success');
    } else {
      proc.signal('error');
    }
  }, 2 * 1000);
}

Methods

constructor

constructor

Parameters

  • props (object | null) The object you can deal with in the Procaser instance. (optional, default {})
  • callback Function Function to be called back when the step or state is changed (optional, default (step:string,proc:Procaser,props:object)=>{})

next

Moving on to the next step, the callback passes through "end@currentStep" and "start@nextStep" case statements

Parameters

  • stepName (string | undefined) Next step name
  • props object Values to be assigned to the properties (optional, default {})
  • wait number Millisecond time before the callback on the next step starting. If negative, immediately. (optional, default -1)

Returns this

exit

Terminates the process, the callback passes through "end@currentStep" and proc.EXIT case statements

Returns this

signal

Apply state to the current step, the callback passes through "yourState@currentStep" case statement

Parameters

  • state string Any state name
  • props object Values to be assigned to the properties (optional, default {})
  • wait number Millisecond time before the callback passes through the "yourState@currentStep" case statement. If negative, immediately. (optional, default -1)

Returns this

confirm

Apply the 'confirm' state to the current step, the callback passes through "confirm@currentStep" case statement

Parameters

  • props object Values to be assigned to the properties (optional, default {})
  • wait number Millisecond time before the callback passes through the "confirm@currentStep" case statement. If negative, immediately. (optional, default -1)

Returns this

cancel

Apply the 'cancel' state to the current step, the callback passes through "cancel@currentStep" case statement

Parameters

  • props object Values to be assigned to the properties (optional, default {})
  • wait number Millisecond time before the callback passes through the "cancel@currentStep" case statement. If negative, immediately. (optional, default -1)

Returns this

error

Apply the 'error' state to the current step, the callback passes through "error@currentStep" case statement

Parameters

  • stepName string
  • props object Values to be assigned to the properties (optional, default {})
  • wait number Millisecond time before the callback passes through the "error@currentStep" case statement. If negative, immediately. (optional, default -1)

Returns this