Skip to content

Instantly share code, notes, and snippets.

@kmalakoff
Created September 18, 2011 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmalakoff/1225029 to your computer and use it in GitHub Desktop.
Save kmalakoff/1225029 to your computer and use it in GitHub Desktop.
Javascript / Coffeescript background task / job / thread - single and multiple array batch iteration with cancellation using background.js (https://github.com/kmalakoff/background)
Using background.js (https://github.com/kmalakoff/background)...
constructor: ->
@processing_count = 0
@transformations_job_queue = new BGJobQueue(100)
_transformationsChangeCallback: ->
# reset the job queue to start again rather than waiting for the last batch to complete
@processing_count++; @_updateProcessingMesage()
@transformations_job_queue.clear()
# break up the processing work into batches of 50 combinations between models and transformations
if(@transformations.length)
@transformations_job_queue.push(
(=> iterator = new BGArrayIterator_xN([@collection.models, @transformations], 50)),
(-> return iterator.nextByItems((items) ->
model = items[0]; transformation = items[1]
model.applyTransformation(transformation)
))
)
# update view's knowledge of visibilities in batches of 100
@transformations_job_queue.push(
(=> iterator = new BGArrayIterator(@collection.models, 100)),
(=> return iterator.nextByRange((range) =>
show_models = []
hide_models = []
while(not range.isDone())
model = @collection.models[range.index]
if(model.isVisible())
show_models.push(model.id)
else
hide_models.push(model.id)
range.step()
@batchShowByModelIds(show_models) if show_models.length
@batchHideByModelIds(hide_models) if hide_models.length
)),
((was_cancelled) =>
@processing_count--; @_updateProcessingMesage()
return if was_cancelled
view.animatedZoomToModel(@collection.models[0]) if @collection.models.length
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment