phaser/src/cache/BaseCache.js

168 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-02-12 16:01:20 +00:00
* @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');
var Events = require('./events');
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
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Cache
2018-02-07 15:27:21 +00:00
* @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
2018-03-23 15:54:12 +00:00
* @type {Phaser.Structs.Map.<String, *>}
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-03-29 12:12:07 +00:00
* @type {Phaser.Events.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();
},
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
* @fires Phaser.Cache.Events#ADD
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(Events.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.
* This performs the same action as `BaseCache.exists`.
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);
},
/**
* Checks if this cache contains an item matching the given key.
* This performs the same action as `BaseCache.has` and is called directly by the Loader.
*
* @method Phaser.Cache.BaseCache#exists
* @since 3.7.0
*
* @param {string} key - The unique key of the item to be checked in this cache.
*
* @return {boolean} Returns `true` if the cache contains an item matching the given key, otherwise `false`.
*/
exists: 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);
},
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
* @fires Phaser.Cache.Events#REMOVE
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(Events.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;