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

Allow remove to remove many entities at once #241

Merged
merged 3 commits into from
Nov 3, 2022
Merged
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
5 changes: 5 additions & 0 deletions .changeset/happy-parents-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@miniplex/bucket": patch
---

`remove` can now also take an iterable as its argument, removing all entities contained within.
4 changes: 1 addition & 3 deletions apps/demo/src/entities/Asteroids.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export const Asteroids = () => {
}

return () => {
for (const asteroid of archetypes.asteroids) {
ECS.world.remove(asteroid)
}
ECS.world.remove(archetypes.asteroids)
}
}, [])

Expand Down
18 changes: 14 additions & 4 deletions packages/miniplex-bucket/src/Bucket.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Event } from "@hmans/event"
import { isIterable } from "./util/isIterable"

/**
* A class wrapping an array of entities of a specific type, providing
Expand Down Expand Up @@ -84,9 +85,20 @@ export class Bucket<E> implements Iterable<E> {
* happens.
*
* @param entity The entity to remove from the bucket.
* @returns The entity passed into this function (regardless of whether it was removed or not).
*/
remove(entity: E) {
remove<D extends E>(entity: D): void

/**
*
* @param entities An `Iterable` of entities to remove from the bucket.
*/
remove(entities: Iterable<E>): void

remove(entity: E | Iterable<E>) {
if (isIterable(entity)) {
for (const e of entity) this.remove(e)
}

if (this.has(entity)) {
/* Emit our own onEntityRemoved event. */
this.onEntityRemoved.emit(entity)
Expand All @@ -105,8 +117,6 @@ export class Bucket<E> implements Iterable<E> {
/* Remove the entity from the entities array. */
this.entities.pop()
}

return entity
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/miniplex-bucket/src/util/isIterable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isIterable<T>(value: any): value is Iterable<T> {
return value != null && typeof value[Symbol.iterator] === "function"
}
19 changes: 10 additions & 9 deletions packages/miniplex-bucket/test/Bucket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ describe(Bucket, () => {
expect(bucket.entities).not.toContain(entity)
})

it("returns the entity", () => {
const bucket = new Bucket()
const entity = { id: "1" }

const result = bucket.remove(entity)

expect(result).toBe(entity)
})

it("emits the onEntityRemoved event", () => {
const bucket = new Bucket()
const entity = bucket.add({ id: "1" })
Expand All @@ -82,6 +73,16 @@ describe(Bucket, () => {

expect(listener).toHaveBeenCalledWith(entity)
})

it("can remove all entities from an iterable", () => {
const bucket = new Bucket()
const entity1 = bucket.add({ id: "1" })
const entity2 = bucket.add({ id: "2" })

bucket.remove([entity1, entity2])

expect(bucket.entities).toHaveLength(0)
})
})

describe("size", () => {
Expand Down