Skip to content

Comments

refactor: opt plugin system lifetime func name and notes#1303

Merged
yiiqii merged 4 commits intofeat/2.8from
refactor/plugin-lifetime-func
Dec 8, 2025
Merged

refactor: opt plugin system lifetime func name and notes#1303
yiiqii merged 4 commits intofeat/2.8from
refactor/plugin-lifetime-func

Conversation

@wumaolinmaoan
Copy link
Contributor

@wumaolinmaoan wumaolinmaoan commented Dec 5, 2025

Summary by CodeRabbit

  • Refactor

    • Standardized plugin API: base plugin renamed and lifecycle hooks consolidated/renamed for consistency.
    • Asset loading flow updated to invoke the revised scene-load start and finish hooks.
    • Plugin registration behavior adjusted to align with the new API.
  • Tests

    • Unit tests updated to use the new plugin types and lifecycle hook names.
  • Chores

    • Demo content URL updated.

✏️ Tip: You can customize this high-level summary in your review settings.

@wumaolinmaoan wumaolinmaoan requested a review from yiiqii December 5, 2025 07:48
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 5, 2025

Walkthrough

Rename and API changes across the plugin system: AbstractPluginPlugin, removal of initialize(), and lifecycle hook renames (processAssetsonAssetsLoadStart, loadResources/prepareResourceonAssetsLoadFinish, onCompositionConstructedonCompositionCreated, onCompositionDestroyedonCompositionDestroy). Call sites, plugin implementations, tests, and asset/scene integration updated.

Changes

