github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/change-counter.go (about)

     1  package vugu
     2  
     3  // ChangeCounter is a number that can be incremented to indicate modification.
     4  // The ModCheck method is implemented using this number.
     5  // A ChangeCounter can be used directly or embedded in a struct, with its
     6  // methods calling Changed on each mutating operation, in order to
     7  // track its modification.
     8  type ChangeCounter int
     9  
    10  // Changed increments the counter to indicate a modification.
    11  func (c *ChangeCounter) Changed() {
    12  	*c++
    13  }
    14  
    15  // ModCheck implements the ModChecker interface.
    16  func (c *ChangeCounter) ModCheck(mt *ModTracker, oldData interface{}) (isModified bool, newData interface{}) {
    17  	oldn, ok := oldData.(ChangeCounter)
    18  	return (!ok) || (oldn != *c), *c
    19  }