Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose players components (ADR-245) and set deprecation apis #743

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/@dcl/ecs/src/engine/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ export interface LastWriteWinElementSetComponentDefinition<T> extends BaseCompon
* @param val - The initial value if it doesn't exist
*/
getOrCreateMutable(entity: Entity, initialValue?: T): T

/**
* @internal
* @returns a set with entities changed
*/
getEntitiesUpdatedFromCrdt(): Set<Entity>
}
/**
* @public
Expand All @@ -190,7 +196,7 @@ export interface GrowOnlyValueSetComponentDefinition<T> extends BaseComponent<T>
* @param val - The final value. The Set will freeze the value, it won't be editable from
* the script.
*/
addValue(entity: Entity, val: DeepReadonly<T>): DeepReadonlySet<T>
addValue(entity: Entity, val: DeepReadonly<T>, ts: number): DeepReadonlySet<T>

/**
* Get the readonly component of the entity (to mutate it, use getMutable instead),
Expand All @@ -199,6 +205,12 @@ export interface GrowOnlyValueSetComponentDefinition<T> extends BaseComponent<T>
* @returns
*/
get(entity: Entity): DeepReadonlySet<T>

/**
* @internal
* @returns a map with entities as key and how many values was appended to the set as value
*/
getEntitiesUpdatedFromCrdt(): Map<Entity, number>
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
export type ValueSetOptions<T> = {
// function that returns a timestamp from the value
timestampFunction: (value: DeepReadonly<T>) => number
timestampFunction?: (value: DeepReadonly<T>) => number
// max elements to store in memory, ordered by timestamp
maxElements: number
}
Expand All @@ -48,6 +48,7 @@
}
const data = new Map<Entity, InternalDatastructure>()
const dirtyIterator = new Set<Entity>()
const updatedFromCrdtIterator = new Map<Entity, number>()
const queuedCommands: AppendValueMessageBody[] = []

// only sort the array if the latest (N) element has a timestamp <= N-1
Expand Down Expand Up @@ -78,14 +79,14 @@
}
}

function append(entity: Entity, value: DeepReadonly<T>) {
function append(entity: Entity, value: DeepReadonly<T>, ts: number) {

Check warning on line 82 in packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts#L82

Added line #L82 was not covered by tests
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)
const timestamp = options.timestampFunction ? options.timestampFunction(usedValue as any) : ts
if (__DEV__) {
// only freeze the objects in dev mode to warn the developers because
// it is an expensive operation
Expand Down Expand Up @@ -121,8 +122,8 @@
return emptyReadonlySet as any
}
},
addValue(entity: Entity, rawValue: DeepReadonly<T>) {
const { set, value } = append(entity, rawValue)
addValue(entity: Entity, rawValue: DeepReadonly<T>, ts: number) {
const { set, value } = append(entity, rawValue, ts)

Check warning on line 126 in packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts#L125-L126

Added lines #L125 - L126 were not covered by tests
dirtyIterator.add(entity)
const buf = new ReadWriteByteBuffer()
schema.serialize(value, buf)
Expand All @@ -148,12 +149,15 @@
getCrdtUpdates() {
// return a copy of the commands, and then clear the local copy
dirtyIterator.clear()
updatedFromCrdtIterator.clear()
return queuedCommands.splice(0, queuedCommands.length)
},
updateFromCrdt(_body) {
if (_body.type === CrdtMessageType.APPEND_VALUE) {
const buf = new ReadWriteByteBuffer(_body.data)
append(_body.entityId, schema.deserialize(buf) as DeepReadonly<T>)
append(_body.entityId, schema.deserialize(buf) as DeepReadonly<T>, _body.timestamp)

Check warning on line 158 in packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts#L158

Added line #L158 was not covered by tests

updatedFromCrdtIterator.set(_body.entityId, (updatedFromCrdtIterator.get(_body.entityId) || 0) + 1)
}
return [null, undefined]
},
Expand All @@ -166,6 +170,9 @@
AppendValueOperation.write(entity, 0, componentId, buf.toBinary(), buffer)
}
}
},
getEntitiesUpdatedFromCrdt(): Map<Entity, number> {
return updatedFromCrdtIterator

Check warning on line 175 in packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/engine/grow-only-value-set-component-definition.ts#L174-L175

Added lines #L174 - L175 were not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
componentId: number,
timestamps: Map<Entity, number>,
schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>,
data: Map<Entity, unknown>
data: Map<Entity, unknown>,
updatedFromCrdtIterator: Set<Entity>
) {
/**
* Process the received message only if the lamport number recieved is higher
Expand Down Expand Up @@ -126,6 +127,7 @@
} else {
data.delete(entity)
}
updatedFromCrdtIterator.add(entity)

return [null, data.get(entity)]
}
Expand Down Expand Up @@ -168,7 +170,8 @@
timestamps: Map<Entity, number>,
dirtyIterator: Set<Entity>,
schema: Pick<ISchema<any>, 'serialize'>,
data: Map<Entity, unknown>
data: Map<Entity, unknown>,
updatedFromCrdtIterator: Set<Entity>
) {
return function* () {
for (const entity of dirtyIterator) {
Expand Down Expand Up @@ -198,6 +201,7 @@
}
}
dirtyIterator.clear()
updatedFromCrdtIterator.clear()
}
}

Expand All @@ -211,6 +215,7 @@
): LastWriteWinElementSetComponentDefinition<T> {
const data = new Map<Entity, T>()
const dirtyIterator = new Set<Entity>()
const updatedFromCrdtIterator = new Set<Entity>()
const timestamps = new Map<Entity, number>()

return {
Expand Down Expand Up @@ -303,8 +308,18 @@
yield entity
}
},
getCrdtUpdates: createGetCrdtMessagesForLww(componentId, timestamps, dirtyIterator, schema, data),
updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data),
getEntitiesUpdatedFromCrdt(): Set<Entity> {
return updatedFromCrdtIterator

Check warning on line 312 in packages/@dcl/ecs/src/engine/lww-element-set-component-definition.ts

View check run for this annotation

Codecov / codecov/patch

packages/@dcl/ecs/src/engine/lww-element-set-component-definition.ts#L311-L312

Added lines #L311 - L312 were not covered by tests
},
getCrdtUpdates: createGetCrdtMessagesForLww(
componentId,
timestamps,
dirtyIterator,
schema,
data,
updatedFromCrdtIterator
),
updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data, updatedFromCrdtIterator),
dumpCrdtStateToBuffer: createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data)
}
}
Loading
Loading