Skip to content

Commit

Permalink
Fix active element (#55)
Browse files Browse the repository at this point in the history
* rename debounced (#52)

* fix active element

* add changeset

* optimize activeElement

* fix lint

* refactor readable a bit
  • Loading branch information
TGlide authored May 17, 2024
1 parent ee1475d commit 27e03ad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-jars-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"runed": patch
---

fix active element not being up to date outside effects
3 changes: 3 additions & 0 deletions packages/runed/src/lib/internal/utils/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function noop() {
// noop
}
24 changes: 15 additions & 9 deletions packages/runed/src/lib/utilities/Readable/readable.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { tick } from "svelte";
import { noop } from "$lib/internal/utils/function.js";

Check failure on line 2 in packages/runed/src/lib/utilities/Readable/readable.svelte.ts

View workflow job for this annotation

GitHub Actions / Lint

'noop' is defined but never used

export type StartNotifier<TValue> = (set: (value: TValue) => void) => VoidFunction;
export type StartNotifier<TValue> = (
set: (value: TValue) => void,
insideEffect: boolean
) => void | VoidFunction;

/**
* A class that contains a reactive `current` property
Expand Down Expand Up @@ -40,7 +44,7 @@ export class Readable<TValue> {
$effect(() => {
this.#subscribers++;
if (this.#subscribers === 1) {
this.#subscribe();
this.#subscribe(true);
}

return () => {
Expand All @@ -52,21 +56,23 @@ export class Readable<TValue> {
});
};
});
} else if (this.#subscribers === 0) {
this.#subscribe(false);
this.#unsubscribe();
}

return this.#current;
}

#subscribe() {
this.#stop = this.#start((value) => {
this.#current = value;
});
#subscribe(inEffect: boolean) {
this.#stop =
this.#start((value) => {
this.#current = value;
}, inEffect) ?? null;
}

#unsubscribe() {
if (this.#stop === null) {
return;
}
if (this.#stop === null) return;

this.#stop();
this.#stop = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { Readable } from "../Readable/readable.svelte.js";
*
* @see {@link https://runed.dev/docs/utilities/use-active-element}
*/
export const activeElement = new Readable<Element | null>(null, (set) => {
export const activeElement = new Readable<Element | null>(null, (set, insideEffect) => {
function update() {
set(document.activeElement);
}

update();

if (!insideEffect) return;

document.addEventListener("focusin", update);
document.addEventListener("focusout", update);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { activeElement } from "./index.js";
import { testWithEffect } from "$lib/test/util.svelte.js";

// Skipping because testing is weird
describe.skip("useActiveElement", () => {
describe("useActiveElement", () => {
testWithEffect("initializes with `document.activeElement`", () => {
expect(activeElement.current).toBe(document.activeElement);
});
Expand Down

0 comments on commit 27e03ad

Please sign in to comment.