1.0.4 • Published 5 years ago

@buckless/offline-queue v1.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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
1.0.4

5 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago