Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Dec 7, 2024
1 parent ce4a3bc commit 3aad04d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/lwc/signals/__tests__/signal-identity.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
import { $signal, isASignal } from "../core";
import { $signal, isSignal } from "../core";

describe("isASignal", () => {
test("checks that a value is a signal", () => {
const signal = $signal(0);
expect(isASignal(signal)).toBe(true);
expect(isSignal(signal)).toBe(true);
});

test("checks that a computed is a signal", () => {
const signal = $signal(0);
const computed = $signal(() => signal.value);
expect(isASignal(computed)).toBe(true);
expect(isSignal(computed)).toBe(true);
});

test("checks that a value is not a signal", () => {
expect(isASignal(0)).toBe(false);
expect(isSignal(0)).toBe(false);
});

test("checks that a function is not a signal", () => {
expect(isASignal(() => {})).toBe(false);
expect(isSignal(() => {})).toBe(false);
});

test("checks that an object is not a signal", () => {
expect(isASignal({})).toBe(false);
expect(isSignal({})).toBe(false);
});

test("checks that an array is not a signal", () => {
expect(isASignal([])).toBe(false);
expect(isSignal([])).toBe(false);
});

test("checks that undefined is not a signal", () => {
expect(isASignal(undefined)).toBe(false);
expect(isSignal(undefined)).toBe(false);
});

test("checks that null is not a signal", () => {
expect(isASignal(null)).toBe(false);
expect(isSignal(null)).toBe(false);
});
});
10 changes: 6 additions & 4 deletions src/lwc/signals/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ class TrackedState<T> implements TrackableState<T> {
}
}

const SIGNAL_OBJECT_BRAND = Symbol.for("lwc-signals");

/**
* Creates a new signal with the provided value. A signal is a reactive
* primitive that can be used to store and update values. Signals can be
Expand Down Expand Up @@ -331,7 +333,7 @@ function $signal<T>(
setter(newValue);
}
},
brand: Symbol.for("lwc-signals"),
brand: SIGNAL_OBJECT_BRAND,
readOnly: {
get value() {
return getter();
Expand Down Expand Up @@ -534,8 +536,8 @@ function $resource<ReturnType, Params>(
};
}

function isASignal(anything: unknown): anything is Signal<unknown> {
return !!anything && (anything as Signal<unknown>).brand === Symbol.for("lwc-signals");
function isSignal(anything: unknown): anything is Signal<unknown> {
return !!anything && (anything as Signal<unknown>).brand === SIGNAL_OBJECT_BRAND;
}

export { $signal, $effect, $computed, $resource, isASignal };
export { $signal, $effect, $computed, $resource, isSignal };

0 comments on commit 3aad04d

Please sign in to comment.