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}
|
|
|
|
*/
|
|
|
|
|
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');
|
2019-01-15 16:17:04 +00:00
|
|
|
var Events = require('./events');
|
2017-01-30 16:56:04 +00:00
|
|
|
|
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
|
|
|
|
*/
|
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-06-30 14:47:51 +00:00
|
|
|
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-06-30 14:47:51 +00:00
|
|
|
|
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
|
|
|
*/
|
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
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @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
|
|
|
*/
|
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
|
|
|
|
2019-01-15 16:17:04 +00:00
|
|
|
this.events.emit(Events.ADD, this, key, data);
|
2018-01-25 03:38:23 +00:00
|
|
|
|
|
|
|
return this;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
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.
|
2018-04-25 16:07:39 +00:00
|
|
|
* 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
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
2018-04-25 16:07:39 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
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
|
|
|
},
|
|
|
|
|
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
|
2019-01-15 16:17:04 +00:00
|
|
|
* @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
|
|
|
*/
|
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);
|
|
|
|
|
2019-01-15 16:17:04 +00:00
|
|
|
this.events.emit(Events.REMOVE, this, key, entry.data);
|
2017-01-30 16:56:04 +00:00
|
|
|
}
|
2018-01-25 03:38:23 +00:00
|
|
|
|
|
|
|
return this;
|
2016-12-09 09:32:24 +00:00
|
|
|
},
|
|
|
|
|
2019-05-02 01:27:29 +00:00
|
|
|
/**
|
|
|
|
* Returns all keys in use in this cache.
|
|
|
|
*
|
|
|
|
* @method Phaser.Cache.BaseCache#getKeys
|
|
|
|
* @since 3.17.0
|
|
|
|
*
|
|
|
|
* @return {string[]} Array containing all the keys.
|
|
|
|
*/
|
|
|
|
getKeys: function ()
|
|
|
|
{
|
|
|
|
return this.entries.keys();
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
*/
|
2016-12-09 09:32:24 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2017-01-16 21:55:04 +00:00
|
|
|
this.entries.clear();
|
2018-01-25 03:38:23 +00:00
|
|
|
this.events.removeAllListeners();
|
|
|
|
|
|
|
|
this.entries = null;
|
|
|
|
this.events = null;
|
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;
|