-
Notifications
You must be signed in to change notification settings - Fork 9
/
assets.js
69 lines (61 loc) · 1.58 KB
/
assets.js
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
import { RealtimeAsset } from './asset.js';
import { Events } from '@playcanvas/observer';
/**
* Provides methods to load assets from sharedb
*
* @category Internal
*/
class RealtimeAssets extends Events {
/** @typedef {import("../realtime").Realtime} Realtime */
/** @typedef {import("../realtime/connection").RealtimeConnection} RealtimeConnection */
/**
* Constructor
*
* @param {Realtime} realtime - The realtime API
* @param {RealtimeConnection} connection - The realtime connection
*/
constructor(realtime, connection) {
super();
this._realtime = realtime;
this._connection = connection;
this._assets = {};
}
/**
* Loads an asset
*
* @param {number} id - The asset's unique id
* @returns {RealtimeAsset} The asset
*/
load(id) {
let asset = this._assets[id];
if (!asset) {
asset = new RealtimeAsset(id, this._realtime, this._connection);
this._assets[id] = asset;
}
if (!asset.loaded) {
asset.load();
}
return asset;
}
/**
* Gets an already loaded asset
*
* @param {number} id - The asset's unique id
* @returns {RealtimeAsset} The asset
*/
get(id) {
return this._assets[id] || null;
}
/**
* Unloads an asset
*
* @param {number} id - The asset's unique id
*/
unload(id) {
if (this._assets[id]) {
this._assets[id].unload();
delete this._assets[id];
}
}
}
export { RealtimeAssets };