Skip to content

Commit

Permalink
utility: nextSubscriber (#23)
Browse files Browse the repository at this point in the history
* next: nextSubscriber

* package: export nextSubscriber, bump version

* package: update scripts
  • Loading branch information
hbbio committed Mar 14, 2024
1 parent a8eedea commit e41cee7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@okcontract/cells",
"version": "0.2.2",
"version": "0.2.3",
"description": "Simplified reactive functional programming for the web",
"private": false,
"main": "dist/cells.umd.cjs",
Expand Down Expand Up @@ -39,8 +39,8 @@
"coverage": "vitest run --coverage",
"definitions": "tsc --project tsconfig.build.json",
"prepublishOnly": "npm test && npm run build && npm run check && npm run definitions",
"check": "npx @biomejs/biome check src --apply",
"format": "npx @biomejs/biome format src --write",
"check": "npx @biomejs/biome check src",
"format": "npx @biomejs/biome format src --write && npx @biomejs/biome check src --apply",
"formatReadme": "prettier README.md --prose-wrap always --print-width 78 -w"
},
"repository": {
Expand All @@ -49,4 +49,4 @@
},
"author": "Henri Binsztok",
"license": "MIT"
}
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export {
reduce,
sort
} from "./array";

export { Debugger } from "./debug";

export { jsonStringify } from "./json";
export { nextSubscriber } from "./next";
27 changes: 27 additions & 0 deletions src/next.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { expect, test } from "vitest";

import { nextSubscriber } from "./next";
import { sleep } from "./promise";
import { SheetProxy } from "./proxy";
import { Sheet } from "./sheet";

test("nextSubscriber", async () => {
const sheet = new Sheet();
const proxy = new SheetProxy(sheet);
const cell = proxy.new(1, "init");

let f = 0;
nextSubscriber(cell, (v) => {
f = v;
});

// we wait
await sleep(10);
expect(f).toBe(0);

cell.set(2);
expect(f).toBe(2);

cell.set(3);
expect(f).toBe(2);
});
24 changes: 24 additions & 0 deletions src/next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AnyCell } from "./cell";
import { Unsubscriber } from "./types";

/**
* nextSubscriber subscribes to get the next value of a cell. This is useful
* when a cell is already defined, but we know it will be updated (e.g. for a
* ValueCell) and we want that next value.
*/
export const nextSubscriber = <V>(
cell: AnyCell<V>,
cb: (v: V) => void,
_expectedCount = 2
) => {
// biome-ignore lint/style/useConst: need reference
let uns: Unsubscriber;
let count = 0;
uns = cell.subscribe((arg) => {
count++;
if (count !== _expectedCount) return;
if (arg instanceof Error) throw arg;
cb(arg);
queueMicrotask(uns);
});
};

0 comments on commit e41cee7

Please sign in to comment.