-
Notifications
You must be signed in to change notification settings - Fork 9
/
Transaction.ts
220 lines (143 loc) · 5.9 KB
/
Transaction.ts
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
import { serializable, exclude, SerializableCustom as Serializable } from "typescript-serializable-mixin/index.js"
import { AnyConstructor } from "../../class/Mixin.js"
import { LeveledQueue } from "../../util/LeveledQueue2.js"
import { Atom } from "../atom/Atom.js"
import { Quark } from "../atom/Quark.js"
import { Immutable, Owner } from "../data/Immutable.js"
import { RejectEffect } from "../Effect.js"
import { ChronoGraph } from "./Graph.js"
import { Iteration, IterationStorage, IterationStorageShredding } from "./Iteration.js"
let transactionIdSequence : number = 0
//----------------------------------------------------------------------------------------------------------------------
@serializable({ id : 'Transaction', mode : 'optOut' })
export class Transaction extends Serializable.derive(Owner) implements Immutable {
propagationStartDate : number = 0
lastProgressNotificationDate : number = 0
startProgressNotificationsAfterMs : number = 500
emitProgressNotificationsEveryMs : number = 200
// TODO auto-adjust this parameter to match the emitProgressNotificationsEveryMs (to avoid calls to time functions)
emitProgressNotificationsEveryCalculations : number = 100
plannedTotalIdentifiersToCalculate : number = 0
@exclude()
iterationClass : typeof Iteration = Iteration
name : string = `transaction#${transactionIdSequence++}`
rejectedWith : RejectEffect<unknown> = undefined
//region Transaction as Owner
$immutable : Iteration = undefined
// @ts-ignore
get immutable () : Iteration {
if (this.$immutable !== undefined) return this.$immutable
return this.$immutable = this.buildImmutable()
}
set immutable (immutable : Iteration) {
this.$immutable = immutable
if (immutable) immutable.owner = this
}
buildImmutable () : Iteration {
// return this.previous ? this.previous.immutable.createNext(this) : this.iterationClass.new({
// owner : this,
// storage : this.owner.historyLimit >= 0 ? new IterationStorageShredding() : new IterationStorage()
// })
return this.iterationClass.new({
owner : this,
previous : this.previous ? this.previous.immutable : undefined
})
}
setCurrent (immutable : Iteration) {
if (this.$immutable && immutable.previous !== this.immutable) throw new Error("Invalid state thread")
if (this.frozen) {
const next = this.createNext()
next.immutable = immutable
this.owner.setCurrent(next)
} else {
this.immutable = immutable
}
}
//endregion
//region transaction as Immutable
owner : ChronoGraph = undefined
previous : this = undefined
frozen : boolean = false
createNext (owner? : ChronoGraph) : this {
this.freeze()
const self = this.constructor as AnyConstructor<this, typeof Transaction>
const next = self.new()
next.previous = this
next.owner = owner || this.owner
next.immutable.previous = this.immutable
return next
}
freeze () {
if (this.frozen) return
this.immutable.freeze()
this.frozen = true
}
//endregion
get graph () : ChronoGraph {
return this.owner
}
get iteration () : Iteration {
return this.immutable
}
startCommit (stack : LeveledQueue<Atom>) {
this.propagationStartDate = Date.now()
this.plannedTotalIdentifiersToCalculate = stack.size
}
getLastIteration () : Iteration {
let iteration = this.immutable
const stopAt = this.previous ? this.previous.immutable : undefined
while (iteration) {
const previous = iteration.previous
if (previous === stopAt) break
iteration = previous
}
return iteration
}
forEveryIteration (func : (iteration : Iteration) => any) {
let iteration = this.immutable
const stopAt = this.previous ? this.previous.immutable : undefined
while (iteration && iteration !== stopAt) {
func(iteration)
iteration = iteration.previous
}
}
forEveryFirstQuark (func : (quark : Quark) => any) {
this.immutable.forEveryFirstQuarkTill(this.previous ? this.previous.immutable : undefined, func)
}
reject (rejectEffect : RejectEffect<unknown>) {
this.rejectedWith = rejectEffect
this.forEveryIteration(iteration => iteration.isRejected = true)
}
mark (reachable : boolean) {
this.forEveryIteration(iteration => iteration.mark(reachable))
}
unmark (reachable : boolean) {
this.forEveryIteration(iteration => iteration.unmark(reachable))
}
immutableForWrite () : this[ 'immutable' ] {
if (this.frozen) {
const next = this.createNext()
this.owner.setCurrent(next)
return next.immutable
}
if (this.immutable.frozen) this.setCurrent(this.immutable.createNext())
return this.immutable
}
addQuark (quark : Quark) {
this.immutableForWrite().addQuark(quark)
}
addAtom (atom : Atom) {
this.immutableForWrite().addAtom(atom)
}
static new<T extends typeof Transaction> (this : T, props? : Partial<InstanceType<T>>) : InstanceType<T> {
const instance = new this()
Object.assign(instance, props)
return instance as InstanceType<T>
}
}
// export const ZeroTransaction = new Transaction()
//
// ZeroTransaction.immutable = ZeroIteration
// ZeroIteration.owner = ZeroTransaction
//
// ZeroTransaction.freeze()