Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/asset/core/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @import { Constructor } from '../../type/index.js' */
import { throws } from '../../logger/index.js'

/**
* @abstract
* @template T
*/
export class Exporter {

/**
* @readonly
* @type {Constructor<T>}
*/
asset

/**
* @param {Constructor<T>} asset
*/
constructor(asset) {
this.asset = asset
}

/**
* @param {T} _asset
* @returns {Promise<BodyInit | undefined>}
*/
async serialize(_asset) {
throws(`Implement the method \`serialize\` on \`${this.constructor.name}\``)

return undefined
}

/**
* @returns {string[]}
*/
getExtensions() {
throws(`Implement the method \`getExtensions\` on \`${this.constructor.name}\``)

return []
}
}
1 change: 1 addition & 0 deletions src/asset/core/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './asset.js'
export * from './exporter.js'
export * from './parser.js'
19 changes: 18 additions & 1 deletion src/asset/events/fail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/** @import {TypeId} from '../../type/index.js' */
/** @import {AssetId} from '../types/index.js' */

/**
* @readonly
* @enum {number}
*/
export const AssetLoadOperation = {
Loading: 1,
Saving: 2
}

export class AssetLoadFail {

/**
Expand All @@ -22,16 +32,23 @@ export class AssetLoadFail {
*/
reason

/**
* @type {number}
*/
operation

/**
* @param {TypeId} typeId
* @param {AssetId} assetId
* @param {string} path
* @param {string} reason
* @param {number} [operation=AssetLoadOperation.Loading]
*/
constructor(typeId, assetId, path, reason) {
constructor(typeId, assetId, path, reason, operation = AssetLoadOperation.Loading) {
this.typeId = typeId
this.assetId = assetId
this.path = path
this.reason = reason
this.operation = operation
}
}
1 change: 1 addition & 0 deletions src/asset/events/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './fail.js'
export * from './success.js'
export * from './save.js'
export * from './assets.js'
31 changes: 31 additions & 0 deletions src/asset/events/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @import {TypeId} from '../../type/index.js' */
/** @import {AssetId} from '../types/index.js' */

export class AssetSaveSuccess {

/**
* @type {TypeId}
*/
typeId

/**
* @type {AssetId}
*/
assetId

/**
* @type {string}
*/
path

/**
* @param {TypeId} typeId
* @param {AssetId} assetId
* @param {string} path
*/
constructor(typeId, assetId, path) {
this.path = path
this.typeId = typeId
this.assetId = assetId
}
}
8 changes: 6 additions & 2 deletions src/asset/plugins/assetServer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { App, Plugin } from '../../app/index.js'
import { EventPlugin } from '../../event/index.js'
import { AssetServer } from '../resources/index.js'
import { AssetLoadFail, AssetLoadSuccess } from '../events/index.js'
import { updateAssets, updateAssetLoadEvents, logFailedLoads, registerAssetServerTypes } from '../systems/index.js'
import { AssetLoadFail, AssetLoadSuccess, AssetSaveSuccess } from '../events/index.js'
import { updateAssets, updateAssetLoadEvents, updateAssetSaveEvents, logFailedLoads, registerAssetServerTypes } from '../systems/index.js'
import { AppSchedule, CoreSystems } from '../../core/index.js'

export class AssetServerPlugin extends Plugin {
Expand All @@ -16,12 +16,16 @@ export class AssetServerPlugin extends Plugin {
.registerPlugin(new EventPlugin({
event: AssetLoadSuccess
}))
.registerPlugin(new EventPlugin({
event: AssetSaveSuccess
}))
.registerPlugin(new EventPlugin({
event: AssetLoadFail
}))
.registerSystem({ schedule: AppSchedule.Startup, systemGroup: CoreSystems.Start, system: registerAssetServerTypes })
.registerSystem({ schedule: AppSchedule.Update, systemGroup: CoreSystems.End, system: updateAssets })
.registerSystem({ schedule: AppSchedule.Update, systemGroup: CoreSystems.End, system: updateAssetLoadEvents })
.registerSystem({ schedule: AppSchedule.Update, systemGroup: CoreSystems.End, system: updateAssetSaveEvents })
.registerSystem({ schedule: AppSchedule.Update, systemGroup: CoreSystems.End, system: logFailedLoads })
}
}
61 changes: 61 additions & 0 deletions src/asset/plugins/exporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/** @import {Constructor} from '../../type/index.js' */
import { App, Plugin } from '../../app/index.js'
import { AppSchedule, CoreSystems } from '../../core/index.js'
import { typeid, typeidGeneric } from '../../type/index.js'
import { Exporter } from '../core/index.js'
import { registerAssetExporterOnAssetServer } from '../systems/index.js'

/**
* @template T
*/
export class AssetExporterPlugin extends Plugin {

/**
* @readonly
* @type {Constructor<T>}
*/
asset

/**
* @readonly
* @type {Exporter<T>}
*/
exporter

/**
* @param {AssetExporterPluginOptions<T>} options
*/
constructor(options) {
super()
const { asset, exporter } = options

this.asset = asset
this.exporter = exporter
}

/**
* @param {App} app
*/
register(app) {
const { asset, exporter } = this

app
.registerSystem({
label: `registerAssetExporterOnAssetServer<${typeid(asset)}>`,
schedule: AppSchedule.Startup,
systemGroup: CoreSystems.Start,
system: registerAssetExporterOnAssetServer(asset, exporter)
})
}

name() {
return typeidGeneric(AssetExporterPlugin, [this.asset])
}
}

/**
* @template T
* @typedef AssetExporterPluginOptions
* @property {Constructor<T>} asset
* @property {Exporter<T>} exporter
*/
1 change: 1 addition & 0 deletions src/asset/plugins/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './asset.js'
export * from './assetServer.js'
export * from './exporter.js'
export * from './parser.js'
Loading
Loading