diff --git a/index.html b/index.html index 208f873..7af21e6 100644 --- a/index.html +++ b/index.html @@ -1032,7 +1032,7 @@

On Change Example:

-
+

Custom Select:

@@ -1107,6 +1107,35 @@

On Change Example:

+
  • + Dwight Schrute + + + +
  • { - el.addEventListener(eventName, event) + [].concat(listener).forEach(({ el, eventName, event }) => { + el.addEventListener(eventName, (e) => { + this.debounce(() => { + event(e) + }, 50) }) - } else { - const { el, eventName, event } = listener - el.addEventListener(eventName, event) - } + }) } + debounce(callback, wait) { + if (this.timeout) return + clearTimeout(this.timeout) + this.timeout = setTimeout(() => { + callback() + this.timeout = null + }, wait) + } + setChangeEvent() { const el = this.entity.element const key = ':change' diff --git a/lib/main.js b/lib/main.js index 10d01ba..cdc30be 100644 --- a/lib/main.js +++ b/lib/main.js @@ -42,6 +42,12 @@ export class Mini { console.log(`MiniJS took ${executionTime}ms to run.`) } + async reset() { + this.observer.disconnect() + this.state.dispose() + await this.init() + } + async _domReady() { return new Promise((resolve) => { if (document.readyState == 'loading') { @@ -86,6 +92,13 @@ const MiniJS = (() => { console.error('Error initializing MiniJS:', error) }) + // Reset MiniJS when the user navigates back + window.addEventListener('popstate', () => { + mini.reset().catch((error) => { + console.error('Error resetting MiniJS after navigation:', error); + }); + }); + return { get debug() { return Mini.debug @@ -108,6 +121,9 @@ const MiniJS = (() => { extendEvents: (events) => { mini.extensions.events.extend(events) }, + reset: async () => { + await mini.reset() + } } })() diff --git a/lib/state.js b/lib/state.js index 4847b2d..a02f9d7 100644 --- a/lib/state.js +++ b/lib/state.js @@ -297,4 +297,11 @@ export class State { const key = `${entityID}.${variable}` this.entityVariables.delete(key) } + + dispose() { + this.shouldUpdate = false + this.entities.clear() + this.entityVariables.clear() + this.variables.clear() + } }