Skip to content

Commit

Permalink
Debounce applying plugins on page load
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 22, 2025
1 parent 657c1c8 commit 419394a
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 40 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Each tagged version of Datastar is accompanied by a release note. Read the [rele
### Fixed

- Fixed a bug in which `datastar-remove-fragments` events were not having any effect ([#664](https://github.com/starfederation/datastar/issues/664)).
- Fixed a bug in which `datastarNaN` could be used as an auto-generated element ID ([#679](https://github.com/starfederation/datastar/issues/679)).
- Fixed a bug in which `datastarNaN` could be used as an auto-generated element ID ([#679](https://github.com/starfederation/datastar/issues/679)).
- Fixed a bug in which plugins were being applied to the DOM twice on page load.
4 changes: 2 additions & 2 deletions bundles/datastar-aliased.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bundles/datastar-aliased.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bundles/datastar-core.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bundles/datastar-core.js.map

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions bundles/datastar.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions bundles/datastar.js.map

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions library/src/engine/engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Hash, elUniqId, walkDOM } from '../utils/dom'
import { camel } from '../utils/text'
import { delay } from '../utils/timing'
import { debounce } from '../utils/timing'
import { effect } from '../vendored/preact-core'
import { DSP, DSS } from './consts'
import { initErr, runtimeErr } from './errors'
Expand Down Expand Up @@ -85,11 +85,11 @@ export class Engine {
return a.name.localeCompare(b.name)
})

this.#delayedApply()
this.#debouncedApply()
}

// Delay applying plugins to give them time to load
#delayedApply = delay(() => {
// Add a debounce so that it is only applyied once, regardless of how many times the load function is called
#debouncedApply = debounce(() => {
this.#apply(document.body)
this.#observe()
}, 1)
Expand Down
8 changes: 5 additions & 3 deletions library/src/plugins/official/dom/attributes/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../../../engine/types'
import { tagHas, tagToMs } from '../../../../utils/tags'
import { camel, modifyCasing } from '../../../../utils/text'
import { debounce, delay, throttle } from '../../../../utils/timing'
import { debounce, throttle } from '../../../../utils/timing'
import { supportsViewTransitions } from '../../../../utils/view-transtions'
import type { Signal } from '../../../../vendored/preact-core'

Expand Down Expand Up @@ -43,8 +43,10 @@ export const On: AttributePlugin = {

const delayArgs = mods.get('delay')
if (delayArgs) {
const wait = tagToMs(delayArgs)
callback = delay(callback, wait)
const delay = tagToMs(delayArgs)
setTimeout(() => {
callback()
}, delay)
}

const debounceArgs = mods.get('debounce')
Expand Down
11 changes: 0 additions & 11 deletions library/src/utils/timing.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
export type TimerHandler = (...args: any[]) => void

export function delay(
callback: TimerHandler,
wait: number,
): TimerHandler {
return (...args: any[]) => {
setTimeout(() => {
callback(...args)
}, wait)
}
}

export function debounce(
callback: TimerHandler,
wait: number,
Expand Down
2 changes: 1 addition & 1 deletion sdk/go/consts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 419394a

Please sign in to comment.