Skip to content

Commit

Permalink
Events switched to per element list, with optional off callback param…
Browse files Browse the repository at this point in the history
…eter
  • Loading branch information
mateussouzaweb committed Mar 22, 2022
1 parent 1d1068c commit 4952093
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions src/core/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ declare interface Trigger {
callback: Function
}

let _events: Array<Trigger> = []
declare interface WithEvents extends EventTarget {
__events?: Array<Trigger>
}

/**
* Attach event to element
Expand All @@ -20,7 +22,7 @@ function _event(
action: "add" | "remove",
element: any,
event: string,
selector: string | Function,
selector?: string | Function,
callback?: Function
) {

Expand All @@ -34,11 +36,16 @@ function _event(
return
}

const items = element instanceof Window ? [element] : $$(element)
let handler: Function

// Determine handler
if (callback === undefined) {
if (callback === undefined && selector === undefined) {

// None
handler = null
selector = null

} else if (callback === undefined) {

// Bind
handler = <Function>selector
Expand All @@ -59,46 +66,59 @@ function _event(
const split = event.split('.')
const theEvent = split.shift()
const namespace = split.join('.')
const items: Array<WithEvents> = element instanceof Window ? [element] : $$(element)

if (action === 'add' && typeof handler === 'function') {

if (action === 'add') {
for (const item of items) {

_events.push({
event: theEvent,
namespace: namespace,
callback: handler
})
if (!item.__events) {
item.__events = []
}

item.__events.push({
event: theEvent,
namespace: namespace,
callback: handler
})

items.forEach((item: Node | Window) => {
item.addEventListener(
theEvent,
handler.bind(item),
false
)
})

return handler
}
}

_events = _events.filter((watcher) => {
} else if (action === 'remove') {

const pass = Boolean(
theEvent !== watcher.event
&& (namespace === '' || namespace !== watcher.namespace)
&& (typeof handler !== 'function' || handler !== watcher.callback)
)
for (const item of items) {

if (!pass) {
items.forEach((item: Node | Window) => {
item.removeEventListener(
watcher.event,
watcher.callback.bind(item),
false
if (!item.__events) {
return
}

item.__events = item.__events.filter((watcher) => {
const pass = Boolean(
theEvent !== watcher.event
&& (namespace === '' || namespace !== watcher.namespace)
&& (typeof handler !== 'function' || handler !== watcher.callback)
)

if (!pass) {
item.removeEventListener(
watcher.event,
watcher.callback.bind(item),
false
)
}

return pass
})

}

return pass
})
}

return handler
}
Expand All @@ -121,7 +141,7 @@ function on(element: any, event: string, selector: string | Function, callback?:
* @param selector
* @param callback
*/
function off(element: any, event: string, selector: string | Function, callback?: Function) {
function off(element: any, event: string, selector?: string | Function, callback?: Function) {
return _event('remove', element, event, selector, callback)
}

Expand All @@ -142,9 +162,9 @@ function trigger(element: any, event: string, selector?: string) {
'cancelable': true
})

items.forEach((item: Node | Window) => {
for (const item of items) {
item.dispatchEvent(theEvent)
})
}

}

Expand Down

0 comments on commit 4952093

Please sign in to comment.