Skip to content

Commit

Permalink
feat: update
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuever committed Sep 17, 2023
1 parent 7754148 commit 504f113
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 136 deletions.
197 changes: 102 additions & 95 deletions packages/data-model/src/ListBaseDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
DISPATCH_METRICS_THRESHOLD,
DEFAULT_ITEM_APPROXIMATE_LENGTH,
ITEM_OFFSET_BEFORE_LAYOUT_READY,
DEFAULT_RECYCLER_TYPE,
} from './common';
import resolveChanged from '@x-oasis/resolve-changed';
import manager from './manager';
Expand Down Expand Up @@ -47,14 +48,15 @@ import defaultBooleanValue from '@x-oasis/default-boolean-value';
// import FixedBuffer from './FixedBuffer';
import ViewabilityConfigTuples from './viewable/ViewabilityConfigTuples';
import Recycler from './Recycler';
import BaseLayout from './BaseLayout';

/**
* item should be first class data model; item's value reference change will
* cause recalculation of item key. However, if key is not changed, its itemMeta
* will not change.
*/
class ListBaseDimensions<ItemT extends {} = {}> {
public _selectValue: SelectValue;
class ListBaseDimensions<ItemT extends {} = {}> extends BaseLayout {
// public _selectValue: SelectValue;
private _getItemLayout: GetItemLayout<ItemT>;
private _getItemSeparatorLength: GetItemSeparatorLength<ItemT>;
private _stateListener: StateListener<ItemT>;
Expand Down Expand Up @@ -93,17 +95,17 @@ class ListBaseDimensions<ItemT extends {} = {}> {
private _stillnessHelper: StillnessHelper;
private _recycler: Recycler;

private _recycleThreshold: number;
readonly _onEndReachedThreshold: number;
readonly _fillingMode: FillingMode;
initialNumToRender: number;

private _persistanceIndices = [];
private _stickyHeaderIndices = [];
private _reservedIndices = [];
private _maxToRenderPerBatch: number;
private _canIUseRIC: boolean;
private _itemOffsetBeforeLayoutReady: number;
// private _recycleThreshold: number;
// readonly _onEndReachedThreshold: number;
// readonly _fillingMode: FillingMode;
// initialNumToRender: number;

// private _persistanceIndices = [];
// private _stickyHeaderIndices = [];
// private _reservedIndices = [];
// // private _maxToRenderPerBatch: number;
// private _canIUseRIC: boolean;
// private _itemOffsetBeforeLayoutReady: number;
_configTuple: ViewabilityConfigTuples;

private memoizedResolveSpaceState: (
Expand All @@ -122,6 +124,7 @@ class ListBaseDimensions<ItemT extends {} = {}> {
private _provider: ListGroupDimensions;

constructor(props: ListBaseDimensionsProps<ItemT>) {
super(props)
const {
store,
getData,
Expand Down Expand Up @@ -166,9 +169,9 @@ class ListBaseDimensions<ItemT extends {} = {}> {
this._itemApproximateLength = itemApproximateLength || 0;
this._getItemLayout = getItemLayout;
this._getData = getData;
this._maxToRenderPerBatch = maxToRenderPerBatch;
this._itemOffsetBeforeLayoutReady = itemOffsetBeforeLayoutReady;
this._canIUseRIC = canIUseRIC;
// this._maxToRenderPerBatch = maxToRenderPerBatch;
// this._itemOffsetBeforeLayoutReady = itemOffsetBeforeLayoutReady;
// this._canIUseRIC = canIUseRIC;

// `_approximateMode` is enabled on default
this._approximateMode = recycleEnabled
Expand Down Expand Up @@ -202,12 +205,12 @@ class ListBaseDimensions<ItemT extends {} = {}> {
? selectHorizontalValue
: selectVerticalValue;

this._fillingMode = recycleEnabled
? FillingMode.RECYCLE
: FillingMode.SPACE;
this._recycleThreshold = recycleEnabled
? recycleThreshold || maxToRenderPerBatch * 2
: 0;
// this._fillingMode = recycleEnabled
// ? FillingMode.RECYCLE
// : FillingMode.SPACE;
// this._recycleThreshold = recycleEnabled
// ? recycleThreshold || maxToRenderPerBatch * 2
// : 0;

this._configTuple = new ViewabilityConfigTuples({
viewabilityConfig,
Expand Down Expand Up @@ -268,56 +271,62 @@ class ListBaseDimensions<ItemT extends {} = {}> {
this.recalculateRecycleResultState.bind(this),
50
);
}

get itemOffsetBeforeLayoutReady() {
return this._itemOffsetBeforeLayoutReady;
this.initializeDefaultRecycleBuffer()
}

get fillingMode() {
return this._fillingMode;
initializeDefaultRecycleBuffer() {
this._recycler.addBuffer(DEFAULT_RECYCLER_TYPE)
}

get reservedIndices() {
return this._reservedIndices;
}
// get itemOffsetBeforeLayoutReady() {
// return this._itemOffsetBeforeLayoutReady;
// }

get maxToRenderPerBatch() {
return this._maxToRenderPerBatch;
}
// get fillingMode() {
// return this._fillingMode;
// }

get canIUseRIC() {
return this._canIUseRIC;
}
// get reservedIndices() {
// return this._reservedIndices;
// }

updateReservedIndices() {
const indices = new Set(
[].concat(this.persistanceIndices, this.stickyHeaderIndices)
);
this._reservedIndices = Array.from(indices).sort((a, b) => a - b);
}
// get maxToRenderPerBatch() {
// return this._maxToRenderPerBatch;
// }

get persistanceIndices() {
return this._persistanceIndices;
}
// get canIUseRIC() {
// return this._canIUseRIC;
// }

set persistanceIndices(indices: Array<number>) {
this._persistanceIndices = indices.sort((a, b) => a - b);
this.updateReservedIndices();
}
// updateReservedIndices() {
// const indices = new Set(
// [].concat(this.persistanceIndices, this.stickyHeaderIndices)
// );
// this._reservedIndices = Array.from(indices).sort((a, b) => a - b);
// }

get stickyHeaderIndices() {
return this._stickyHeaderIndices;
}
// get persistanceIndices() {
// return this._persistanceIndices;
// }

set stickyHeaderIndices(indices: Array<number>) {
this._stickyHeaderIndices = indices.sort((a, b) => a - b);
this.updateReservedIndices();
}
// set persistanceIndices(indices: Array<number>) {
// this._persistanceIndices = indices.sort((a, b) => a - b);
// this.updateReservedIndices();
// }

get recycleThreshold() {
return this._recycleThreshold;
}
// get stickyHeaderIndices() {
// return this._stickyHeaderIndices;
// }

// set stickyHeaderIndices(indices: Array<number>) {
// this._stickyHeaderIndices = indices.sort((a, b) => a - b);
// this.updateReservedIndices();
// }

// get recycleThreshold() {
// return this._recycleThreshold;
// }

get length() {
return this._data.length;
Expand Down Expand Up @@ -434,6 +443,14 @@ class ListBaseDimensions<ItemT extends {} = {}> {
return this._provider.getData();
}

getDataLength() {
return this._provider.getDataLength()
}

getTotalLength() {
return this._provider.getTotalLength()
}

getReflowItemsLength() {
return this._provider.getReflowItemsLength();
}
Expand Down Expand Up @@ -461,6 +478,10 @@ class ListBaseDimensions<ItemT extends {} = {}> {
return this.getReflowItemsLength() < this._data.length;
}

computeIndexRange(minOffset: number, maxOffset: number) {
return this._provider.computeIndexRange(minOffset, maxOffset)
}

_recycleEnabled() {
if (this.fillingMode !== FillingMode.RECYCLE) return false;
return this.getReflowItemsLength() >= this.initialNumToRender;
Expand Down Expand Up @@ -1024,7 +1045,13 @@ class ListBaseDimensions<ItemT extends {} = {}> {
scrollMetrics,
});
if (isEmpty(state)) return state;
this.updateState(state, scrollMetrics);
this.setState({
...state,
// @ts-ignore
data: this.getData(),
})

// this.updateState(state, scrollMetrics);
return state;
}

Expand Down Expand Up @@ -1060,52 +1087,32 @@ class ListBaseDimensions<ItemT extends {} = {}> {
this._stillnessHelper.isStill;
}

/**
* When to trigger updateScrollMetrics..
* - on scroll
* - layout change.
* - In rn, use contentSizeChanged. `setIntervalTreeValue` has remove update scroll logic.
* - In web, maybe `setIntervalTreeValue` to trigger state updating..
*/
updateScrollMetrics(
scrollMetrics: ScrollMetrics = this._scrollMetrics,
useCache = true
_scrollMetrics?: ScrollMetrics,
_options?: {
useCache?: boolean;
flush?: boolean;
}
) {

const scrollMetrics = _scrollMetrics || this._scrollMetrics;
// const useCache = defaultBooleanValue(_options?.useCache, true);
const flush = defaultBooleanValue(_options?.flush, false);

if (!scrollMetrics) return;
if (!this.dispatchScrollMetricsEnabled()) {
this._scrollMetrics = scrollMetrics;
return;
}

if (!useCache) {
this._scrollMetrics = scrollMetrics;
this._dispatchMetricsBatchinator.schedule(scrollMetrics);
return;
}

if (this._scrollMetrics?.offset !== scrollMetrics?.offset) {
// this._stillnessHelper.startClockBatchinateLast.schedule();
}

if (
!this._scrollMetrics ||
scrollMetrics.contentLength !== this._scrollMetrics.contentLength ||
scrollMetrics.offset !== this._scrollMetrics.offset ||
scrollMetrics.visibleLength !== this._scrollMetrics.visibleLength
) {
this._scrollMetrics = scrollMetrics;
this._dispatchMetricsBatchinator.schedule(scrollMetrics);
} else if (scrollMetrics.offset !== this._offsetTriggerCachedState) {
this._scrollMetrics = scrollMetrics;
this._dispatchMetricsBatchinator.schedule(scrollMetrics);
this._scrollMetrics = scrollMetrics;
if (flush) {
this._dispatchMetricsBatchinator.flush(scrollMetrics);
} else {
this._dispatchMetricsBatchinator.dispose({
abort: true,
});
this.updateState(this._state, scrollMetrics);
this._dispatchMetricsBatchinator.schedule(scrollMetrics);
}

this._scrollMetrics = scrollMetrics;
return
}
}

Expand Down
44 changes: 22 additions & 22 deletions packages/data-model/src/ListDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,28 +284,28 @@ class ListDimensions<ItemT extends {} = {}> extends BaseDimensions {
this._renderStateListeners = [];
}

resolveInitialActiveValue(active: boolean) {
if (this._deps.length) {
let isActive = true;
for (let index = 0; index < this._deps.length; index++) {
const listKey = this._deps[index];
const listHandler = manager.getKeyList(listKey);
if (!listHandler) continue;

if (
listHandler.getRenderState() !== ListRenderState.ON_RENDER_FINISHED
) {
this._renderStateListenersCleaner.push(
listHandler.addRenderStateListener(this.handleDeps.bind(this))
);
isActive = false;
}
}
return isActive;
}

return active;
}
// resolveInitialActiveValue(active: boolean) {
// if (this._deps.length) {
// let isActive = true;
// for (let index = 0; index < this._deps.length; index++) {
// const listKey = this._deps[index];
// const listHandler = manager.getKeyList(listKey);
// if (!listHandler) continue;

// if (
// listHandler.getRenderState() !== ListRenderState.ON_RENDER_FINISHED
// ) {
// this._renderStateListenersCleaner.push(
// listHandler.addRenderStateListener(this.handleDeps.bind(this))
// );
// isActive = false;
// }
// }
// return isActive;
// }

// return active;
// }

getRenderState() {
return this._renderState;
Expand Down
23 changes: 5 additions & 18 deletions packages/data-model/src/ListGroupDimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class ListGroupDimensions<ItemT extends {} = {}>
this.startInspection = this.startInspection.bind(this);

this._listBaseDimension = new ListBaseDimensions({
...props,
keyExtractor: () => null,
id: 'listGroupDimensions',
data: this._flattenData,
Expand Down Expand Up @@ -1352,24 +1353,10 @@ class ListGroupDimensions<ItemT extends {} = {}>
}
) {

const scrollMetrics = _scrollMetrics || this._scrollMetrics;
// const useCache = defaultBooleanValue(_options?.useCache, true);
const flush = defaultBooleanValue(_options?.flush, false);

if (!scrollMetrics) return;
if (!this.dispatchScrollMetricsEnabled()) {
this._scrollMetrics = scrollMetrics;
return;
}

this._scrollMetrics = scrollMetrics;
if (flush) {
this._dispatchMetricsBatchinator.flush(scrollMetrics);
} else {
this._dispatchMetricsBatchinator.schedule(scrollMetrics);
}

return
this._listBaseDimension.updateScrollMetrics(
_scrollMetrics,
_options
)
}
}

Expand Down
Loading

0 comments on commit 504f113

Please sign in to comment.