-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgrow-only-value-set-component-definition.ts
More file actions
238 lines (222 loc) · 7.68 KB
/
Copy pathgrow-only-value-set-component-definition.ts
File metadata and controls
238 lines (222 loc) · 7.68 KB
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
import { ISchema } from '../schemas'
import { ReadWriteByteBuffer } from '../serialization/ByteBuffer'
import {
AppendValueMessageBody,
AppendValueOperation,
CrdtMessageType,
ProcessMessageResultType
} from '../serialization/crdt'
import { ComponentType, GrowOnlyValueSetComponentDefinition, ValidateCallback } from './component'
import { __DEV__ } from '../runtime/invariant'
import { Entity } from './entity'
import { DeepReadonly, DeepReadonlySet } from './readonly'
const emptyReadonlySet = freezeSet(new Set())
const __GLOBAL_ENTITY = '__GLOBAL_ENTITY' as any as Entity
function frozenError() {
throw new Error('The set is frozen')
}
function freezeSet<T>(set: Set<T>): ReadonlySet<T> {
;(set as any).add = frozenError
;(set as any).clear = frozenError
return set as ReadonlySet<T>
}
function sortByTimestamp(a: { timestamp: number }, b: { timestamp: number }) {
return a.timestamp - b.timestamp
}
/**
* @public
*/
export type ValueSetOptions<T> = {
// function that returns a timestamp from the value
timestampFunction: (value: DeepReadonly<T>) => number
// max elements to store in memory, ordered by timestamp
maxElements: number
}
/**
* @internal
*/
export function createValueSetComponentDefinitionFromSchema<T>(
componentName: string,
componentId: number,
schema: ISchema<T>,
options: ValueSetOptions<T>
): GrowOnlyValueSetComponentDefinition<T> {
type InternalDatastructure = {
raw: Array<{ value: DeepReadonly<T>; timestamp: number }>
frozenSet: DeepReadonlySet<T>
}
const data = new Map<Entity, InternalDatastructure>()
const dirtyIterator = new Set<Entity>()
const queuedCommands: AppendValueMessageBody[] = []
const onChangeCallbacks = new Map<Entity, ((data: T | undefined) => void)[]>()
const validateCallbacks = new Map<Entity, ValidateCallback<T>>()
// only sort the array if the latest (N) element has a timestamp <= N-1
function shouldSort(row: InternalDatastructure) {
const len = row.raw.length
if (len > 1 && row.raw[len - 1].timestamp <= row.raw[len - 2].timestamp) {
return true
}
return false
}
function gotUpdated(entity: Entity): DeepReadonlySet<T> {
const row = data.get(entity)
/* istanbul ignore else */
if (row) {
if (shouldSort(row)) {
row.raw.sort(sortByTimestamp)
}
while (row.raw.length > options.maxElements) {
row.raw.shift()
}
const frozenSet: DeepReadonlySet<T> = freezeSet(new Set(row?.raw.map(($) => $.value)))
row.frozenSet = frozenSet
return frozenSet
} else {
/* istanbul ignore next */
return emptyReadonlySet as any
}
}
function append(entity: Entity, value: DeepReadonly<T>) {
let row = data.get(entity)
if (!row) {
row = { raw: [], frozenSet: emptyReadonlySet as any }
data.set(entity, row)
}
const usedValue = schema.extend ? (schema.extend(value) as DeepReadonly<T>) : value
const timestamp = options.timestampFunction(usedValue as any)
if (__DEV__) {
// only freeze the objects in dev mode to warn the developers because
// it is an expensive operation
Object.freeze(usedValue)
}
row.raw.push({ value: usedValue, timestamp })
return { set: gotUpdated(entity), value: usedValue }
}
const ret: GrowOnlyValueSetComponentDefinition<T> = {
get componentId() {
return componentId
},
get componentName() {
return componentName
},
get componentType() {
// a getter is used here to prevent accidental changes
return ComponentType.GrowOnlyValueSet as const
},
schema,
has(entity: Entity): boolean {
return data.has(entity)
},
entityDeleted(entity: Entity, markAsDirty: boolean): void {
data.delete(entity)
if (markAsDirty) {
// For grow-only sets, we don't need to mark as dirty since deletion doesn't generate CRDT messages
}
},
get(entity: Entity): DeepReadonlySet<T> {
const values = data.get(entity)
if (values) {
return values.frozenSet
} else {
return emptyReadonlySet as any
}
},
addValue(entity: Entity, rawValue: DeepReadonly<T>) {
const { set, value } = append(entity, rawValue)
dirtyIterator.add(entity)
const buf = new ReadWriteByteBuffer()
schema.serialize(value, buf)
queuedCommands.push({
componentId,
data: buf.toBinary(),
entityId: entity,
timestamp: 0,
type: CrdtMessageType.APPEND_VALUE
})
return set
},
*iterator(): Iterable<[Entity, Iterable<DeepReadonly<T>>]> {
for (const [entity, component] of data) {
yield [entity, component.frozenSet]
}
},
*dirtyIterator(): Iterable<Entity> {
for (const entity of dirtyIterator) {
yield entity
}
},
getCrdtUpdates() {
// return a copy of the commands, and then clear the local copy
dirtyIterator.clear()
return queuedCommands.splice(0, queuedCommands.length)
},
updateFromCrdt(_body) {
if (_body.type === CrdtMessageType.APPEND_VALUE) {
const buf = new ReadWriteByteBuffer(_body.data)
const { value } = append(_body.entityId, schema.deserialize(buf) as DeepReadonly<T>)
return [null, value as T]
}
return [null, undefined]
},
dumpCrdtStateToBuffer: function (buffer, filterEntity): void {
for (const [entity, { raw }] of data) {
if (filterEntity && !filterEntity(entity)) continue
for (const it of raw) {
const buf = new ReadWriteByteBuffer()
schema.serialize(it.value, buf)
AppendValueOperation.write(entity, 0, componentId, buf.toBinary(), buffer)
}
}
},
onChange(entity, cb) {
const cbs = onChangeCallbacks.get(entity) ?? []
cbs.push(cb)
onChangeCallbacks.set(entity, cbs)
},
__onChangeCallbacks(entity, value) {
const cbs = onChangeCallbacks.get(entity)
if (!cbs) return
for (const cb of cbs) {
cb(value)
}
},
__dry_run_updateFromCrdt(_body) {
return ProcessMessageResultType.StateUpdatedData
},
validateBeforeChange(entityOrCb: Entity | ValidateCallback<T>, cb?: ValidateCallback<T>): void {
if (arguments.length === 1) {
// Second overload: just callback (global validation)
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb as ValidateCallback<T>)
} else {
if (cb) {
validateCallbacks.set(entityOrCb as Entity, cb)
}
}
},
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy): boolean {
const cb = entity && validateCallbacks.get(entity)
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY)
const currentValue = [...this.get(entity).values()]
const value = { entity, currentValue: currentValue as T, newValue, senderAddress, createdBy }
const globalResult = globalCb?.(value) ?? true
const entityResult = (globalResult && cb?.(value)) ?? true
return globalResult && entityResult
},
getCrdtState(entity: Entity): { data: Uint8Array; timestamp: number } | null {
const row = data.get(entity)
if (!row || row.raw.length === 0) {
return null
}
// For GrowOnlySet, we need to return the complete CRDT messages for all values
// This is complex because GrowOnlySet uses APPEND messages, not a single PUT
// For now, return null to indicate this component type doesn't support simple corrections
return null
},
__forceUpdateFromCrdt(_msg) {
// GrowOnlySet doesn't support authoritative corrections in the same way as LWW
// since it uses APPEND_VALUE messages instead of PUT_COMPONENT messages
return [null, undefined]
}
}
return ret
}