Skip to content

Commit 799f37f

Browse files
authored
refactor: standardize globalThis access with centralized utility module (#1299)
1 parent 1897857 commit 799f37f

File tree

17 files changed

+68
-46
lines changed

17 files changed

+68
-46
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Internal utilities for standardized globalThis access.
3+
*/
4+
5+
/**
6+
* Type-safe globalThis property access.
7+
* @internal
8+
*/
9+
export function getGlobal<T>(key: string): T | undefined {
10+
return (globalThis as any)[key]
11+
}
12+
13+
/**
14+
* Sets a globalThis property as a polyfill (only if undefined/null).
15+
* @internal
16+
*/
17+
export function setGlobalPolyfill<T>(key: string, value: T): void {
18+
;(globalThis as any)[key] = (globalThis as any)[key] ?? value
19+
}

packages/@dcl/ecs/src/runtime/initialization/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { TweenSystem, createTweenSystem } from '../../systems/tween'
1313
import { pointerEventColliderChecker } from '../../systems/pointer-event-collider-checker'
1414
import { createTriggerAreaEventsSystem, TriggerAreaEventsSystem } from '../../systems/triggerArea'
1515
import { createTimers, Timers } from '../helpers/timers'
16+
import { setGlobalPolyfill } from '../globals'
1617

1718
/**
1819
* @public
@@ -80,10 +81,10 @@ export { TriggerAreaEventsSystem }
8081
*/
8182
export const timers: Timers = /* @__PURE__ */ createTimers(engine)
8283
export { Timers, createTimers }
83-
;(globalThis as any).setTimeout = (globalThis as any).setTimeout ?? timers.setTimeout
84-
;(globalThis as any).clearTimeout = (globalThis as any).clearTimeout ?? timers.clearTimeout
85-
;(globalThis as any).setInterval = (globalThis as any).setInterval ?? timers.setInterval
86-
;(globalThis as any).clearInterval = (globalThis as any).clearInterval ?? timers.clearInterval
84+
setGlobalPolyfill('setTimeout', timers.setTimeout)
85+
setGlobalPolyfill('clearTimeout', timers.clearTimeout)
86+
setGlobalPolyfill('setInterval', timers.setInterval)
87+
setGlobalPolyfill('clearInterval', timers.clearInterval)
8788

8889
/**
8990
* Adds pointer event collider system only in DEV env

packages/@dcl/ecs/src/systems/tween.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PBTween, TweenLoop, TweenStateStatus } from '../components'
33
import { Entity, IEngine } from '../engine'
44
import { ReadWriteByteBuffer } from '../serialization/ByteBuffer'
55
import { dataCompare } from './crdt/utils'
6+
import { getGlobal } from '../runtime/globals'
67
export type TweenSystem = {
78
tweenCompleted(entity: Entity): boolean
89
}
@@ -162,7 +163,7 @@ export function createTweenSystem(engine: IEngine): TweenSystem {
162163
}
163164

164165
// Some Explorers may not inject the flag and TweenSequence logic must be enabled in that case
165-
const enableTweenSequenceLogic = (globalThis as any).ENABLE_SDK_TWEEN_SEQUENCE
166+
const enableTweenSequenceLogic = getGlobal<boolean>('ENABLE_SDK_TWEEN_SEQUENCE')
166167
if (enableTweenSequenceLogic !== false) initializeTweenSequenceSystem()
167168

168169
const tweenSystem: TweenSystem = {

packages/@dcl/playground-assets/etc/playground-assets.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3960,7 +3960,7 @@ export namespace ReactEcs {
39603960
useEffect: EffectHook;
39613961
const // (undocumented)
39623962
useState: StateHook;
3963-
{};
3963+
export {};
39643964
}
39653965

39663966
// Warning: (tsdoc-at-sign-in-word) The "@" character looks like part of a TSDoc tag; use a backslash to escape it
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import TextEncodingPolyfill from 'text-encoding'
2+
import { setGlobalPolyfill } from '@dcl/ecs/src/runtime/globals'
23

34
/* @__PURE__ */
45
export function polyfillTextEncoder() {
5-
;(globalThis as any).TextEncoder = (globalThis as any).TextEncoder ?? TextEncodingPolyfill.TextEncoder
6-
;(globalThis as any).TextDecoder = (globalThis as any).TextDecoder ?? TextEncodingPolyfill.TextDecoder
6+
setGlobalPolyfill('TextEncoder', TextEncodingPolyfill.TextEncoder)
7+
setGlobalPolyfill('TextDecoder', TextEncodingPolyfill.TextDecoder)
78
}

test/snapshots/development-bundles/static-scene.test.ts.crdt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SCENE_COMPILED_JS_SIZE_PROD=504.2k bytes
1+
SCENE_COMPILED_JS_SIZE_PROD=504.4k bytes
22
THE BUNDLE HAS SOURCEMAPS
33
(start empty vm 0.21.0-3680274614.commit-1808aa1)
44
OPCODES ~= 0k
@@ -11,8 +11,8 @@ EVAL test/snapshots/development-bundles/static-scene.test.js
1111
REQUIRE: ~system/EngineApi
1212
REQUIRE: ~system/EngineApi
1313
OPCODES ~= 61k
14-
MALLOC_COUNT = 14796
15-
ALIVE_OBJS_DELTA ~= 2.93k
14+
MALLOC_COUNT = 14808
15+
ALIVE_OBJS_DELTA ~= 2.94k
1616
CALL onStart()
1717
main.crdt: PUT_COMPONENT e=0x200 c=1 t=0 data={"position":{"x":5.880000114440918,"y":2.7916901111602783,"z":7.380000114440918},"rotation":{"x":0,"y":0,"z":0,"w":1},"scale":{"x":1,"y":1,"z":1},"parent":0}
1818
main.crdt: PUT_COMPONENT e=0x202 c=1 t=0 data={"position":{"x":4,"y":0.800000011920929,"z":8},"rotation":{"x":0,"y":0,"z":0,"w":1},"scale":{"x":1,"y":1,"z":1},"parent":0}
@@ -56,4 +56,4 @@ CALL onUpdate(0.1)
5656
OPCODES ~= 3k
5757
MALLOC_COUNT = -5
5858
ALIVE_OBJS_DELTA ~= 0.00k
59-
MEMORY_USAGE_COUNT ~= 1308.50k bytes
59+
MEMORY_USAGE_COUNT ~= 1309.11k bytes

test/snapshots/development-bundles/testing-fw.test.ts.crdt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SCENE_COMPILED_JS_SIZE_PROD=504.8k bytes
1+
SCENE_COMPILED_JS_SIZE_PROD=504.9k bytes
22
THE BUNDLE HAS SOURCEMAPS
33
(start empty vm 0.21.0-3680274614.commit-1808aa1)
44
OPCODES ~= 0k
@@ -11,7 +11,7 @@ EVAL test/snapshots/development-bundles/testing-fw.test.js
1111
REQUIRE: ~system/EngineApi
1212
REQUIRE: ~system/EngineApi
1313
OPCODES ~= 70k
14-
MALLOC_COUNT = 15326
14+
MALLOC_COUNT = 15338
1515
ALIVE_OBJS_DELTA ~= 3.09k
1616
CALL onStart()
1717
LOG: ["Adding one to position.y=0"]
@@ -64,4 +64,4 @@ CALL onUpdate(0.1)
6464
OPCODES ~= 5k
6565
MALLOC_COUNT = -40
6666
ALIVE_OBJS_DELTA ~= -0.01k
67-
MEMORY_USAGE_COUNT ~= 1317.39k bytes
67+
MEMORY_USAGE_COUNT ~= 1318.00k bytes

test/snapshots/development-bundles/two-way-crdt.test.ts.crdt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SCENE_COMPILED_JS_SIZE_PROD=504.8k bytes
1+
SCENE_COMPILED_JS_SIZE_PROD=504.9k bytes
22
THE BUNDLE HAS SOURCEMAPS
33
(start empty vm 0.21.0-3680274614.commit-1808aa1)
44
OPCODES ~= 0k
@@ -11,7 +11,7 @@ EVAL test/snapshots/development-bundles/two-way-crdt.test.js
1111
REQUIRE: ~system/EngineApi
1212
REQUIRE: ~system/EngineApi
1313
OPCODES ~= 70k
14-
MALLOC_COUNT = 15326
14+
MALLOC_COUNT = 15338
1515
ALIVE_OBJS_DELTA ~= 3.09k
1616
CALL onStart()
1717
LOG: ["Adding one to position.y=0"]
@@ -64,4 +64,4 @@ CALL onUpdate(0.1)
6464
OPCODES ~= 5k
6565
MALLOC_COUNT = -40
6666
ALIVE_OBJS_DELTA ~= -0.01k
67-
MEMORY_USAGE_COUNT ~= 1317.39k bytes
67+
MEMORY_USAGE_COUNT ~= 1318.00k bytes

test/snapshots/production-bundles/append-value-crdt.ts.crdt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ EVAL test/snapshots/production-bundles/append-value-crdt.js
99
REQUIRE: ~system/EngineApi
1010
REQUIRE: ~system/EngineApi
1111
OPCODES ~= 73k
12-
MALLOC_COUNT = 13766
12+
MALLOC_COUNT = 13776
1313
ALIVE_OBJS_DELTA ~= 3.08k
1414
CALL onStart()
1515
Renderer: APPEND_VALUE e=0x200 c=1063 t=0 data={"button":0,"hit":{"position":{"x":1,"y":2,"z":3},"globalOrigin":{"x":1,"y":2,"z":3},"direction":{"x":1,"y":2,"z":3},"normalHit":{"x":1,"y":2,"z":3},"length":10,"meshName":"mesh","entityId":512},"state":1,"timestamp":1,"analog":5,"tickNumber":0}
@@ -55,4 +55,4 @@ CALL onUpdate(0.1)
5555
OPCODES ~= 14k
5656
MALLOC_COUNT = 31
5757
ALIVE_OBJS_DELTA ~= 0.01k
58-
MEMORY_USAGE_COUNT ~= 974.81k bytes
58+
MEMORY_USAGE_COUNT ~= 975.32k bytes

test/snapshots/production-bundles/billboard.ts.crdt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SCENE_COMPILED_JS_SIZE_PROD=262.6k bytes
1+
SCENE_COMPILED_JS_SIZE_PROD=262.5k bytes
22
(start empty vm 0.21.0-3680274614.commit-1808aa1)
33
OPCODES ~= 0k
44
MALLOC_COUNT = 1005
@@ -9,8 +9,8 @@ EVAL test/snapshots/production-bundles/billboard.js
99
REQUIRE: ~system/EngineApi
1010
REQUIRE: ~system/EngineApi
1111
OPCODES ~= 74k
12-
MALLOC_COUNT = 16276
13-
ALIVE_OBJS_DELTA ~= 3.55k
12+
MALLOC_COUNT = 16286
13+
ALIVE_OBJS_DELTA ~= 3.56k
1414
CALL onStart()
1515
OPCODES ~= 0k
1616
MALLOC_COUNT = 4
@@ -77,4 +77,4 @@ CALL onUpdate(0.1)
7777
OPCODES ~= 10k
7878
MALLOC_COUNT = 0
7979
ALIVE_OBJS_DELTA ~= 0.00k
80-
MEMORY_USAGE_COUNT ~= 1162.53k bytes
80+
MEMORY_USAGE_COUNT ~= 1163.05k bytes

0 commit comments

Comments
 (0)