-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow binding any object to globalThis variable
- Loading branch information
Showing
7 changed files
with
73 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,5 @@ | |
"prettier": "^3.2.4", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.2.2" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2 | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Safely binds functions to globalThis | ||
* | ||
* @example | ||
* ```ts | ||
* type bindings = { test: Function }; | ||
* | ||
* set<bindings>("wallet", bindings); | ||
* ``` | ||
* | ||
* @export | ||
* @template T | ||
* @param {string} name | ||
* @param {T} bindings | ||
*/ | ||
export function set<T = object>(name: string, bindings: T) { | ||
( | ||
globalThis as typeof globalThis & { | ||
[key in typeof name]: T; | ||
} | ||
)[name] = bindings; | ||
} | ||
|
||
/** | ||
* Returns bindings assigned to globalThis | ||
* | ||
* @example | ||
* ``` | ||
* type bindings = { test: Function }; | ||
* | ||
* const wallet = get<bindings>("wallet"); | ||
* ``` | ||
* | ||
* @export | ||
* @template T | ||
* @param {string} name | ||
* @return {T} | ||
*/ | ||
export function get<T = object>(name: string): T { | ||
return ( | ||
globalThis as typeof globalThis & { | ||
[key in typeof name]: T; | ||
} | ||
)[name]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import { ProxyAPI, bindToWindow } from "./extension"; | ||
import * as bindings from "./binder"; | ||
import { expect, test } from "vitest"; | ||
|
||
declare global { | ||
var wallet: ProxyAPI; | ||
// eslint-disable-next-line no-var | ||
var wallet: typeof testBindings; | ||
} | ||
|
||
const testBindings = { | ||
test1() {}, | ||
test2() {}, | ||
test3() {}, | ||
}; | ||
|
||
test("test binding to 'window' or 'global' variables", () => { | ||
const bindingName = "wallet"; | ||
|
||
const target = typeof window !== "undefined" ? window : global; | ||
|
||
expect(typeof target).not.toBe("undefined"); | ||
expect(Object.hasOwn(target, "wallet")).toBe(false); | ||
expect(Object.hasOwn(target, bindingName)).toBe(false); | ||
|
||
bindToWindow(target, "wallet"); | ||
bindings.set(bindingName, testBindings); | ||
|
||
expect(typeof target.wallet).not.toBe("undefined"); | ||
expect(typeof target[bindingName]).not.toBe("undefined"); | ||
|
||
expect(typeof target.wallet?.test1).toBe("function"); | ||
expect(typeof target.wallet?.test2).toBe("function"); | ||
expect(typeof target.wallet?.test3).toBe("function"); | ||
const result = bindings.get<typeof testBindings>(bindingName); | ||
expect(typeof result.test1).toBe("function"); | ||
expect(typeof result.test2).toBe("function"); | ||
expect(typeof result.test3).toBe("function"); | ||
}); |