@infinite-list/data-model
Advanced tools
Comparing version 0.2.12 to 0.2.13
@@ -41,4 +41,4 @@ import ItemMetaStateEventHelper from './ItemMetaStateEventHelper'; | ||
getIndexInfo(): import("./types").IndexInfo; | ||
addStateEventListener(event: string, callback: StateEventListener): () => void; | ||
addStateEventListener(event: string, callback: StateEventListener, triggerOnceIfTrue?: boolean): () => void; | ||
} | ||
export default ItemMeta; |
@@ -8,3 +8,3 @@ import { StateEventListener } from './types'; | ||
private _triggerBatchinator; | ||
private _handleCount; | ||
private _handleCountMap; | ||
private _once; | ||
@@ -19,5 +19,9 @@ readonly _key: string; | ||
}); | ||
addListener(listener: StateEventListener): () => void; | ||
addListener(listener: StateEventListener, triggerOnceIfTrue: boolean): () => void; | ||
getHandleCount(handler: StateEventListener): any; | ||
incrementHandleCount(handler: StateEventListener): void; | ||
get value(): boolean; | ||
setValue(value: boolean): void; | ||
guard(): boolean; | ||
listenerGuard(cb: StateEventListener): boolean; | ||
trigger(value: boolean): void; | ||
@@ -24,0 +28,0 @@ _trigger(value: any): void; |
import { ViewabilityConfig, OnViewableItemsChanged, ViewabilityConfigCallbackPairs, ViewabilityScrollMetrics } from '../types'; | ||
import ViewabilityItemMeta from './ViewabilityItemMeta'; | ||
import ViewablityHelper from './ViewablityHelper'; | ||
import ItemMeta from '../ItemMeta'; | ||
declare class ViewabilityConfigTuples { | ||
@@ -18,4 +18,4 @@ private _tuple; | ||
getViewabilityHelpers(): ViewablityHelper[]; | ||
resolveItemMetaState(itemMeta: ViewabilityItemMeta, viewabilityScrollMetrics: ViewabilityScrollMetrics, getItemOffset?: (itemMeta: ViewabilityItemMeta) => number): {}; | ||
resolveItemMetaState(itemMeta: ItemMeta, viewabilityScrollMetrics: ViewabilityScrollMetrics, getItemOffset?: (itemMeta: ItemMeta) => number): {}; | ||
} | ||
export default ViewabilityConfigTuples; |
{ | ||
"name": "@infinite-list/data-model", | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"files": [ | ||
@@ -20,4 +20,4 @@ "dist", | ||
"@x-oasis/batchinator": "^0.0.13", | ||
"@x-oasis/boolean-with-default": "^0.0.13", | ||
"@x-oasis/capitalize": "^0.0.6", | ||
"@x-oasis/default-boolean-value": "^0.1.4", | ||
"@x-oasis/integer-buffer-set": "^0.0.19", | ||
@@ -24,0 +24,0 @@ "@x-oasis/is-clamped": "^0.0.8", |
@@ -12,2 +12,3 @@ import BaseDimensions from './BaseDimensions'; | ||
import noop from '@x-oasis/noop'; | ||
import defaultBooleanValue from '@x-oasis/default-boolean-value'; | ||
import ViewabilityItemMeta from './viewable/ViewabilityItemMeta'; | ||
@@ -165,12 +166,7 @@ | ||
// addStateListener(setState: Function) { | ||
// if (typeof setState === 'function') this._setState = setState; | ||
// return this.removeStateListener; | ||
// } | ||
// removeStateListener() { | ||
// this._setState = null; | ||
// } | ||
addStateEventListener(event: string, callback: StateEventListener) { | ||
addStateEventListener( | ||
event: string, | ||
callback: StateEventListener, | ||
triggerOnceIfTrue?: boolean | ||
) { | ||
if (typeof callback !== 'function') return noop; | ||
@@ -181,3 +177,6 @@ const stateEventHelper = this.ensureStateHelper( | ||
); | ||
return stateEventHelper.addListener(callback); | ||
return stateEventHelper.addListener( | ||
callback, | ||
defaultBooleanValue(triggerOnceIfTrue, true) | ||
); | ||
} | ||
@@ -184,0 +183,0 @@ } |
@@ -10,3 +10,3 @@ import Batchinator from '@x-oasis/batchinator'; | ||
private _triggerBatchinator: Batchinator; | ||
private _handleCount = 0; | ||
private _handleCountMap = new Map(); | ||
private _once: boolean; | ||
@@ -52,11 +52,32 @@ readonly _key: string; | ||
addListener(listener: StateEventListener) { | ||
addListener(listener: StateEventListener, triggerOnceIfTrue: boolean) { | ||
const index = this._listeners.findIndex((cb) => cb === listener); | ||
if (index === -1) this._listeners.push(listener); | ||
if (index === -1) { | ||
this._handleCountMap.set(listener, 0); | ||
this._listeners.push(listener); | ||
} | ||
if (triggerOnceIfTrue && this._value) { | ||
this.incrementHandleCount(listener); | ||
listener(this._value); | ||
} | ||
return () => { | ||
const index = this._listeners.findIndex((cb) => cb === listener); | ||
this._listeners.splice(index, 1); | ||
if (index !== -1) { | ||
this._listeners.splice(index, 1); | ||
this._handleCountMap.delete(listener); | ||
} | ||
}; | ||
} | ||
getHandleCount(handler: StateEventListener) { | ||
return this._handleCountMap.get(handler) || 0; | ||
} | ||
incrementHandleCount(handler: StateEventListener) { | ||
const value = this._handleCountMap.get(handler) || 0; | ||
this._handleCountMap.set(handler, value + 1); | ||
} | ||
get value() { | ||
@@ -68,14 +89,26 @@ return this._value; | ||
this.trigger(value); | ||
// return value !== this._value; | ||
} | ||
guard() { | ||
if (!this._once) return true; | ||
for (const value of this._handleCountMap.values()) { | ||
if (!value) return true; | ||
} | ||
return false; | ||
} | ||
listenerGuard(cb: StateEventListener) { | ||
if (!this._once) return true; | ||
if (this._handleCountMap.get(cb)) return false; | ||
return true; | ||
} | ||
trigger(value: boolean) { | ||
if (this._once && this._handleCount) return; | ||
if (value && this._batchUpdateEnabled) { | ||
this._triggerBatchinator.dispose({ | ||
abort: true, | ||
}); | ||
const shouldPerformScheduler = this.guard(); | ||
if (!shouldPerformScheduler) return; | ||
if (value && !this._batchUpdateEnabled) { | ||
this._trigger(value); | ||
return; | ||
} | ||
this._triggerBatchinator.dispose({ | ||
@@ -89,4 +122,8 @@ abort: true, | ||
if (this._value !== value) { | ||
this._handleCount++; | ||
this._listeners.forEach((cb) => cb(value)); | ||
this._listeners.forEach((cb) => { | ||
if (this.listenerGuard(cb)) { | ||
this.incrementHandleCount(cb); | ||
cb(value); | ||
} | ||
}); | ||
} | ||
@@ -93,0 +130,0 @@ this._value = value; |
@@ -1015,11 +1015,13 @@ import noop from '@x-oasis/noop'; | ||
const itemMetaState = this._scrollMetrics | ||
? this._configTuple.resolveItemMetaState( | ||
itemMeta, | ||
this._scrollMetrics, | ||
// should add container offset, because indexToOffsetMap containerOffset is | ||
// exclusive. | ||
() => indexToOffsetMap[targetIndex] + this.getContainerOffset() | ||
) | ||
: itemMeta.getState(); | ||
const itemMetaState = | ||
!this._scrollMetrics || !itemMeta?.getLayout() | ||
? itemMeta.getState() | ||
: this._configTuple.resolveItemMetaState( | ||
itemMeta, | ||
this._scrollMetrics, | ||
// should add container offset, because indexToOffsetMap containerOffset is | ||
// exclusive. | ||
() => indexToOffsetMap[targetIndex] + this.getContainerOffset() | ||
); | ||
itemMeta?.setItemMetaState(itemMetaState); | ||
@@ -1196,9 +1198,11 @@ | ||
const itemMetaState = this._scrollMetrics | ||
? this._configTuple.resolveItemMetaState( | ||
itemMeta, | ||
this._scrollMetrics, | ||
() => indexToOffsetMap[index] | ||
) | ||
: itemMeta.getState(); | ||
const itemMetaState = | ||
!this._scrollMetrics || !itemMeta?.getLayout() | ||
? itemMeta.getState() | ||
: this._configTuple.resolveItemMetaState( | ||
itemMeta, | ||
this._scrollMetrics, | ||
() => indexToOffsetMap[index] | ||
); | ||
itemMeta?.setItemMetaState(itemMetaState); | ||
@@ -1205,0 +1209,0 @@ |
@@ -6,3 +6,3 @@ import Batchinator from '@x-oasis/batchinator'; | ||
import resolveChanged from '@x-oasis/resolve-changed'; | ||
import booleanWithDefault from '@x-oasis/boolean-with-default'; | ||
import defaultBooleanValue from '@x-oasis/default-boolean-value'; | ||
import PrefixIntervalTree from '@x-oasis/prefix-interval-tree'; | ||
@@ -1114,4 +1114,4 @@ import BaseLayout from './BaseLayout'; | ||
const scrollMetrics = _scrollMetrics || this._scrollMetrics; | ||
const useCache = booleanWithDefault(_options?.useCache, true); | ||
const flush = booleanWithDefault(_options?.flush, false); | ||
const useCache = defaultBooleanValue(_options?.useCache, true); | ||
const flush = defaultBooleanValue(_options?.flush, false); | ||
@@ -1118,0 +1118,0 @@ if (!scrollMetrics) return; |
@@ -8,5 +8,5 @@ import uniqueArrayObject from '@x-oasis/unique-array-object'; | ||
} from '../types'; | ||
import ViewabilityItemMeta from './ViewabilityItemMeta'; | ||
import ViewablityHelper from './ViewablityHelper'; | ||
import { DEFAULT_VIEWABILITY_CONFIG } from './constants'; | ||
import ItemMeta from '../ItemMeta'; | ||
@@ -106,7 +106,8 @@ class ViewabilityConfigTuples { | ||
resolveItemMetaState( | ||
itemMeta: ViewabilityItemMeta, | ||
itemMeta: ItemMeta, | ||
viewabilityScrollMetrics: ViewabilityScrollMetrics, | ||
getItemOffset?: (itemMeta: ViewabilityItemMeta) => number | ||
getItemOffset?: (itemMeta: ItemMeta) => number | ||
) { | ||
if (!viewabilityScrollMetrics || !itemMeta) return {}; | ||
if (!itemMeta.getLayout()) return {}; | ||
return this.viewabilityHelpers.reduce((value, helper) => { | ||
@@ -113,0 +114,0 @@ const falsy = helper.checkItemViewability( |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
1620938
19558
+ Added@x-oasis/default-boolean-value@0.1.35(transitive)
+ Added@x-oasis/default-value@0.1.35(transitive)
- Removed@x-oasis/boolean-with-default@0.0.13(transitive)