Skip to content

Commit 6811564

Browse files
committed
refactor(table-helper): enhance observer callbacks to include Y.Transaction
1 parent 5906f8f commit 6811564

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

packages/epicenter/src/core/db/table-helper.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export type UpdateResult =
7676
*/
7777
export type UpdateManyResult =
7878
| { status: 'all_applied'; applied: string[] }
79-
| { status: 'partially_applied'; applied: string[]; notFoundLocally: string[] }
79+
| {
80+
status: 'partially_applied';
81+
applied: string[];
82+
notFoundLocally: string[];
83+
}
8084
| { status: 'none_applied'; notFoundLocally: string[] };
8185

8286
/**
@@ -99,7 +103,11 @@ export type DeleteResult =
99103
*/
100104
export type DeleteManyResult =
101105
| { status: 'all_deleted'; deleted: string[] }
102-
| { status: 'partially_deleted'; deleted: string[]; notFoundLocally: string[] }
106+
| {
107+
status: 'partially_deleted';
108+
deleted: string[];
109+
notFoundLocally: string[];
110+
}
103111
| { status: 'none_deleted'; notFoundLocally: string[] };
104112

105113
/**
@@ -605,21 +613,21 @@ function createTableHelper<TTableSchema extends TableSchema>({
605613
*
606614
* @example
607615
* const unsubscribe = table.observe({
608-
* onAdd: (result) => {
616+
* onAdd: (result, transaction) => {
609617
* if (result.error) {
610618
* console.error('Invalid row:', result.error);
611619
* return;
612620
* }
613-
* console.log('New row:', result.data);
621+
* console.log('New row:', result.data, 'origin:', transaction.origin);
614622
* },
615-
* onUpdate: (result) => {
623+
* onUpdate: (result, transaction) => {
616624
* if (result.error) {
617625
* console.error('Invalid row:', result.error);
618626
* return;
619627
* }
620-
* console.log('Row changed:', result.data);
628+
* console.log('Row changed:', result.data, 'origin:', transaction.origin);
621629
* },
622-
* onDelete: (id) => console.log('Row removed:', id),
630+
* onDelete: (id, transaction) => console.log('Row removed:', id, 'origin:', transaction.origin),
623631
* });
624632
*
625633
* // Later, stop watching
@@ -729,23 +737,31 @@ function createTableHelper<TTableSchema extends TableSchema>({
729737
* rather than silently skipping them.
730738
*
731739
* @param callbacks Object with optional callbacks for row lifecycle events
732-
* @param callbacks.onAdd Called when a new row is added (receives Result with row or validation errors)
733-
* @param callbacks.onUpdate Called when any field within an existing row changes (receives Result with row or validation errors)
734-
* @param callbacks.onDelete Called when a row is removed (receives row ID only)
740+
* @param callbacks.onAdd Called when a new row is added (receives Result with row or validation errors, and the Y.Transaction)
741+
* @param callbacks.onUpdate Called when any field within an existing row changes (receives Result with row or validation errors, and the Y.Transaction)
742+
* @param callbacks.onDelete Called when a row is removed (receives row ID and the Y.Transaction)
735743
* @returns Unsubscribe function to stop observing changes
736744
*/
737745
observe(callbacks: {
738746
onAdd?: (
739747
result: Result<TRow, RowValidationError>,
748+
transaction: Y.Transaction,
740749
) => void | Promise<void>;
741750
onUpdate?: (
742751
result: Result<TRow, RowValidationError>,
752+
transaction: Y.Transaction,
753+
) => void | Promise<void>;
754+
onDelete?: (
755+
id: string,
756+
transaction: Y.Transaction,
743757
) => void | Promise<void>;
744-
onDelete?: (id: string) => void | Promise<void>;
745758
}): () => void {
746759
const yjsValidator = validators.toYjsArktype();
747760

748-
const observer = (events: Y.YEvent<Y.Map<YRow> | YRow>[]) => {
761+
const observer = (
762+
events: Y.YEvent<Y.Map<YRow> | YRow>[],
763+
transaction: Y.Transaction,
764+
) => {
749765
for (const event of events) {
750766
// Top-level events: row additions/deletions in the table
751767
// event.target === getYTable() means the change happened directly on the table Y.Map
@@ -769,14 +785,15 @@ function createTableHelper<TTableSchema extends TableSchema>({
769785
summary: result.summary,
770786
},
771787
}),
788+
transaction,
772789
);
773790
} else {
774-
callbacks.onAdd?.(Ok(row));
791+
callbacks.onAdd?.(Ok(row), transaction);
775792
}
776793
}
777794
} else if (change.action === 'delete') {
778795
// A row Y.Map was removed from the table
779-
callbacks.onDelete?.(key);
796+
callbacks.onDelete?.(key, transaction);
780797
}
781798
// Note: We intentionally don't handle 'update' here because:
782799
// - 'update' only fires if an entire row Y.Map is replaced with another Y.Map
@@ -805,9 +822,10 @@ function createTableHelper<TTableSchema extends TableSchema>({
805822
summary: result.summary,
806823
},
807824
}),
825+
transaction,
808826
);
809827
} else {
810-
callbacks.onUpdate?.(Ok(row));
828+
callbacks.onUpdate?.(Ok(row), transaction);
811829
}
812830
}
813831
}

0 commit comments

Comments
 (0)