Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type updates #83

Merged
merged 12 commits into from
Jan 16, 2025
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default [
'jsdoc/require-param': 'off',
'jsdoc/require-param-type': 'off',
'jsdoc/require-returns': 'off',
'jsdoc/require-returns-type': 'off',
'no-use-before-define': 'off'
}
},
Expand Down
88 changes: 42 additions & 46 deletions src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { globals as api } from './globals';
*/
export type AssetObserver = Observer & {
/**
* The history of changes made to the observer.
* The API asset associated with this observer.
*/
history: ObserverHistory;
apiAsset: Asset;

/**
* The API asset associated with this observer.
* The history of changes made to the observer.
*/
apiAsset: Asset;
history: ObserverHistory;
};

/**
Expand Down Expand Up @@ -359,13 +359,13 @@ class Asset extends Events {
/**
* Constructor
*
* @param {object} data - The asset data
* @param data - The asset data
*/
constructor(data: any = {}) {
super();

// allow duplicate values in data.frameKeys of sprite asset
let options = null;
let options = {};
if (data.type === 'sprite') {
options = {
pathsWithDuplicates: ['data.frameKeys']
Expand All @@ -381,7 +381,7 @@ class Asset extends Events {
path: []
}, data);

this._observer = new Observer(data, options || {}) as AssetObserver;
this._observer = new Observer(data, options) as AssetObserver;
this._observer.apiAsset = this;
this._observer.addEmitter(this);

Expand All @@ -407,7 +407,7 @@ class Asset extends Events {
this._history = {};
}

_initializeHistory() {
initializeHistory() {
if (this._observer.history) return;

this._history = new ObserverHistory({
Expand All @@ -419,7 +419,7 @@ class Asset extends Events {
this._observer.history = this._history as ObserverHistory;
}

_resetThumbnailUrls() {
private _resetThumbnailUrls() {
const type = this.get('type') || '';
if (!type.startsWith('texture')) return;

Expand All @@ -436,7 +436,7 @@ class Asset extends Events {
}
}

_onSet(path: string, value: any) {
private _onSet(path: string, value: any) {
if (this._suspendOnSet || !path.startsWith('file') || path.endsWith('.url') || !this.get('file')) {
return;
}
Expand All @@ -461,8 +461,8 @@ class Asset extends Events {
/**
* Checks if path exists. See the {@link Asset} overview for a full list of properties.
*
* @param {string} path - The path
* @returns {boolean} True if path exists
* @param path - The path
* @returns True if path exists
*/
has(path: string) {
return this._observer.has(path);
Expand All @@ -471,8 +471,8 @@ class Asset extends Events {
/**
* Gets value at path. See the {@link Asset} overview for a full list of properties.
*
* @param {string} path - The path
* @returns {any} The value
* @param path - The path
* @returns The value
*/
get(path: string) {
return this._observer.get(path);
Expand All @@ -481,9 +481,9 @@ class Asset extends Events {
/**
* Sets value at path. See the {@link Asset} overview for a full list of properties.
*
* @param {string} path - The path
* @param {any} value - The value
* @returns {boolean} Whether the value was set
* @param path - The path
* @param value - The value
* @returns Whether the value was set
*/
set(path: string, value: any) {
return this._observer.set(path, value);
Expand All @@ -492,8 +492,8 @@ class Asset extends Events {
/**
* Unsets value at path. See the {@link Asset} overview for a full list of properties.
*
* @param {string} path - The path
* @returns {boolean} Whether the value was unset
* @param path - The path
* @returns Whether the value was unset
*/
unset(path: string) {
return this._observer.unset(path);
Expand All @@ -503,10 +503,10 @@ class Asset extends Events {
* Inserts value in array at path, at specified index. See the {@link Asset} overview for a
* full list of properties.
*
* @param {string} path - The path
* @param {any} value - The value
* @param {number} index - The index (if undefined the value will be inserted in the end)
* @returns {boolean} Whether the value was inserted
* @param path - The path
* @param value - The value
* @param index - The index (if undefined the value will be inserted in the end)
* @returns Whether the value was inserted
*/
insert(path: any, value: any, index: any) {
return this._observer.insert(path, value, index);
Expand All @@ -515,9 +515,9 @@ class Asset extends Events {
/**
* Remove value from array at path. See the {@link Asset} overview for a full list of properties.
*
* @param {string} path - The path
* @param {any} value - The value
* @returns {boolean} Whether the value was removed
* @param path - The path
* @param value - The value
* @returns Whether the value was removed
*/
removeValue(path: any, value: any) {
return this._observer.removeValue(path, value);
Expand All @@ -526,7 +526,7 @@ class Asset extends Events {
/**
* Returns JSON representation of entity data
*
* @returns {object} - The data
* @returns - The data
*/
json() {
return this._observer.json();
Expand All @@ -535,7 +535,7 @@ class Asset extends Events {
/**
* Returns the latest version of the Asset from the Assets API.
*
* @returns {Asset} The asset
* @returns The asset
*/
latest() {
return api.assets.get(this._observer.get('id'));
Expand Down Expand Up @@ -619,27 +619,25 @@ class Asset extends Events {
/**
* Creates an instance of this template asset. Assumes this asset is a template asset.
*
* @param {Entity} parent - The parent entity
* @param {object} options - Options
* @param {number} options.index - The desired index under the parent to instantiate the template.
* @param {boolean} options.history - Whether to record a history action.
* @param {boolean} options.select - Whether to select the new entity.
* @param {object} options.extraData - Extra data passed to the backend. Used by the Editor on specific cases.
* @returns {Promise<Entity>} The new entity.
* @param parent - The parent entity
* @param options.index - The desired index under the parent to instantiate the template.
* @param options.history - Whether to record a history action.
* @param options.select - Whether to select the new entity.
* @param options.extraData - Extra data passed to the backend. Used by the Editor on specific cases.
* @returns The new entity.
*/
async instantiateTemplate(parent: Entity, options: any) {
async instantiateTemplate(parent: Entity, options: { index?: number, history?: boolean, select?: boolean, extraData?: object } = {}) {
const entities = await api.assets.instantiateTemplates([this], parent, options);
return entities[0];
}

/**
* Replaces any references to this asset with references to the new asset specified.
*
* @param {Asset} asset - The new asset.
* @param {object} options - Options.
* @param {boolean} options.history - Whether to record a history action.
* @param asset - The new asset.
* @param options.history - Whether to record a history action.
*/
replace(asset: Asset, options: any = {}) {
replace(asset: Asset, options: { history?: boolean } = {}) {
replace(this, asset, options);
}

Expand All @@ -652,19 +650,17 @@ class Asset extends Events {

/**
* Gets observer history for this asset.
*
* @type {ObserverHistory}
*/
get history() {
return this._history;
return this._history as ObserverHistory;
}

/**
* Gets the file URL for an asset file.
*
* @param {number} id - The asset id
* @param {string} filename - The desired filename
* @returns {string} The file URL
* @param id - The asset id
* @param filename - The desired filename
* @returns The file URL
*/
static getFileUrl(id: number, filename: string) {
return `/api/assets/${id}/file/${encodeURIComponent(filename)}?branchId=${api.branchId}`;
Expand Down
Loading
Loading