Skip to content

Commit

Permalink
feat: allow component overrides to be null
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 28, 2022
1 parent 22bc4d8 commit f9baf44
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
9 changes: 5 additions & 4 deletions packages/recs/src/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function overridableComponent<S extends Schema>(component: Component<S>):
const overrides = new Map<string, { update: Override<S>; nonce: number }>();

// Map from EntityIndex to current overridden component value
const overriddenEntityValues = new Map<EntityIndex, Partial<ComponentValue<S>>>();
const overriddenEntityValues = new Map<EntityIndex, Partial<ComponentValue<S>> | null>();

// Update event stream that takes into account overridden entity values
const update$ = new Subject<{
Expand Down Expand Up @@ -188,7 +188,7 @@ export function overridableComponent<S extends Schema>(component: Component<S>):
function getOverriddenComponentValue(entity: EntityIndex): ComponentValue<S> | undefined {
const originalValue = getComponentValue(component, entity);
const overriddenValue = overriddenEntityValues.get(entity);
return originalValue || overriddenValue
return (originalValue || overriddenValue) && overriddenValue !== null // null is a valid override, in this case return undefined
? ({ ...originalValue, ...overriddenValue } as ComponentValue<S>)
: undefined;
}
Expand Down Expand Up @@ -241,9 +241,10 @@ export function overridableComponent<S extends Schema>(component: Component<S>):
}) as OverridableComponent<S>;

// Internal function to set the current overridden component value and emit the update event
function setOverriddenComponentValue(entity: EntityIndex, value?: Partial<ComponentValue<S>>) {
function setOverriddenComponentValue(entity: EntityIndex, value?: Partial<ComponentValue<S>> | null) {
// Check specifically for undefined - null is a valid override
const prevValue = getOverriddenComponentValue(entity);
if (value) overriddenEntityValues.set(entity, value);
if (value !== undefined) overriddenEntityValues.set(entity, value);
else overriddenEntityValues.delete(entity);
update$.next({ entity, value: [getOverriddenComponentValue(entity), prevValue], component: overriddenComponent });
}
Expand Down
8 changes: 7 additions & 1 deletion packages/recs/src/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export function createWorld() {

function registerEntity({ id, idSuffix }: { id?: EntityID; idSuffix?: string } = {}) {
const entity = (id || entities.length + (idSuffix ? "-" + idSuffix : "")) as EntityID;
const index = (entities.push(entity) - 1) as EntityIndex;

// Skip if entity already exists
let index = entityToIndex.get(entity);
if (index != null) return index;

// Register entity
index = (entities.push(entity) - 1) as EntityIndex;
entityToIndex.set(entity, index);
return index;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/recs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export type SchemaOf<C extends Component<Schema>> = C extends Component<infer S>

export type Override<T extends Schema> = {
entity: EntityIndex;
value: Partial<ComponentValue<T>>;
value: Partial<ComponentValue<T>> | null;
};

export type OverridableComponent<T extends Schema = Schema> = Component<T> & {
Expand Down
2 changes: 1 addition & 1 deletion packages/recs/tests/System.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("System", () => {
entity = createEntity(world, [withValue(Position, { x: 1, y: 2 })]);
});

it("defineSystem should rerun the system if the query result changes (enter, update, exit)", () => {
it.only("defineSystem should rerun the system if the query result changes (enter, update, exit)", () => {
const mock = jest.fn();
defineSystem(world, [Has(Position)], mock);

Expand Down

0 comments on commit f9baf44

Please sign in to comment.