Skip to content

Commit

Permalink
issue-171 base process
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilt committed Dec 1, 2020
1 parent b7c7743 commit b398f32
Show file tree
Hide file tree
Showing 37 changed files with 342 additions and 336 deletions.
34 changes: 17 additions & 17 deletions src/component/classes/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IAdapterProp,
AdapterMethodResult,
IAdapter,
Process,
AdapterProcess,
ProcessStatus,
ItemAdapter,
ItemsPredicate,
Expand All @@ -38,10 +38,10 @@ const fixScalarWanted = (name: string, container: { [key: string]: boolean }) =>
}
};

const convertAppendArgs = (isAppend: boolean, options: any, eof?: boolean) => {
const convertAppendArgs = (prepend: boolean, options: any, eof?: boolean) => {
if (!(options !== null && typeof options === 'object' && options.hasOwnProperty('items'))) {
const items = !Array.isArray(options) ? [options] : options;
options = isAppend ? { items, eof } : { items, bof: eof };
options = prepend ? { items, bof: eof } : { items, eof };
}
return options;
};
Expand Down Expand Up @@ -265,7 +265,7 @@ export class Adapter implements IAdapter {
this.reloadCounter++;
this.logger.logAdapterMethod(`reset`, options, ` of ${this.reloadId}`);
this.workflow.call({
process: Process.reset,
process: AdapterProcess.reset,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -275,36 +275,36 @@ export class Adapter implements IAdapter {
this.reloadCounter++;
this.logger.logAdapterMethod(`reload`, options, ` of ${this.reloadId}`);
this.workflow.call({
process: Process.reload,
process: AdapterProcess.reload,
status: ProcessStatus.start,
payload: options
});
}

append(options: AdapterAppendOptions | any, eof?: boolean): any {
options = convertAppendArgs(true, options, eof); // support old signature
options = convertAppendArgs(false, options, eof); // support old signature
this.logger.logAdapterMethod('append', [options.items, options.eof]);
this.workflow.call({
process: Process.append,
process: AdapterProcess.append,
status: ProcessStatus.start,
payload: options
payload: { prepend: false, options }
});
}

prepend(options: AdapterPrependOptions | any, bof?: boolean): any {
options = convertAppendArgs(false, options, bof); // support old signature
options = convertAppendArgs(true, options, bof); // support old signature
this.logger.logAdapterMethod('prepend', [options.items, options.bof]);
this.workflow.call({
process: Process.prepend,
process: AdapterProcess.append,
status: ProcessStatus.start,
payload: options
payload: { prepend: true, options }
});
}

check(): any {
this.logger.logAdapterMethod('check');
this.workflow.call({
process: Process.check,
process: AdapterProcess.check,
status: ProcessStatus.start
});
}
Expand All @@ -313,7 +313,7 @@ export class Adapter implements IAdapter {
options = convertRemoveArgs(options); // support old signature
this.logger.logAdapterMethod('remove', options);
this.workflow.call({
process: Process.remove,
process: AdapterProcess.remove,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -322,7 +322,7 @@ export class Adapter implements IAdapter {
clip(options?: AdapterClipOptions): any {
this.logger.logAdapterMethod('clip', options);
this.workflow.call({
process: Process.userClip,
process: AdapterProcess.clip,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -331,7 +331,7 @@ export class Adapter implements IAdapter {
insert(options: AdapterInsertOptions): any {
this.logger.logAdapterMethod('insert', options);
this.workflow.call({
process: Process.insert,
process: AdapterProcess.insert,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -340,7 +340,7 @@ export class Adapter implements IAdapter {
replace(options: AdapterReplaceOptions): any {
this.logger.logAdapterMethod('replace', options);
this.workflow.call({
process: Process.replace,
process: AdapterProcess.replace,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -349,7 +349,7 @@ export class Adapter implements IAdapter {
fix(options: AdapterFixOptions): any {
this.logger.logAdapterMethod('fix', options);
this.workflow.call({
process: Process.fix,
process: AdapterProcess.fix,
status: ProcessStatus.start,
payload: options
});
Expand Down
5 changes: 5 additions & 0 deletions src/component/classes/adapter/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export const ADAPTER_PROPS = (nullItem: any): IAdapterProp[] => [
name: 'insert',
value: noop
},
{
type: Prop.WorkflowRunner,
name: 'replace',
value: noop
},
{
type: Prop.WorkflowRunner,
name: 'fix',
Expand Down
8 changes: 4 additions & 4 deletions src/component/classes/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Scroller } from '../scroller';
import { Process, ProcessStatus as Status, ProcessSubject } from '../interfaces/index';
import { CommonProcess, AdapterProcess, ProcessStatus as Status, ProcessSubject } from '../interfaces/index';

type LogType = [any?, ...any[]];

Expand Down Expand Up @@ -112,15 +112,15 @@ export class Logger {
// inner loop start-end log
const loopLog: string[] = [];
if (
process === Process.init && status === Status.next
process === CommonProcess.init && status === Status.next
) {
loopLog.push(`%c---=== loop ${this.getLoopIdNext()} start`);
} else if (
process === Process.end
process === CommonProcess.end
) {
loopLog.push(`%c---=== loop ${this.getLoopId()} done`);
const parent = payload && payload.process;
if (status === Status.next && (parent !== Process.reset && parent !== Process.reload)) {
if (status === Status.next && (parent !== AdapterProcess.reset && parent !== AdapterProcess.reload)) {
loopLog[0] += `, loop ${this.getLoopIdNext()} start`;
}
}
Expand Down
40 changes: 24 additions & 16 deletions src/component/inputs/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VALIDATORS } from './validation';
import { DatasourceProps } from './datasource';
import { ICommonProps } from '../interfaces/index';
import { ICommonProps, AdapterProcessMap, AdapterProcess as Process } from '../interfaces/index';

const {
INTEGER,
Expand All @@ -15,6 +15,9 @@ const {
OR,
} = VALIDATORS;

enum AdapterNoParams { }
const NO_METHOD_PARAMS: ICommonProps<AdapterNoParams> = {};

const RESET_METHOD_PARAMS: ICommonProps<DatasourceProps> = {
[DatasourceProps.get]: {
validators: [FUNC_WITH_X_AND_MORE_ARGUMENTS(2)]
Expand Down Expand Up @@ -159,21 +162,26 @@ const FIX_METHOD_PARAMS: ICommonProps<AdapterFixParams> = {
},
};

export const AdapterMethods = {
Reset: AdapterInsertParams,
Reload: AdapterReloadParams,
Clip: AdapterClipParams,
Insert: AdapterInsertParams,
Fix: AdapterFixParams,
export const AdapterMethods: AdapterProcessMap<any> = {
[Process.reset]: DatasourceProps,
[Process.reload]: AdapterReloadParams,
[Process.append]: AdapterAppendParams,
[Process.check]: AdapterNoParams,
[Process.remove]: AdapterRemoveParams,
[Process.clip]: AdapterClipParams,
[Process.insert]: AdapterInsertParams,
[Process.replace]: AdapterReplaceParams,
[Process.fix]: AdapterFixParams,
};

export const ADAPTER_METHODS = {
RESET: RESET_METHOD_PARAMS,
RELOAD: RELOAD_METHOD_PARAMS,
APPEND: APPEND_METHOD_PARAMS,
REMOVE: REMOVE_METHOD_PARAMS,
CLIP: CLIP_METHOD_PARAMS,
INSERT: INSERT_METHOD_PARAMS,
REPLACE: REPLACE_METHOD_PARAMS,
FIX: FIX_METHOD_PARAMS,
export const ADAPTER_METHODS: AdapterProcessMap<ICommonProps<any>> = {
[Process.reset]: RESET_METHOD_PARAMS,
[Process.reload]: RELOAD_METHOD_PARAMS,
[Process.append]: APPEND_METHOD_PARAMS,
[Process.check]: NO_METHOD_PARAMS,
[Process.remove]: REMOVE_METHOD_PARAMS,
[Process.clip]: CLIP_METHOD_PARAMS,
[Process.insert]: INSERT_METHOD_PARAMS,
[Process.replace]: REPLACE_METHOD_PARAMS,
[Process.fix]: FIX_METHOD_PARAMS,
};
3 changes: 2 additions & 1 deletion src/component/interfaces/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export interface IAdapter {
remove(args: AdapterRemoveOptions | ItemsPredicate): MethodResult; // + old signature
clip(options?: AdapterClipOptions): MethodResult;
insert(options: AdapterInsertOptions): MethodResult;
replace(options: AdapterReplaceOptions): MethodResult;
fix(options: AdapterFixOptions): MethodResult; // experimental
relax(callback?: Function): MethodResult; // experimental
relax(callback?: Function): MethodResult;
showLog(): void;
}
5 changes: 4 additions & 1 deletion src/component/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { Settings, DevSettings } from './settings';
import { Direction } from './direction';
import { ScrollEventData, ScrollState, State } from './state';
import { Process, ProcessStatus, ProcessSubject } from './process';
import { CommonProcess, AdapterProcess, Process, ProcessStatus, ProcessSubject, AdapterProcessMap } from './process';
import {
ValidatorType,
ValidatedValue,
Expand Down Expand Up @@ -71,9 +71,12 @@ export {
ScrollEventData,
ScrollState,
State,
CommonProcess,
AdapterProcess,
Process,
ProcessStatus,
ProcessSubject,
AdapterProcessMap,
ValidatorType,
ValidatedValue,
IValidator,
Expand Down
31 changes: 19 additions & 12 deletions src/component/interfaces/process.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
export enum Process {
export enum CommonProcess {
init = 'init',
scroll = 'scroll',
reset = 'adapter.reset',
reload = 'adapter.reload',
append = 'adapter.append',
prepend = 'adapter.prepend',
check = 'adapter.check',
remove = 'adapter.remove',
replace = 'adapter.replace',
userClip = 'adapter.clip',
insert = 'adapter.insert',
fix = 'adapter.fix',
start = 'start',
preFetch = 'preFetch',
fetch = 'fetch',
Expand All @@ -19,9 +9,22 @@ export enum Process {
preClip = 'preClip',
clip = 'clip',
adjust = 'adjust',
end = 'end'
end = 'end',
}
export enum AdapterProcess {
reset = 'adapter.reset',
reload = 'adapter.reload',
append = 'adapter.append',
check = 'adapter.check',
remove = 'adapter.remove',
replace = 'adapter.replace',
clip = 'adapter.clip',
insert = 'adapter.insert',
fix = 'adapter.fix',
}

export type Process = CommonProcess | AdapterProcess;

export enum ProcessStatus {
start = 'start',
next = 'next',
Expand All @@ -34,3 +37,7 @@ export interface ProcessSubject {
status: ProcessStatus;
payload?: any;
}

export type AdapterProcessMap<T> = {
[key in AdapterProcess]: T;
};
9 changes: 9 additions & 0 deletions src/component/processes/_base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Process } from '../interfaces/index';

export const getBaseProcess = (process: Process) =>

class BaseAdapterProcess {

static process: Process = process;

};
39 changes: 39 additions & 0 deletions src/component/processes/adapter/_base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { getBaseProcess } from '../_base';
import { Scroller } from '../../scroller';
import { ADAPTER_METHODS, validate } from '../../inputs/index';
import { AdapterProcess, IValidatedData, ProcessStatus } from '../../interfaces/index';

interface IParseInput<T> {
data: IValidatedData;
params?: T;
}

export const getBaseAdapterProcess = (process: AdapterProcess) =>

class BaseAdapterProcess extends getBaseProcess(process) {

static process: AdapterProcess = process;

static parseInput<T>(scroller: Scroller, options: T): IParseInput<T> {
const inputData = validate(options, ADAPTER_METHODS[process]);
const result: IParseInput<T> = { data: inputData };

if (inputData.isValid) {
result.params = Object.entries(inputData.params)
.reduce((acc, [key, { value }]) => ({
...acc,
[key]: value
}), {} as T);
} else {
scroller.logger.log(() => inputData.showErrors());
scroller.workflow.call({
process,
status: ProcessStatus.error,
payload: { error: `Wrong argument of the "${process}" method call` }
});
}

return result;
}

};
Loading

0 comments on commit b398f32

Please sign in to comment.