forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCache.js
More file actions
181 lines (161 loc) · 5.3 KB
/
Copy pathBaseCache.js
File metadata and controls
181 lines (161 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2026 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var CustomMap = require('../structs/Map');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
/**
* @classdesc
* A key-value store used by the Phaser framework to cache loaded assets and other data.
* Items are stored and retrieved using string-based keys. The BaseCache emits events when
* items are added or removed, allowing other systems to react to cache changes. Multiple
* BaseCache instances are owned by the CacheManager, one per asset type (JSON, binary,
* audio, etc.), and you can also create custom caches via `CacheManager.addCustom()`.
*
* @class BaseCache
* @memberof Phaser.Cache
* @constructor
* @since 3.0.0
*/
var BaseCache = new Class({
initialize:
function BaseCache ()
{
/**
* The Map in which the cache objects are stored.
*
* You can query the Map directly or use the BaseCache methods.
*
* @name Phaser.Cache.BaseCache#entries
* @type {Phaser.Structs.Map.<String, *>}
* @since 3.0.0
*/
this.entries = new CustomMap();
/**
* An instance of EventEmitter used by the cache to emit related events.
*
* @name Phaser.Cache.BaseCache#events
* @type {Phaser.Events.EventEmitter}
* @since 3.0.0
*/
this.events = new EventEmitter();
},
/**
* 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.
*
* @method Phaser.Cache.BaseCache#add
* @fires Phaser.Cache.Events#ADD
* @since 3.0.0
*
* @param {string} key - The unique key by which the data added to the cache will be referenced.
* @param {*} data - The data to be stored in the cache.
*
* @return {this} This BaseCache object.
*/
add: function (key, data)
{
this.entries.set(key, data);
this.events.emit(Events.ADD, this, key, data);
return this;
},
/**
* Checks if this cache contains an item matching the given key.
* This performs the same action as `BaseCache.exists`.
*
* @method Phaser.Cache.BaseCache#has
* @since 3.0.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`.
*/
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);
},
/**
* Gets an item from this cache based on the given key.
*
* @method Phaser.Cache.BaseCache#get
* @since 3.0.0
*
* @param {string} key - The unique key of the item to be retrieved from this cache.
*
* @return {*} The item in the cache, or `null` if no item matching the given key was found.
*/
get: function (key)
{
return this.entries.get(key);
},
/**
* Removes an 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.
*
* @method Phaser.Cache.BaseCache#remove
* @fires Phaser.Cache.Events#REMOVE
* @since 3.0.0
*
* @param {string} key - The unique key of the item to remove from the cache.
*
* @return {this} This BaseCache object.
*/
remove: function (key)
{
var entry = this.get(key);
if (entry)
{
this.entries.delete(key);
this.events.emit(Events.REMOVE, this, key, entry.data);
}
return this;
},
/**
* Returns all keys in use in this cache.
*
* @method Phaser.Cache.BaseCache#getKeys
* @since 3.17.0
*
* @return {string[]} An array of strings containing all keys currently stored in this cache.
*/
getKeys: function ()
{
return this.entries.keys();
},
/**
* Destroys this cache and all items within it. Clears the entries Map, removes all
* event listeners from the EventEmitter, and nulls the internal references.
*
* @method Phaser.Cache.BaseCache#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.entries.clear();
this.events.removeAllListeners();
this.entries = null;
this.events = null;
}
});
module.exports = BaseCache;