Files
discourse/app/assets/javascripts/preload_store.js
T

91 lines
2.2 KiB
JavaScript
Raw Normal View History

2013-03-08 15:04:37 -05: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
**/
2013-12-30 12:42:05 -05:00
window.PreloadStore = {
data: {},
2013-03-08 15:04:37 -05:00
/**
Store an object in the store
@method store
@param {String} key the key to store the object with
@param {String} value the object we're inserting into the store
**/
store: function(key, value) {
this.data[key] = value;
},
2013-03-08 15:04:37 -05:00
/**
2013-03-20 16:26:46 +01:00
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.
2013-03-08 15:04:37 -05:00
2013-03-20 16:26:46 +01:00
@method getAndRemove
2013-03-08 15:04:37 -05:00
@param {String} key the key to look up the object with
@param {function} finder a function to find the object with
2014-09-24 14:17:09 -04:00
@returns {Promise} a promise that will eventually be the object we want.
2013-03-08 15:04:37 -05:00
**/
2013-03-20 16:26:46 +01:00
getAndRemove: function(key, finder) {
2013-10-10 18:33:24 +02:00
if (this.data[key]) {
2014-05-15 15:52:09 +10:00
var promise = Em.RSVP.resolve(this.data[key]);
2013-10-10 18:33:24 +02:00
delete this.data[key];
2013-07-15 19:47:13 -04:00
return promise;
}
2013-07-15 19:47:13 -04:00
if (finder) {
2014-09-24 14:17:09 -04:00
return new Ember.RSVP.Promise(function(resolve, reject) {
2013-07-15 19:47:13 -04:00
var result = finder();
// If the finder returns a promise, we support that too
if (result.then) {
result.then(function(result) {
2014-09-24 14:17:09 -04:00
return resolve(result);
2013-07-15 19:47:13 -04:00
}, function(result) {
2014-09-24 14:17:09 -04:00
return reject(result);
2013-07-15 19:47:13 -04:00
});
} else {
2014-09-24 14:17:09 -04:00
resolve(result);
}
2013-07-15 19:47:13 -04:00
});
}
2014-09-24 14:17:09 -04:00
return Ember.RSVP.resolve(null);
},
2013-03-08 15:04:37 -05:00
/**
2013-03-20 16:26:46 +01:00
If we are sure it's preloaded, we don't have to supply a finder.
Just returns undefined if it's not in the store.
2013-03-08 15:04:37 -05:00
2013-03-20 16:26:46 +01:00
@method get
2013-03-08 15:04:37 -05:00
@param {String} key the key to look up the object with
2013-03-20 16:26:46 +01:00
@returns {Object} the object from the store
2013-03-08 15:04:37 -05:00
**/
2013-04-02 17:44:08 +11:00
"get": function(key) {
2013-03-20 16:26:46 +01:00
return this.data[key];
},
2013-03-08 15:04:37 -05:00
/**
2013-03-20 16:26:46 +01:00
Removes the stored value if the key exists
2013-03-08 15:04:37 -05:00
2013-03-20 16:26:46 +01:00
@method remove
@param {String} key the key to remove
2013-03-08 15:04:37 -05:00
**/
2013-03-20 16:26:46 +01:00
remove: function(key) {
if (this.data[key]) delete this.data[key];
2014-07-31 13:24:07 -04:00
},
/**
Resets the contents of the store. Used in testing.
**/
reset: function() {
this.data = {};
}
2013-03-08 15:04:37 -05:00
};