@buckless/offline-queue

1.0.4 • Public • Published

offline-queue

Simple job queue, mostly used for offline apps.

Failing jobs will be pushed to the queue again.

Usage

const Queue = require('@buckless/offline-queue')

const queue = new Queue({
  // your main queue processor
  process(job) {
    return axios[job.data.method || 'get'](
      job.data.url,
      job.data.body,
      Object.assign({}, authHeaders, job.data.params)
    )
  },

  interval: 60 * 1000 * 1000,
  storage: localStorage
})

queue.on('process', (job, res) => {
  // do something with res (result from your process call)
  console.log(`${job.data.method} on ${job.data.url} succeeded`)
})

queue.on('error', (err, job) => {
  console.error(`${job.data.method} on ${job.data.url} failed because of ${err.message}`)
})

queue
  .push({
    data: {
      method: 'post',
      url: '/some/endpoint',
      data: { foo: 'bar' },
      params: { headers: {} }
    },

    // set to true if you want the job to get executed immediately (it will resolve or reject naturally).
    // if it fails, it will be pushed to the queue
    immediate: someBoolean,

    // a value or a promise that will be resolved (if the job is not immediate, or if the immediate job failed)
    mock: {
      data: {
        value: 12
      }
    }
  })
  .then((res) => {

  })

API

Type definitions

Job

{
  data: Object,
  immediate: Boolean,
  mock: Any|Promise<Any>
}

Options

{
  process: (job: Job) -> Promise,
  autostart: Boolean,
  interval: Number,
  storage: Storage
}

Events

synchronizing(queue: Jobs[])
synchronized(queue: Jobs[])
process(job: Job, data: Any)
processed(job: Job, data: Any)
error(job: Job, error: Error)

Methods

constructor(opts: Options) -> Queue
push(job: Job) -> Promise
sync() -> Promise
stop() -> void
# internal
loadFromStorage() -> Promise
# internal
writeToStorage() -> Promise

Readme

Keywords

none

Package Sidebar

Install

npm i @buckless/offline-queue

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

117 kB

Total Files

17

Last publish

Collaborators

  • buckless