Skip to content

Instantly share code, notes, and snippets.

@ericeslinger
Last active August 29, 2015 14:05
Show Gist options
  • Save ericeslinger/f2111549574bcfaa3c1b to your computer and use it in GitHub Desktop.
Save ericeslinger/f2111549574bcfaa3c1b to your computer and use it in GitHub Desktop.
Batch loader with bassmaster
angular.module 'clientApp.apiModel.batchRequest', ['ng']
.service 'BatchRequest', ($q, Server, $http, $timeout)->
timeoutPromise = undefined
queue = []
postBatch = ->
requestQueue = queue.map (req)-> {method: 'GET', path: req.url, deferred: req.deferred}
queue = []
timeoutPromise = undefined
$http
method: 'POST'
url: "#{Server.api}/api/batch"
data:
requests: requestQueue.map (i)->{method: i.method, path: i.path}
.then (data)->
requestQueue.forEach (val, i)->
if data.data[i].statusCode?
val.deferred.reject(data.data[i])
else
val.deferred.resolve(data.data[i])
get: (url)->
deferred = $q.defer()
unless timeoutPromise?
timeoutPromise = $timeout postBatch, 50
queue.push
url: url
deferred: deferred
return deferred.promise
@ericeslinger
Copy link
Author

Then just make sure you have bassmaster running on your server. I did it with

server.pack.registerAsync
  plugin: require('bassmaster')
  options:
    batchEndpoint: '/api/batch'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment