Skip to content

Commit 799dc20

Browse files
committed
issue-171 Replace process flow (single replace works)
1 parent b1a4ecb commit 799dc20

File tree

7 files changed

+41
-28
lines changed

7 files changed

+41
-28
lines changed

src/component/inputs/adapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ const INSERT_METHOD_PARAMS: ICommonProps<AdapterInsertParams> = {
113113
validators: [FUNC_WITH_X_ARGUMENTS(1), ONE_OF_MUST([AdapterInsertParams.before])]
114114
},
115115
[AdapterInsertParams.decrease]: {
116-
validators: [BOOLEAN]
116+
validators: [BOOLEAN],
117+
defaultValue: false
117118
},
118119
};
119120

src/component/processes/adapter/insert.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,34 @@ import { getBaseAdapterProcess } from './_base';
22
import { Scroller } from '../../scroller';
33
import { Item } from '../../classes/item';
44
import {
5-
AdapterProcess, ProcessStatus, AdapterInsertOptions, ItemsPredicate, IValidatedData
5+
AdapterProcess, ProcessStatus, AdapterInsertOptions, ItemsPredicate
66
} from '../../interfaces/index';
77

88
export default class Insert extends getBaseAdapterProcess(AdapterProcess.insert) {
99

1010
static run(scroller: Scroller, options: AdapterInsertOptions) {
1111

12-
const { data } = Insert.parseInput(scroller, options);
13-
if (!data.isValid) {
12+
const { params } = Insert.parseInput(scroller, options);
13+
if (!params) {
1414
return;
1515
}
1616

17-
const next = Insert.doInsert(scroller, data);
17+
const shouldInsert = Insert.doInsert(scroller, params);
1818

1919
scroller.workflow.call({
2020
process: Insert.process,
21-
status: next ? ProcessStatus.next : ProcessStatus.done
21+
status: shouldInsert ? ProcessStatus.next : ProcessStatus.done
2222
});
2323
}
2424

25-
static doInsert(scroller: Scroller, { params }: IValidatedData): boolean {
26-
const { buffer } = scroller;
25+
static doInsert(scroller: Scroller, params: AdapterInsertOptions): boolean {
2726
const { before, after, items, decrease } = params;
28-
const method: ItemsPredicate = before.isSet ? before.value : after.value;
29-
const found = buffer.items.find(item => method(item.get()));
27+
const method = (before || after) as ItemsPredicate;
28+
const found = scroller.buffer.items.find(item => method(item.get()));
3029
if (!found) {
3130
return false;
3231
}
33-
const decrement = decrease.isSet && decrease.value;
34-
return Insert.simulateFetch(scroller, found, items.value, before.isSet, decrement);
32+
return Insert.simulateFetch(scroller, found, items, !!before, !!decrease);
3533
}
3634

3735
static simulateFetch(

src/component/processes/adapter/remove.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export default class Remove extends getBaseAdapterProcess(AdapterProcess.remove)
1111
return;
1212
}
1313

14-
const shouldRemove = Remove.removeItems(scroller, params);
14+
const shouldRemove = Remove.doRemove(scroller, params);
1515

1616
scroller.workflow.call({
1717
process: Remove.process,
1818
status: shouldRemove ? ProcessStatus.next : ProcessStatus.done
1919
});
2020
}
2121

22-
static removeItems(scroller: Scroller, params: AdapterRemoveOptions, sequenceOnly = false): boolean {
22+
static doRemove(scroller: Scroller, params: AdapterRemoveOptions, sequenceOnly = false): boolean {
2323
const shouldRemove = Remove.removeBufferedItems(scroller, params);
2424
const shouldRemoveVirtual = Remove.removeVirtualItems(scroller, params, sequenceOnly);
2525

src/component/processes/adapter/replace.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getBaseAdapterProcess } from './_base';
22
import { Scroller } from '../../scroller';
3-
import { AdapterProcess, ProcessStatus, AdapterReplaceOptions } from '../../interfaces/index';
3+
import { AdapterProcess, ProcessStatus, AdapterReplaceOptions, AdapterInsertOptions } from '../../interfaces/index';
44
import Remove from './remove';
5+
import Insert from './insert';
56

67
export default class Replace extends getBaseAdapterProcess(AdapterProcess.replace) {
78

@@ -11,7 +12,7 @@ export default class Replace extends getBaseAdapterProcess(AdapterProcess.replac
1112
return;
1213
}
1314

14-
const shouldRemove = Remove.removeItems(scroller, params, true);
15+
const shouldRemove = Remove.doRemove(scroller, params, true);
1516
if (!shouldRemove) {
1617
scroller.logger.log(() => 'no items to replace (not found)');
1718
return scroller.workflow.call({
@@ -20,9 +21,22 @@ export default class Replace extends getBaseAdapterProcess(AdapterProcess.replac
2021
});
2122
}
2223

24+
const insertOptions: AdapterInsertOptions = {
25+
items: params.items,
26+
before: params.predicate,
27+
decrease: false
28+
};
29+
const shouldInsert = Insert.doInsert(scroller, insertOptions);
30+
if (!shouldInsert) {
31+
return scroller.workflow.call({
32+
process: Replace.process,
33+
status: ProcessStatus.done
34+
});
35+
}
36+
2337
scroller.workflow.call({
2438
process: Replace.process,
25-
status: ProcessStatus.done
39+
status: ProcessStatus.next
2640
});
2741
}
2842

src/component/workflow-transducer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export const runStateMachine = ({
135135
case AdapterProcess.append:
136136
case AdapterProcess.check:
137137
case AdapterProcess.insert:
138+
case AdapterProcess.replace:
138139
run(Render)();
139140
break;
140141
case AdapterProcess.remove:
@@ -181,6 +182,9 @@ export const runStateMachine = ({
181182
case AdapterProcess.remove:
182183
run(Adjust)();
183184
break;
185+
case AdapterProcess.replace:
186+
run(Clip)();
187+
break;
184188
default:
185189
run(PreClip)();
186190
}

tests/adapter.remove.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,20 +145,21 @@ const doRemove = async (config: TestBedConfig, misc: Misc, byId = false) => {
145145

146146
const shouldRemove = (config: TestBedConfig, byId = false) => (misc: Misc) => async (done: Function) => {
147147
await misc.relaxNext();
148-
const bufferSizeBeforeRemove = misc.scroller.buffer.size;
148+
const { size: bufferSizeBeforeRemove, minIndex, maxIndex, absMinIndex, absMaxIndex } = misc.scroller.buffer;
149149
const { remove: indexList, increase } = config.custom;
150-
const { minIndex, maxIndex } = misc.scroller.buffer;
151150
const viewportSizeBeforeRemove = misc.getScrollableSize();
152151
const sizeToRemove = indexList.length * misc.getItemSize();
153152
const deltaSize = viewportSizeBeforeRemove - sizeToRemove;
154153

155154
const loopPendingSub = misc.adapter.loopPending$.subscribe(loopPending => {
156155
if (!loopPending) { // when the first loop after the Remove is done
157156
const len = indexList.length;
158-
const { minIndex: min, maxIndex: max, size } = misc.scroller.buffer;
157+
const { size, minIndex: min, maxIndex: max, absMinIndex: absMin, absMaxIndex: absMax } = misc.scroller.buffer;
159158
expect(size).toEqual(bufferSizeBeforeRemove - len);
160159
expect(min).toBe(minIndex + (increase ? len : 0));
161160
expect(max).toBe(maxIndex - (increase ? 0 : len));
161+
expect(absMin).toBe(absMinIndex + (increase ? len : 0));
162+
expect(absMax).toBe(absMaxIndex - (increase ? 0 : len));
162163
expect(deltaSize).toEqual(misc.getScrollableSize());
163164
loopPendingSub.unsubscribe();
164165
}

tests/adapter.replace.spec.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ const getDatasourceClass = (settings: Settings) =>
6363
if (itemToReplace) {
6464
Object.assign(itemToReplace, item);
6565
}
66-
// console.log('RUN', this.data);
6766
}
6867
};
6968

@@ -79,15 +78,11 @@ const shouldReplace = (config: TestBedConfig) => (misc: Misc) => async (done: Fu
7978
newItem.text += '*';
8079

8180
replaceOne(indexToReplace, newItem);
82-
await adapter.remove(({ $index }) => $index === indexToReplace);
83-
await adapter.insert({
84-
before: ({ $index }) => $index === indexToReplace,
81+
82+
await adapter.replace({
83+
predicate: ({ $index }) => $index === indexToReplace,
8584
items: [newItem]
8685
});
87-
// await adapter.replace({
88-
// predicate: ({ $index }) => $index === indexToReplace,
89-
// items: [newItem]
90-
// });
9186

9287
await misc.scrollMinMax();
9388
adapter.fix({ scrollPosition });

0 commit comments

Comments
 (0)