-
Notifications
You must be signed in to change notification settings - Fork 7
Description
Context: I'm using S.js/SArray in a Surplus application, with a lot of async updates (via socket.io). I'm trying to par down the number of DOM updates, specifically with code that has to deal with multiple operations on SArray; my bottleneck was in code similar to:
var filtering_field = S.data('hello') // value from UI element
var filtering_function = S( (value) =>
return value === filtering_field()
)
var original_list = SArray( [] ) // updated over socket.io
var filtered_list = S( => // displayed using Surplus
return original_list.filter(filtering_function).sort().splice(0,99)
)In this example, filtering_field is the value of a UI element, while original_list is updated over socket.io. The filtered_list is then displayed using Surplus.
I've been debouncing native values using S.js with code that looks like the following (this is more-or-less my coffeescript code with-parentheses-added for clarity):
var debounce = (src,timeout,conclude) =>
var timer = null
S( () =>
var v = src()
clearTimeout(timer)
timer = setTimeout( ( () => conclude(v) ), timeout )
return
)
returnTo create a debounced signal I then use:
var debounced_signal = S.data( null )
debounce( original_signal, timeout, debounced_signal )However this is clearly sub-optimal when using SArray:
var debounced_array = SArray( [] )
debounce( original_array, timeout, debounced_array )because the entire SArray gets overwritten every time, which means that .filter etc. are not optimized on changed values, but are re-applied to the entire Array every time. (Assuming I understand what SArray does!!)
Is there a better way to debounce an SArray?