| 26 | * @category Resources > JSON |
| 27 | */ |
| 28 | export class JsonManager implements gdjs.ResourceManager { |
| 29 | _resourceLoader: ResourceLoader; |
| 30 | |
| 31 | _loadedJsons = new gdjs.ResourceCache<Object>(); |
| 32 | _callbacks = new gdjs.ResourceCache<Array<JsonManagerRequestCallback>>(); |
| 33 | |
| 34 | /** |
| 35 | * @param resourceLoader The resources loader of the game. |
| 36 | */ |
| 37 | constructor(resourceLoader: gdjs.ResourceLoader) { |
| 38 | this._resourceLoader = resourceLoader; |
| 39 | } |
| 40 | |
| 41 | getResourceKinds(): ResourceKind[] { |
| 42 | return resourceKinds; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Request all the json resources to be preloaded (unless they are marked as not preloaded). |
| 47 | * |
| 48 | * Note that even if a JSON is already loaded, it will be reloaded (useful for hot-reloading, |
| 49 | * as JSON files can have been modified without the editor knowing). |
| 50 | */ |
| 51 | async loadResource(resourceName: string): Promise<void> { |
| 52 | const resource = this._resourceLoader.getResource(resourceName); |
| 53 | if (!resource) { |
| 54 | logger.warn('Unable to find json for resource "' + resourceName + '".'); |
| 55 | return; |
| 56 | } |
| 57 | if (resource.disablePreload) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | try { |
| 62 | await this.loadJsonAsync(resource.name); |
| 63 | } catch (error) { |
| 64 | logger.error( |
| 65 | `Error while preloading json resource ${resource.name}:`, |
| 66 | error |
| 67 | ); |
| 68 | throw error; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | loadJsonAsync(resourceName: string): Promise<Object | null> { |
| 73 | const that = this; |
| 74 | return new Promise((resolve, reject) => { |
| 75 | that.loadJson(resourceName, (error, content) => { |
| 76 | if (error) { |
| 77 | reject(error.message); |
| 78 | } |
| 79 | resolve(content); |
| 80 | }); |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | private _getJsonResource = (resourceName: string): ResourceData | null => { |
| 85 | const resource = this._resourceLoader.getResource(resourceName); |
nothing calls this directly
no test coverage detected