Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools. Co-authored-by: Cursor <cursoragent@cursor.com>
85 lines
2.6 KiB
TypeScript
85 lines
2.6 KiB
TypeScript
/**
|
|
* Vendored from developit/mitt (MIT)
|
|
* https://github.com/developit/mitt
|
|
* Stars verified >1000 — see docs/OSS_RESEARCH.md
|
|
*
|
|
* Copyright (c) Jason Miller
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy...
|
|
*/
|
|
|
|
export type EventType = string | symbol
|
|
export type Handler<T = unknown> = (event: T) => void
|
|
export type WildcardHandler<T = Record<string, unknown>> = (
|
|
type: keyof T,
|
|
event: T[keyof T],
|
|
) => void
|
|
export type EventHandlerList<T = unknown> = Array<Handler<T>>
|
|
export type WildCardEventHandlerList<T = Record<string, unknown>> = Array<
|
|
WildcardHandler<T>
|
|
>
|
|
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
|
|
}
|
|
|
|
export default function mitt<Events extends Record<EventType, unknown>>(
|
|
all?: EventHandlerMap<Events>,
|
|
): Emitter<Events> {
|
|
type GenericEventHandler =
|
|
| Handler<Events[keyof Events]>
|
|
| WildcardHandler<Events>
|
|
all = all || new Map()
|
|
|
|
return {
|
|
all,
|
|
on(type: EventType, handler: GenericEventHandler) {
|
|
const handlers = all!.get(type)
|
|
if (handlers) {
|
|
;(handlers as GenericEventHandler[]).push(handler)
|
|
} else {
|
|
all!.set(type, [handler] as EventHandlerList<Events[keyof Events]>)
|
|
}
|
|
},
|
|
off(type: EventType, handler?: GenericEventHandler) {
|
|
const handlers = all!.get(type)
|
|
if (handlers) {
|
|
if (handler) {
|
|
handlers.splice(handlers.indexOf(handler as never) >>> 0, 1)
|
|
} else {
|
|
all!.set(type, [])
|
|
}
|
|
}
|
|
},
|
|
emit(type: EventType, evt?: unknown) {
|
|
let handlers = all!.get(type)
|
|
if (handlers) {
|
|
;(handlers as EventHandlerList)
|
|
.slice()
|
|
.map((handler) => {
|
|
handler(evt!)
|
|
})
|
|
}
|
|
handlers = all!.get('*')
|
|
if (handlers) {
|
|
;(handlers as WildCardEventHandlerList)
|
|
.slice()
|
|
.map((handler) => {
|
|
handler(type, evt!)
|
|
})
|
|
}
|
|
},
|
|
}
|
|
}
|