Skip to content

Commit

Permalink
Return registered events on initialize and register
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed Mar 18, 2024
1 parent 15f7d0c commit 5a080f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ debounced.unregisterEvent('keyup')

// unregister a list of events
debounced.unregister(['click', 'input', 'keydown'])

// unregister all events
debounced.unregister(debounced.registeredEventNames)
```

### Debounced Event Prefix
Expand Down
12 changes: 11 additions & 1 deletion dist/debounced.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,29 @@ var unregisterEvent = (name) => {
var _a;
document.removeEventListener(name, (_a = registeredEvents[name]) == null ? void 0 : _a.handler);
delete registeredEvents[name];
return name;
};
var registerEvent = (name, options = {}) => {
unregisterEvent(name);
options = __spreadValues(__spreadValues({}, defaultOptions), options);
options.handler = buildDebounceEventHandler(options);
registeredEvents[name] = options;
document.addEventListener(name, options.handler);
return { [name]: registeredEvents[name] };
};
var unregister = (eventNames = []) => {
const names = __spreadValues({}, eventNames);
eventNames.forEach((name) => unregisterEvent(name));
return names;
};
var unregister = (eventNames = []) => eventNames.forEach((name) => unregisterEvent(name));
var register = (eventNames = [], options = {}) => {
if (!eventNames || eventNames.length === 0)
eventNames = nativeBubblingEventNames;
eventNames.forEach((name) => registerEvent(name, options));
return eventNames.reduce((memo, name) => {
memo[name] = registeredEvents[name];
return memo;
}, {});
};
var src_default = {
initialize: register,
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const buildDebounceEventHandler = (options = {}) => {
const unregisterEvent = name => {
document.removeEventListener(name, registeredEvents[name]?.handler)
delete registeredEvents[name]
return name
}

/**
Expand All @@ -78,13 +79,19 @@ const registerEvent = (name, options = {}) => {
options.handler = buildDebounceEventHandler(options)
registeredEvents[name] = options
document.addEventListener(name, options.handler)
return { [name]: registeredEvents[name] }
}

/**
* Unregisters a list of events.
* @param {Array} eventNames - List of event names to unregister
* @param {Array<String>} eventNames - List of event names to unregister
* @returns {Array<String>} - List of event names that were unregistered
*/
const unregister = (eventNames = []) => eventNames.forEach(name => unregisterEvent(name))
const unregister = (eventNames = []) => {
const names = { ...eventNames }
eventNames.forEach(name => unregisterEvent(name))
return names
}

/**
* Initializes debounced events.
Expand All @@ -101,6 +108,10 @@ const unregister = (eventNames = []) => eventNames.forEach(name => unregisterEve
const register = (eventNames = [], options = {}) => {
if (!eventNames || eventNames.length === 0) eventNames = nativeBubblingEventNames
eventNames.forEach(name => registerEvent(name, options))
return eventNames.reduce((memo, name) => {
memo[name] = registeredEvents[name]
return memo
}, {})
}

export default {
Expand Down

0 comments on commit 5a080f7

Please sign in to comment.