oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package store
2
3import "gorm.io/gorm"
4
5// Guard runs one write inside that write's own transaction, and may
6// refuse it: before the write, after it, or both. A refusal rolls the
7// transaction back. The app's team mode passes teammode.Guard here, so a
8// person's roster standing is checked inside the SQLite writer
9// transaction that commits their edit, not only in the handler before it
10// (Home implementation/teammode-package.md §3, invariant 13). A Guard
11// must not wait on anything that needs the writer: teammode's never asks
12// the roster, it reads the lease the request already holds.
13//
14// A nil Guard is no guard, and every write takes exactly the path it
15// took before guards existed.
16type Guard func(write func() error) error
17
18// Run is write under g, or write alone when there is no guard.
19func (g Guard) Run(write func() error) error {
20 if g == nil {
21 return write()
22 }
23 return g(write)
24}
25
26// Writer is a Handle whose writes run under a Guard. Reads, and the
27// row and column allocation (EnsureRow, EnsureCol), stay the Handle's:
28// an empty grid coordinate is no edit, and the write that uses it is
29// guarded.
30type Writer struct {
31 h *Handle
32 g Guard
33}
34
35// Guarded is h with every write below run under g.
36func (h *Handle) Guarded(g Guard) Writer { return Writer{h: h, g: g} }
37
38// SetReport is Handle.SetReport under the guard. Only the transaction
39// that commits the input is guarded; the recalculation that follows
40// persists derived values, which are nobody's edit, and Open recovers
41// them if they are missing.
42func (w Writer) SetReport(rowID, colID uint64, raw string, actor uint64) (Report, error) {
43 return w.h.setReport(w.g, rowID, colID, raw, actor)
44}
45
46// SetBatch is Handle.SetBatch under the guard: the batch's one
47// transaction.
48func (w Writer) SetBatch(writes []CellWrite, actor uint64) (Report, error) {
49 return w.h.setBatch(w.g, writes, actor)
50}
51
52// SetFormat is Handle.SetFormat under the guard, in one transaction.
53func (w Writer) SetFormat(rowID, colID uint64, f Format) error {
54 return w.h.setFormat(w.g, rowID, colID, f)
55}
56
57// SetColWidth is Handle.SetColWidth under the guard, in one transaction.
58func (w Writer) SetColWidth(colID uint64, width int) error {
59 return w.h.setColWidth(w.g, colID, width)
60}
61
62// SetRowHeight is Handle.SetRowHeight under the guard, in one transaction.
63func (w Writer) SetRowHeight(rowID uint64, height int) error {
64 return w.h.setRowHeight(w.g, rowID, height)
65}
66
67// guardedDB runs op against h.db exactly as it always has when g is nil,
68// and otherwise against one transaction, inside g. For writes that had no
69// transaction of their own.
70func (h *Handle) guardedDB(g Guard, op func(db *gorm.DB) error) error {
71 if g == nil {
72 return op(h.db)
73 }
74 return h.db.Transaction(func(tx *gorm.DB) error {
75 return g(func() error { return op(tx) })
76 })
77}
78