@dcl/ecs 7.20.1 → 7.20.2-22102228700.commit-61000fc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +5 -0
- package/dist/components/index.js +5 -2
- package/dist/components/manual/CreatedBy.d.ts +9 -0
- package/dist/components/manual/CreatedBy.js +8 -0
- package/dist/components/manual/Transform.d.ts +9 -0
- package/dist/components/manual/Transform.js +3 -3
- package/dist/components/types.d.ts +1 -0
- package/dist/engine/component.d.ts +52 -1
- package/dist/engine/grow-only-value-set-component-definition.js +45 -2
- package/dist/engine/lww-element-set-component-definition.d.ts +3 -1
- package/dist/engine/lww-element-set-component-definition.js +65 -12
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/serialization/crdt/authoritativePutComponent.d.ts +15 -0
- package/dist/serialization/crdt/authoritativePutComponent.js +47 -0
- package/dist/serialization/crdt/index.d.ts +1 -0
- package/dist/serialization/crdt/index.js +1 -0
- package/dist/serialization/crdt/types.d.ts +25 -3
- package/dist/serialization/crdt/types.js +3 -1
- package/dist/systems/crdt/index.d.ts +0 -1
- package/dist/systems/crdt/index.js +55 -146
- package/dist-cjs/components/index.d.ts +5 -0
- package/dist-cjs/components/index.js +7 -3
- package/dist-cjs/components/manual/CreatedBy.d.ts +9 -0
- package/dist-cjs/components/manual/CreatedBy.js +10 -0
- package/dist-cjs/components/manual/Transform.d.ts +9 -0
- package/dist-cjs/components/manual/Transform.js +3 -3
- package/dist-cjs/components/types.d.ts +1 -0
- package/dist-cjs/engine/component.d.ts +52 -1
- package/dist-cjs/engine/grow-only-value-set-component-definition.js +44 -1
- package/dist-cjs/engine/lww-element-set-component-definition.d.ts +3 -1
- package/dist-cjs/engine/lww-element-set-component-definition.js +68 -13
- package/dist-cjs/index.d.ts +2 -1
- package/dist-cjs/index.js +2 -1
- package/dist-cjs/serialization/crdt/authoritativePutComponent.d.ts +15 -0
- package/dist-cjs/serialization/crdt/authoritativePutComponent.js +50 -0
- package/dist-cjs/serialization/crdt/index.d.ts +1 -0
- package/dist-cjs/serialization/crdt/index.js +1 -0
- package/dist-cjs/serialization/crdt/types.d.ts +25 -3
- package/dist-cjs/serialization/crdt/types.js +3 -1
- package/dist-cjs/systems/crdt/index.d.ts +0 -1
- package/dist-cjs/systems/crdt/index.js +55 -169
- package/package.json +2 -2
- package/dist/serialization/crdt/network/utils.d.ts +0 -9
- package/dist/serialization/crdt/network/utils.js +0 -60
- package/dist-cjs/serialization/crdt/network/utils.d.ts +0 -9
- package/dist-cjs/serialization/crdt/network/utils.js +0 -67
|
@@ -19,6 +19,7 @@ import { InputModifierComponentDefinitionExtended } from './extended/InputModifi
|
|
|
19
19
|
import { LightSourceComponentDefinitionExtended } from './extended/LightSource';
|
|
20
20
|
import { TriggerAreaComponentDefinitionExtended } from './extended/TriggerArea';
|
|
21
21
|
import { TagsComponentDefinitionExtended } from './manual/Tags';
|
|
22
|
+
import { ICreatedByType } from './manual/CreatedBy';
|
|
22
23
|
export * from './generated/index.gen';
|
|
23
24
|
export type { GrowOnlyValueSetComponentDefinition, LastWriteWinElementSetComponentDefinition, LwwComponentGetter, GSetComponentGetter };
|
|
24
25
|
export declare const Transform: LwwComponentGetter<TransformComponentExtended>;
|
|
@@ -50,4 +51,8 @@ export declare const NetworkEntity: (engine: Pick<IEngine, 'defineComponent'>) =
|
|
|
50
51
|
* @alpha
|
|
51
52
|
*/
|
|
52
53
|
export declare const NetworkParent: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<INetowrkParentType>;
|
|
54
|
+
/**
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export declare const CreatedBy: (engine: Pick<IEngine, 'defineComponent'>) => LastWriteWinElementSetComponentDefinition<ICreatedByType>;
|
|
53
58
|
export { MediaState };
|
package/dist/components/index.js
CHANGED
|
@@ -16,6 +16,7 @@ import { defineInputModifierComponent } from './extended/InputModifier';
|
|
|
16
16
|
import { defineLightSourceComponent } from './extended/LightSource';
|
|
17
17
|
import { defineTriggerAreaComponent } from './extended/TriggerArea';
|
|
18
18
|
import defineTagsComponent from './manual/Tags';
|
|
19
|
+
import defineCreatedBy from './manual/CreatedBy';
|
|
19
20
|
export * from './generated/index.gen';
|
|
20
21
|
/* @__PURE__ */
|
|
21
22
|
export const Transform = (engine) => defineTransformComponent(engine);
|
|
@@ -56,11 +57,13 @@ export const SyncComponents = (engine) => defineSyncComponent(engine);
|
|
|
56
57
|
/**
|
|
57
58
|
* @alpha
|
|
58
59
|
*/
|
|
59
|
-
/* @__PURE__ */
|
|
60
60
|
export const NetworkEntity = (engine) => defineNetworkEntity(engine);
|
|
61
61
|
/**
|
|
62
62
|
* @alpha
|
|
63
63
|
*/
|
|
64
|
-
/* @__PURE__ */
|
|
65
64
|
export const NetworkParent = (engine) => defineNetworkParent(engine);
|
|
65
|
+
/**
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export const CreatedBy = (engine) => defineCreatedBy(engine);
|
|
66
69
|
export { MediaState };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IEngine, LastWriteWinElementSetComponentDefinition } from '../../engine/types';
|
|
2
|
+
export interface ICreatedByType {
|
|
3
|
+
address: string;
|
|
4
|
+
}
|
|
5
|
+
export type ICreatedBy = LastWriteWinElementSetComponentDefinition<ICreatedByType>;
|
|
6
|
+
declare function defineCreatedBy(engine: Pick<IEngine, 'defineComponent'>): import("../../engine/types").MapComponentDefinition<import("../..").MapResult<{
|
|
7
|
+
address: import("../../schemas").ISchema<string>;
|
|
8
|
+
}>>;
|
|
9
|
+
export default defineCreatedBy;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { LastWriteWinElementSetComponentDefinition, IEngine } from '../../engine';
|
|
2
2
|
import { Entity } from '../../engine/entity';
|
|
3
|
+
import type { ISchema } from '../../schemas/ISchema';
|
|
3
4
|
/**
|
|
4
5
|
* @public
|
|
5
6
|
*/
|
|
@@ -11,6 +12,10 @@ export interface TransformComponentExtended extends TransformComponent {
|
|
|
11
12
|
create(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
|
|
12
13
|
createOrReplace(entity: Entity, val?: TransformTypeWithOptionals): TransformType;
|
|
13
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* @public
|
|
17
|
+
*/
|
|
18
|
+
export declare const COMPONENT_ID = 1;
|
|
14
19
|
/**
|
|
15
20
|
* @public
|
|
16
21
|
*/
|
|
@@ -33,6 +38,10 @@ export type TransformType = {
|
|
|
33
38
|
};
|
|
34
39
|
parent?: Entity;
|
|
35
40
|
};
|
|
41
|
+
/** @public */
|
|
42
|
+
export declare const TRANSFORM_LENGTH = 44;
|
|
43
|
+
/** @public */
|
|
44
|
+
export declare const TransformSchema: ISchema<TransformType>;
|
|
36
45
|
/**
|
|
37
46
|
* @public
|
|
38
47
|
*/
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
2
|
+
* @public
|
|
3
3
|
*/
|
|
4
4
|
export const COMPONENT_ID = 1;
|
|
5
|
-
/** @
|
|
5
|
+
/** @public */
|
|
6
6
|
export const TRANSFORM_LENGTH = 44;
|
|
7
|
-
/** @
|
|
7
|
+
/** @public */
|
|
8
8
|
export const TransformSchema = {
|
|
9
9
|
serialize(value, builder) {
|
|
10
10
|
const ptr = builder.incrementWriteOffset(TRANSFORM_LENGTH);
|
|
@@ -12,6 +12,7 @@ export type { TagsComponentDefinitionExtended, TagsType } from './manual/Tags';
|
|
|
12
12
|
export type { ISyncComponents, ISyncComponentsType } from './manual/SyncComponents';
|
|
13
13
|
export type { INetowrkEntity, INetowrkEntityType } from './manual/NetworkEntity';
|
|
14
14
|
export type { INetowrkParent, INetowrkParentType } from './manual/NetworkParent';
|
|
15
|
+
export type { ICreatedBy, ICreatedByType } from './manual/CreatedBy';
|
|
15
16
|
export type { InputModifierHelper, InputModifierComponentDefinitionExtended } from './extended/InputModifier';
|
|
16
17
|
export type { LightSourceHelper, LightSourceComponentDefinitionExtended } from './extended/LightSource';
|
|
17
18
|
export type { TriggerAreaComponentDefinitionExtended } from './extended/TriggerArea';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ISchema } from '../schemas';
|
|
2
2
|
import { ByteBuffer } from '../serialization/ByteBuffer';
|
|
3
|
-
import { CrdtMessageBody, DeleteComponentMessageBody, PutComponentMessageBody } from '../serialization/crdt';
|
|
3
|
+
import { CrdtMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, PutComponentMessageBody } from '../serialization/crdt';
|
|
4
4
|
import { Entity } from './entity';
|
|
5
5
|
import { DeepReadonly, DeepReadonlySet } from './readonly';
|
|
6
6
|
/**
|
|
@@ -70,7 +70,58 @@ export interface BaseComponent<T> {
|
|
|
70
70
|
* If the value is undefined, the component was deleted.
|
|
71
71
|
*/
|
|
72
72
|
onChange(entity: Entity, cb: (value: T | undefined) => void): void;
|
|
73
|
+
/**
|
|
74
|
+
* @public
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
validateBeforeChange(entity: Entity, cb: ValidateCallback<T>): void;
|
|
78
|
+
validateBeforeChange(cb: ValidateCallback<T>): void;
|
|
79
|
+
/**
|
|
80
|
+
* Get the CRDT state for an entity (serialized data and timestamp)
|
|
81
|
+
* @param entity - Entity to get the CRDT state for
|
|
82
|
+
* @returns Object with serialized data and timestamp, or null if entity doesn't have the component
|
|
83
|
+
* @public
|
|
84
|
+
*/
|
|
85
|
+
getCrdtState(entity: Entity): {
|
|
86
|
+
data: Uint8Array;
|
|
87
|
+
timestamp: number;
|
|
88
|
+
} | null;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Internal component interface that exposes all internal methods for SDK use
|
|
92
|
+
* This is not exposed to users, only for internal SDK operations
|
|
93
|
+
*/
|
|
94
|
+
export interface InternalBaseComponent<T> extends BaseComponent<T> {
|
|
95
|
+
/**
|
|
96
|
+
* @public
|
|
97
|
+
* Dry run update to check if a CRDT message would be accepted without actually applying it
|
|
98
|
+
*/
|
|
99
|
+
__dry_run_updateFromCrdt(body: CrdtMessageBody): ProcessMessageResultType;
|
|
100
|
+
/**
|
|
101
|
+
* @public
|
|
102
|
+
* Get the iterator to every entity has the component
|
|
103
|
+
*/
|
|
104
|
+
iterator(): Iterable<[Entity, any]>;
|
|
105
|
+
/**
|
|
106
|
+
* @public
|
|
107
|
+
*/
|
|
108
|
+
dirtyIterator(): Iterable<Entity>;
|
|
109
|
+
/**
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
__onChangeCallbacks(entity: Entity, value: T): void;
|
|
113
|
+
/**
|
|
114
|
+
* @public
|
|
115
|
+
*/
|
|
116
|
+
__run_validateBeforeChange(entity: Entity, newValue: T | undefined, senderAddress: string, createdBy: string): boolean;
|
|
73
117
|
}
|
|
118
|
+
export type ValidateCallback<T> = (value: {
|
|
119
|
+
entity: Entity;
|
|
120
|
+
currentValue: T | undefined;
|
|
121
|
+
newValue: T | undefined;
|
|
122
|
+
senderAddress: string;
|
|
123
|
+
createdBy: string;
|
|
124
|
+
}) => boolean;
|
|
74
125
|
/**
|
|
75
126
|
* @public
|
|
76
127
|
*/
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ReadWriteByteBuffer } from '../serialization/ByteBuffer';
|
|
2
|
-
import { AppendValueOperation, CrdtMessageType } from '../serialization/crdt';
|
|
2
|
+
import { AppendValueOperation, CrdtMessageType, ProcessMessageResultType } from '../serialization/crdt';
|
|
3
3
|
import { __DEV__ } from '../runtime/invariant';
|
|
4
4
|
const emptyReadonlySet = freezeSet(new Set());
|
|
5
|
+
const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
|
|
5
6
|
function frozenError() {
|
|
6
7
|
throw new Error('The set is frozen');
|
|
7
8
|
}
|
|
@@ -22,6 +23,7 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
|
|
|
22
23
|
const dirtyIterator = new Set();
|
|
23
24
|
const queuedCommands = [];
|
|
24
25
|
const onChangeCallbacks = new Map();
|
|
26
|
+
const validateCallbacks = new Map();
|
|
25
27
|
// only sort the array if the latest (N) element has a timestamp <= N-1
|
|
26
28
|
function shouldSort(row) {
|
|
27
29
|
const len = row.raw.length;
|
|
@@ -80,8 +82,11 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
|
|
|
80
82
|
has(entity) {
|
|
81
83
|
return data.has(entity);
|
|
82
84
|
},
|
|
83
|
-
entityDeleted(entity) {
|
|
85
|
+
entityDeleted(entity, markAsDirty) {
|
|
84
86
|
data.delete(entity);
|
|
87
|
+
if (markAsDirty) {
|
|
88
|
+
// For grow-only sets, we don't need to mark as dirty since deletion doesn't generate CRDT messages
|
|
89
|
+
}
|
|
85
90
|
},
|
|
86
91
|
get(entity) {
|
|
87
92
|
const values = data.get(entity);
|
|
@@ -152,6 +157,44 @@ export function createValueSetComponentDefinitionFromSchema(componentName, compo
|
|
|
152
157
|
for (const cb of cbs) {
|
|
153
158
|
cb(value);
|
|
154
159
|
}
|
|
160
|
+
},
|
|
161
|
+
__dry_run_updateFromCrdt(_body) {
|
|
162
|
+
return ProcessMessageResultType.StateUpdatedData;
|
|
163
|
+
},
|
|
164
|
+
validateBeforeChange(entityOrCb, cb) {
|
|
165
|
+
if (arguments.length === 1) {
|
|
166
|
+
// Second overload: just callback (global validation)
|
|
167
|
+
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
if (cb) {
|
|
171
|
+
validateCallbacks.set(entityOrCb, cb);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
|
|
176
|
+
const cb = entity && validateCallbacks.get(entity);
|
|
177
|
+
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
|
|
178
|
+
const currentValue = [...this.get(entity).values()];
|
|
179
|
+
const value = { entity, currentValue: currentValue, newValue, senderAddress, createdBy };
|
|
180
|
+
const globalResult = globalCb?.(value) ?? true;
|
|
181
|
+
const entityResult = (globalResult && cb?.(value)) ?? true;
|
|
182
|
+
return globalResult && entityResult;
|
|
183
|
+
},
|
|
184
|
+
getCrdtState(entity) {
|
|
185
|
+
const row = data.get(entity);
|
|
186
|
+
if (!row || row.raw.length === 0) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
// For GrowOnlySet, we need to return the complete CRDT messages for all values
|
|
190
|
+
// This is complex because GrowOnlySet uses APPEND messages, not a single PUT
|
|
191
|
+
// For now, return null to indicate this component type doesn't support simple corrections
|
|
192
|
+
return null;
|
|
193
|
+
},
|
|
194
|
+
__forceUpdateFromCrdt(_msg) {
|
|
195
|
+
// GrowOnlySet doesn't support authoritative corrections in the same way as LWW
|
|
196
|
+
// since it uses APPEND_VALUE messages instead of PUT_COMPONENT messages
|
|
197
|
+
return [null, undefined];
|
|
155
198
|
}
|
|
156
199
|
};
|
|
157
200
|
return ret;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { ISchema } from '../schemas';
|
|
2
2
|
import { ByteBuffer } from '../serialization/ByteBuffer';
|
|
3
|
-
import { PutComponentMessageBody, DeleteComponentMessageBody, CrdtMessageBody } from '../serialization/crdt';
|
|
3
|
+
import { PutComponentMessageBody, DeleteComponentMessageBody, ProcessMessageResultType, CrdtMessageBody, PutNetworkComponentMessageBody, DeleteComponentNetworkMessageBody, AuthoritativePutComponentMessageBody } from '../serialization/crdt';
|
|
4
4
|
import { Entity } from './entity';
|
|
5
5
|
export declare function incrementTimestamp(entity: Entity, timestamps: Map<Entity, number>): number;
|
|
6
6
|
export declare function createDumpLwwFunctionFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (buffer: ByteBuffer, filterEntity?: ((entity: Entity) => boolean) | undefined) => void;
|
|
7
|
+
export declare function createCrdtRuleValidator(timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (message: PutComponentMessageBody | DeleteComponentMessageBody | PutNetworkComponentMessageBody | DeleteComponentNetworkMessageBody) => ProcessMessageResultType;
|
|
8
|
+
export declare function createForceUpdateLwwFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (msg: AuthoritativePutComponentMessageBody) => [null, any];
|
|
7
9
|
export declare function createUpdateLwwFromCrdt(componentId: number, timestamps: Map<Entity, number>, schema: Pick<ISchema<any>, 'serialize' | 'deserialize'>, data: Map<Entity, unknown>): (msg: CrdtMessageBody) => [null | PutComponentMessageBody | DeleteComponentMessageBody, any];
|
|
8
10
|
export declare function createGetCrdtMessagesForLww(componentId: number, timestamps: Map<Entity, number>, dirtyIterator: Set<Entity>, schema: Pick<ISchema<any>, 'serialize'>, data: Map<Entity, unknown>): () => Generator<PutComponentMessageBody | DeleteComponentMessageBody, void, unknown>;
|
|
@@ -30,16 +30,12 @@ export function createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, d
|
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
const __GLOBAL_ENTITY = '__GLOBAL_ENTITY';
|
|
34
|
+
export function createCrdtRuleValidator(timestamps, schema, data) {
|
|
34
35
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* Returns the recieved data if the lamport number was bigger than ours.
|
|
40
|
-
* If it was an outdated message, then we return void
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
36
|
+
* Shared CRDT conflict resolution logic
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
43
39
|
function crdtRuleForCurrentState(message) {
|
|
44
40
|
const { entityId, timestamp } = message;
|
|
45
41
|
const currentTimestamp = timestamps.get(entityId);
|
|
@@ -49,7 +45,6 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
49
45
|
}
|
|
50
46
|
// Outdated Message. Resend our state message through the wire.
|
|
51
47
|
if (currentTimestamp > timestamp) {
|
|
52
|
-
// console.log('2', currentTimestamp, timestamp)
|
|
53
48
|
return ProcessMessageResultType.StateOutdatedTimestamp;
|
|
54
49
|
}
|
|
55
50
|
// Deletes are idempotent
|
|
@@ -66,7 +61,6 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
66
61
|
currentDataGreater = dataCompare(null, message.data);
|
|
67
62
|
}
|
|
68
63
|
// Same data, same timestamp. Weirdo echo message.
|
|
69
|
-
// console.log('3', currentDataGreater, writeBuffer.toBinary(), (message as any).data || null)
|
|
70
64
|
if (currentDataGreater === 0) {
|
|
71
65
|
return ProcessMessageResultType.NoChanges;
|
|
72
66
|
}
|
|
@@ -75,10 +69,36 @@ export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
|
75
69
|
return ProcessMessageResultType.StateOutdatedData;
|
|
76
70
|
}
|
|
77
71
|
else {
|
|
78
|
-
//
|
|
72
|
+
// Current data is lower
|
|
79
73
|
return ProcessMessageResultType.StateUpdatedData;
|
|
80
74
|
}
|
|
81
75
|
}
|
|
76
|
+
return crdtRuleForCurrentState;
|
|
77
|
+
}
|
|
78
|
+
export function createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
79
|
+
/**
|
|
80
|
+
* Force update component state regardless of timestamp - used for server authoritative messages
|
|
81
|
+
*/
|
|
82
|
+
return (msg) => {
|
|
83
|
+
console.log('[ BOEDO ] [ CASLA ] ', msg);
|
|
84
|
+
const buffer = new ReadWriteByteBuffer(msg.data);
|
|
85
|
+
const deserializedValue = schema.deserialize(buffer);
|
|
86
|
+
data.set(msg.entityId, deserializedValue);
|
|
87
|
+
timestamps.set(msg.entityId, msg.timestamp);
|
|
88
|
+
return [null, deserializedValue];
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function createUpdateLwwFromCrdt(componentId, timestamps, schema, data) {
|
|
92
|
+
/**
|
|
93
|
+
* Process the received message only if the lamport number recieved is higher
|
|
94
|
+
* than the stored one. If its lower, we spread it to the network to correct the peer.
|
|
95
|
+
* If they are equal, the bigger raw data wins.
|
|
96
|
+
|
|
97
|
+
* Returns the recieved data if the lamport number was bigger than ours.
|
|
98
|
+
* If it was an outdated message, then we return void
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
const crdtRuleForCurrentState = createCrdtRuleValidator(timestamps, schema, data);
|
|
82
102
|
return (msg) => {
|
|
83
103
|
/* istanbul ignore next */
|
|
84
104
|
if (msg.type !== CrdtMessageType.PUT_COMPONENT &&
|
|
@@ -171,6 +191,7 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
|
|
|
171
191
|
const dirtyIterator = new Set();
|
|
172
192
|
const timestamps = new Map();
|
|
173
193
|
const onChangeCallbacks = new Map();
|
|
194
|
+
const validateCallbacks = new Map();
|
|
174
195
|
return {
|
|
175
196
|
get componentId() {
|
|
176
197
|
return componentId;
|
|
@@ -262,7 +283,39 @@ export function createComponentDefinitionFromSchema(componentName, componentId,
|
|
|
262
283
|
},
|
|
263
284
|
getCrdtUpdates: createGetCrdtMessagesForLww(componentId, timestamps, dirtyIterator, schema, data),
|
|
264
285
|
updateFromCrdt: createUpdateLwwFromCrdt(componentId, timestamps, schema, data),
|
|
286
|
+
__forceUpdateFromCrdt: createForceUpdateLwwFromCrdt(componentId, timestamps, schema, data),
|
|
287
|
+
__dry_run_updateFromCrdt: createCrdtRuleValidator(timestamps, schema, data),
|
|
265
288
|
dumpCrdtStateToBuffer: createDumpLwwFunctionFromCrdt(componentId, timestamps, schema, data),
|
|
289
|
+
validateBeforeChange(entityOrCb, cb) {
|
|
290
|
+
if (arguments.length === 1) {
|
|
291
|
+
// Second overload: just callback (global validation)
|
|
292
|
+
validateCallbacks.set(__GLOBAL_ENTITY, entityOrCb);
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
if (cb) {
|
|
296
|
+
validateCallbacks.set(entityOrCb, cb);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
__run_validateBeforeChange(entity, newValue, senderAddress, createdBy) {
|
|
301
|
+
const cb = entity && validateCallbacks.get(entity);
|
|
302
|
+
const globalCb = validateCallbacks.get(__GLOBAL_ENTITY);
|
|
303
|
+
const currentValue = data.get(entity);
|
|
304
|
+
const value = { entity, currentValue, newValue, senderAddress, createdBy };
|
|
305
|
+
const globalResult = globalCb?.(value) ?? true;
|
|
306
|
+
const entityResult = (globalResult && cb?.(value)) ?? true;
|
|
307
|
+
return globalResult && entityResult;
|
|
308
|
+
},
|
|
309
|
+
getCrdtState(entity) {
|
|
310
|
+
const componentData = data.get(entity);
|
|
311
|
+
const timestamp = timestamps.get(entity);
|
|
312
|
+
if (componentData && timestamp !== undefined) {
|
|
313
|
+
const buffer = new ReadWriteByteBuffer();
|
|
314
|
+
schema.serialize(deepReadonly(componentData), buffer);
|
|
315
|
+
return { data: buffer.toBinary(), timestamp };
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
},
|
|
266
319
|
onChange(entity, cb) {
|
|
267
320
|
const cbs = onChangeCallbacks.get(entity) ?? [];
|
|
268
321
|
cbs.push(cb);
|
package/dist/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export * from './systems/tween';
|
|
|
13
13
|
export * from './systems/triggerArea';
|
|
14
14
|
export * from './engine/entity';
|
|
15
15
|
export * from './components/types';
|
|
16
|
-
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended } from './components/types';
|
|
16
|
+
import { MaterialComponentDefinitionExtended, MeshColliderComponentDefinitionExtended, MeshRendererComponentDefinitionExtended, TransformComponentExtended, AnimatorComponentDefinitionExtended, AudioSourceComponentDefinitionExtended, AudioStreamComponentDefinitionExtended, ISyncComponents, TweenComponentDefinitionExtended, INetowrkEntity, INetowrkParent, VirtualCameraComponentDefinitionExtended, InputModifierComponentDefinitionExtended, LightSourceComponentDefinitionExtended, TriggerAreaComponentDefinitionExtended, ICreatedBy } from './components/types';
|
|
17
17
|
import { NameComponent } from './components/manual/Name';
|
|
18
18
|
import { TagsComponentDefinitionExtended } from './components/manual/Tags';
|
|
19
19
|
export declare const Transform: TransformComponentExtended;
|
|
@@ -46,6 +46,7 @@ export declare const NetworkEntity: INetowrkEntity;
|
|
|
46
46
|
* Tag a entity to be syncronized through comms
|
|
47
47
|
*/
|
|
48
48
|
export declare const NetworkParent: INetowrkParent;
|
|
49
|
+
export declare const CreatedBy: ICreatedBy;
|
|
49
50
|
export * from './components/generated/global.gen';
|
|
50
51
|
export * from './components/generated/types.gen';
|
|
51
52
|
export * from './serialization/crdt';
|
package/dist/index.js
CHANGED
|
@@ -50,6 +50,7 @@ export const NetworkEntity = /* @__PURE__*/ components.NetworkEntity(engine);
|
|
|
50
50
|
* Tag a entity to be syncronized through comms
|
|
51
51
|
*/
|
|
52
52
|
export const NetworkParent = /* @__PURE__*/ components.NetworkParent(engine);
|
|
53
|
+
export const CreatedBy = /* @__PURE__*/ components.CreatedBy(engine);
|
|
53
54
|
// export components for global engine
|
|
54
55
|
export * from './components/generated/global.gen';
|
|
55
56
|
export * from './components/generated/types.gen';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Entity } from '../../engine/entity';
|
|
2
|
+
import { ByteBuffer } from '../ByteBuffer';
|
|
3
|
+
import { AuthoritativePutComponentMessage } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
7
|
+
export declare namespace AuthoritativePutComponentOperation {
|
|
8
|
+
const MESSAGE_HEADER_LENGTH = 16;
|
|
9
|
+
/**
|
|
10
|
+
* Call this function for an optimal writing data passing the ByteBuffer
|
|
11
|
+
* already allocated
|
|
12
|
+
*/
|
|
13
|
+
function write(entity: Entity, timestamp: number, componentId: number, data: Uint8Array, buf: ByteBuffer): void;
|
|
14
|
+
function read(buf: ByteBuffer): AuthoritativePutComponentMessage | null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CrdtMessageProtocol } from './crdtMessageProtocol';
|
|
2
|
+
import { CrdtMessageType, CRDT_MESSAGE_HEADER_LENGTH } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export var AuthoritativePutComponentOperation;
|
|
7
|
+
(function (AuthoritativePutComponentOperation) {
|
|
8
|
+
AuthoritativePutComponentOperation.MESSAGE_HEADER_LENGTH = 16;
|
|
9
|
+
/**
|
|
10
|
+
* Call this function for an optimal writing data passing the ByteBuffer
|
|
11
|
+
* already allocated
|
|
12
|
+
*/
|
|
13
|
+
function write(entity, timestamp, componentId, data, buf) {
|
|
14
|
+
// reserve the beginning
|
|
15
|
+
const startMessageOffset = buf.incrementWriteOffset(CRDT_MESSAGE_HEADER_LENGTH + AuthoritativePutComponentOperation.MESSAGE_HEADER_LENGTH);
|
|
16
|
+
// write body
|
|
17
|
+
buf.writeBuffer(data, false);
|
|
18
|
+
const messageLength = buf.currentWriteOffset() - startMessageOffset;
|
|
19
|
+
// Write CrdtMessage header
|
|
20
|
+
buf.setUint32(startMessageOffset, messageLength);
|
|
21
|
+
buf.setUint32(startMessageOffset + 4, CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT);
|
|
22
|
+
// Write ComponentOperation header
|
|
23
|
+
buf.setUint32(startMessageOffset + 8, entity);
|
|
24
|
+
buf.setUint32(startMessageOffset + 12, componentId);
|
|
25
|
+
buf.setUint32(startMessageOffset + 16, timestamp);
|
|
26
|
+
const newLocal = messageLength - AuthoritativePutComponentOperation.MESSAGE_HEADER_LENGTH - CRDT_MESSAGE_HEADER_LENGTH;
|
|
27
|
+
buf.setUint32(startMessageOffset + 20, newLocal);
|
|
28
|
+
}
|
|
29
|
+
AuthoritativePutComponentOperation.write = write;
|
|
30
|
+
function read(buf) {
|
|
31
|
+
const header = CrdtMessageProtocol.readHeader(buf);
|
|
32
|
+
if (!header) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (header.type !== CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT) {
|
|
36
|
+
throw new Error('AuthoritativePutComponentOperation tried to read another message type.');
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
...header,
|
|
40
|
+
entityId: buf.readUint32(),
|
|
41
|
+
componentId: buf.readUint32(),
|
|
42
|
+
timestamp: buf.readUint32(),
|
|
43
|
+
data: buf.readBuffer()
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
AuthoritativePutComponentOperation.read = read;
|
|
47
|
+
})(AuthoritativePutComponentOperation || (AuthoritativePutComponentOperation = {}));
|
|
@@ -11,7 +11,8 @@ export declare enum CrdtMessageType {
|
|
|
11
11
|
PUT_COMPONENT_NETWORK = 5,
|
|
12
12
|
DELETE_COMPONENT_NETWORK = 6,
|
|
13
13
|
DELETE_ENTITY_NETWORK = 7,
|
|
14
|
-
|
|
14
|
+
AUTHORITATIVE_PUT_COMPONENT = 8,
|
|
15
|
+
MAX_MESSAGE_TYPE = 9
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* Min length = 8 bytes
|
|
@@ -48,6 +49,23 @@ export type PutNetworkComponentMessageBody = Omit<PutComponentMessageBody, 'type
|
|
|
48
49
|
type: CrdtMessageType.PUT_COMPONENT_NETWORK;
|
|
49
50
|
networkId: number;
|
|
50
51
|
};
|
|
52
|
+
/**
|
|
53
|
+
* Server authoritative message - identical to PutComponentMessageBody but with forced processing
|
|
54
|
+
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
55
|
+
*
|
|
56
|
+
* @param entity - Uint32 number of the entity
|
|
57
|
+
* @param componentId - Uint32 number of id
|
|
58
|
+
* @param timestamp - Uint32 Lamport timestamp (server's authoritative timestamp)
|
|
59
|
+
* @param data - Uint8[] data of component => length(4 bytes) + block of bytes[0..length-1]
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export type AuthoritativePutComponentMessageBody = {
|
|
63
|
+
type: CrdtMessageType.AUTHORITATIVE_PUT_COMPONENT;
|
|
64
|
+
entityId: Entity;
|
|
65
|
+
componentId: number;
|
|
66
|
+
timestamp: number;
|
|
67
|
+
data: Uint8Array;
|
|
68
|
+
};
|
|
51
69
|
/**
|
|
52
70
|
* Min. length = header (8 bytes) + 16 bytes = 24 bytes
|
|
53
71
|
*
|
|
@@ -114,6 +132,10 @@ export type AppendValueMessage = CrdtMessageHeader & AppendValueMessageBody;
|
|
|
114
132
|
* @public
|
|
115
133
|
*/
|
|
116
134
|
export type PutComponentMessage = CrdtMessageHeader & PutComponentMessageBody;
|
|
135
|
+
/**
|
|
136
|
+
* @public
|
|
137
|
+
*/
|
|
138
|
+
export type AuthoritativePutComponentMessage = CrdtMessageHeader & AuthoritativePutComponentMessageBody;
|
|
117
139
|
/**
|
|
118
140
|
* @public
|
|
119
141
|
*/
|
|
@@ -137,7 +159,7 @@ export type DeleteEntityNetworkMessage = CrdtMessageHeader & DeleteEntityNetwork
|
|
|
137
159
|
/**
|
|
138
160
|
* @public
|
|
139
161
|
*/
|
|
140
|
-
export type CrdtMessage = PutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
162
|
+
export type CrdtMessage = PutComponentMessage | AuthoritativePutComponentMessage | DeleteComponentMessage | AppendValueMessage | DeleteEntityMessage | PutNetworkComponentMessage | DeleteComponentNetworkMessage | DeleteEntityNetworkMessage;
|
|
141
163
|
/**
|
|
142
164
|
* @public
|
|
143
165
|
*/
|
|
@@ -145,7 +167,7 @@ export type CrdtNetworkMessageBody = PutNetworkComponentMessageBody | DeleteComp
|
|
|
145
167
|
/**
|
|
146
168
|
* @public
|
|
147
169
|
*/
|
|
148
|
-
export type CrdtMessageBody = PutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
170
|
+
export type CrdtMessageBody = PutComponentMessageBody | AuthoritativePutComponentMessageBody | DeleteComponentMessageBody | DeleteEntityMessageBody | AppendValueMessageBody | CrdtNetworkMessageBody;
|
|
149
171
|
export declare enum ProcessMessageResultType {
|
|
150
172
|
/**
|
|
151
173
|
* Typical message and new state set.
|
|
@@ -13,7 +13,9 @@ export var CrdtMessageType;
|
|
|
13
13
|
CrdtMessageType[CrdtMessageType["PUT_COMPONENT_NETWORK"] = 5] = "PUT_COMPONENT_NETWORK";
|
|
14
14
|
CrdtMessageType[CrdtMessageType["DELETE_COMPONENT_NETWORK"] = 6] = "DELETE_COMPONENT_NETWORK";
|
|
15
15
|
CrdtMessageType[CrdtMessageType["DELETE_ENTITY_NETWORK"] = 7] = "DELETE_ENTITY_NETWORK";
|
|
16
|
-
|
|
16
|
+
// Server authoritative operation - forces component state regardless of timestamp
|
|
17
|
+
CrdtMessageType[CrdtMessageType["AUTHORITATIVE_PUT_COMPONENT"] = 8] = "AUTHORITATIVE_PUT_COMPONENT";
|
|
18
|
+
CrdtMessageType[CrdtMessageType["MAX_MESSAGE_TYPE"] = 9] = "MAX_MESSAGE_TYPE";
|
|
17
19
|
})(CrdtMessageType || (CrdtMessageType = {}));
|
|
18
20
|
/**
|
|
19
21
|
* @public
|