-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
history.go
331 lines (263 loc) · 6.97 KB
/
history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package core
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/inoxlang/inox/internal/commonfmt"
"github.com/inoxlang/inox/internal/core/symbolic"
"github.com/inoxlang/inox/internal/utils"
)
const (
DEFAULT_MAX_HISTORY_LEN = 5
)
var (
VALUE_HISTORY_PROPNAMES = []string{"value_at", "forget_last", "last-value", "selected-datetime", "value-at-selection"}
)
func init() {
RegisterSymbolicGoFunction(NewValueHistory, func(ctx *symbolic.Context, v symbolic.InMemorySnapshotable, config *symbolic.Object) *symbolic.ValueHistory {
//TODO: check config
return symbolic.NewValueHistory()
})
}
// ValueHistory stores the history about a single value, it implements Value.
type ValueHistory struct {
startValue *Snapshot
changes []Change
start DateTime
selectedDate DateTime
providedSelection bool // if no date is provided the selected date is the current date
lock sync.Mutex
shared atomic.Bool
maxItemCount int
renderCurrentToHTMLFn *InoxFunction // can be nil
}
func NewValueHistory(ctx *Context, v InMemorySnapshotable, config *Object) *ValueHistory {
current := v
history := &ValueHistory{
maxItemCount: DEFAULT_MAX_HISTORY_LEN,
}
config.ForEachEntry(func(k string, v Serializable) error {
switch k {
case "max-length":
history.maxItemCount = int(v.(Int))
case "render":
history.renderCurrentToHTMLFn = v.(*InoxFunction)
default:
panic(commonfmt.FmtUnexpectedPropInArgX(k, "config"))
}
return nil
})
var handle CallbackHandle
registerMutationCallback := func(ctx *Context) {
var err error
handle, err = current.OnMutation(ctx, func(ctx *Context, mutation Mutation) (registerAgain bool) {
registerAgain = true
history.AddChange(ctx, NewChange(mutation, DateTime(time.Now())))
return
}, MutationWatchingConfiguration{
Depth: ShallowWatching,
})
if err != nil {
panic(err)
}
}
if dyn, ok := v.(*DynamicValue); ok {
current = dyn.Resolve(ctx).(InMemorySnapshotable)
dyn.OnMutation(ctx, func(ctx *Context, mutation Mutation) (registerAgain bool) {
registerAgain = true
newVal := dyn.Resolve(ctx).(InMemorySnapshotable)
if current != newVal {
if handle.Valid() {
current.RemoveMutationCallback(ctx, handle)
}
current = newVal
registerMutationCallback(ctx)
}
return
}, MutationWatchingConfiguration{Depth: ShallowWatching})
}
history.startValue = utils.Must(TakeSnapshot(ctx, current, false))
registerMutationCallback(ctx)
return history
}
func (h *ValueHistory) RenderCurrentToHTMLFn() *InoxFunction {
return h.renderCurrentToHTMLFn
}
func (h *ValueHistory) AddChange(ctx *Context, c Change) {
h.lock.Lock()
defer h.lock.Unlock()
h.changes = append(h.changes, c)
if len(h.changes) == h.maxItemCount {
h.forgetChangesBeforeIndex(ctx, 1)
}
}
func (h *ValueHistory) ValueAt(ctx *Context, d DateTime) Value {
h.lock.Lock()
defer h.lock.Unlock()
v, err := h.startValue.InstantiateValue(ctx)
if err != nil {
panic(err)
}
index, ok := h.indexAtOrBeforeMoment(d)
if !ok {
return v
}
for i := 0; i <= index; i++ {
if err := h.changes[i].mutation.ApplyTo(ctx, v); err != nil {
panic(err)
}
}
return v
}
func (h *ValueHistory) LastValue(ctx *Context) Value {
h.lock.Lock()
defer h.lock.Unlock()
v, err := h.startValue.InstantiateValue(ctx)
if err != nil {
panic(err)
}
for _, c := range h.changes {
if err := c.mutation.ApplyTo(ctx, v); err != nil {
panic(err)
}
}
return v
}
func (h *ValueHistory) SelectDate(ctx *Context, d DateTime) {
h.lock.Lock()
defer h.lock.Unlock()
h.selectedDate = d
h.providedSelection = true
}
func (h *ValueHistory) ValueAtSelection(ctx *Context) Value {
if h.providedSelection {
return h.ValueAt(ctx, h.selectedDate)
}
return Nil
}
func (h *ValueHistory) ForgetChangesBeforeDate(ctx *Context, d DateTime) {
h.lock.Lock()
defer h.lock.Unlock()
index, ok := h.indexAtOrAfterMoment(d)
if !ok {
return
}
h.forgetChangesBeforeIndex(ctx, index)
}
func (h *ValueHistory) ForgetLast(ctx *Context) {
h.lock.Lock()
defer h.lock.Unlock()
if len(h.changes) == 0 {
return
}
newLength := len(h.changes) - 1
h.changes = h.changes[:newLength]
}
func (h *ValueHistory) forgetChangesBeforeIndex(ctx *Context, index int) {
if index == 0 {
return
}
newLength := len(h.changes) - index
for i := 0; i < index; i++ {
newSnap, err := h.startValue.WithChangeApplied(ctx, h.changes[i])
if err != nil {
panic(err)
}
h.startValue = newSnap
}
lastForgottenChange := h.changes[index]
copy(h.changes[0:], h.changes[index:])
h.changes = h.changes[:newLength]
h.start = lastForgottenChange.datetime
}
func (h *ValueHistory) indexAtOrAfterMoment(d DateTime) (int, bool) {
//TODO: use binary search
t := time.Time(d)
for i, c := range h.changes {
if time.Time(c.datetime).Equal(t) || time.Time(c.datetime).After(t) {
return i, true
}
}
return -1, false
}
func (h *ValueHistory) indexAtOrBeforeMoment(d DateTime) (int, bool) {
//TODO: use binary search
t := time.Time(d)
for i := len(h.changes) - 1; i >= 0; i-- {
c := h.changes[i]
if time.Time(c.datetime).Equal(t) || time.Time(c.datetime).Before(t) {
return i, true
}
}
return -1, false
}
func (h *ValueHistory) IsSharable(originState *GlobalState) (bool, string) {
if h.renderCurrentToHTMLFn == nil {
return true, ""
}
ok, expl := h.renderCurrentToHTMLFn.IsSharable(originState)
if ok {
return true, ""
}
return false, fmt.Sprintf("value history is not sharable because rendering fn is not sharable: %s", expl)
}
func (h *ValueHistory) Share(originState *GlobalState) {
if h.shared.CompareAndSwap(false, true) {
if h.renderCurrentToHTMLFn != nil {
h.renderCurrentToHTMLFn.Share(originState)
}
}
}
func (h *ValueHistory) IsShared() bool {
return h.shared.Load()
}
func (h *ValueHistory) SmartLock(state *GlobalState) {
}
func (h *ValueHistory) SmartUnlock(state *GlobalState) {
}
func (h *ValueHistory) GetGoMethod(name string) (*GoFunction, bool) {
switch name {
case "value_at":
return WrapGoMethod(h.ValueAt), true
case "forget_last":
return WrapGoMethod(h.ForgetLast), true
}
return nil, false
}
func (h *ValueHistory) Prop(ctx *Context, name string) Value {
switch name {
case "last-value":
return h.LastValue(ctx)
case "value-at-selection":
return h.ValueAtSelection(ctx)
case "selected-datetime":
h.lock.Lock()
defer h.lock.Unlock()
if h.providedSelection {
return h.selectedDate
}
return Nil
}
method, ok := h.GetGoMethod(name)
if !ok {
panic(FormatErrPropertyDoesNotExist(name, h))
}
return method
}
func (h *ValueHistory) SetProp(ctx *Context, name string, value Value) error {
switch name {
case "selected-datetime":
date, ok := value.(DateTime)
if !ok {
return commonfmt.FmtFailedToSetPropXAcceptXButZProvided(name, "datetime", fmt.Sprintf("%T", value))
}
h.SelectDate(ctx, date)
return nil
}
return ErrCannotSetProp
}
func (*ValueHistory) PropertyNames(ctx *Context) []string {
return VALUE_HISTORY_PROPNAMES
}
// -------------------------------