2016-07-04 14:15:51 -04:00
|
|
|
/**
|
|
|
|
We can insert data into the PreloadStore when the document is loaded.
|
|
|
|
The data can be accessed once by a key, after which it is removed
|
|
|
|
|
|
|
|
@class PreloadStore
|
|
|
|
**/
|
2019-11-08 13:28:11 -05:00
|
|
|
import { Promise } from "rsvp";
|
|
|
|
|
2016-07-04 14:15:51 -04:00
|
|
|
export default {
|
|
|
|
data: {},
|
|
|
|
|
|
|
|
store(key, value) {
|
|
|
|
this.data[key] = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
To retrieve a key, you provide the key you want, plus a finder to load
|
|
|
|
it if the key cannot be found. Once the key is used once, it is removed
|
|
|
|
from the store.
|
|
|
|
So, for example, you can't load a preloaded topic more than once.
|
|
|
|
**/
|
|
|
|
getAndRemove(key, finder) {
|
|
|
|
if (this.data[key]) {
|
2019-11-19 15:58:26 -05:00
|
|
|
let promise = Promise.resolve(this.data[key]);
|
2016-07-04 14:15:51 -04:00
|
|
|
delete this.data[key];
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (finder) {
|
2019-11-08 13:28:11 -05:00
|
|
|
return new Promise(function(resolve, reject) {
|
2019-11-19 15:58:26 -05:00
|
|
|
let result = finder();
|
2016-07-04 14:15:51 -04:00
|
|
|
|
|
|
|
// If the finder returns a promise, we support that too
|
|
|
|
if (result && result.then) {
|
2018-06-15 11:03:24 -04:00
|
|
|
result
|
|
|
|
.then(toResolve => resolve(toResolve))
|
|
|
|
.catch(toReject => reject(toReject));
|
2016-07-04 14:15:51 -04:00
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:28:11 -05:00
|
|
|
return Promise.resolve(null);
|
2016-07-04 14:15:51 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
get(key) {
|
|
|
|
return this.data[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
remove(key) {
|
|
|
|
if (this.data[key]) delete this.data[key];
|
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this.data = {};
|
|
|
|
}
|
|
|
|
};
|