From f5e326ef1ad7df09bfdcb553ccc39034fd6539e9 Mon Sep 17 00:00:00 2001 From: RigidStudios Date: Thu, 16 Mar 2023 15:47:01 +0400 Subject: [PATCH 1/2] refactor: remove `forEach`, rename `iterate` to `items` --- src/lib/ecs/query.ts | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/src/lib/ecs/query.ts b/src/lib/ecs/query.ts index 2de068f..4fb5b9e 100644 --- a/src/lib/ecs/query.ts +++ b/src/lib/ecs/query.ts @@ -218,42 +218,18 @@ export class Query { } /** - * Runs a callback for each entity that matches the query. - * - * If the callback returns `false`, the iteration will stop, and no other - * entities in this query will be iterated over. - * - * #### Usage Example: - * ```ts - * query.forEach((entity) => { - * // ... - * }); - * ``` - * - * @param callback The callback to run for each entity. + * @returns an array of all matching entities mapped to their IDs. */ - public forEach(callback: (entityId: EntityId) => boolean | void): void { - for (const archetype of this.archetypes) { - for (const entityId of archetype.entities) { - if (callback(entityId) === false) { - return; - } - } - } - } + public items(): Array { + const idList: Array = new Array(); - /** - * Runs a callback for each entity that matches the query. - * - * TODO: This should be turned into a *[Symbol.iterator] method whenever - * that is supported. - */ - public *iterate(): Generator { for (const archetype of this.archetypes) { for (const entityId of archetype.entities) { - yield entityId; + idList.push(entityId); } } + + return idList; } /** From c68ad7e9c63a7d0ba1f797dbb7c0620d06e9258c Mon Sep 17 00:00:00 2001 From: RigidStudios Date: Thu, 16 Mar 2023 16:04:37 +0400 Subject: [PATCH 2/2] refactor(ecs): restore iterate as iter --- src/lib/ecs/query.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/ecs/query.ts b/src/lib/ecs/query.ts index 4fb5b9e..e978d2c 100644 --- a/src/lib/ecs/query.ts +++ b/src/lib/ecs/query.ts @@ -217,6 +217,20 @@ export class Query { this.exited = new SparseSet(); } + /** + * Runs a callback for each entity that matches the query. + * + * TODO: This should be turned into a *[Symbol.iterator] method whenever + * that is supported. + */ + public *iter(): Generator { + for (const archetype of this.archetypes) { + for (const entityId of archetype.entities) { + yield entityId; + } + } + } + /** * @returns an array of all matching entities mapped to their IDs. */