Skip to content

refactor(scene): expose scene services via the lib interface, drop browser globals#743

Closed
bofeng-song wants to merge 1 commit into
cocos:mainfrom
bofeng-song:refactor-scene-service-interface
Closed

refactor(scene): expose scene services via the lib interface, drop browser globals#743
bofeng-song wants to merge 1 commit into
cocos:mainfrom
bofeng-song:refactor-scene-service-interface

Conversation

@bofeng-song

Copy link
Copy Markdown
Contributor

Problem

The scene editor and preview pages reached the scene services through ad-hoc browser globals (window.cli.Scene / window.cli.SceneEvents) and read their server address from another global (window.WebEnv). This is not a real interface, leaks globals, and cannot be shared with external IDEs that consume the src/lib layer. src/lib/cli.ts only re-exported types and had no importers.

window.cli.SceneEvents was also wired to messageManager (an isolated event emitter), while service events such as editor:open are emitted on ServiceEvents (a GlobalEventManager). Listeners registered on the former never fired.

Changes

ServiceManager — single typed entry

  • Add init(serverURL), getServices(): IServiceManager and getServiceEvents(): GlobalEventManager. getServiceEvents() returns ServiceEvents, where service events are actually emitted (this also fixes the dead editor:open listener).

Public lib interface

  • Delete the unused src/lib/cli.ts.
  • Re-export Services (IServiceManager), IPublicServiceManager and GlobalEventManager from src/lib/scene/scene.ts, so external IDEs use Scene.Services / Scene.GlobalEventManager.

Drop browser globals; pass the address through the boot interface

  • engine-bootstrap.ts: remove the globalThis.cli assignment; startup() calls serviceManager.init(serverURL).
  • scene-editor-boot.js / game-boot.js: boot({ ip, port, https }) composes the server URL (falls back to location.origin) and returns a context { services, events, serverURL } from serviceManager.getServices() / getServiceEvents().
  • engine-loader.js: takes serverURL as an argument (new exported composeServerURL) instead of reading window.WebEnv; editor-stub-preload.js becomes initEditorStub(serverURL).
  • load-scene.js / preview-app.js: consume the boot context (services / events) instead of window.cli.*.
  • scene-editor.ejs / preview.ejs / game.ejs: call boot({ ip, port, https }); the scene editor page uses the returned context via window.SceneCtx. Removed window.WebEnv.
  • scene.scripting.middleware.ts / game-preview.middleware.ts: inject ip / port / https into the templates instead of a combined serverURL.
  • main.ts (native scene worker): use serviceManager.init(...); the server URL still arrives via the --serverURL fork argument.

The engine-required window.CC_EDITOR / window.CC_PREVIEW flags and globalThis.cce.Script (used by the engine) are intentionally kept.

Test

  • npx tsc -b passes.
  • npm run build:static-web rebuilds the scene bundle: no globalThis.cli, and getServices / getServiceEvents are present.
  • Scene editor (/scene-editor/), resource preview (/preview) and game preview (/) load; editor:open now fires; no window.cli / window.WebEnv remain in the served static assets.

…owser globals

Scene editor and preview pages reached the scene services through ad-hoc browser
globals (window.cli.Scene / window.cli.SceneEvents) and read the server address
from another global (window.WebEnv). That is not a real interface, leaks globals,
and cannot be shared with external IDEs that consume the src/lib layer.
src/lib/cli.ts only re-exported types and had no importers.

window.cli.SceneEvents was also wired to messageManager (an isolated emitter),
while service events such as editor:open are emitted on ServiceEvents
(a GlobalEventManager); listeners on the former never fired.

ServiceManager (single typed entry):
- Add init(serverURL), getServices(): IServiceManager and
  getServiceEvents(): GlobalEventManager. getServiceEvents() returns ServiceEvents,
  where service events are actually emitted (fixes the dead editor:open listener).

Public lib interface:
- Delete the unused src/lib/cli.ts.
- Re-export Services (IServiceManager), IPublicServiceManager and
  GlobalEventManager from src/lib/scene/scene.ts.

Drop browser globals, pass the address through the boot interface:
- engine-bootstrap.ts: remove the globalThis.cli assignment; startup() calls
  serviceManager.init(serverURL).
- scene-editor-boot.js / game-boot.js: boot({ ip, port, https }) composes the
  server URL (falls back to location.origin) and returns { services, events,
  serverURL } from serviceManager.getServices() / getServiceEvents().
- engine-loader.js: takes serverURL as an argument (new composeServerURL) instead
  of reading window.WebEnv; editor-stub-preload.js becomes initEditorStub(serverURL).
- load-scene.js / preview-app.js: consume the boot context instead of window.cli.
- scene-editor.ejs / preview.ejs / game.ejs: call boot({ ip, port, https });
  the scene editor page uses the returned context via window.SceneCtx. Removed
  window.WebEnv.
- scene.scripting.middleware.ts / game-preview.middleware.ts: inject ip / port /
  https into the templates instead of a combined serverURL.
- main.ts (native scene worker): use serviceManager.init(...); the server URL still
  arrives via the --serverURL fork argument.

The engine-required window.CC_EDITOR / window.CC_PREVIEW flags and
globalThis.cce.Script are intentionally kept.
@bofeng-song

Copy link
Copy Markdown
Contributor Author

Consolidated into #745 (single optimization PR). Closing this one.

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.

1 participant