Skip to content

Commit 100fcc4

Browse files
committed
issue-171 replace method and process stub
1 parent a52515a commit 100fcc4

File tree

8 files changed

+77
-6
lines changed

8 files changed

+77
-6
lines changed

src/component/classes/adapter.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ import {
1919
AdapterAppendOptions,
2020
AdapterClipOptions,
2121
AdapterInsertOptions,
22+
AdapterReplaceOptions,
2223
AdapterFixOptions,
2324
ScrollerWorkflow,
2425
IDatasourceOptional,
25-
ProcessSubject
26+
ProcessSubject,
2627
} from '../interfaces/index';
2728

2829
const ADAPTER_PROPS_STUB = ADAPTER_PROPS(EMPTY_ITEM);
@@ -109,9 +110,9 @@ export class Adapter implements IAdapter {
109110
// restore original values from the publicContext if present
110111
const adapterProps = publicContext
111112
? ADAPTER_PROPS_STUB.map(prop => ({
112-
...prop,
113-
value: (publicContext as any)[prop.name]
114-
}))
113+
...prop,
114+
value: (publicContext as any)[prop.name]
115+
}))
115116
: ADAPTER_PROPS(EMPTY_ITEM);
116117

117118
// Scalar permanent props
@@ -247,7 +248,7 @@ export class Adapter implements IAdapter {
247248
this.relax$.complete();
248249
}
249250
Object.values(this.source).forEach(observable => observable.complete());
250-
}
251+
}
251252

252253
reset(options?: IDatasourceOptional): any {
253254
this.reloadCounter++;
@@ -324,6 +325,15 @@ export class Adapter implements IAdapter {
324325
});
325326
}
326327

328+
replace(options: AdapterReplaceOptions): any {
329+
this.logger.logAdapterMethod('replace', options);
330+
this.workflow.call({
331+
process: Process.replace,
332+
status: ProcessStatus.start,
333+
payload: options
334+
});
335+
}
336+
327337
fix(options: AdapterFixOptions): any {
328338
this.logger.logAdapterMethod('fix', options);
329339
this.workflow.call({

src/component/inputs/adapter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ const INSERT_METHOD_PARAMS: ICommonProps<AdapterInsertParams> = {
106106
}
107107
};
108108

109+
enum AdapterReplaceParams {
110+
items = 'items',
111+
predicate = 'predicate',
112+
}
113+
114+
const REPLACE_METHOD_PARAMS: ICommonProps<AdapterReplaceParams> = {
115+
[AdapterInsertParams.items]: {
116+
validators: [ITEM_LIST],
117+
mandatory: true
118+
},
119+
[AdapterReplaceParams.predicate]: {
120+
validators: [FUNC_WITH_X_ARGUMENTS(1)]
121+
}
122+
};
123+
109124
enum AdapterFixParams {
110125
scrollPosition = 'scrollPosition',
111126
minIndex = 'minIndex',
@@ -151,5 +166,6 @@ export const ADAPTER_METHODS = {
151166
REMOVE: REMOVE_METHOD_PARAMS,
152167
CLIP: CLIP_METHOD_PARAMS,
153168
INSERT: INSERT_METHOD_PARAMS,
169+
REPLACE: REPLACE_METHOD_PARAMS,
154170
FIX: FIX_METHOD_PARAMS,
155171
};

src/component/interfaces/adapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ export interface AdapterInsertOptions {
5151
decrease?: boolean;
5252
}
5353

54+
export interface AdapterReplaceOptions {
55+
items: any[];
56+
predicate: ItemsPredicate;
57+
}
58+
5459
export interface AdapterFixOptions {
5560
scrollPosition?: number;
5661
minIndex?: number;

src/component/interfaces/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
AdapterPrependOptions,
2020
AdapterClipOptions,
2121
AdapterInsertOptions,
22+
AdapterReplaceOptions,
2223
AdapterFixOptions,
2324
AdapterMethodResult,
2425
IAdapter,
@@ -60,6 +61,7 @@ export {
6061
AdapterPrependOptions,
6162
AdapterClipOptions,
6263
AdapterInsertOptions,
64+
AdapterReplaceOptions,
6365
AdapterFixOptions,
6466
Settings,
6567
DevSettings,

src/component/interfaces/process.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export enum Process {
77
prepend = 'adapter.prepend',
88
check = 'adapter.check',
99
remove = 'adapter.remove',
10+
replace = 'adapter.replace',
1011
userClip = 'adapter.clip',
1112
insert = 'adapter.insert',
1213
fix = 'adapter.fix',
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Scroller } from '../../scroller';
2+
import { ADAPTER_METHODS, validate } from '../../inputs/index';
3+
import { Process, ProcessStatus, AdapterReplaceOptions } from '../../interfaces/index';
4+
5+
export default class Replace {
6+
7+
static process = Process.replace;
8+
9+
static run(scroller: Scroller, options: AdapterReplaceOptions) {
10+
const methodData = validate(options, ADAPTER_METHODS.REPLACE);
11+
if (!methodData.isValid) {
12+
scroller.logger.log(() => methodData.showErrors());
13+
scroller.workflow.call({
14+
process: Process.replace,
15+
status: ProcessStatus.error,
16+
payload: { error: `Wrong argument of the "Adapter.replace" method call` }
17+
});
18+
return;
19+
}
20+
21+
scroller.workflow.call({
22+
process: Process.replace,
23+
status: ProcessStatus.done
24+
});
25+
}
26+
27+
}

src/component/processes/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Check from './adapter/check';
77
import Remove from './adapter/remove';
88
import UserClip from './adapter/clip';
99
import Insert from './adapter/insert';
10+
import Replace from './adapter/replace';
1011
import Fix from './adapter/fix';
1112
import Start from './start';
1213
import PreFetch from './preFetch';
@@ -20,6 +21,6 @@ import End from './end';
2021

2122
export {
2223
Init, Scroll,
23-
Reset, Reload, Append, Check, Remove, UserClip, Insert, Fix,
24+
Reset, Reload, Append, Check, Remove, UserClip, Insert, Replace, Fix,
2425
Start, PreFetch, Fetch, PostFetch, Render, PreClip, Clip, Adjust, End
2526
};

src/component/workflow-transducer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Remove,
99
UserClip,
1010
Insert,
11+
Replace,
1112
Fix,
1213
Start,
1314
PreFetch,
@@ -117,6 +118,14 @@ export const runStateMachine = ({
117118
run(Init)(process);
118119
}
119120
break;
121+
case Process.replace:
122+
if (status === Status.start) {
123+
run(Replace)(payload);
124+
}
125+
if (status === Status.next) {
126+
run(Init)(process);
127+
}
128+
break;
120129
case Process.fix:
121130
if (status === Status.start) {
122131
run(Fix)(payload);

0 commit comments

Comments
 (0)