Skip to content

Commit

Permalink
removed Class type patching (#6140)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpal81xd authored Mar 12, 2024
1 parent 001f5c1 commit dcd2634
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/framework/components/script/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class ScriptComponent extends Component {
/**
* Detect if script is attached to an entity.
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @returns {boolean} If script is attached to an entity.
* @example
Expand All @@ -571,7 +571,7 @@ class ScriptComponent extends Component {
/**
* Get a script instance (if attached).
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @returns {import('../../script/script-type.js').ScriptType|null} If script is attached, the
* instance is returned. Otherwise null is returned.
Expand All @@ -595,7 +595,7 @@ class ScriptComponent extends Component {
/**
* Create a script instance and attach to an entity script component.
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {object} [args] - Object with arguments for a script.
* @param {boolean} [args.enabled] - If script instance is enabled after creation. Defaults to
Expand Down Expand Up @@ -700,7 +700,7 @@ class ScriptComponent extends Component {
/**
* Destroy the script instance that is attached to an entity.
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @returns {boolean} If it was successfully destroyed.
* @example
Expand Down Expand Up @@ -757,7 +757,7 @@ class ScriptComponent extends Component {
/**
* Swap the script instance.
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @returns {boolean} If it was successfully swapped.
* @private
Expand Down Expand Up @@ -928,7 +928,7 @@ class ScriptComponent extends Component {
/**
* Move script instance to different position to alter update order of scripts within entity.
*
* @param {string|Class<import('../../script/script-type.js').ScriptType>} nameOrType - The
* @param {string|typeof import('../../script/script-type.js').ScriptType} nameOrType - The
* name or type of {@link ScriptType}.
* @param {number} ind - New position index.
* @returns {boolean} If it was successfully moved.
Expand Down
4 changes: 2 additions & 2 deletions src/framework/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for the first script instance of specified type.
*
* @param {string|Class<import('./script/script-type.js').ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @param {string|typeof import('./script/script-type.js').ScriptType} nameOrType - The name or type of {@link ScriptType}.
* @returns {import('./script/script-type.js').ScriptType|undefined} A script instance of specified type, if the entity or any of its descendants
* has one. Returns undefined otherwise.
* @example
Expand All @@ -417,7 +417,7 @@ class Entity extends GraphNode {
/**
* Search the entity and all of its descendants for all script instances of specified type.
*
* @param {string|Class<import('./script/script-type.js').ScriptType>} nameOrType - The name or type of {@link ScriptType}.
* @param {string|typeof import('./script/script-type.js').ScriptType} nameOrType - The name or type of {@link ScriptType}.
* @returns {import('./script/script-type.js').ScriptType[]} All script instances of specified type in the entity or any of its
* descendants. Returns empty array if none found.
* @example
Expand Down
2 changes: 1 addition & 1 deletion src/framework/script/script-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class ScriptAttributes {
/**
* Create a new ScriptAttributes instance.
*
* @param {Class<import('./script-type.js').ScriptType>} scriptType - Script Type that attributes relate to.
* @param {typeof import('./script-type.js').ScriptType} scriptType - Script Type that attributes relate to.
*/
constructor(scriptType) {
this.scriptType = scriptType;
Expand Down
10 changes: 5 additions & 5 deletions src/framework/script/script-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ScriptRegistry extends EventHandler {
* registry, and the new script has a `swap` method defined, it will perform code hot swapping
* automatically in async manner.
*
* @param {Class<import('./script-type.js').ScriptType>} script - Script Type that is created
* @param {typeof import('./script-type.js').ScriptType} script - Script Type that is created
* using {@link createScript}.
* @returns {boolean} True if added for the first time or false if script already exists.
* @example
Expand Down Expand Up @@ -139,7 +139,7 @@ class ScriptRegistry extends EventHandler {
/**
* Remove {@link ScriptType}.
*
* @param {string|Class<import('./script-type.js').ScriptType>} nameOrType - The name or type
* @param {string|typeof import('./script-type.js').ScriptType} nameOrType - The name or type
* of {@link ScriptType}.
* @returns {boolean} True if removed or False if already not in registry.
* @example
Expand Down Expand Up @@ -172,7 +172,7 @@ class ScriptRegistry extends EventHandler {
* Get {@link ScriptType} by name.
*
* @param {string} name - Name of a {@link ScriptType}.
* @returns {Class<import('./script-type.js').ScriptType>} The Script Type if it exists in the
* @returns {typeof import('./script-type.js').ScriptType} The Script Type if it exists in the
* registry or null otherwise.
* @example
* var PlayerController = app.scripts.get('playerController');
Expand All @@ -184,7 +184,7 @@ class ScriptRegistry extends EventHandler {
/**
* Check if a {@link ScriptType} with the specified name is in the registry.
*
* @param {string|Class<import('./script-type.js').ScriptType>} nameOrType - The name or type
* @param {string|typeof import('./script-type.js').ScriptType} nameOrType - The name or type
* of {@link ScriptType}.
* @returns {boolean} True if {@link ScriptType} is in registry.
* @example
Expand All @@ -205,7 +205,7 @@ class ScriptRegistry extends EventHandler {
/**
* Get list of all {@link ScriptType}s from registry.
*
* @returns {Array<Class<import('./script-type.js').ScriptType>>} list of all {@link ScriptType}s
* @returns {Array<typeof import('./script-type.js').ScriptType>} list of all {@link ScriptType}s
* in registry.
* @example
* // logs array of all Script Type names available in registry
Expand Down
4 changes: 2 additions & 2 deletions src/framework/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getReservedScriptNames() {
* @param {AppBase} [app] - Optional application handler, to choose which {@link ScriptRegistry}
* to add a script to. By default it will use `Application.getApplication()` to get current
* {@link AppBase}.
* @returns {Class<ScriptType>|null} A class type (constructor function) that inherits {@link ScriptType},
* @returns {typeof ScriptType|null} A class type (constructor function) that inherits {@link ScriptType},
* which the developer is meant to further extend by adding attributes and prototype methods.
* Returns null if there was an error.
* @example
Expand Down Expand Up @@ -92,7 +92,7 @@ createScript.reservedAttributes = reservedAttributes;
* Register a existing class type as a Script Type to {@link ScriptRegistry}. Useful when defining
* a ES6 script class that extends {@link ScriptType} (see example).
*
* @param {Class<ScriptType>} script - The existing class type (constructor function) to be
* @param {typeof ScriptType} script - The existing class type (constructor function) to be
* registered as a Script Type. Class must extend {@link ScriptType} (see example). Please note: A
* class created using {@link createScript} is auto-registered, and should therefore not be pass
* into {@link registerScript} (which would result in swapping out all related script instances).
Expand Down
30 changes: 0 additions & 30 deletions utils/types-fixup.mjs
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
import fs from 'fs';

// Create a regex that matches any string starting with Class< and ending with >
const regex = /Class<(.*?)>/g;
const paths = [
'./types/framework/components/script/component.d.ts',
'./types/framework/entity.d.ts',
'./types/framework/script/script-attributes.d.ts',
'./types/framework/script/script-registry.d.ts',
'./types/framework/script/script.d.ts'
];

paths.forEach((path, index) => {
let dts = fs.readFileSync(path, 'utf8');
dts = dts.replace(regex, 'typeof ScriptType');
// The .d.ts files don't know what a ScriptType is, so import it
if (index === 0) {
dts += `
import { ScriptType } from '../../script/script-type.js';
`;
} else if (index === 1) {
dts += `
import { ScriptType } from './script/script-type.js';
`;
} else {
dts += `
import { ScriptType } from './script-type.js';
`;
}
fs.writeFileSync(path, dts);
});

// Fix up description parameter for VertexFormat constructor because tsc
// doesn't recognize it as an array
let path = './types/platform/graphics/vertex-format.d.ts';
Expand Down

0 comments on commit dcd2634

Please sign in to comment.