-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Latest build adds better event typing and removes prefix
Prefix may be added back later on but for right now it is not a necessary Other small changes include - Changing j to J and let to const - better typing of event listener options with AddEventListenerOptions - Adds EventMap type from microsoft/TypeScript#33047 (comment) - Updates JuhlaMethodsFunc & JuhlaEmitFunc with EventMap generic - Removes JuhlaEventListenerOptions
- Loading branch information
1 parent
b861b85
commit 2a11286
Showing
2 changed files
with
57 additions
and
56 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 |
---|---|---|
@@ -1,24 +1,19 @@ | ||
let s = (E = "", o = new EventTarget()) => new Proxy({ | ||
emit(e, p) { | ||
e.map((t) => o.dispatchEvent(new CustomEvent(t, p))); | ||
const d = (o = "", m = new EventTarget()) => new Proxy({ | ||
emit(e, t) { | ||
e.map((n) => m.dispatchEvent(new CustomEvent(n, t))); | ||
}, | ||
on(e, p, t) { | ||
e.map((i) => o.addEventListener(i, p, t)); | ||
on(e, t, n) { | ||
e.map((r) => m.addEventListener(r, t, n)); | ||
}, | ||
off(e, p, t) { | ||
e.map((i) => o.removeEventListener(i, p, t)); | ||
off(e, t, n) { | ||
e.map((r) => m.removeEventListener(r, t, n)); | ||
}, | ||
one(e, t, n = {}) { | ||
n.once = !0, e.map((r) => m.addEventListener(r, t, n)); | ||
} | ||
}, { | ||
get: (e, p) => (t, i, m) => e[p] ? e[p]( | ||
t.split(" ").map((n) => E + n), | ||
i, | ||
m | ||
) : e.on( | ||
[p], | ||
t, | ||
i | ||
) | ||
get: (e, t) => (n, r, p) => t in e ? e[t](n.split(" ").map((v) => o + v), r, p) : e.on([t], n, r) | ||
}); | ||
export { | ||
s as juhla | ||
d as juhla | ||
}; |
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,52 +1,58 @@ | ||
type Context = EventTarget | HTMLElement; | ||
type PossibleName = keyof HTMLElementEventMap | keyof JuhlaMethods | string | ||
type Context = EventTarget | Document | Window | HTMLElement; | ||
|
||
// https://github.com/microsoft/TypeScript/issues/33047#issuecomment-704005614 | ||
type EventMap<T extends Context> = T extends Document | ||
? DocumentEventMap | ||
: T extends Window | ||
? WindowEventMap | ||
: HTMLElementEventMap; | ||
type EventTypes<T extends EventTarget> = keyof EventMap<T> & string; | ||
type EventValue<T extends EventTarget, K extends EventTypes<T>> = Extract<EventMap<T>[K], Event>; | ||
|
||
type Juhla = (prefix?: string, ctx?: Context) => JuhlaInstance; | ||
/* | ||
type JuhlaMethods = { | ||
emit: JuhlaEmitFunc; | ||
on: JuhlaMethodsFunc; | ||
off: JuhlaMethodsFunc; | ||
} | ||
*/ | ||
|
||
interface JuhlaMethods { | ||
emit: JuhlaEmitFunc; | ||
on: JuhlaMethodsFunc; | ||
off: JuhlaMethodsFunc; | ||
one: JuhlaMethodsFunc; | ||
} | ||
|
||
type JuhlaAliasFunc = (handler: EventListener, options?: JuhlaEventListenerOptions) => unknown; | ||
type JuhlaMethodsFunc = (name: PossibleName[], handler: EventListener, options?: JuhlaEventListenerOptions) => unknown; | ||
type JuhlaEmitFunc = (name: PossibleName[], options?: Event) => unknown; | ||
type JuhlaMethodsFunc<T = EventMap<Context>> = (names: (keyof T)[], handler: EventListener, options?: AddEventListenerOptions) => unknown; | ||
type JuhlaEmitFunc<T = EventMap<Context>> = (names: (keyof T)[], options?: CustomEventInit) => unknown; | ||
|
||
type JuhlaInstance = { | ||
[index in keyof HTMLElementEventMap]: JuhlaAliasFunc; | ||
type JuhlaInstance ={ | ||
[index in EventTypes<Context>]: (handler: EventListener, options?: AddEventListenerOptions) => unknown; | ||
} & JuhlaMethods; | ||
|
||
type JuhlaEventListenerOptions = { | ||
once?: boolean; | ||
passive?: boolean; | ||
} & EventListenerOptions; | ||
|
||
/** All events will pass through this EventTarget */ | ||
let j: Juhla = (prefix = '', ctx = new EventTarget): JuhlaInstance => | ||
new Proxy<JuhlaInstance>({ | ||
emit(names, options) { names.map(n => ctx.dispatchEvent(new CustomEvent(n, options))) }, | ||
on(names, handler, options) { names.map(n => ctx.addEventListener(n, handler, options)) }, | ||
off(names, handler, options) { names.map(n => ctx.removeEventListener(n, handler, options)) } | ||
} as JuhlaInstance, { | ||
get: (juhlaInstance: JuhlaInstance, eventNameOrMethod: PossibleName) => | ||
(name: PossibleName, handler: EventListener, options?: JuhlaEventListenerOptions) => | ||
!juhlaInstance[eventNameOrMethod] ? juhlaInstance.on( | ||
[eventNameOrMethod], | ||
name as unknown as EventListener, | ||
handler as unknown as JuhlaEventListenerOptions | ||
) : juhlaInstance[eventNameOrMethod]( | ||
name.split(' ').map(n => prefix + n), | ||
handler, | ||
options | ||
) | ||
|
||
|
||
const J = <T extends EventTypes<Context>>(ctx:Context = new EventTarget) => { | ||
return new Proxy({ | ||
emit(names, options?: CustomEventInit) { | ||
names.map(n => ctx.dispatchEvent(new CustomEvent(n, options))); | ||
}, | ||
on(names, handler, options?: AddEventListenerOptions) { | ||
names.map(n => ctx.addEventListener(n, handler, options)); | ||
}, | ||
off(names, handler, options?: AddEventListenerOptions) { | ||
names.map(n => ctx.removeEventListener(n, handler, options)); | ||
}, | ||
one(names, handler, options:AddEventListenerOptions = {}) { | ||
options.once = true; | ||
names.map(n => ctx.addEventListener(n, handler, options)); | ||
} | ||
}, { | ||
get: (juhlaInstance: JuhlaMethods, possibleEventAlias: keyof JuhlaInstance) => | ||
<E>( | ||
name: E extends keyof EventTypes<typeof ctx> ? EventTypes<typeof ctx> : string, | ||
handler: typeof possibleEventAlias extends keyof HTMLElementEventMap ? | ||
AddEventListenerOptions : | ||
EventListener, | ||
options?: AddEventListenerOptions | ||
) => | ||
possibleEventAlias in juhlaInstance ? | ||
juhlaInstance[possibleEventAlias](name.split(' '), handler, options) : | ||
juhlaInstance.on([possibleEventAlias as T], name as unknown as EventListener, handler as unknown as AddEventListenerOptions) | ||
}); | ||
export { j as juhla } | ||
} | ||
export { J as juhla } |