-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from argentlabs/fix/window-folder
fix: add window types
- Loading branch information
Showing
18 changed files
with
2,535 additions
and
436 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,11 @@ | |
"types": "./dist/injectedConnector.d.ts", | ||
"import": "./dist/injectedConnector.js", | ||
"require": "./dist/injectedConnector.cjs" | ||
}, | ||
"./window": { | ||
"types": "./dist/window.d.ts", | ||
"import": "./dist/window.js", | ||
"require": "./dist/window.cjs" | ||
} | ||
}, | ||
"main": "./dist/starknetkit.cjs", | ||
|
@@ -102,8 +107,11 @@ | |
"typescript": "^5.1.6", | ||
"vite": "^4.3.8", | ||
"vite-plugin-dts": "^3.0.0", | ||
"vitest": "^1.6.0", | ||
"ws": "^8.8.1", | ||
"zod": "^3.20.6" | ||
"zod": "^3.20.6", | ||
"starknet4": "npm:[email protected]", | ||
"starknet5": "npm:[email protected]" | ||
}, | ||
"peerDependencies": { | ||
"starknet": "^6.9.0" | ||
|
Large diffs are not rendered by default.
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
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 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./modal" | ||
export * from "./starknet" | ||
export * from "./utils/mittx" | ||
export * from "./window" |
File renamed without changes.
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,52 @@ | ||
import type { StarknetWindowObject, Permission } from "get-starknet-core" | ||
import { | ||
ConnectedStarknetWindowObject as ConnectedStarknetWindowObjectV3, | ||
IStarknetWindowObject as IStarknetWindowObjectV3, | ||
} from "get-starknet-coreV3" | ||
import type { ProviderInterface } from "starknet" | ||
import type { ProviderInterface as ProviderInterface4 } from "starknet4" | ||
import type { ProviderInterface as ProviderInterface5 } from "starknet5" | ||
|
||
import type { AccountInterface } from "starknet" | ||
import type { AccountInterface as AccountInterface4 } from "starknet4" | ||
import type { AccountInterface as AccountInterface5 } from "starknet5" | ||
|
||
type CommonOmittedProperties = | ||
| "on" | ||
| "off" | ||
| "request" | ||
| "icon" | ||
| "provider" | ||
| "account" | ||
|
||
export type BackwardsCompatibleStarknetWindowObject = Omit< | ||
StarknetWindowObject, | ||
"provider" | "account" | ||
> & | ||
Omit<IStarknetWindowObjectV3, CommonOmittedProperties> & { | ||
isConnected?: boolean | ||
} & { | ||
provider?: ProviderInterface | ProviderInterface5 | ProviderInterface4 | ||
account?: AccountInterface | AccountInterface5 | AccountInterface4 | ||
} | ||
|
||
export type BackwardsCompatibleConnectedStarknetWindowObject = Omit< | ||
StarknetWindowObject, | ||
"provider" | "account" | ||
> & | ||
Omit<ConnectedStarknetWindowObjectV3, CommonOmittedProperties> & { | ||
provider?: ProviderInterface | ProviderInterface5 | ProviderInterface4 | ||
account?: AccountInterface | AccountInterface5 | AccountInterface4 | ||
} | ||
|
||
export type Variant = "argentX" | "argentWebWallet" | ||
|
||
export interface GetArgentStarknetWindowObject { | ||
id: Variant | ||
icon: string | ||
name: string | ||
version: string | ||
host: string | ||
} | ||
|
||
export { Permission } |
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,122 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
export type EventType = string | symbol | ||
|
||
// An event handler can take an optional event argument | ||
// and should not return a value | ||
export type Handler<T = unknown> = (event: T) => void | ||
export type WildcardHandler<T = Record<string, unknown>> = ( | ||
type: keyof T, | ||
event: T[keyof T], | ||
) => void | ||
|
||
// An array of all currently registered event handlers for a type | ||
export type EventHandlerList<T = unknown> = Array<Handler<T>> | ||
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array< | ||
WildcardHandler<T> | ||
> | ||
|
||
// A map of event types and their corresponding event handlers. | ||
export type EventHandlerMap<Events extends Record<EventType, unknown>> = Map< | ||
keyof Events | "*", | ||
EventHandlerList<Events[keyof Events]> | WildCardEventHandlerList<Events> | ||
> | ||
|
||
export interface Emitter<Events extends Record<EventType, unknown>> { | ||
all: EventHandlerMap<Events> | ||
|
||
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void | ||
on(type: "*", handler: WildcardHandler<Events>): void | ||
|
||
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void | ||
off(type: "*", handler: WildcardHandler<Events>): void | ||
|
||
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void | ||
emit<Key extends keyof Events>( | ||
type: undefined extends Events[Key] ? Key : never, | ||
): void | ||
} | ||
|
||
/** | ||
* Taken from https://github.com/developit/mitt/blob/main/src/index.ts | ||
* Mitt: Tiny (~200b) functional event emitter / pubsub. | ||
* @name mittx | ||
* @returns {MittX} | ||
*/ | ||
export function mittx<Events extends Record<EventType, unknown>>( | ||
all?: EventHandlerMap<Events>, | ||
): Emitter<Events> { | ||
type GenericEventHandler = | ||
| Handler<Events[keyof Events]> | ||
| WildcardHandler<Events> | ||
all = all || new Map() | ||
|
||
return { | ||
/** | ||
* A Map of event names to registered handler functions. | ||
*/ | ||
all, | ||
|
||
/** | ||
* Register an event handler for the given type. | ||
* @param {string|symbol} type Type of event to listen for, or `'*'` for all events | ||
* @param {Function} handler Function to call in response to given event | ||
* @memberOf mittx | ||
*/ | ||
on<Key extends keyof Events>(type: Key, handler: GenericEventHandler) { | ||
const handlers: Array<GenericEventHandler> | undefined = all!.get(type) | ||
if (handlers) { | ||
handlers.push(handler) | ||
} else { | ||
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>) | ||
} | ||
}, | ||
|
||
/** | ||
* Remove an event handler for the given type. | ||
* If `handler` is omitted, all handlers of the given type are removed. | ||
* @param {string|symbol} type Type of event to unregister `handler` from (`'*'` to remove a wildcard handler) | ||
* @param {Function} [handler] Handler function to remove | ||
* @memberOf mittx | ||
*/ | ||
off<Key extends keyof Events>(type: Key, handler?: GenericEventHandler) { | ||
const handlers: Array<GenericEventHandler> | undefined = all!.get(type) | ||
if (handlers) { | ||
if (handler) { | ||
handlers.splice(handlers.indexOf(handler) >>> 0, 1) | ||
} else { | ||
all!.set(type, []) | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* Invoke all handlers for the given type. | ||
* If present, `'*'` handlers are invoked after type-matched handlers. | ||
* | ||
* Note: Manually firing '*' handlers is not supported. | ||
* | ||
* @param {string|symbol} type The event type to invoke | ||
* @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler | ||
* @memberOf mittx | ||
*/ | ||
emit<Key extends keyof Events>(type: Key, evt?: Events[Key]) { | ||
let handlers = all?.get(type) | ||
if (handlers) { | ||
;(handlers as EventHandlerList<Events[keyof Events]>) | ||
.slice() | ||
.map((handler) => { | ||
handler(evt!) | ||
}) | ||
} | ||
|
||
handlers = all!.get("*") | ||
if (handlers) { | ||
;(handlers as WildCardEventHandlerList<Events>) | ||
.slice() | ||
.forEach((handler) => { | ||
handler(type, evt!) | ||
}) | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.