@infinite-list/data-model
Advanced tools
Comparing version 0.2.13 to 0.2.14
@@ -41,4 +41,7 @@ import ItemMetaStateEventHelper from './ItemMetaStateEventHelper'; | ||
getIndexInfo(): import("./types").IndexInfo; | ||
addStateReusableEventListener(event: string, key: string, callback: StateEventListener, triggerOnceIfTrue?: boolean): { | ||
remover: Function; | ||
}; | ||
addStateEventListener(event: string, callback: StateEventListener, triggerOnceIfTrue?: boolean): () => void; | ||
} | ||
export default ItemMeta; |
@@ -11,2 +11,3 @@ import { StateEventListener } from './types'; | ||
readonly _key: string; | ||
private _reusableEventListenerMap; | ||
constructor(props: { | ||
@@ -19,2 +20,6 @@ key: string; | ||
}); | ||
remover(listener: StateEventListener, key?: string): () => void; | ||
addReusableListener(listener: StateEventListener, key: string, triggerOnceIfTrue: boolean): { | ||
remover: () => void; | ||
}; | ||
addListener(listener: StateEventListener, triggerOnceIfTrue: boolean): () => void; | ||
@@ -21,0 +26,0 @@ getHandleCount(handler: StateEventListener): any; |
{ | ||
"name": "@infinite-list/data-model", | ||
"version": "0.2.13", | ||
"version": "0.2.14", | ||
"files": [ | ||
@@ -5,0 +5,0 @@ "dist", |
@@ -165,2 +165,35 @@ import BaseDimensions from './BaseDimensions'; | ||
/** | ||
* | ||
* @param event | ||
* @param callback | ||
* @param triggerOnceIfTrue | ||
* | ||
* In reuse condition, once add listener, then it will not be changed anymore. | ||
* | ||
*/ | ||
addStateReusableEventListener( | ||
event: string, | ||
key: string, | ||
callback: StateEventListener, | ||
triggerOnceIfTrue?: boolean | ||
): { | ||
remover: Function; | ||
} { | ||
if (typeof callback !== 'function') | ||
return { | ||
remover: noop, | ||
}; | ||
const stateEventHelper = this.ensureStateHelper( | ||
event, | ||
// get initial value | ||
event === 'impression' ? this._state['viewable'] : this._state[event] | ||
); | ||
return stateEventHelper.addReusableListener( | ||
callback, | ||
key, | ||
defaultBooleanValue(triggerOnceIfTrue, true) | ||
); | ||
} | ||
addStateEventListener( | ||
@@ -174,2 +207,3 @@ event: string, | ||
event, | ||
// get initial value | ||
event === 'impression' ? this._state['viewable'] : this._state[event] | ||
@@ -176,0 +210,0 @@ ); |
@@ -13,2 +13,3 @@ import Batchinator from '@x-oasis/batchinator'; | ||
readonly _key: string; | ||
private _reusableEventListenerMap = new Map(); | ||
@@ -46,2 +47,3 @@ constructor(props: { | ||
this._triggerBatchinator = new Batchinator(this._trigger.bind(this), 50); | ||
this.remover = this.remover.bind(this); | ||
@@ -53,2 +55,30 @@ if (defaultValue) { | ||
remover(listener: StateEventListener, key?: string) { | ||
return () => { | ||
const index = this._listeners.findIndex((cb) => cb === listener); | ||
if (index !== -1) { | ||
this._listeners.splice(index, 1); | ||
this._handleCountMap.delete(listener); | ||
} | ||
if (key) this._reusableEventListenerMap.delete(key); | ||
}; | ||
} | ||
addReusableListener( | ||
listener: StateEventListener, | ||
key: string, | ||
triggerOnceIfTrue: boolean | ||
) { | ||
if (this._reusableEventListenerMap.has(key)) | ||
return { | ||
remover: this.remover(listener, key), | ||
}; | ||
this._reusableEventListenerMap.set(key, listener); | ||
this.addListener(listener, triggerOnceIfTrue); | ||
return { | ||
remover: this.remover(listener, key), | ||
}; | ||
} | ||
addListener(listener: StateEventListener, triggerOnceIfTrue: boolean) { | ||
@@ -66,9 +96,3 @@ const index = this._listeners.findIndex((cb) => cb === listener); | ||
return () => { | ||
const index = this._listeners.findIndex((cb) => cb === listener); | ||
if (index !== -1) { | ||
this._listeners.splice(index, 1); | ||
this._handleCountMap.delete(listener); | ||
} | ||
}; | ||
return this.remover(listener); | ||
} | ||
@@ -75,0 +99,0 @@ |
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
1632976
19668