0.0.4 • Published 6 years ago

ember-data-autoreload v0.0.4

Weekly downloads
36
License
MIT
Repository
github
Last release
6 years ago

ember-data-autoreload

Build Status

ember-data-autoreload allows you to auto-reload models after some time has passed. It provides a mixin you can add to any Ember Data model:

import DS from 'ember-data';
import AutoReload from 'ember-data-autoreload';

export default DS.Model.extend(AutoReload, {
  // ...
});

Now you can call startAutoReloading() and stopAutoReloading() on this model to start or stop automatically reloading, respectively.

Configuration

ember-data-autoreload supports the following configuration options, all of which are set as properties on the model:

PropertyPossible valuesDescriptionDefault value
autoReloadStrategy'fixed', 'incremental'Determines how the delay between automatic reloads is determined. See below for a more complete explanation.'incremental'
autoReloadDelayAny number > 0The (initial) delay between automatic reloads.30000
autoReloadMaximumDelayAny number > 0 and >= autoReloadDelayThe maximum delay between automatic reloads, if autoReloadStrategy === 'incremental'300000
autoReloadDelayGrowthAny number > 1The rate with which the delay between automatic reloads grows, if autoReloadStrategy === 'incremental'2

Reload strategies

The autoReloadStrategy property is used to influence the delay between automatic reloads of your models. If this property is set to 'fixed', the model always reloads itself after a fixed interval, determined by the autoReloadDelay property.

If the property is set to 'incremental', the delay between automatic reloads will keep increasing every time the attributes on the model do not change. This will reduce the load on your API server if the model doesn't change often. As soon as the model does change in between reloads, the delay is reset. To increase or decrease the amount with which the delay grows, you can use the autoDelayGrowth property. Every time the model doesn't change, the new delay is calculated using previousDelay * autoReloadDelayGrowth.

Hooks

ember-data-autoreload provides three hooks into the auto reload process: shouldAutoReload, willAutoReload and didAutoReload. If you want to manually adjust the delay between auto reloads, or trigger side-effects, you can do so here. All hooks are promise-aware.

shouldAutoReload (snapshot)

This hook is called before a reload occurs. If you return false (or promise that resolves to false) from here, the model will not be reloaded until the next timeout period has passed. If you return true, the model will reload.

Parameters:

snapshot Ember Data Snapshot
A snapshot of the model before reloading.

willAutoReload (snapshot)

This hook is called right before this.reload is called on the model.

Parameters:

snapshot Ember Data Snapshot
A snapshot of the model before reloading.

didAutoReload (attributesChanged, newSnapshot, oldSnapshot)

This hook is called after this.reload is called on the model, and before the delay for the next automatic reload is scheduled.

Parameters:

attributesChanged Boolean
true if the model's attributes changed since before the reload, otherwise false.

newSnapshot Ember Data Snapshot
A snapshot of the model after reloading.

oldSnapshot Ember Data Snapshot
A snapshot of the model before reloading.

0.0.4

6 years ago

0.0.3

7 years ago

0.0.2

8 years ago

0.0.1

8 years ago