Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add Box #207

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
113 changes: 113 additions & 0 deletions packages/runed/src/lib/utilities/box/box.svelte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { box, ref } from "./box.svelte.js";

describe("box", () => {
it("updates when the referenced value updates", () => {
let count = $state(0);
const boxed = box(() => count);
expect(boxed.current).toBe(0);

count = 1;
expect(boxed.current).toBe(1);
});

it("creates a readonly box when only a getter is passed", () => {
const boxed = box(() => 0);

function set() {
// @ts-expect-error It's readonly
boxed.current = 1;
}

expect(set).toThrowError();
});

it("creates a writable box when a getter and setter are passed", () => {
let count = $state(0);
const boxed = box(
() => count,
(value) => {
count = value;
}
);
expect(boxed.current).toBe(0);

boxed.current = 1;
expect(boxed.current).toBe(1);
expect(count).toBe(1);

count = 2;
expect(boxed.current).toBe(2);
});

it("memoizes the getter", () => {
let count = $state(0);
const fn = vi.fn(() => count);
const boxed = box(fn);
expect(fn).not.toHaveBeenCalled();

boxed.current;
expect(fn).toHaveBeenCalledTimes(1);

boxed.current;
expect(fn).toHaveBeenCalledTimes(1);

count = 1;
boxed.current;
expect(fn).toHaveBeenCalledTimes(2);

boxed.current;
expect(fn).toHaveBeenCalledTimes(2);
});
});

describe("ref", () => {
it("updates when the referenced value updates", () => {
let count = 0;
const boxed = ref(() => count);
expect(boxed.current).toBe(0);

count = 1;
expect(boxed.current).toBe(1);
});

it("creates a readonly box when only a getter is passed", () => {
const boxed = ref(() => 0);

function set() {
// @ts-expect-error It's readonly
boxed.current = 1;
}

expect(set).toThrowError();
});

it("creates a writable box when a getter and setter are passed", () => {
let count = 0;
const boxed = ref(
() => count,
(value) => {
count = value;
}
);
expect(boxed.current).toBe(0);

boxed.current = 1;
expect(boxed.current).toBe(1);
expect(count).toBe(1);

count = 2;
expect(boxed.current).toBe(2);
});

it("does not memoize the getter", () => {
const fn = vi.fn(() => 0);
const boxed = ref(fn);
expect(fn).not.toHaveBeenCalled();

boxed.current;
expect(fn).toHaveBeenCalledTimes(1);

boxed.current;
expect(fn).toHaveBeenCalledTimes(2);
});
});
61 changes: 61 additions & 0 deletions packages/runed/src/lib/utilities/box/box.svelte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import type { Getter, Setter } from "$lib/internal/types.js";

export type Box<T> = {
readonly current: T;
readonly get: Getter<T>;
};

export type WritableBox<T> = {
current: T;
readonly get: Getter<T>;
readonly set: Setter<T>;
};

export function box<T>(get: Getter<T>): Box<T>;
export function box<T>(get: Getter<T>, set: Setter<T>): WritableBox<T>;
export function box<T>(get: Getter<T>, set?: Setter<T>): Box<T> {
const current = $derived.by(get);
if (set !== undefined) {
return new WritableRef(() => current, set);
}
return new Ref(() => current);
}

export function ref<T>(get: Getter<T>): Box<T>;
export function ref<T>(get: Getter<T>, set: Setter<T>): WritableBox<T>;
export function ref<T>(get: Getter<T>, set?: Setter<T>): Box<T> {
if (set !== undefined) {
return new WritableRef(get, set);
}
return new Ref(get);
}

class Ref<T> {
readonly get: Getter<T>;

constructor(get: Getter<T>) {
this.get = get;
}

get current(): T {
return this.get();
}
}

class WritableRef<T> {
readonly get: Getter<T>;
readonly set: Setter<T>;

constructor(get: Getter<T>, set: Setter<T>) {
this.get = get;
this.set = set;
}

get current(): T {
return this.get();
}

set current(value: T) {
this.set(value);
}
}
1 change: 1 addition & 0 deletions packages/runed/src/lib/utilities/box/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./box.svelte.js";