Adjust signal again
This commit is contained in:
+8
-4
@@ -1,11 +1,15 @@
|
||||
import { Bench } from "tinybench";
|
||||
|
||||
const bench = new Bench({ name: "", time: 1000 });
|
||||
const bench = new Bench({ name: "bench", time: 1000 });
|
||||
let sink = 0;
|
||||
|
||||
bench.add("", () => {
|
||||
sink += 1;
|
||||
});
|
||||
bench
|
||||
.add("a", () => {
|
||||
sink++;
|
||||
})
|
||||
.add("b", () => {
|
||||
sink++;
|
||||
});
|
||||
|
||||
await bench.run();
|
||||
|
||||
|
||||
@@ -4,22 +4,17 @@ let current: Current = null;
|
||||
|
||||
export function createSignal<T>(value: T): [() => T, (value: T) => void] {
|
||||
let innerValue = value;
|
||||
const observers: Array<Current> = [];
|
||||
const indices = new Map<Current, number>();
|
||||
const observers = new Set<() => void>();
|
||||
|
||||
const getter = () => {
|
||||
if (current !== null && !indices.has(current)) {
|
||||
observers.push(current);
|
||||
indices.set(current, observers.length);
|
||||
}
|
||||
|
||||
current !== null && observers.add(current);
|
||||
return innerValue;
|
||||
};
|
||||
|
||||
const setter = (value: T) => {
|
||||
innerValue = value;
|
||||
for (let i = 0; i < observers.length; i++) {
|
||||
observers[i]!();
|
||||
for (const observer of observers) {
|
||||
observer();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user