Skip to content

Commit

Permalink
Modified tests to use async
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogball committed Dec 16, 2017
1 parent 1b4a876 commit e8e8039
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 644 deletions.
63 changes: 43 additions & 20 deletions addon/adapters/base.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Ember from 'ember';
import DS from 'ember-data';
import ImportExportMixin from '../mixins/adapters/import-export';
import StorageArray from '../indexeddb/array';
import ArrayProxyMixin from '../mixins/array';
import { createStorage } from '../helpers/storage';

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

Expand Down Expand Up @@ -177,11 +178,13 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
});
}

const recordPromise = RSVP.all(
this._getIndex(type).map((storageKey) => {
const recordPromise = this._getIndex(type).then((index) => {
return index.map((storageKey) => {
return storage.getItem(storageKey);
})
).then((records) => {
});
}).then((promises) => {
return RSVP.all(promises);
}).then((records) => {
return records.filter((record) => {
return record !== null && record !== undefined;
});
Expand All @@ -203,26 +206,29 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
const storage = get(this, '_storage');
const storageKey = this._storageKey(type, id);

this._addToIndex(type, storageKey);
return storage.setItem(storageKey, record.data);
return this._addToIndex(type, storageKey).then(() => {
return storage.setItem(storageKey, record.data);
});
},

_handlePATCHRequest(url, record) {
const { type, id } = record.data;
const storage = get(this, '_storage');
const storageKey = this._storageKey(type, id);

this._addToIndex(type, storageKey);
return storage.setItem(storageKey, record.data);
return this._addToIndex(type, storageKey).then(() => {
return storage.setItem(storageKey, record.data);
});
},

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

this._removeFromIndex(type, storageKey);
return storage.removeItem(storageKey);
return this._removeFromIndex(type, storageKey).then(() => {
return storage.removeItem(storageKey);
});
},

_queryFilter(data, serializer, query = {}) {
Expand Down Expand Up @@ -327,33 +333,50 @@ export default JSONAPIAdapter.extend(ImportExportMixin, {
};
},

_createIndex(type) {
// Inherit index storage type from storage type
// i.e. this._storage._storageType
const storage = get(this, '_storage');
const storageType = storage._storageType;
const IndexArrayType = Ember.ArrayProxy.extend(ArrayProxyMixin, {
_storageType: storageType
});
return createStorage(this, null, null, {}, IndexArrayType, `index-${type}`);
},

_storageKey(type, id) {
return type + '-' + id;
return `${type}-${id}`;
},

_getIndex(type) {
const indices = get(this, '_indices');

if (!indices[type]) {
indices[type] = StorageArray
.extend({ _storageKey: 'index-' + type })
.create();
indices[type] = this._createIndex(type);
}

return indices[type];
},

_indexHasKey(type, id) {
return this._getIndex(type).indexOf(id) !== -1;
return this._getIndex(type).then((index) => {
return index.indexOf(id) >= 0;
});
},

_addToIndex(type, id) {
if (!this._indexHasKey(type, id)) {
this._getIndex(type).addObject(id);
}
return this._indexHasKey(type, id).then((hasKey) => {
if (!hasKey) {
this._getIndex(type).then((index) => {
index.addObject(id);
})
}
});
},

_removeFromIndex(type, id) {
this._getIndex(type).removeObject(id);
return this._getIndex(type).then((index) => {
return index.removeObject(id);
})
}
});
24 changes: 15 additions & 9 deletions addon/helpers/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function tryStorage(name) {
forageInstance.setDriver(forageDriver);
forageInstance.setItem('emberlocalstorage.test', 'ok').then(() => {
return forageInstance.removeItem('emberlocalstorage.test');
})
});
}
try {
const forage = localforage.createInstance();
Expand All @@ -53,6 +53,7 @@ function tryStorage(name) {
} else {
setDriverAndTest(forage, driver);
}
forage._storageType = name;
return forage;
} catch (e) {
return undefined;
Expand Down Expand Up @@ -116,32 +117,32 @@ function storageFor(key, modelName, options = {}) {
* Looks up the storage factory on the container and sets initial state
* on the instance if desired.
*/
function createStorage(context, key, modelKey, options) {
function createStorage(context, key, modelKey, options, FactoryType, preferredKey) {
const owner = getOwner(context);
const factoryType = 'storage';
const storageFactory = `${factoryType}:${key}`;

let storageKey;

owner.registerOptionsForType(factoryType, { instantiate: false });

if (options.legacyKey) {
storageKey = options.legacyKey;
} else {
storageKey = modelKey ? `${storageFactory}:${modelKey}` : storageFactory;
let storageKey = preferredKey;
if (!storageKey) {
storageKey = options.legacyKey || (modelKey
? `${storageFactory}:${modelKey}`
: storageFactory);
}

const initialState = {},
defaultState = {
_storageKey: storageKey
},
StorageFactory = owner.lookup(storageFactory);
StorageFactory = FactoryType || owner.lookup(storageFactory);

if (!StorageFactory) {
throw new TypeError(`Unknown StorageFactory: ${storageFactory}`);
}

if (typeof(StorageFactory.initialState) === 'function') {
// Wrap normal initial state array if it is an array
const initialContent = StorageFactory.initialState.call(context);
initialState._initialContent = Ember.isArray(initialContent)
? Ember.A(initialContent)
Expand All @@ -167,9 +168,13 @@ function createStorage(context, key, modelKey, options) {

set(storageObj, '_initialContentString', JSON.stringify(storageObj._initialContent));
set(storageObj, 'content', content);
return storage.setItem(storageKey, content);
}).then(() => {
return storageObj;
});

// Wrap in the correct promise type with mixing to
// allow access to `reset()` and `clear()`
return storageObj._containedType === 'array'
? ArrayStoragePromise.create({ promise: storagePromise })
: ObjectStoragePromise.create({ promise: storagePromise });
Expand All @@ -195,5 +200,6 @@ export {
tryStorage,
getStorage,
storageFor,
createStorage,
_resetStorages
};
6 changes: 4 additions & 2 deletions addon/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
function save() {
this._super.apply(this, arguments);
this._save();
return this._save();
}

function saveIfChanged(key) {
this._super.apply(this, arguments);

if (key !== '_isInitialContent') {
this._save();
return this._save();
} else {
return this;
}
}

Expand Down
8 changes: 4 additions & 4 deletions addon/mixins/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Ember from 'ember';

export default Ember.Mixin.create({
clear() {
this.then((storage) => {
storage.clear();
return this.then((storage) => {
return storage.clear();
});
},
reset() {
this.then((storage) => {
storage.reset();
return this.then((storage) => {
return storage.reset();
});
}
});
8 changes: 5 additions & 3 deletions addon/mixins/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export default Mixin.create({
set(this, '_isInitialContent', false);
}

storage.setItem(storageKey, content);
return storage.setItem(storageKey, content).then(() => this);
} else {
return this;
}
},

Expand Down Expand Up @@ -111,7 +113,7 @@ export default Mixin.create({
// Do not change to set(this, 'content', content)
this.set('content', content);
set(this, '_isInitialContent', true);
storage.setItem(storageKey, content);
return storage.setItem(storageKey, content).then(() => this);
},

// clear the content
Expand All @@ -120,6 +122,6 @@ export default Mixin.create({
const storage = this._storage();
const storageKey = get(this, '_storageKey');
this._clear();
storage.removeItem(storageKey);
return storage.removeItem(storageKey).then(() => this);
}
});
Loading

0 comments on commit e8e8039

Please sign in to comment.