Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(localForage): Add localForage support #155

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Ember from 'ember';
import DS from 'ember-data';
import ImportExportMixin from '../mixins/adapters/import-export';
import { isLocalForage } from '../helpers/storage';

const keys = Object.keys || Ember.keys;

Expand Down Expand Up @@ -162,19 +163,32 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {

_handleGETRequest(url, query) {
const { type, id } = this._urlParts(url);
const storage = get(this, '_storage'),
storageKey = this._storageKey(type, id);
const storageKey = this._storageKey(type, id);
const storage = get(this, '_storage');

if (id) {
return storage[storageKey] ? JSON.parse(storage[storageKey]) : null;
if (isLocalForage()) {
return storage.getItem(storageKey);
} else {
return storage[storageKey] ? JSON.parse(storage[storageKey]) : null;
}
}

// TODO: we can increase performance by using a forEarch and push to records array or just remove empty items
const records = this._getIndex(type)
.filter(function(storageKey) {
return storage[storageKey];
if (isLocalForage()) {
return storage.getItem(storageKey);
} else {
return storage[storageKey];
}
})
.map(function(storageKey) {
return JSON.parse(storage[storageKey]);
if (isLocalForage()) {
return storage.getItem(storageKey);
} else {
return JSON.parse(storage[storageKey]);
}
});

if (query && query.filter) {
Expand All @@ -191,29 +205,48 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
_handlePOSTRequest(url, record) {
const { type, id } = record.data;
const storageKey = this._storageKey(type, id);
const storage = get(this, '_storage');

this._addToIndex(type, storageKey);
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

if (isLocalForage()) {
storage.setItem(storageKey, record.data);
} else {
storage[storageKey] = JSON.stringify(record.data);
}

return null;
},

// TODO: Alias to _handlePOSTRequest
_handlePATCHRequest(url, record) {
const { type, id } = record.data;
const storageKey = this._storageKey(type, id);
const storage = get(this, '_storage');

this._addToIndex(type, storageKey);
get(this, '_storage')[storageKey] = JSON.stringify(record.data);

if (isLocalForage()) {
storage.setItem(storageKey, record.data);
} else {
storage[storageKey] = JSON.stringify(record.data);
}

return null;
},

_handleDELETERequest(url) {
const { type, id } = this._urlParts(url);
const storageKey = this._storageKey(type, id);
const storage = get(this, '_storage');

this._removeFromIndex(type, storageKey);
delete get(this, '_storage')[storageKey];

if (isLocalForage()) {
storage.removeItem(storageKey);
} else {
delete storage[storageKey];
}

return null;
},
Expand Down
11 changes: 11 additions & 0 deletions addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@ function tryStorage(name) {
}

function getStorage(name) {
// TODO: localForage - add mappings for session and local if (isLocalForage())
// local -> localStorageWrapper, etc
if (storage[name]) {
return storage[name];
} else {
// TODO: localForage - check if the localForage check is sufficient (safari private mode)
// TODO: localForage - if (isLocalForage()) use localforage
return storage[name] = tryStorage(name) || {};
}
}

// Detect if localforage is used
// TODO: localForage - maybe we should check the config as well?
function isLocalForage() {
return typeof localforage !== 'undefined';
}

let storages = {};

function storageFor(key, modelName, options = {}) {
Expand Down Expand Up @@ -144,5 +154,6 @@ export {
tryStorage,
getStorage,
storageFor,
isLocalForage,
_resetStorages
};
1 change: 1 addition & 0 deletions addon/mixins/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default Ember.Mixin.create(StorageProxyMixin, {
},

_clear: function() {
// TODO: localForage - use setItem
set(this, 'content', Ember.A());
}
});
1 change: 1 addition & 0 deletions addon/mixins/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default Ember.Mixin.create(StorageProxyMixin, {
},

_clear: function() {
// TODO: localForage - use setItem
set(this, 'content', {});
}
});
11 changes: 9 additions & 2 deletions addon/mixins/storage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Ember from 'ember';
import { getStorage } from '../helpers/storage';
import { getStorage, isLocalForage } from '../helpers/storage';

const {
Mixin,
Expand Down Expand Up @@ -54,6 +54,7 @@ export default Mixin.create({

set(this, '_initialContentString', JSON.stringify(initialContent));

// TODO: localForage - use getItem()
// Retrieve the serialized version from storage..
serialized = storage[storageKey];

Expand Down Expand Up @@ -86,6 +87,7 @@ export default Mixin.create({
const storage = this._storage(),
storageKey = get(this, '_storageKey');

// TODO: localForage - do we have to check for the driver?
if (window.addEventListener) {
this._storageEventHandler = (event) => {
if (this.isDestroying) { return; }
Expand Down Expand Up @@ -124,7 +126,11 @@ export default Mixin.create({
set(this, '_isInitialContent', false);
}

storage[storageKey] = json;
if (isLocalForage()) {
storage.setItem(storageKey, content);
} else {
storage[storageKey] = json;
}
}
},

Expand Down Expand Up @@ -157,6 +163,7 @@ export default Mixin.create({
// returns void
clear: function() {
this._clear();
// TODO: localForage - use removeItem()
delete this._storage()[get(this, '_storageKey')];
}
});