Skip to content

Commit

Permalink
feat(core): add unique identifier for signals (#405)
Browse files Browse the repository at this point in the history
* add unique identifier for signals

* avoid name collision
  • Loading branch information
JoviDeCroock committed Sep 4, 2023
1 parent 40b270f commit 9355d96
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-spies-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals-core": minor
---

Add unique identifier to every `Signal`, this will be present on the `type` property of a Signal coming from either `signal()` or `computed()`
6 changes: 5 additions & 1 deletion mangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
},
"minify": {
"mangle": {
"reserved": ["useSignal", "useComputed", "useSignalEffect"],
"reserved": [
"useSignal",
"useComputed",
"useSignalEffect"
],
"keep_classnames": true,
"properties": {
"regex": "^_[^_]",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ function mutationDetected(): never {
throw new Error("Computed cannot have side-effects");
}

const identifier = Symbol.for('preact-signals')

// Flags for Computed and Effect.
const RUNNING = 1 << 0;
const NOTIFIED = 1 << 1;
Expand Down Expand Up @@ -242,6 +244,8 @@ declare class Signal<T = any> {

peek(): T;

brand: typeof identifier;

get value(): T;
set value(value: T);
}
Expand All @@ -255,6 +259,8 @@ function Signal(this: Signal, value?: unknown) {
this._targets = undefined;
}

Signal.prototype.brand = identifier

Signal.prototype._refresh = function () {
return true;
};
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/signal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ describe("signal", () => {
expect(spy).not.to.be.called;
});
});

it("signals should be identified with a symbol", () => {
const a = signal(0);
expect(a.brand).to.equal(Symbol.for('preact-signals'))
})

it("should be identified with a symbol", () => {
const a = computed(() => {});
expect(a.brand).to.equal(Symbol.for('preact-signals'))
})
});

describe("effect()", () => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2021.WeakRef"],
"lib": ["ES2021.WeakRef", "ES2015.Symbol"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
Expand Down

0 comments on commit 9355d96

Please sign in to comment.