Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/elements/play-pen/play-pen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {Empty, LinkedBundle} from '@devvit/protos'
import {throttle} from '@devvit/shared-types/throttle.js'
import type {DevvitUIError} from '@devvit/ui-renderer/client/devvit-custom-post.js'
import type {VirtualTypeScriptEnvironment} from '@typescript/vfs'
import {
Expand Down Expand Up @@ -57,6 +56,7 @@ import {
emptyAssetsState,
PlayAssets
} from '../play-assets/play-assets.js'
import {debounce, type DebounceCleanupFn} from '../../utils/debounce.js'

declare global {
interface HTMLElementTagNameMap {
Expand All @@ -74,9 +74,7 @@ declare global {
export class PlayPen extends LitElement {
static override readonly styles: CSSResultGroup = css`
${cssReset}

${unsafeCSS(penVars)}

:host {
/* Light mode. */
color: var(--color-foreground);
Expand Down Expand Up @@ -118,6 +116,7 @@ export class PlayPen extends LitElement {
}

/* Makes dropdowns appear over other content */

play-pen-header,
play-pen-footer {
z-index: var(--z-menu);
Expand Down Expand Up @@ -181,6 +180,8 @@ export class PlayPen extends LitElement {
private _src: string | undefined
#template?: boolean

#cleanupDelayedSideEffects: DebounceCleanupFn = () => {}

override connectedCallback(): void {
super.connectedCallback()

Expand Down Expand Up @@ -218,6 +219,11 @@ export class PlayPen extends LitElement {
this.#setName(pen.name, false)
}

override disconnectedCallback() {
this.#cleanupDelayedSideEffects()
super.disconnectedCallback()
}

protected override render(): TemplateResult {
return html`
<play-assets
Expand Down Expand Up @@ -440,11 +446,11 @@ export class PlayPen extends LitElement {
appEntrypointFilename
)
}
this.#setSrcSideEffects(save)
this.#cleanupDelayedSideEffects = this.#setSrcSideEffects(save)
}

/** Throttled changes after updating sources. */
#setSrcSideEffects = throttle((save: boolean): void => {
/** Debounced changes after updating sources. */
#setSrcSideEffects = debounce((save: boolean): void => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried this but trust your judgement 👍 the intent of the original design was to give regular feedback as fast as possible.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rollback would be easy in case we hate it, let's give it a try

this.#version++
this._bundle = link(
compile(this.#env),
Expand Down
45 changes: 45 additions & 0 deletions src/utils/debounce.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {debounce} from './debounce.js'
import {expect, it, describe, vi} from 'vitest'

describe('debounce', () => {
it('executes with a delay', async () => {
const out = {val: 0}
const fn = debounce((val: number) => (out.val = val), 100)
fn(1)
await new Promise(resolve => setTimeout(resolve, 0))
expect(out.val).toBe(0)
await new Promise(resolve => setTimeout(resolve, 100))
expect(out.val).toBe(1)
})

it('executes the last call of a series', async () => {
const out = {val: 0}
const fn = debounce((val: number) => (out.val = val), 100)
fn(1)
await new Promise(resolve => setTimeout(resolve, 0))
expect(out.val).toBe(0)

fn(2)
await new Promise(resolve => setTimeout(resolve, 0))
expect(out.val).toBe(0)

fn(3)
await new Promise(resolve => setTimeout(resolve, 100))
expect(out.val).toBe(3)

fn(4)
await new Promise(resolve => setTimeout(resolve, 100))
expect(out.val).toBe(4)
})

it('has a cleanup function that cancels the delayed execution', async () => {
const innerFn = vi.fn()
const debouncedFn = debounce(innerFn, 1000)
const cleanupFn = debouncedFn(1)
await new Promise(resolve => setTimeout(resolve, 0))
expect(innerFn).toBeCalledTimes(0)
cleanupFn()
await new Promise(resolve => setTimeout(resolve, 1000))
expect(innerFn).toBeCalledTimes(0)
})
})
18 changes: 18 additions & 0 deletions src/utils/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** Delay function execution until after the invocations stopped. */
export type DebounceCleanupFn = () => void

export const debounce = <T extends unknown[]>(
fn: (...args: T) => void,
period: number
): ((...args: T) => DebounceCleanupFn) => {
let timeout: ReturnType<typeof setTimeout> | undefined
return (...args: T) => {
clearTimeout(timeout)
timeout = setTimeout(() => {
fn(...args)
}, period)
return () => {
clearTimeout(timeout)
}
}
}