Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapter.replace, one-to-one only #229

Merged
merged 8 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 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 @@ -20,10 +20,11 @@ import {
AdapterRemoveOptions,
AdapterClipOptions,
AdapterInsertOptions,
AdapterReplaceOptions,
AdapterFixOptions,
ScrollerWorkflow,
IDatasourceOptional,
ProcessSubject
ProcessSubject,
} from '../interfaces/index';

const ADAPTER_PROPS_STUB = ADAPTER_PROPS(EMPTY_ITEM);
Expand All @@ -37,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 @@ -264,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 @@ -274,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 @@ -312,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 @@ -321,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 @@ -330,7 +331,16 @@ 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
});
}

replace(options: AdapterReplaceOptions): any {
this.logger.logAdapterMethod('replace', options);
this.workflow.call({
process: AdapterProcess.replace,
status: ProcessStatus.start,
payload: options
});
Expand All @@ -339,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
57 changes: 41 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 @@ -110,8 +113,24 @@ const INSERT_METHOD_PARAMS: ICommonProps<AdapterInsertParams> = {
validators: [FUNC_WITH_X_ARGUMENTS(1), ONE_OF_MUST([AdapterInsertParams.before])]
},
[AdapterInsertParams.decrease]: {
validators: [BOOLEAN]
validators: [BOOLEAN],
defaultValue: false
},
};

enum AdapterReplaceParams {
items = 'items',
predicate = 'predicate',
}

const REPLACE_METHOD_PARAMS: ICommonProps<AdapterReplaceParams> = {
[AdapterInsertParams.items]: {
validators: [ITEM_LIST],
mandatory: true
},
[AdapterReplaceParams.predicate]: {
validators: [FUNC_WITH_X_ARGUMENTS(1)]
}
};

enum AdapterFixParams {
Expand Down Expand Up @@ -144,20 +163,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,
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,
};
8 changes: 7 additions & 1 deletion src/component/interfaces/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export interface AdapterInsertOptions {
decrease?: boolean;
}

export interface AdapterReplaceOptions {
items: any[];
predicate: ItemsPredicate;
}

export interface AdapterFixOptions {
scrollPosition?: number;
minIndex?: number;
Expand Down Expand Up @@ -100,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;
}
7 changes: 6 additions & 1 deletion src/component/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import {
AdapterRemoveOptions,
AdapterClipOptions,
AdapterInsertOptions,
AdapterReplaceOptions,
AdapterFixOptions,
AdapterMethodResult,
IAdapter,
} from './adapter';
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 @@ -62,16 +63,20 @@ export {
AdapterRemoveOptions,
AdapterClipOptions,
AdapterInsertOptions,
AdapterReplaceOptions,
AdapterFixOptions,
Settings,
DevSettings,
Direction,
ScrollEventData,
ScrollState,
State,
CommonProcess,
AdapterProcess,
Process,
ProcessStatus,
ProcessSubject,
AdapterProcessMap,
ValidatorType,
ValidatedValue,
IValidator,
Expand Down
30 changes: 19 additions & 11 deletions src/component/interfaces/process.ts
Original file line number Diff line number Diff line change
@@ -1,15 +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',
userClip = 'adapter.clip',
insert = 'adapter.insert',
fix = 'adapter.fix',
start = 'start',
preFetch = 'preFetch',
fetch = 'fetch',
Expand All @@ -18,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 @@ -33,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;

};
Loading