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

ESM script base class #6367

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

ESM script base class #6367

wants to merge 18 commits into from

Conversation

marklundin
Copy link
Member

@marklundin marklundin commented May 14, 2024

New Script Base class

This PR adds a new base class Script to replace the current ScriptType class which is incompatible with ESM class syntax.

The Script class extracts a small subset of features from ScriptType and keeps anything related to ScriptAttributes in ScriptType. ScriptType now inherits from Script. This update minimizes the code changes required in the component system.

Files Changes

script.js renamed to script-create.js
script.js contains Script base class
script-type.js contains legacy ScriptType class

API Changes

ScriptAttributes.rawToValue is now exposed as a utility. This will also be used by the launcher page
ScriptRegistry.addSchema/getSchema - This stores a map of the the attribute schema <scriptName, schema>

Tests

Passes the ScriptType tests innpm run test:karma

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

Copy link

vercel bot commented May 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
engine ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 21, 2024 4:05pm

@Maksims
Copy link
Contributor

Maksims commented May 14, 2024

What would be the impact to users in terms of migrations or API changes?

@marklundin
Copy link
Member Author

What would be the impact to users in terms of migrations or API changes?

pc.createScript() will still work and return a ScriptType which should function in the same way. ESM users will use Script as a base class without any ScriptAttribute functionality (events, rawToValue deep copying)

script.off('enable', onFirstEnabled);
}
};
script.on('enable', onFirstEnabled);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also use script.once?

Comment on lines +154 to +157
script[name] = [];
for (let i = 0, len = value.length; i < len; i++) {
script[name].push(rawToValue(app, args, value[i], old ? old[i] : null));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to construct and populate the array first then assign it to the script attribute?

this could be useful for users that have implemented a setter, for example.

Comment on lines +22 to +24
function getReservedScriptNames() {
return reservedScriptNames;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering why not just export reservedScriptNames?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking of which - the Editor UI should not allow creating a script with a reserved name, e.g. "script".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I should create a separate issue in Editor repo.

function getReservedScriptNames() {
return reservedScriptNames;
}
export class Script extends EventHandler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth adding some documentation for this class explaining how it fits into the broader scripting context?

ScriptType.prototype.initScriptType.call(this, args);
};

scriptType.prototype = Object.create(ScriptType.prototype);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the purpose/need of this custom class/initialisation code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is legacy code, and it's not clear why ScriptType are initialized this way. This was before ES classes and super syntax, so it's likely this is trying to polyfill some functionality. I don't really want to change how this works or modify anything related to ScriptType as we will eventually deprecate this

* };
* @category Script
*/
function createScript(name, app) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand naming is probably a bit tricky given the existing codebase, but I feel like the name createScript is misleading and would more accurately be called createScriptType or similar.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is public API var Rotator = pc.createScript('Rotator') and can't be changed without updating everyones projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

4 participants