2017-06-30 14:47:51 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-10-04 23:09:12 +00:00
|
|
|
var CustomMap = require('../structs/Map');
|
2018-01-12 17:09:09 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2017-01-30 16:56:04 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
var BaseCache = new Class({
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
initialize:
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class BaseCache
|
|
|
|
* @memberOf Phaser.Cache
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-06-30 14:47:51 +00:00
|
|
|
function BaseCache ()
|
|
|
|
{
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Structs.Map} entries
|
|
|
|
*/
|
|
|
|
this.entries = new CustomMap();
|
2017-06-30 14:47:51 +00:00
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Events.EventDispatcher} events
|
|
|
|
*/
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events = new EventEmitter();
|
2017-06-30 14:47:51 +00:00
|
|
|
},
|
2016-12-09 09:32:24 +00:00
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#add
|
|
|
|
* @fires CacheAddEvent
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key [description]
|
|
|
|
* @param {any} data [description]
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
add: function (key, data)
|
|
|
|
{
|
2017-01-16 21:55:04 +00:00
|
|
|
this.entries.set(key, data);
|
2017-01-30 16:56:04 +00:00
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events.emit('add', this, key, data);
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#has
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key [description]
|
2017-10-04 23:58:42 +00:00
|
|
|
*
|
2017-10-04 23:09:12 +00:00
|
|
|
* @return {boolean} [description]
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
has: function (key)
|
|
|
|
{
|
2017-01-16 21:55:04 +00:00
|
|
|
return this.entries.has(key);
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#get
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key [description]
|
2017-10-04 23:58:42 +00:00
|
|
|
*
|
2017-10-04 23:09:12 +00:00
|
|
|
* @return {any} [description]
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
get: function (key)
|
|
|
|
{
|
2017-01-16 21:55:04 +00:00
|
|
|
return this.entries.get(key);
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#remove
|
|
|
|
* @fires CacheRemoveEvent
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} key [description]
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
remove: function (key)
|
|
|
|
{
|
2017-01-30 16:56:04 +00:00
|
|
|
var entry = this.get(key);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
this.entries.delete(key);
|
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
this.events.emit('remove', this, key, entry.data);
|
2017-01-30 16:56:04 +00:00
|
|
|
}
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2017-10-04 23:09:12 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2017-01-16 21:55:04 +00:00
|
|
|
this.entries.clear();
|
2016-12-09 09:32:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 14:47:51 +00:00
|
|
|
});
|
2016-12-09 09:32:24 +00:00
|
|
|
|
|
|
|
module.exports = BaseCache;
|