phaser/src/cache/BaseCache.js

172 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../utils/Class');
2017-10-04 23:09:12 +00:00
var CustomMap = require('../structs/Map');
var EventEmitter = require('eventemitter3');
2018-02-07 15:27:21 +00:00
/**
* @classdesc
* The BaseCache is a base Cache class that can be used for storing references to any kind of data.
*
* Data can be added, retrieved and removed based on the given keys.
*
* Keys are string-based.
*
* @class BaseCache
* @memberOf Phaser.Cache
* @constructor
* @since 3.0.0
*/
var BaseCache = new Class({
initialize:
function BaseCache ()
{
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* The Map in which the cache objects are stored.
*
* You can query the Map directly or use the BaseCache methods.
2017-10-04 23:09:12 +00:00
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.BaseCache#entries
* @type {Phaser.Structs.Map}
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.entries = new CustomMap();
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* An instance of EventEmitter used by the cache to emit related events.
2017-10-04 23:09:12 +00:00
*
2018-02-12 23:51:47 +00:00
* @name Phaser.Cache.BaseCache#events
2018-02-13 00:40:51 +00:00
* @type {EventEmitter}
2018-01-25 03:38:23 +00:00
* @since 3.0.0
2017-10-04 23:09:12 +00:00
*/
this.events = new EventEmitter();
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Cache add event.
*
* This event is fired by the Cache each time a new object is added to it.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Cache.BaseCache#addEvent
2018-01-25 03:38:23 +00:00
* @param {Phaser.Cache.BaseCache} The BaseCache to which the object was added.
* @param {string} The key of the object added to the cache.
2018-03-20 16:15:49 +00:00
* @param {*} A reference to the object added to the cache.
2018-01-25 03:38:23 +00:00
*/
/**
* Adds an item to this cache. The item is referenced by a unique string, which you are responsible
* for setting and keeping track of. The item can only be retrieved by using this string.
2017-10-04 23:09:12 +00:00
*
* @method Phaser.Cache.BaseCache#add
2018-02-12 23:51:47 +00:00
* @fires Phaser.Cache.BaseCache#addEvent
2017-10-04 23:09:12 +00:00
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {string} key - The unique key by which the data added to the cache will be referenced.
2018-03-20 16:15:49 +00:00
* @param {*} data - The data to be stored in the cache.
2018-01-25 03:38:23 +00:00
*
* @return {Phaser.Cache.BaseCache} This BaseCache object.
2017-10-04 23:09:12 +00:00
*/
add: function (key, data)
{
this.entries.set(key, data);
this.events.emit('add', this, key, data);
2018-01-25 03:38:23 +00:00
return this;
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Checks if this cache contains an item matching the given key.
2017-10-04 23:09:12 +00:00
*
* @method Phaser.Cache.BaseCache#has
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {string} key - The unique key of the item to be checked in this cache.
2018-03-20 16:15:49 +00:00
*
2018-01-25 03:38:23 +00:00
* @return {boolean} Returns `true` if the cache contains an item matching the given key, otherwise `false`.
2017-10-04 23:09:12 +00:00
*/
has: function (key)
{
return this.entries.has(key);
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Gets an item from this cache based on the given key.
2017-10-04 23:09:12 +00:00
*
* @method Phaser.Cache.BaseCache#get
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {string} key - The unique key of the item to be retrieved from this cache.
2018-03-20 16:15:49 +00:00
*
* @return {*} The item in the cache, or `null` if no item matching the given key was found.
2017-10-04 23:09:12 +00:00
*/
get: function (key)
{
return this.entries.get(key);
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Cache remove event.
*
* This event is fired by the Cache each time an object is removed from it.
*
2018-02-12 23:51:47 +00:00
* @event Phaser.Cache.BaseCache#removeEvent
2018-01-25 03:38:23 +00:00
* @param {Phaser.Cache.BaseCache} The BaseCache from which the object was removed.
* @param {string} The key of the object removed from the cache.
2018-03-20 16:15:49 +00:00
* @param {*} The object that was removed from the cache.
2018-01-25 03:38:23 +00:00
*/
/**
* Removes and item from this cache based on the given key.
*
* If an entry matching the key is found it is removed from the cache and a `remove` event emitted.
* No additional checks are done on the item removed. If other systems or parts of your game code
* are relying on this item, it is up to you to sever those relationships prior to removing the item.
2017-10-04 23:09:12 +00:00
*
* @method Phaser.Cache.BaseCache#remove
2018-02-12 23:51:47 +00:00
* @fires Phaser.Cache.BaseCache#removeEvent
2017-10-04 23:09:12 +00:00
* @since 3.0.0
*
2018-01-25 03:38:23 +00:00
* @param {string} key - The unique key of the item to remove from the cache.
*
* @return {Phaser.Cache.BaseCache} This BaseCache object.
2017-10-04 23:09:12 +00:00
*/
remove: function (key)
{
var entry = this.get(key);
if (entry)
{
this.entries.delete(key);
this.events.emit('remove', this, key, entry.data);
}
2018-01-25 03:38:23 +00:00
return this;
},
2017-10-04 23:09:12 +00:00
/**
2018-01-25 03:38:23 +00:00
* Destroys this cache and all items within it.
2017-10-04 23:09:12 +00:00
*
* @method Phaser.Cache.BaseCache#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.entries.clear();
2018-01-25 03:38:23 +00:00
this.events.removeAllListeners();
this.entries = null;
this.events = null;
}
});
module.exports = BaseCache;