Skip to content

Commit 6dc1422

Browse files
committed
issue-171 base process
1 parent b7c7743 commit 6dc1422

37 files changed

+347
-338
lines changed

src/component/classes/adapter.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
IAdapterProp,
1212
AdapterMethodResult,
1313
IAdapter,
14-
Process,
14+
AdapterProcess,
1515
ProcessStatus,
1616
ItemAdapter,
1717
ItemsPredicate,
@@ -38,10 +38,10 @@ const fixScalarWanted = (name: string, container: { [key: string]: boolean }) =>
3838
}
3939
};
4040

41-
const convertAppendArgs = (isAppend: boolean, options: any, eof?: boolean) => {
41+
const convertAppendArgs = (prepend: boolean, options: any, eof?: boolean) => {
4242
if (!(options !== null && typeof options === 'object' && options.hasOwnProperty('items'))) {
4343
const items = !Array.isArray(options) ? [options] : options;
44-
options = isAppend ? { items, eof } : { items, bof: eof };
44+
options = prepend ? { items, bof: eof } : { items, eof };
4545
}
4646
return options;
4747
};
@@ -265,7 +265,7 @@ export class Adapter implements IAdapter {
265265
this.reloadCounter++;
266266
this.logger.logAdapterMethod(`reset`, options, ` of ${this.reloadId}`);
267267
this.workflow.call({
268-
process: Process.reset,
268+
process: AdapterProcess.reset,
269269
status: ProcessStatus.start,
270270
payload: options
271271
});
@@ -275,36 +275,36 @@ export class Adapter implements IAdapter {
275275
this.reloadCounter++;
276276
this.logger.logAdapterMethod(`reload`, options, ` of ${this.reloadId}`);
277277
this.workflow.call({
278-
process: Process.reload,
278+
process: AdapterProcess.reload,
279279
status: ProcessStatus.start,
280280
payload: options
281281
});
282282
}
283283

284284
append(options: AdapterAppendOptions | any, eof?: boolean): any {
285-
options = convertAppendArgs(true, options, eof); // support old signature
285+
options = convertAppendArgs(false, options, eof); // support old signature
286286
this.logger.logAdapterMethod('append', [options.items, options.eof]);
287287
this.workflow.call({
288-
process: Process.append,
288+
process: AdapterProcess.append,
289289
status: ProcessStatus.start,
290-
payload: options
290+
payload: { prepend: false, options }
291291
});
292292
}
293293

294294
prepend(options: AdapterPrependOptions | any, bof?: boolean): any {
295-
options = convertAppendArgs(false, options, bof); // support old signature
295+
options = convertAppendArgs(true, options, bof); // support old signature
296296
this.logger.logAdapterMethod('prepend', [options.items, options.bof]);
297297
this.workflow.call({
298-
process: Process.prepend,
298+
process: AdapterProcess.append,
299299
status: ProcessStatus.start,
300-
payload: options
300+
payload: { prepend: true, options }
301301
});
302302
}
303303

304304
check(): any {
305305
this.logger.logAdapterMethod('check');
306306
this.workflow.call({
307-
process: Process.check,
307+
process: AdapterProcess.check,
308308
status: ProcessStatus.start
309309
});
310310
}
@@ -313,7 +313,7 @@ export class Adapter implements IAdapter {
313313
options = convertRemoveArgs(options); // support old signature
314314
this.logger.logAdapterMethod('remove', options);
315315
this.workflow.call({
316-
process: Process.remove,
316+
process: AdapterProcess.remove,
317317
status: ProcessStatus.start,
318318
payload: options
319319
});
@@ -322,7 +322,7 @@ export class Adapter implements IAdapter {
322322
clip(options?: AdapterClipOptions): any {
323323
this.logger.logAdapterMethod('clip', options);
324324
this.workflow.call({
325-
process: Process.userClip,
325+
process: AdapterProcess.clip,
326326
status: ProcessStatus.start,
327327
payload: options
328328
});
@@ -331,7 +331,7 @@ export class Adapter implements IAdapter {
331331
insert(options: AdapterInsertOptions): any {
332332
this.logger.logAdapterMethod('insert', options);
333333
this.workflow.call({
334-
process: Process.insert,
334+
process: AdapterProcess.insert,
335335
status: ProcessStatus.start,
336336
payload: options
337337
});
@@ -340,7 +340,7 @@ export class Adapter implements IAdapter {
340340
replace(options: AdapterReplaceOptions): any {
341341
this.logger.logAdapterMethod('replace', options);
342342
this.workflow.call({
343-
process: Process.replace,
343+
process: AdapterProcess.replace,
344344
status: ProcessStatus.start,
345345
payload: options
346346
});
@@ -349,7 +349,7 @@ export class Adapter implements IAdapter {
349349
fix(options: AdapterFixOptions): any {
350350
this.logger.logAdapterMethod('fix', options);
351351
this.workflow.call({
352-
process: Process.fix,
352+
process: AdapterProcess.fix,
353353
status: ProcessStatus.start,
354354
payload: options
355355
});

src/component/classes/adapter/props.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export const ADAPTER_PROPS = (nullItem: any): IAdapterProp[] => [
107107
name: 'insert',
108108
value: noop
109109
},
110+
{
111+
type: Prop.WorkflowRunner,
112+
name: 'replace',
113+
value: noop
114+
},
110115
{
111116
type: Prop.WorkflowRunner,
112117
name: 'fix',

src/component/classes/logger.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Scroller } from '../scroller';
2-
import { Process, ProcessStatus as Status, ProcessSubject } from '../interfaces/index';
2+
import { CommonProcess, AdapterProcess, ProcessStatus as Status, ProcessSubject } from '../interfaces/index';
33

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

@@ -112,15 +112,15 @@ export class Logger {
112112
// inner loop start-end log
113113
const loopLog: string[] = [];
114114
if (
115-
process === Process.init && status === Status.next
115+
process === CommonProcess.init && status === Status.next
116116
) {
117117
loopLog.push(`%c---=== loop ${this.getLoopIdNext()} start`);
118118
} else if (
119-
process === Process.end
119+
process === CommonProcess.end
120120
) {
121121
loopLog.push(`%c---=== loop ${this.getLoopId()} done`);
122122
const parent = payload && payload.process;
123-
if (status === Status.next && (parent !== Process.reset && parent !== Process.reload)) {
123+
if (status === Status.next && (parent !== AdapterProcess.reset && parent !== AdapterProcess.reload)) {
124124
loopLog[0] += `, loop ${this.getLoopIdNext()} start`;
125125
}
126126
}

src/component/inputs/adapter.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { VALIDATORS } from './validation';
22
import { DatasourceProps } from './datasource';
3-
import { ICommonProps } from '../interfaces/index';
3+
import { ICommonProps, AdapterProcessMap, AdapterProcess as Process } from '../interfaces/index';
44

55
const {
66
INTEGER,
@@ -15,6 +15,9 @@ const {
1515
OR,
1616
} = VALIDATORS;
1717

18+
enum AdapterNoParams { }
19+
const NO_METHOD_PARAMS: ICommonProps<AdapterNoParams> = {};
20+
1821
const RESET_METHOD_PARAMS: ICommonProps<DatasourceProps> = {
1922
[DatasourceProps.get]: {
2023
validators: [FUNC_WITH_X_AND_MORE_ARGUMENTS(2)]
@@ -159,21 +162,26 @@ const FIX_METHOD_PARAMS: ICommonProps<AdapterFixParams> = {
159162
},
160163
};
161164

162-
export const AdapterMethods = {
163-
Reset: AdapterInsertParams,
164-
Reload: AdapterReloadParams,
165-
Clip: AdapterClipParams,
166-
Insert: AdapterInsertParams,
167-
Fix: AdapterFixParams,
165+
export const AdapterMethods: AdapterProcessMap<any> = {
166+
[Process.reset]: DatasourceProps,
167+
[Process.reload]: AdapterReloadParams,
168+
[Process.append]: AdapterAppendParams,
169+
[Process.check]: AdapterNoParams,
170+
[Process.remove]: AdapterRemoveParams,
171+
[Process.clip]: AdapterClipParams,
172+
[Process.insert]: AdapterInsertParams,
173+
[Process.replace]: AdapterReplaceParams,
174+
[Process.fix]: AdapterFixParams,
168175
};
169176

170-
export const ADAPTER_METHODS = {
171-
RESET: RESET_METHOD_PARAMS,
172-
RELOAD: RELOAD_METHOD_PARAMS,
173-
APPEND: APPEND_METHOD_PARAMS,
174-
REMOVE: REMOVE_METHOD_PARAMS,
175-
CLIP: CLIP_METHOD_PARAMS,
176-
INSERT: INSERT_METHOD_PARAMS,
177-
REPLACE: REPLACE_METHOD_PARAMS,
178-
FIX: FIX_METHOD_PARAMS,
177+
export const ADAPTER_METHODS: AdapterProcessMap<ICommonProps<any>> = {
178+
[Process.reset]: RESET_METHOD_PARAMS,
179+
[Process.reload]: RELOAD_METHOD_PARAMS,
180+
[Process.append]: APPEND_METHOD_PARAMS,
181+
[Process.check]: NO_METHOD_PARAMS,
182+
[Process.remove]: REMOVE_METHOD_PARAMS,
183+
[Process.clip]: CLIP_METHOD_PARAMS,
184+
[Process.insert]: INSERT_METHOD_PARAMS,
185+
[Process.replace]: REPLACE_METHOD_PARAMS,
186+
[Process.fix]: FIX_METHOD_PARAMS,
179187
};

src/component/interfaces/adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ export interface IAdapter {
105105
remove(args: AdapterRemoveOptions | ItemsPredicate): MethodResult; // + old signature
106106
clip(options?: AdapterClipOptions): MethodResult;
107107
insert(options: AdapterInsertOptions): MethodResult;
108+
replace(options: AdapterReplaceOptions): MethodResult;
108109
fix(options: AdapterFixOptions): MethodResult; // experimental
109-
relax(callback?: Function): MethodResult; // experimental
110+
relax(callback?: Function): MethodResult;
110111
showLog(): void;
111112
}

src/component/interfaces/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
import { Settings, DevSettings } from './settings';
2929
import { Direction } from './direction';
3030
import { ScrollEventData, ScrollState, State } from './state';
31-
import { Process, ProcessStatus, ProcessSubject } from './process';
31+
import { CommonProcess, AdapterProcess, Process, ProcessStatus, ProcessSubject, AdapterProcessMap } from './process';
3232
import {
3333
ValidatorType,
3434
ValidatedValue,
@@ -71,9 +71,12 @@ export {
7171
ScrollEventData,
7272
ScrollState,
7373
State,
74+
CommonProcess,
75+
AdapterProcess,
7476
Process,
7577
ProcessStatus,
7678
ProcessSubject,
79+
AdapterProcessMap,
7780
ValidatorType,
7881
ValidatedValue,
7982
IValidator,

src/component/interfaces/process.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
export enum Process {
1+
export enum CommonProcess {
22
init = 'init',
33
scroll = 'scroll',
4-
reset = 'adapter.reset',
5-
reload = 'adapter.reload',
6-
append = 'adapter.append',
7-
prepend = 'adapter.prepend',
8-
check = 'adapter.check',
9-
remove = 'adapter.remove',
10-
replace = 'adapter.replace',
11-
userClip = 'adapter.clip',
12-
insert = 'adapter.insert',
13-
fix = 'adapter.fix',
144
start = 'start',
155
preFetch = 'preFetch',
166
fetch = 'fetch',
@@ -19,9 +9,22 @@ export enum Process {
199
preClip = 'preClip',
2010
clip = 'clip',
2111
adjust = 'adjust',
22-
end = 'end'
12+
end = 'end',
13+
}
14+
export enum AdapterProcess {
15+
reset = 'adapter.reset',
16+
reload = 'adapter.reload',
17+
append = 'adapter.append',
18+
check = 'adapter.check',
19+
remove = 'adapter.remove',
20+
replace = 'adapter.replace',
21+
clip = 'adapter.clip',
22+
insert = 'adapter.insert',
23+
fix = 'adapter.fix',
2324
}
2425

26+
export type Process = CommonProcess | AdapterProcess;
27+
2528
export enum ProcessStatus {
2629
start = 'start',
2730
next = 'next',
@@ -34,3 +37,7 @@ export interface ProcessSubject {
3437
status: ProcessStatus;
3538
payload?: any;
3639
}
40+
41+
export type AdapterProcessMap<T> = {
42+
[key in AdapterProcess]: T;
43+
};

src/component/processes/_base.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Process } from '../interfaces/index';
2+
3+
export const getBaseProcess = (process: Process) =>
4+
5+
class BaseAdapterProcess {
6+
7+
static process: Process = process;
8+
9+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { getBaseProcess } from '../_base';
2+
import { Scroller } from '../../scroller';
3+
import { ADAPTER_METHODS, validate } from '../../inputs/index';
4+
import { AdapterProcess, IValidatedData, ProcessStatus } from '../../interfaces/index';
5+
6+
export interface IParseInput<T> {
7+
data: IValidatedData;
8+
params?: T;
9+
}
10+
11+
export const getBaseAdapterProcess = (process: AdapterProcess) =>
12+
13+
class BaseAdapterProcess extends getBaseProcess(process) {
14+
15+
static process: AdapterProcess = process;
16+
17+
static parseInput<T>(scroller: Scroller, options: T): IParseInput<T> {
18+
const inputData = validate(options, ADAPTER_METHODS[process]);
19+
const result: IParseInput<T> = { data: inputData };
20+
21+
if (inputData.isValid) {
22+
result.params = Object.entries(inputData.params)
23+
.reduce((acc, [key, { value }]) => ({
24+
...acc,
25+
[key]: value
26+
}), {} as T);
27+
} else {
28+
scroller.logger.log(() => inputData.showErrors());
29+
scroller.workflow.call({
30+
process,
31+
status: ProcessStatus.error,
32+
payload: { error: `Wrong argument of the "${process}" method call` }
33+
});
34+
}
35+
36+
return result;
37+
}
38+
39+
};

0 commit comments

Comments
 (0)