refactor(scene): expose scene services via the lib interface, drop browser globals#743
Closed
bofeng-song wants to merge 1 commit into
Closed
refactor(scene): expose scene services via the lib interface, drop browser globals#743bofeng-song wants to merge 1 commit into
bofeng-song wants to merge 1 commit into
Conversation
…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.
Contributor
Author
|
Consolidated into #745 (single optimization PR). Closing this one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thesrc/liblayer.src/lib/cli.tsonly re-exported types and had no importers.window.cli.SceneEventswas also wired tomessageManager(an isolated event emitter), while service events such aseditor:openare emitted onServiceEvents(aGlobalEventManager). Listeners registered on the former never fired.Changes
ServiceManager — single typed entry
init(serverURL),getServices(): IServiceManagerandgetServiceEvents(): GlobalEventManager.getServiceEvents()returnsServiceEvents, where service events are actually emitted (this also fixes the deadeditor:openlistener).Public lib interface
src/lib/cli.ts.Services(IServiceManager),IPublicServiceManagerandGlobalEventManagerfromsrc/lib/scene/scene.ts, so external IDEs useScene.Services/Scene.GlobalEventManager.Drop browser globals; pass the address through the boot interface
engine-bootstrap.ts: remove theglobalThis.cliassignment;startup()callsserviceManager.init(serverURL).scene-editor-boot.js/game-boot.js:boot({ ip, port, https })composes the server URL (falls back tolocation.origin) and returns a context{ services, events, serverURL }fromserviceManager.getServices()/getServiceEvents().engine-loader.js: takesserverURLas an argument (new exportedcomposeServerURL) instead of readingwindow.WebEnv;editor-stub-preload.jsbecomesinitEditorStub(serverURL).load-scene.js/preview-app.js: consume the boot context (services/events) instead ofwindow.cli.*.scene-editor.ejs/preview.ejs/game.ejs: callboot({ ip, port, https }); the scene editor page uses the returned context viawindow.SceneCtx. Removedwindow.WebEnv.scene.scripting.middleware.ts/game-preview.middleware.ts: injectip/port/httpsinto the templates instead of a combinedserverURL.main.ts(native scene worker): useserviceManager.init(...); the server URL still arrives via the--serverURLfork argument.The engine-required
window.CC_EDITOR/window.CC_PREVIEWflags andglobalThis.cce.Script(used by the engine) are intentionally kept.Test
npx tsc -bpasses.npm run build:static-webrebuilds the scene bundle: noglobalThis.cli, andgetServices/getServiceEventsare present./scene-editor/), resource preview (/preview) and game preview (/) load;editor:opennow fires; nowindow.cli/window.WebEnvremain in the served static assets.