@latticexyz/recs 2.0.0-skystrife-playtest-9e9511d4 → 2.0.0-transaction-context-324984c5
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/CHANGELOG.md +92 -11
- package/dist/{chunk-J6QWRVRL.js → chunk-YDQFEK6R.js} +1 -1
- package/dist/{chunk-J6QWRVRL.js.map → chunk-YDQFEK6R.js.map} +1 -1
- package/dist/deprecated/index.d.ts +51 -0
- package/dist/deprecated/index.js +1 -1
- package/dist/index.d.ts +606 -0
- package/dist/index.js +1 -1
- package/dist/types-0972fad2.d.ts +224 -0
- package/package.json +3 -3
- package/src/Indexer.ts +2 -1
@@ -0,0 +1,224 @@
|
|
1
|
+
import { Subject } from 'rxjs';
|
2
|
+
import { Opaque } from 'type-fest';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Type enum is used to specify value types in {@link ComponentSchema} to be able
|
6
|
+
* to access type values in JavaScript in addition to TypeScript type checks.
|
7
|
+
*/
|
8
|
+
declare enum Type {
|
9
|
+
Boolean = 0,
|
10
|
+
Number = 1,
|
11
|
+
OptionalNumber = 2,
|
12
|
+
BigInt = 3,
|
13
|
+
OptionalBigInt = 4,
|
14
|
+
String = 5,
|
15
|
+
OptionalString = 6,
|
16
|
+
NumberArray = 7,
|
17
|
+
OptionalNumberArray = 8,
|
18
|
+
BigIntArray = 9,
|
19
|
+
OptionalBigIntArray = 10,
|
20
|
+
StringArray = 11,
|
21
|
+
OptionalStringArray = 12,
|
22
|
+
Entity = 13,
|
23
|
+
OptionalEntity = 14,
|
24
|
+
EntityArray = 15,
|
25
|
+
OptionalEntityArray = 16,
|
26
|
+
T = 17,
|
27
|
+
OptionalT = 18
|
28
|
+
}
|
29
|
+
/**
|
30
|
+
* Used to specify type of {@link ComponentUpdate}.
|
31
|
+
* - Enter: Update added a value to an entity that did not have a value before
|
32
|
+
* - Exit: Update removed a value from an entity that had a value before
|
33
|
+
* - Update: Update changed a value of an entity that already had a value before. Note: the value doesn't need to be different from the previous value.
|
34
|
+
* - Noop: Update did nothing (removed a value from an entity that did not have a value)
|
35
|
+
*/
|
36
|
+
declare enum UpdateType {
|
37
|
+
Enter = 0,
|
38
|
+
Exit = 1,
|
39
|
+
Update = 2,
|
40
|
+
Noop = 3
|
41
|
+
}
|
42
|
+
/**
|
43
|
+
* Helper constant with all optional {@link Type}s.
|
44
|
+
*/
|
45
|
+
declare const OptionalTypes: Type[];
|
46
|
+
|
47
|
+
/**
|
48
|
+
* Entities are represented as symbols internally for memory efficiency.
|
49
|
+
* To get the entity's string representation, use `getEntityString(entitySymbol)`
|
50
|
+
*/
|
51
|
+
type EntitySymbol = Opaque<symbol, "EntitySymbol">;
|
52
|
+
type Entity = Opaque<string, "Entity">;
|
53
|
+
/**
|
54
|
+
* Used to define the schema of a {@link Component}.
|
55
|
+
* Uses {@link Type} enum to be able to access the component type in JavaScript as well as have TypeScript type checks.
|
56
|
+
*/
|
57
|
+
type Schema = {
|
58
|
+
[key: string]: Type;
|
59
|
+
};
|
60
|
+
/**
|
61
|
+
* Used to add arbitrary metadata to components.
|
62
|
+
*/
|
63
|
+
type Metadata = {
|
64
|
+
[key: string]: unknown;
|
65
|
+
} | undefined;
|
66
|
+
/**
|
67
|
+
* Mapping between JavaScript {@link Type} enum and corresponding TypeScript type.
|
68
|
+
*/
|
69
|
+
type ValueType<T = unknown> = {
|
70
|
+
[Type.Boolean]: boolean;
|
71
|
+
[Type.Number]: number;
|
72
|
+
[Type.BigInt]: bigint;
|
73
|
+
[Type.String]: string;
|
74
|
+
[Type.NumberArray]: number[];
|
75
|
+
[Type.BigIntArray]: bigint[];
|
76
|
+
[Type.StringArray]: string[];
|
77
|
+
[Type.Entity]: Entity;
|
78
|
+
[Type.EntityArray]: Entity[];
|
79
|
+
[Type.OptionalNumber]: number | undefined;
|
80
|
+
[Type.OptionalBigInt]: bigint | undefined;
|
81
|
+
[Type.OptionalBigIntArray]: bigint[] | undefined;
|
82
|
+
[Type.OptionalString]: string | undefined;
|
83
|
+
[Type.OptionalNumberArray]: number[] | undefined;
|
84
|
+
[Type.OptionalStringArray]: string[] | undefined;
|
85
|
+
[Type.OptionalEntity]: Entity | undefined;
|
86
|
+
[Type.OptionalEntityArray]: Entity[] | undefined;
|
87
|
+
[Type.T]: T;
|
88
|
+
[Type.OptionalT]: T | undefined;
|
89
|
+
};
|
90
|
+
/**
|
91
|
+
* Used to infer the TypeScript type of a component value corresponding to a given {@link Schema}.
|
92
|
+
*/
|
93
|
+
type ComponentValue<S extends Schema = Schema, T = unknown> = {
|
94
|
+
[key in keyof S]: ValueType<T>[S[key]];
|
95
|
+
};
|
96
|
+
/**
|
97
|
+
* Type of a component update corresponding to a given {@link Schema}.
|
98
|
+
*/
|
99
|
+
type ComponentUpdate<S extends Schema = Schema, T = unknown> = {
|
100
|
+
entity: Entity;
|
101
|
+
value: [ComponentValue<S, T> | undefined, ComponentValue<S, T> | undefined];
|
102
|
+
component: Component<S, Metadata, T>;
|
103
|
+
};
|
104
|
+
/**
|
105
|
+
* Type of component returned by {@link defineComponent}.
|
106
|
+
*/
|
107
|
+
interface Component<S extends Schema = Schema, M extends Metadata = Metadata, T = unknown> {
|
108
|
+
id: string;
|
109
|
+
values: {
|
110
|
+
[key in keyof S]: Map<EntitySymbol, ValueType<T>[S[key]]>;
|
111
|
+
};
|
112
|
+
schema: S;
|
113
|
+
metadata: M;
|
114
|
+
entities: () => IterableIterator<Entity>;
|
115
|
+
world: World;
|
116
|
+
update$: Subject<ComponentUpdate<S, T>> & {
|
117
|
+
observers: any;
|
118
|
+
};
|
119
|
+
}
|
120
|
+
/**
|
121
|
+
* Type of indexer returned by {@link createIndexer}.
|
122
|
+
*/
|
123
|
+
type Indexer<S extends Schema, M extends Metadata = Metadata, T = unknown> = Component<S, M, T> & {
|
124
|
+
getEntitiesWithValue: (value: ComponentValue<S, T>) => Set<Entity>;
|
125
|
+
};
|
126
|
+
type Components = {
|
127
|
+
[key: string]: Component;
|
128
|
+
};
|
129
|
+
interface ComponentWithStream<S extends Schema, T = unknown> extends Component<S, Metadata, T> {
|
130
|
+
stream$: Subject<{
|
131
|
+
entity: Entity;
|
132
|
+
value: ComponentValue<S, T> | undefined;
|
133
|
+
}>;
|
134
|
+
}
|
135
|
+
type AnyComponentValue = ComponentValue<Schema>;
|
136
|
+
type AnyComponent = Component<Schema>;
|
137
|
+
/**
|
138
|
+
* Type of World returned by {@link createWorld}.
|
139
|
+
*/
|
140
|
+
type World = {
|
141
|
+
registerEntity: (options?: {
|
142
|
+
id?: string;
|
143
|
+
idSuffix?: string;
|
144
|
+
}) => Entity;
|
145
|
+
registerComponent: (component: Component) => void;
|
146
|
+
components: Component[];
|
147
|
+
getEntities: () => IterableIterator<Entity>;
|
148
|
+
dispose: () => void;
|
149
|
+
registerDisposer: (disposer: () => void) => void;
|
150
|
+
hasEntity: (entity: Entity) => boolean;
|
151
|
+
deleteEntity: (entity: Entity) => void;
|
152
|
+
entitySymbols: Set<EntitySymbol>;
|
153
|
+
};
|
154
|
+
declare enum QueryFragmentType {
|
155
|
+
Has = 0,
|
156
|
+
HasValue = 1,
|
157
|
+
Not = 2,
|
158
|
+
NotValue = 3,
|
159
|
+
ProxyRead = 4,
|
160
|
+
ProxyExpand = 5
|
161
|
+
}
|
162
|
+
type HasQueryFragment<T extends Schema> = {
|
163
|
+
type: QueryFragmentType.Has;
|
164
|
+
component: Component<T>;
|
165
|
+
};
|
166
|
+
type HasValueQueryFragment<T extends Schema> = {
|
167
|
+
type: QueryFragmentType.HasValue;
|
168
|
+
component: Component<T>;
|
169
|
+
value: Partial<ComponentValue<T>>;
|
170
|
+
};
|
171
|
+
type NotQueryFragment<T extends Schema> = {
|
172
|
+
type: QueryFragmentType.Not;
|
173
|
+
component: Component<T>;
|
174
|
+
};
|
175
|
+
type NotValueQueryFragment<T extends Schema> = {
|
176
|
+
type: QueryFragmentType.NotValue;
|
177
|
+
component: Component<T>;
|
178
|
+
value: Partial<ComponentValue<T>>;
|
179
|
+
};
|
180
|
+
type ProxyReadQueryFragment = {
|
181
|
+
type: QueryFragmentType.ProxyRead;
|
182
|
+
component: Component<{
|
183
|
+
value: Type.Entity;
|
184
|
+
}>;
|
185
|
+
depth: number;
|
186
|
+
};
|
187
|
+
type ProxyExpandQueryFragment = {
|
188
|
+
type: QueryFragmentType.ProxyExpand;
|
189
|
+
component: Component<{
|
190
|
+
value: Type.Entity;
|
191
|
+
}>;
|
192
|
+
depth: number;
|
193
|
+
};
|
194
|
+
type QueryFragment<T extends Schema = Schema> = HasQueryFragment<T> | HasValueQueryFragment<T> | NotQueryFragment<T> | NotValueQueryFragment<T> | ProxyReadQueryFragment | ProxyExpandQueryFragment;
|
195
|
+
type EntityQueryFragment<T extends Schema = Schema> = HasQueryFragment<T> | HasValueQueryFragment<T> | NotQueryFragment<T> | NotValueQueryFragment<T>;
|
196
|
+
type SettingQueryFragment = ProxyReadQueryFragment | ProxyExpandQueryFragment;
|
197
|
+
type QueryFragments = QueryFragment<Schema>[];
|
198
|
+
type SchemaOf<C extends Component<Schema>> = C extends Component<infer S> ? S : never;
|
199
|
+
type Override<S extends Schema, T = unknown> = {
|
200
|
+
entity: Entity;
|
201
|
+
value: Partial<ComponentValue<S, T>> | null;
|
202
|
+
};
|
203
|
+
/**
|
204
|
+
* Type of overridable component returned by {@link overridableComponent}.
|
205
|
+
*/
|
206
|
+
type OverridableComponent<S extends Schema = Schema, M extends Metadata = Metadata, T = unknown> = Component<S, M, T> & {
|
207
|
+
addOverride: (overrideId: string, update: Override<S, T>) => void;
|
208
|
+
removeOverride: (overrideId: string) => void;
|
209
|
+
};
|
210
|
+
type OptionalType = Type.OptionalNumber | Type.OptionalBigInt | Type.OptionalString | Type.OptionalEntity | Type.OptionalNumberArray | Type.OptionalBigIntArray | Type.OptionalStringArray | Type.OptionalEntityArray;
|
211
|
+
declare function isOptionalType(t: Type): t is OptionalType;
|
212
|
+
type ArrayType = Type.NumberArray | Type.OptionalNumberArray | Type.BigIntArray | Type.OptionalBigIntArray | Type.StringArray | Type.OptionalStringArray | Type.EntityArray | Type.OptionalEntityArray;
|
213
|
+
declare function isArrayType(t: Type): t is ArrayType;
|
214
|
+
type NumberType = Type.Number | Type.OptionalNumber;
|
215
|
+
declare function isNumberType(t: Type): t is NumberType;
|
216
|
+
type EntityType = Type.Entity | Type.OptionalEntity;
|
217
|
+
declare function isEntityType(t: Type): t is EntityType;
|
218
|
+
type Layer = {
|
219
|
+
world: World;
|
220
|
+
components: Record<string, Component<Schema>>;
|
221
|
+
};
|
222
|
+
type Layers = Record<string, Layer>;
|
223
|
+
|
224
|
+
export { AnyComponentValue as A, Component as C, Entity as E, HasQueryFragment as H, Indexer as I, Layer as L, Metadata as M, NotQueryFragment as N, OverridableComponent as O, ProxyReadQueryFragment as P, QueryFragment as Q, Schema as S, Type as T, UpdateType as U, ValueType as V, World as W, ComponentValue as a, EntitySymbol as b, ComponentUpdate as c, HasValueQueryFragment as d, NotValueQueryFragment as e, ProxyExpandQueryFragment as f, Components as g, ComponentWithStream as h, AnyComponent as i, QueryFragmentType as j, EntityQueryFragment as k, SettingQueryFragment as l, QueryFragments as m, SchemaOf as n, Override as o, OptionalType as p, isOptionalType as q, ArrayType as r, isArrayType as s, NumberType as t, isNumberType as u, EntityType as v, isEntityType as w, Layers as x, OptionalTypes as y };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@latticexyz/recs",
|
3
|
-
"version": "2.0.0-
|
3
|
+
"version": "2.0.0-transaction-context-324984c5",
|
4
4
|
"repository": {
|
5
5
|
"type": "git",
|
6
6
|
"url": "https://github.com/latticexyz/mud.git",
|
@@ -25,8 +25,8 @@
|
|
25
25
|
"dependencies": {
|
26
26
|
"mobx": "^6.7.0",
|
27
27
|
"rxjs": "7.5.5",
|
28
|
-
"@latticexyz/schema-type": "2.0.0-
|
29
|
-
"@latticexyz/utils": "2.0.0-
|
28
|
+
"@latticexyz/schema-type": "2.0.0-transaction-context-324984c5",
|
29
|
+
"@latticexyz/utils": "2.0.0-transaction-context-324984c5"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
32
|
"@types/jest": "^27.4.1",
|
package/src/Indexer.ts
CHANGED
@@ -7,7 +7,8 @@ import { Component, ComponentValue, Entity, EntitySymbol, Indexer, Metadata, Sch
|
|
7
7
|
*
|
8
8
|
* @remarks
|
9
9
|
* An indexed component keeps a "reverse mapping" from {@link ComponentValue} to the Set of {@link createEntity Entities} with this value.
|
10
|
-
* This adds a performance overhead to modifying component values and a memory overhead since in the worst case there is one
|
10
|
+
* This adds a performance overhead to modifying component values and a memory overhead since in the worst case there is one
|
11
|
+
* Set per entity (if every entity has a different component value).
|
11
12
|
* In return the performance for querying for entities with a given component value is close to O(1) (instead of O(#entities) in a regular non-indexed component).
|
12
13
|
* As a rule of thumb only components that are added to many entities and are queried with {@link HasValue} a lot should be indexed (eg. the Position component).
|
13
14
|
*
|