|
| 1 | +import Signal from "@rbxts/lemon-signal"; |
| 2 | +import { LoadVirtualModule } from "./Utils"; |
| 3 | + |
| 4 | +const HttpService = game.GetService("HttpService"); |
| 5 | + |
| 6 | +type Dependencies = Map<ModuleScript, { Result: unknown }>; |
| 7 | +type DependencyLoaders = Map<ModuleScript, Promise<unknown>>; |
| 8 | +type Listeners = Map<ModuleScript, RBXScriptConnection>; |
| 9 | + |
| 10 | +export class Environment { |
| 11 | + private _ActiveConnections = true; |
| 12 | + private _Dependencies: Dependencies = new Map(); |
| 13 | + private _DependencyLoaders: DependencyLoaders = new Map(); |
| 14 | + private _Listeners: Listeners = new Map(); |
| 15 | + |
| 16 | + readonly EnvironmentUID: string; |
| 17 | + private _GlobalInjection?: Record<keyof any, unknown>; |
| 18 | + |
| 19 | + readonly Shared: {} = {}; |
| 20 | + OnDependencyChanged = new Signal<[module: ModuleScript]>(); |
| 21 | + private _DestroyedHooked?: () => void; |
| 22 | + |
| 23 | + constructor() { |
| 24 | + const uid = HttpService.GenerateGUID(false); |
| 25 | + this.EnvironmentUID = uid; |
| 26 | + } |
| 27 | + EnableGlobalInjection() { |
| 28 | + if (!this._GlobalInjection) { |
| 29 | + this._GlobalInjection = {}; |
| 30 | + } |
| 31 | + } |
| 32 | + InjectGlobal(key: keyof any, value: unknown) { |
| 33 | + this.EnableGlobalInjection(); |
| 34 | + this._GlobalInjection![key] = value; |
| 35 | + } |
| 36 | + GetGlobalInjection() { |
| 37 | + return this._GlobalInjection; |
| 38 | + } |
| 39 | + |
| 40 | + private _RegistryDependency(module: ModuleScript, result?: any) { |
| 41 | + this._Dependencies.set(module, { Result: result }); |
| 42 | + } |
| 43 | + |
| 44 | + IsDependency(module: ModuleScript) { |
| 45 | + return this._Dependencies.has(module); |
| 46 | + } |
| 47 | + GetDependencyResult<T = unknown>(module: ModuleScript): T | undefined { |
| 48 | + return this._Dependencies.get(module)?.Result as T; |
| 49 | + } |
| 50 | + |
| 51 | + ListenDependency(module: ModuleScript) { |
| 52 | + if (!this._ActiveConnections) return; |
| 53 | + |
| 54 | + const listener = module.GetPropertyChangedSignal("Source").Connect(() => { |
| 55 | + if (!this._ActiveConnections) return; |
| 56 | + this.OnDependencyChanged.Fire(module); |
| 57 | + }); |
| 58 | + this._Listeners.set(module, listener); |
| 59 | + } |
| 60 | + |
| 61 | + LoadDependency<T = unknown>(dependency: ModuleScript): Promise<T> { |
| 62 | + const cached = this.GetDependencyResult(dependency); |
| 63 | + if (cached !== undefined) { |
| 64 | + return Promise.resolve(cached as T); |
| 65 | + } |
| 66 | + const cachedLoader = this._DependencyLoaders.get(dependency) as Promise<T>; |
| 67 | + if (cachedLoader) { |
| 68 | + return cachedLoader.tap(() => {}); |
| 69 | + } |
| 70 | + |
| 71 | + this.ListenDependency(dependency); |
| 72 | + |
| 73 | + const promise = LoadVirtualModule(dependency, this).tap((result) => { |
| 74 | + this._RegistryDependency(dependency, result); |
| 75 | + }); |
| 76 | + this._DependencyLoaders.set(dependency, promise); |
| 77 | + |
| 78 | + return promise as Promise<T>; |
| 79 | + } |
| 80 | + |
| 81 | + HookOnDestroyed(callback: () => void) { |
| 82 | + this._DestroyedHooked = callback; |
| 83 | + } |
| 84 | + |
| 85 | + Destroy() { |
| 86 | + if (this._DestroyedHooked) { |
| 87 | + this._DestroyedHooked(); |
| 88 | + } |
| 89 | + |
| 90 | + this._ActiveConnections = false; |
| 91 | + this._Listeners.forEach((connection) => { |
| 92 | + connection.Disconnect(); |
| 93 | + }); |
| 94 | + this.OnDependencyChanged.Destroy(); |
| 95 | + } |
| 96 | +} |
0 commit comments