Skip to content

Commit

Permalink
initial: new helper
Browse files Browse the repository at this point in the history
  • Loading branch information
hbbio committed May 15, 2024
1 parent c1a0eaa commit e87e164
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/initial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { AnyCell, MapCell } from "./cell";
import type { SheetProxy } from "./proxy";

export const initialValue = <T>(
proxy: SheetProxy,
v0: T,
v: AnyCell<T>,
name = "initial"
): MapCell<T, true> => {
const cell = proxy.new(v0, name);
v.subscribe((v) => {
// We do not propagate errors yet.
if (v instanceof Error) return;
cell.set(v);
});
// We fake being a MapCell to prevent setting the cell
// outside of this function.
return cell as unknown as MapCell<T, true>;
};

0 comments on commit e87e164

Please sign in to comment.