Add logging with filters
This commit is contained in:
committed by
Gerla, J. (Justin)
parent
b7eb0cb5ec
commit
231d7a5ba1
29
src/utils/cellStore.ts
Normal file
29
src/utils/cellStore.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {useSyncExternalStore} from "react";
|
||||
|
||||
type Unsub = () => void;
|
||||
|
||||
export type Cell<T> = {
|
||||
get: () => T;
|
||||
set: (next: T | ((prev: T) => T)) => void;
|
||||
subscribe: (callback: () => void) => Unsub;
|
||||
};
|
||||
|
||||
export function cell<T>(initial: T): Cell<T> {
|
||||
let value = initial;
|
||||
const listeners = new Set<() => void>();
|
||||
return {
|
||||
get: () => value,
|
||||
set: (next) => {
|
||||
value = typeof next === "function" ? (next as (v: T) => T)(value) : next;
|
||||
for (const l of listeners) l();
|
||||
},
|
||||
subscribe: (callback) => {
|
||||
listeners.add(callback);
|
||||
return () => listeners.delete(callback);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function useCell<T>(c: Cell<T>) {
|
||||
return useSyncExternalStore(c.subscribe, c.get, c.get);
|
||||
}
|
||||
Reference in New Issue
Block a user