Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions typescript/packages/common-runner/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,6 @@ export type DocImpl<T> = {
*/
updates(callback: (value: T, path: PropertyKey[]) => void): Cancel;

/**
* Add callback for updates. Will call immediately with current value.
*
* @param callback - Callback to call on updates.
* @returns Cancel function.
*/
sink(callback: (value: T, path: PropertyKey[]) => void): Cancel;

/**
* Freeze cell, making it read-only.
*
Expand Down Expand Up @@ -263,11 +255,6 @@ export function getDoc<T>(value?: T, cause?: any, space?: Space): DocImpl<T> {
callbacks.add(callback);
return () => callbacks.delete(callback);
},
sink: (callback: (value: T, path: PropertyKey[]) => void) => {
callback(value as T, []);
callbacks.add(callback);
return () => callbacks.delete(callback);
},
getAtPath: (path: PropertyKey[]) => getValueAtPath(value, path),
setAtPath: (path: PropertyKey[], newValue: any, log?: ReactivityLog) => {
if (readOnly) throw new Error("Cell is read-only");
Expand Down
7 changes: 2 additions & 5 deletions typescript/packages/common-runner/src/reactivity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Cancel, isCancel, noOp } from "./cancel.js";
import { Cell, isCell } from "./cell.js";
import { DocImpl, isDoc } from "./doc.js";

/**
* Effect that runs a callback when the value changes. The callback is also
Expand All @@ -12,12 +11,10 @@ import { DocImpl, isDoc } from "./doc.js";
* @returns {function} - A function to cancel the effect.
*/
export const effect = <T>(
value: Cell<T> | DocImpl<T> | T,
value: Cell<T> | T,
callback: (value: T) => Cancel | undefined | void,
): Cancel => {
if (isDoc(value)) value = value.asCell();

if (isCell(value) || isDoc(value)) {
if (isCell(value)) {
return value.sink(callback);
} else {
const cancel = callback(value as T);
Expand Down
6 changes: 3 additions & 3 deletions typescript/packages/common-runner/test/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ describe("Cell", () => {
expect(c.get()).toEqual({ a: { b: { c: 100 } } });
});

it("should sink changes", () => {
it("should call updates callback when value changes", () => {
const c = getDoc(0);
const values: number[] = [];
const unsink = c.sink((value) => values.push(value));
const unsink = c.updates((value) => values.push(value));
c.send(1);
c.send(2);
c.send(3);
unsink();
c.send(4);
expect(values).toEqual([0, 1, 2, 3]);
expect(values).toEqual([1, 2, 3]);
});
});

Expand Down
Loading