Repository navigation
Expand file tree
/
Copy pathnpm.ts
More file actions
58 lines (51 loc) · 2.05 KB
/
Copy pathnpm.ts
File metadata and controls
58 lines (51 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* This file is part of Playgama Bridge.
*
* Playgama Bridge is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Playgama Bridge is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Playgama Bridge. If not, see <https://www.gnu.org/licenses/>.
*/
import './global'
import type PlaygamaBridge from './PlaygamaBridge'
const RUNTIME_MISSING_MESSAGE = 'Playgama Bridge runtime is not loaded. '
+ 'Add the SDK script to index.html before the game script, '
+ 'or add the Vite plugin: import playgamaBridge from \'@playgama/bridge/vite\'. '
+ 'For constants only, import from \'@playgama/bridge/constants\'.'
const readRuntime = (): PlaygamaBridge | undefined => (
typeof window === 'undefined' ? undefined : window.bridge || window.playgamaBridge
)
const requireRuntime = (): PlaygamaBridge => {
const runtime = readRuntime()
if (!runtime) {
throw new Error(RUNTIME_MISSING_MESSAGE)
}
return runtime
}
const deferred = new Proxy({} as PlaygamaBridge, {
get(_, key) {
const runtime = requireRuntime()
const value: unknown = Reflect.get(runtime, key, runtime)
return typeof value === 'function' ? value.bind(runtime) : value
},
set(_, key, value) {
return Reflect.set(requireRuntime(), key, value)
},
has(_, key) {
const runtime = readRuntime()
return runtime !== undefined && Reflect.has(runtime, key)
},
})
const bridge: PlaygamaBridge = readRuntime() || deferred
export default bridge
export { bridge }
export * from './publicConstants'
export type { default as PlaygamaBridge, PlaygamaInitOptions } from './PlaygamaBridge'