Skip to content

Commit

Permalink
feat: isASignal function allows to check if an object is a signal
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra committed Dec 6, 2024
1 parent 74f2521 commit c6be621
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
5 changes: 4 additions & 1 deletion force-app/lwc/signals/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,7 @@ function $resource(fn, source, options) {
}
};
}
export { $signal, $effect, $computed, $resource };
function isASignal(anything) {
return !!anything && anything.brand === Symbol.for("lwc-signals");
}
export { $signal, $effect, $computed, $resource, isASignal };
38 changes: 38 additions & 0 deletions src/lwc/signals/__tests__/signal-identity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { $signal, isASignal } from "../core";

describe("isASignal", () => {
test("checks that a value is a signal", () => {
const signal = $signal(0);
expect(isASignal(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);
});

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

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

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

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

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

test("checks that null is not a signal", () => {
expect(isASignal(null)).toBe(false);
});
});
6 changes: 5 additions & 1 deletion src/lwc/signals/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,8 @@ function $resource<ReturnType, Params>(
};
}

export { $signal, $effect, $computed, $resource };
function isASignal(anything: unknown): anything is Signal<unknown> {
return !!anything && (anything as Signal<unknown>).brand === Symbol.for("lwc-signals");
}

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

0 comments on commit c6be621

Please sign in to comment.