Skip to content

Commit 7089fda

Browse files
committed
Reduce the use of global
1 parent 177964e commit 7089fda

File tree

4 files changed

+533
-682
lines changed

4 files changed

+533
-682
lines changed

packages/loopring-sdk/src/lib/bridge/bridge.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
/* eslint-disable prefer-spread, @typescript-eslint/ban-ts-comment, @typescript-eslint/no-var-requires, prefer-const, no-async-promise-executor */
2-
// @ts-nocheck
1+
/* eslint-disable prefer-spread, no-async-promise-executor */
2+
33
// Initially, the __go_wasm__ object will be an empty object.
44

5-
import './wasm_exec';
5+
declare global {
6+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
7+
var __go_wasm__: any | undefined;
8+
}
9+
10+
import Go from './go';
611
import instantiateWasm from './instantiateWasm';
712

813
const g = global || window || self;
@@ -33,8 +38,9 @@ const bridge = g.__go_wasm__;
3338
*
3439
* @returns {Function} returns a function that take arguments which are used to call the Go function.
3540
*/
36-
function wrapper(goFunc) {
37-
return (...args) => {
41+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
42+
function wrapper(goFunc: any) {
43+
return (...args: unknown[]) => {
3844
const result = goFunc.apply(undefined, args);
3945
if (result.error instanceof Error) {
4046
throw result.error;
@@ -52,7 +58,7 @@ function wrapper(goFunc) {
5258
* @returns {Promise} an always-resolving promise when a tick has been completed.
5359
*/
5460
function sleep() {
55-
return new Promise((res) => {
61+
return new Promise<void>((res) => {
5662
requestAnimationFrame(() => res());
5763
setTimeout(() => {
5864
res();
@@ -71,17 +77,16 @@ function sleep() {
7177
* If a non-function value is returned however arguments are provided, a warning will be printed.
7278
*/
7379
export default function () {
74-
let proxy;
75-
let go;
80+
let go: Go;
7681

7782
async function init() {
7883
bridge.__wrapper__ = wrapper;
7984

80-
go = new g.Go();
85+
go = new Go();
8186

8287
const wasm = await instantiateWasm(go);
8388

84-
go.run(wasm);
89+
go.run(wasm, g);
8590
}
8691

8792
init();
@@ -93,11 +98,11 @@ export default function () {
9398
}
9499
}, maxTime);
95100

96-
proxy = new Proxy(
101+
const proxy = new Proxy(
97102
{},
98103
{
99104
get: (_, key) => {
100-
return (...args) => {
105+
return (...args: unknown[]) => {
101106
return new Promise(async (res, rej) => {
102107
if (!go || go.exited) {
103108
return rej(new Error('The Go instance is not active.'));

0 commit comments

Comments
 (0)