Cohort / File(s) Summary
Core Plugin Types & System
packages/effects-core/src/plugins/plugin.ts, packages/effects-core/src/plugin-system.ts
Rename AbstractPluginPlugin; remove initialize(); change PluginConstructor type; rename lifecycle methods: processAssetsonAssetsLoadStart, prepareResource/loadResourcesonAssetsLoadFinish, onCompositionConstructedonCompositionCreated, onCompositionDestroyedonCompositionDestroy; update PluginSystem to use Plugin and new method names.
Asset & Scene Integration
packages/effects-core/src/asset-manager.ts, packages/effects-core/src/scene-loader.ts, packages/effects-threejs/src/three-display-object.ts
Replace plugin asset-phase hook labels and handlers: plugin:processAssetsplugin:onAssetsLoadStart; handler processPluginAssetsonPluginSceneLoadStart; call sites updated to invoke PluginSystem.onAssetsLoadStart and PluginSystem.onAssetsLoadFinish where applicable.
Core Plugin Loaders
packages/effects-core/src/plugins/*
(e.g., calculate-loader.ts, camera-vfx-item-loader.ts, interact-loader.ts, particle-loader.ts, sprite-loader.ts, text-loader.ts)
Switch imports and base class to Plugin (was AbstractPlugin) for all loader plugins; no other logic changes shown.
Framework / Third‑party Plugins
plugin-packages/* (examples: alipay-downgrade/src/alipay-downgrade-plugin.ts, downgrade/src/downgrade-plugin.ts, editor-gizmo/src/gizmo-loader.ts, ffd/src/ffd-loader.ts, model/src/plugin/model-plugin.ts, multimedia/*, orientation-transformer/*, rich-text/*, spine/*)
Update imports and extend Plugin; rename lifecycle methods per core changes (processAssetsonAssetsLoadStart, prepareResourceonAssetsLoadFinish, composition hooks renamed); some plugins add constructors calling super() and set order; remove initialize() overrides where present.
Tests & Fixtures
plugin-packages/*/test/*, web-packages/test/unit/src/effects-core/composition/*
Tests updated to import/extend Plugin and to use renamed hooks (onAssetsLoadStart, onAssetsLoadFinish, onCompositionCreated, etc.); assertions updated where hook names appear in error messages.
Consumers / Demo
web-packages/demo/src/inspire/threejs.ts, web-packages/imgui-demo/src/ge.ts
Update registration/import to use Plugin (orientation-transformer registration). ge.ts changes a demo URL (unrelated to plugin API).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Focus review on:
    • packages/effects-core/src/plugins/plugin.ts and packages/effects-core/src/plugin-system.ts for consistent signatures and removed initialize() semantics.
    • packages/effects-core/src/asset-manager.ts, scene-loader.ts, and three-display-object.ts to ensure correct hook labels and timing.
    • Plugins that added constructors or removed initialize() to verify ordering/side-effects.
    • Tests that updated expected error messages to confirm accuracy.

Possibly related PRs

  • refactor: plugin creation logic #1280 — Modifies plugin lifecycle and plugin-system/asset loading (same areas: plugin-system.ts, plugin.ts, asset-manager.ts, scene-loader.ts); likely directly related to the lifecycle rename work.

Suggested reviewers

  • yiiqii

Poem

"🐰 I hopped through code and changed a name,
Abstract became Plugin — a tidy game,
Hooks hopped over to start and to end,
Tests adjusted, all neatly penned,
A carrot for CI — hop, merge, acclaim!"

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: refactoring and optimizing plugin system lifetime/lifecycle function names and their documentation.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/plugin-lifetime-func

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (8)
web-packages/test/unit/src/effects-core/composition/load-tex.spec.ts (1)

112-112: Consider adding override keyword consistently.

Line 51 uses the override keyword, but the lifecycle hook overrides on these lines do not. For consistency and better type safety, consider adding override to all lifecycle hook method declarations.

Apply this pattern consistently:

-onAssetsLoadFinish (scene, options) {
+override onAssetsLoadFinish (scene, options) {
-onCompositionCreated (composition, scene) {
+override onCompositionCreated (composition, scene) {

Also applies to: 179-179, 267-267, 273-273, 322-322, 330-330

web-packages/imgui-demo/src/ge.ts (1)

19-19: Consider removing commented-out code.

The commented URL is no longer in use and can be removed to keep the codebase clean.

Apply this diff:

-    // const json = 'https://mdn.alipayobjects.com/mars/afts/file/A*oF1NRJG7GU4AAAAAAAAAAAAADlB4AQ'; // 春促\
packages/effects-core/src/plugin-system.ts (3)

2-42: Plugin typing change looks correct; consider stricter duplicate-registration handling

Switching to Plugin / PluginConstructor and typing plugins as Plugin[] is consistent with the new API; registration, sorting by order, and unregisterPlugin behaviour are otherwise unchanged and look fine.

One behavioural edge case remains: if registerPlugin is called twice with the same name, you log an error but still:

  • Overwrite pluginLoaderMap[name] with the new class, and
  • Push another instance into plugins.

Later, unregisterPlugin deletes pluginLoaderMap[name] and only removes the first plugin with that name, leaving a second instance in plugins that will continue to receive lifecycle callbacks but is no longer in pluginLoaderMap.

If duplicate registration is considered invalid (which the log message suggests), consider:

  • Returning early after logging and not adding the second plugin instance, or
  • Removing all plugins with that name in unregisterPlugin to avoid stale instances.

This isn’t introduced by this refactor, but it’s a good place to tighten the behaviour.


46-48: getPlugins exposes internal mutable array; consider ReadonlyArray or a copy

Returning the backing plugins array directly allows external callers to mutate it (e.g., push, splice, resort), which can silently break ordering and lifecycle dispatch.

If you want to keep invariants local to this module, consider:

  • Changing the signature to static getPlugins(): ReadonlyArray<Plugin> and
  • Either returning plugins typed as ReadonlyArray<Plugin> or return plugins.slice() to hand out a shallow copy.

This keeps the public API compatible from a read perspective while preventing accidental external mutation.


64-66: Clarify whether onSceneLoadFinish should also await plugin hooks and align options signature

onSceneLoadFinish currently:

static onSceneLoadFinish (scene: Scene, options: SceneLoadOptions, engine: Engine) {
  plugins.forEach(loader => loader.onAssetsLoadFinish(scene, options, engine));
}

Two points to double‑check:

  1. Async semantics
    If any Plugin.onAssetsLoadFinish implementation performs async work and returns a Promise, this method will currently fire them and ignore their completion. If callers need to wait for “finish” hooks (symmetrically to onSceneLoadStart), it may be safer to aggregate:
static async onSceneLoadFinish (scene: Scene, options: SceneLoadOptions, engine: Engine) {
  await Promise.all(plugins.map(loader => loader.onAssetsLoadFinish(scene, options, engine)));
}

or at least document that onAssetsLoadFinish hooks are fire‑and‑forget and must not be awaited.

  1. options optionality consistency
    onSceneLoadStart takes options?: SceneLoadOptions, whereas onSceneLoadFinish requires options: SceneLoadOptions. If both are always called with options, this is fine, but if the start path allows undefined, consider either making both optional or both required for API symmetry.

Please verify intent for both points against plugin implementations and call sites.

plugin-packages/downgrade/src/downgrade-plugin.ts (1)

2-36: Align comment with actual lifecycle hook name

The doc still says “onSceneLoadStart”, but this plugin now overrides onAssetsLoadStart. For clarity and to avoid confusion with any PluginSystem-level naming, consider updating the comment to reference onAssetsLoadStart (or explicitly say “scene load start phase”) to match the code.

plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1)

2-35: Keep lifecycle wording consistent with method name

Here too the comment refers to “onSceneLoadStart” while the implemented hook is onAssetsLoadStart. To reduce ambiguity for future readers, it’s worth updating the wording to match the method name (or clearly distinguish PluginSystem event vs. plugin hook if both exist).

web-packages/test/unit/src/effects-core/composition/plugin.spec.ts (1)

29-41: Consider matching onAssetsLoadFinish’s full signature for clarity

Plugin.onAssetsLoadFinish is declared as (scene, options, engine), but the test override only takes (scene, options). This works at runtime (the third arg is simply ignored), but mirroring the full signature—e.g. onAssetsLoadFinish(scene, options, _engine)—would make the intent explicit and keep the test resilient to future type changes.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 772e320 and f0a88c5.

📒 Files selected for processing (10)
  • packages/effects-core/src/plugin-system.ts (2 hunks)
  • packages/effects-core/src/plugins/plugin.ts (2 hunks)
  • plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1 hunks)
  • plugin-packages/downgrade/src/downgrade-plugin.ts (1 hunks)
  • plugin-packages/model/src/plugin/model-plugin.ts (4 hunks)
  • plugin-packages/multimedia/src/audio/audio-loader.ts (1 hunks)
  • plugin-packages/multimedia/src/video/video-loader.ts (1 hunks)
  • web-packages/imgui-demo/src/ge.ts (1 hunks)
  • web-packages/test/unit/src/effects-core/composition/load-tex.spec.ts (6 hunks)
  • web-packages/test/unit/src/effects-core/composition/plugin.spec.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • plugin-packages/multimedia/src/video/video-loader.ts
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2024-10-18T09:15:08.038Z
Learnt from: wumaolinmaoan
Repo: galacean/effects-runtime PR: 691
File: packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts:39-42
Timestamp: 2024-10-18T09:15:08.038Z
Learning: In `packages/effects-core/src/plugins/timeline/playables/color-property-mixer-playable.ts`, when accumulating color components, avoid using `clone()` methods because it can cause additional garbage collection overhead.

Applied to files:

  • packages/effects-core/src/plugins/plugin.ts
  • plugin-packages/model/src/plugin/model-plugin.ts
  • packages/effects-core/src/plugin-system.ts
📚 Learning: 2024-07-26T21:12:19.313Z
Learnt from: liuxi150
Repo: galacean/effects-runtime PR: 0
File: :0-0
Timestamp: 2024-07-26T21:12:19.313Z
Learning: Imports in `plugin-packages/model/src/utility/plugin-helper.ts` do not need to be merged as per project preference or coding standards.

Applied to files:

  • plugin-packages/model/src/plugin/model-plugin.ts
  • plugin-packages/downgrade/src/downgrade-plugin.ts
  • web-packages/test/unit/src/effects-core/composition/plugin.spec.ts
  • packages/effects-core/src/plugin-system.ts
🧬 Code graph analysis (4)
plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1)
packages/effects-core/src/scene.ts (2)
  • Scene (12-35)
  • SceneLoadOptions (62-140)
web-packages/test/unit/src/effects-core/composition/load-tex.spec.ts (3)
packages/effects-core/src/plugin-system.ts (1)
  • registerPlugin (18-31)
packages/effects-core/src/scene.ts (1)
  • Scene (12-35)
packages/effects-core/src/gl/index.ts (1)
  • glContext (8-8)
plugin-packages/downgrade/src/downgrade-plugin.ts (1)
packages/effects-core/src/scene.ts (2)
  • Scene (12-35)
  • SceneLoadOptions (62-140)
web-packages/test/unit/src/effects-core/composition/plugin.spec.ts (2)
packages/effects-core/src/vfx-item.ts (2)
  • composition (184-186)
  • composition (191-199)
packages/effects-core/src/composition.ts (1)
  • Composition (132-990)
🔇 Additional comments (9)
web-packages/test/unit/src/effects-core/composition/load-tex.spec.ts (3)

3-3: LGTM! Import updated correctly.

The import statement has been properly updated to use Plugin instead of AbstractPlugin, aligning with the plugin system refactoring.


50-61: LGTM! Test plugin updated correctly.

The test plugin has been properly refactored to extend Plugin and use the onAssetsLoadFinish lifecycle hook. The override keyword is correctly used for type safety.


111-124: LGTM! All test plugins updated consistently.

All test plugins have been correctly refactored to:

  • Extend Plugin instead of AbstractPlugin
  • Use onAssetsLoadFinish instead of prepareResource
  • Use onCompositionCreated instead of onCompositionConstructed

The test logic remains unchanged, ensuring the refactoring maintains existing test coverage and behavior.

Also applies to: 178-193, 266-277, 321-334

web-packages/imgui-demo/src/ge.ts (1)

25-25: The URL is accessible and returns valid content. This appears to be an intentional demo asset swap (the previous URL is commented on line 19), appropriate for a demo/test file. No concerns.

packages/effects-core/src/plugin-system.ts (2)

50-56: Composition lifecycle rename/dispatch looks consistent

The renaming to onCompositionCreated / onCompositionDestroy and the corresponding dispatch in initializeComposition / destroyComposition is straightforward and matches the new Plugin lifecycle naming. No issues from a control-flow or typing standpoint.


58-62: onSceneLoadStart async aggregation is sound

Using async onSceneLoadStart with return Promise.all(plugins.map(plugin => plugin.onAssetsLoadStart(...))) correctly aggregates all plugin onAssetsLoadStart hooks, propagating both rejections and synchronous throws through the returned promise. This keeps the call site able to await all start hooks. Looks good.

packages/effects-core/src/plugins/plugin.ts (1)

5-47: Core Plugin API migration looks consistent

The renaming from AbstractPlugin to Plugin plus the new lifecycle hooks (onAssetsLoadStart, onAssetsLoadFinish, onCompositionCreated, onCompositionDestroy) is internally consistent and matches the usage in the updated plugins and tests. The PluginConstructor type and the default name = 'Plugin' also look reasonable; just keep in mind that logs or tooling relying on an empty default name will now see 'Plugin' when subclasses don’t override it.

plugin-packages/multimedia/src/audio/audio-loader.ts (1)

2-22: AudioLoader migration to Plugin and onAssetsLoadStart looks good

The switch to Plugin and renaming processAssetsonAssetsLoadStart keeps the original loading logic intact and aligns with the new lifecycle API. No issues from a functional standpoint.

plugin-packages/model/src/plugin/model-plugin.ts (1)

17-100: ModelPlugin lifecycle rename and base-class switch look correct

The move to Plugin plus the renames to onAssetsLoadStart, onAssetsLoadFinish, and onCompositionCreated keep the original 3D-model behavior intact (static resource preload, scene preprocessing, shader setup, and injection of ModelPluginComponent). The overrides and their parameter types line up with the new Plugin API.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1)

14-35: Consider extracting common downgrade logic.

AlipayDowngradePlugin and DowngradePlugin share nearly identical implementations. The only difference is the class name. Consider extracting the common downgrade checking and renderLevel assignment logic into a shared base class or utility function to reduce duplication.

Example approach:

// Create a shared base class
abstract class BaseDowngradePlugin extends Plugin {
  constructor() {
    super();
    this.order = 1;
  }

  override async onAssetsLoadStart(scene: Scene, options?: SceneLoadOptions): Promise<void> {
    // Existing logic here
  }
}

// Then extend it
export class AlipayDowngradePlugin extends BaseDowngradePlugin {}
export class DowngradePlugin extends BaseDowngradePlugin {}
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f0a88c5 and c82f9af.

📒 Files selected for processing (8)
  • packages/effects-core/src/asset-manager.ts (2 hunks)
  • packages/effects-core/src/plugin-system.ts (2 hunks)
  • packages/effects-core/src/scene-loader.ts (1 hunks)
  • packages/effects-threejs/src/three-display-object.ts (1 hunks)
  • plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1 hunks)
  • plugin-packages/alipay-downgrade/test/src/player.spec.ts (2 hunks)
  • plugin-packages/downgrade/src/downgrade-plugin.ts (1 hunks)
  • plugin-packages/downgrade/test/src/player.spec.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (6)
  • plugin-packages/alipay-downgrade/test/src/player.spec.ts
  • packages/effects-core/src/scene-loader.ts
  • packages/effects-threejs/src/three-display-object.ts
  • plugin-packages/downgrade/test/src/player.spec.ts
  • packages/effects-core/src/asset-manager.ts
  • packages/effects-core/src/plugin-system.ts
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2024-07-26T21:12:19.313Z
Learnt from: liuxi150
Repo: galacean/effects-runtime PR: 0
File: :0-0
Timestamp: 2024-07-26T21:12:19.313Z
Learning: Imports in `plugin-packages/model/src/utility/plugin-helper.ts` do not need to be merged as per project preference or coding standards.

Applied to files:

  • plugin-packages/downgrade/src/downgrade-plugin.ts
  • plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts
🧬 Code graph analysis (1)
plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1)
packages/effects-core/src/scene.ts (2)
  • Scene (12-35)
  • SceneLoadOptions (62-140)
🔇 Additional comments (4)
plugin-packages/downgrade/src/downgrade-plugin.ts (3)

2-2: LGTM: Base class import updated.

The import change from AbstractPlugin to Plugin aligns with the plugin system refactoring.


16-19: LGTM: Explicit constructor with execution order.

The constructor explicitly sets the execution order, making it clear this plugin runs early in the lifecycle.


9-9: LGTM: Lifecycle hook renamed with updated documentation.

The rename from processAssets to onAssetsLoadStart is more descriptive and aligns with the new plugin lifecycle naming. The comment has been updated accordingly.

Also applies to: 21-21

plugin-packages/alipay-downgrade/src/alipay-downgrade-plugin.ts (1)

2-2: LGTM: Plugin system refactoring applied consistently.

The changes mirror those in downgrade-plugin.ts: base class updated to Plugin, explicit constructor added, and lifecycle hook renamed to onAssetsLoadStart.

Also applies to: 14-18, 20-20

@yiiqii yiiqii merged commit f617ad3 into feat/2.8 Dec 8, 2025
2 checks passed
@yiiqii yiiqii deleted the refactor/plugin-lifetime-func branch December 8, 2025 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants