balena-io-modules / pinejs-client-js

4 stars 4 forks source link

Add a polling option #50

Closed LucianBuzzo closed 6 years ago

LucianBuzzo commented 6 years ago

Connects to https://github.com/resin-io/resin-ui/issues/1285

The basic idea is that you can pass a poll value in the params when using the get method ( https://github.com/resin-io/pinejs-client-js/blob/master/core.coffee#L391 ) that will cause the request to be run every X seconds. If the poll value is present and is a number, the number will represent the poll interval in ms, otherwise, a sensible default will be used.

If the poll value is present, the get method will return an event emitter that will emit the following events:

The returned emitter should have the following methods:

An example of use with the sdk could look like this:

const poll = sdk.pine.get({
  resource: 'device',
  id: 85917,
  poll: 2000
})

poll.on('data', (response) => 
   console.log('response', response))

poll.destroy()

In the future, the polling could be replaced with socket events.