Skip to content

@jsvision/datagrid / commitCell

Function: commitCell()

commitCell<T, V>(args): Promise<{ committed: boolean; value: V; }>

Defined in: datagrid/src/commit.ts:134

Apply a cell edit immediately, run the optional beforeSave then onCommit veto gates, and revert on veto.

The write happens first (apply(row, columnId, next)), so the record and grid reflect the edit at once. Then the two post-apply gates run in order: beforeSave decides whether to proceed, and onCommit accepts/persists. Each is awaited; a false or a rejected promise from either reverts the record with apply(row, columnId, previous) through the one shared revert path. A beforeSave veto short-circuits — onCommit is never called. With neither gate the commit succeeds. This is a single round-trip — serializing overlapping in-flight commits is a caller concern.

Type Parameters

T

T

V

V

Parameters

args

The edit: the row, columnId, rowKey, the previous/next values, an apply writer that mutates the record, an optional beforeSave gate, and an optional onCommit sink.

apply

(row, columnId, v) => void

beforeSave?

BeforeSave<T>

columnId

string

next

V

onCommit?

OnCommit<T>

previous

V

row

T

rowKey

string | number

Returns

Promise<{ committed: boolean; value: V; }>

{ committed, value } — whether the edit stands and the value now in the record.

Example

ts
import { commitCell } from '@jsvision/datagrid';
const row = { balance: 1 };
const apply = (r: typeof row, _col: string, v: number) => { r.balance = v; };
const ledger: number[] = []; // stands in for the real persistence layer (a database write, an API call)
const res = await commitCell({
  row, columnId: 'balance', rowKey: 1, previous: 1, next: 2, apply,
  // beforeSave/onCommit see `value` as `unknown` (it is not typed by the call's inferred V), so a
  // gate narrows it before using it — the same guard the grid's own examples use.
  beforeSave: (c) => typeof c.value === 'number' && c.value >= 0, // gate: refuse negative balances
  onCommit: (c) => {
    if (typeof c.value !== 'number') return false;
    ledger.push(c.value); // authoritative persistence
    return true;
  },
});
res.committed; // true — row.balance is now 2