-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy: new utility function to copy any Cell as ValueCell
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect, test } from "vitest"; | ||
|
||
import { copy } from "./copy"; | ||
import { delayed } from "./promise"; | ||
import { SheetProxy } from "./proxy"; | ||
import { Sheet } from "./sheet"; | ||
|
||
test("copy cell", async () => { | ||
const sheet = new Sheet(); | ||
const proxy = new SheetProxy(sheet); | ||
|
||
const a = proxy.new("foo"); | ||
const b = a.map((s) => s.length); | ||
const c = copy(proxy, b); | ||
|
||
await expect(c.get()).resolves.toBe(3); | ||
}); | ||
|
||
test("copy cell async", async () => { | ||
const sheet = new Sheet(); | ||
const proxy = new SheetProxy(sheet); | ||
|
||
const a = proxy.new("foo"); | ||
const b = a.map((s) => delayed(s.length, 10)); | ||
const c = copy(proxy, b); | ||
|
||
await expect(c.get()).resolves.toBe(3); | ||
}); | ||
|
||
test("copy cell error", async () => { | ||
const sheet = new Sheet(); | ||
const proxy = new SheetProxy(sheet); | ||
|
||
const err = new Error("error"); | ||
|
||
const a = proxy.new("foo"); | ||
const b = a.map((_s) => { | ||
throw err; | ||
}); | ||
const c = copy(proxy, b); | ||
await expect(c.get()).resolves.toBe(err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { AnyCell, ValueCell } from "./cell"; | ||
import type { SheetProxy } from "./proxy"; | ||
|
||
// Copy any Cell as a ValueCell. | ||
export const copy = <T>( | ||
proxy: SheetProxy, | ||
cell: AnyCell<T>, | ||
name = `copy:${cell.id}` | ||
): ValueCell<T> => { | ||
return proxy.new( | ||
new Promise((resolve, reject) => { | ||
cell.get().then((v) => (v instanceof Error ? reject(v) : resolve(v))); | ||
}), | ||
name | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters