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
1 change: 1 addition & 0 deletions packages/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Cell<T = any> {
push(...value: T extends (infer U)[] ? U[] : never): void;
equals(other: Cell<any>): boolean;
key<K extends keyof T>(valueKey: K): Cell<T[K]>;
resolveAsCell(): Cell<T>;
}

// Cell type with only public methods
Expand Down
14 changes: 12 additions & 2 deletions packages/runner/src/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ import { ContextualFlowControl } from "./cfc.ts";
* @returns {void}
*
* @method push Adds an item to the end of an array cell.
* @param {U | Cell<U>} value - The value to add, where U is
* the array element type.
* @param {U | Cell<U>} value - The value to add, where U is the array element
* type.
* @returns {void}
*
* @method equals Compares two cells for equality.
Expand Down Expand Up @@ -102,6 +102,10 @@ import { ContextualFlowControl } from "./cfc.ts";
* @method sync Syncs the cell to the storage.
* @returns {Promise<void>}
*
* @method resolveAsCell Resolves the cell to a new cell with the resolved link.
* Equivalent to `cell.getAsSchema(<current schema wrapped in Cell<>).get()`
* @returns {Cell<T>}
*
* @method getAsQueryResult Returns a query result for the cell.
* @param {Path} path - The optional path to follow.
* @param {ReactivityLog} log - Optional reactivity log.
Expand Down Expand Up @@ -197,6 +201,7 @@ declare module "@commontools/api" {
withTx(tx?: IExtendedStorageTransaction): Cell<T>;
sink(callback: (value: Readonly<T>) => Cancel | undefined | void): Cancel;
sync(): Promise<Cell<T>> | Cell<T>;
resolveAsCell(): Cell<T>;
getAsQueryResult<Path extends PropertyKey[]>(
path?: Readonly<Path>,
tx?: IExtendedStorageTransaction,
Expand Down Expand Up @@ -646,6 +651,11 @@ export class RegularCell<T> implements Cell<T> {
return this.runtime.storageManager.syncCell<T>(this);
}

resolveAsCell(): Cell<T> {
const link = resolveLink(this.runtime.readTx(this.tx), this.link);
return createCell(this.runtime, link, this.tx, true, this.synced);
}

getAsQueryResult<Path extends PropertyKey[]>(
path?: Readonly<Path>,
tx?: IExtendedStorageTransaction,
Expand Down
29 changes: 29 additions & 0 deletions packages/runner/test/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3642,4 +3642,33 @@ describe("Cell success callbacks", () => {
expect(link0.id).not.toBe(link1.id);
});
});

describe("resolveAsCell", () => {
it("should resolve a cell reference to the actual cell", () => {
const innerCell = runtime.getCell<number>(
space,
"inner-cell",
{ type: "number" },
tx,
);
innerCell.set(42);

const outerCell = runtime.getCell<{ inner: unknown }>(
space,
"outer-cell",
{
type: "object",
properties: {
inner: { type: "number" },
},
},
tx,
);
outerCell.set({ inner: innerCell });

const resolvedCell = outerCell.key("inner").resolveAsCell();

expect(resolvedCell.equals(innerCell)).toBe(true);
});
});
});
1 change: 1 addition & 0 deletions packages/static/assets/types/commontools.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Cell<T = any> {
push(...value: T extends (infer U)[] ? U[] : never): void;
equals(other: Cell<any>): boolean;
key<K extends keyof T>(valueKey: K): Cell<T[K]>;
resolveAsCell(): Cell<T>;
}
export interface Stream<T> {
send(event: T): void;
Expand Down