@inglorious/store 7.1.0 → 7.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/store",
3
- "version": "7.1.0",
3
+ "version": "7.1.1",
4
4
  "description": "A state manager for real-time, collaborative apps, inspired by game development patterns and compatible with Redux.",
5
5
  "author": "IceOnFire <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
6
6
  "license": "MIT",
@@ -58,7 +58,7 @@
58
58
  "prettier": "^3.6.2",
59
59
  "vite": "^7.1.3",
60
60
  "vitest": "^1.6.1",
61
- "@inglorious/eslint-config": "1.0.1"
61
+ "@inglorious/eslint-config": "1.1.0"
62
62
  },
63
63
  "engines": {
64
64
  "node": ">= 22"
package/src/event-map.js CHANGED
@@ -96,7 +96,8 @@ export class EventMap {
96
96
  * Supports scoped events:
97
97
  * - 'submit' -> all entities with 'submit' handler
98
98
  * - 'form:submit' -> all form entities with 'submit' handler
99
- * - 'form[loginForm]:submit' -> only loginForm entity
99
+ * - 'form#loginForm:submit' -> only loginForm entity (of type form)
100
+ * - '#loginForm:submit' -> only loginForm entity
100
101
  *
101
102
  * @param {string} eventString - The event string (e.g., 'submit', 'form:submit', 'form[id]:submit')
102
103
  * @returns {string[]} An array of entity IDs that should handle this event.
@@ -111,9 +112,9 @@ export class EventMap {
111
112
  const typeMap = this.handlerToTypeToEntities.get(handlerName)
112
113
  if (!typeMap) return []
113
114
 
114
- // form[loginForm]:submit - specific entity
115
- if (targetType && targetEntityId) {
116
- const entitySet = typeMap.get(targetType)
115
+ if (targetEntityId) {
116
+ const type = targetType ?? this.entityTypes.get(targetEntityId)
117
+ const entitySet = typeMap.get(type)
117
118
  return entitySet && entitySet.has(targetEntityId) ? [targetEntityId] : []
118
119
  }
119
120
 
@@ -134,7 +135,7 @@ export class EventMap {
134
135
 
135
136
  /**
136
137
  * Parses an event string into its components.
137
- * @param {string} eventString - The event string (e.g., 'submit', 'form:submit', 'form[loginForm]:submit')
138
+ * @param {string} eventString - The event string (e.g., 'submit', 'form:submit', 'form#loginForm:submit')
138
139
  * @returns {{ type: string|null, entityId: string|null, event: string }}
139
140
  */
140
141
  export function parseEvent(eventString) {
@@ -109,6 +109,53 @@ test("getEntitiesForEvent should return the correct set of entities for an event
109
109
  expect(fireEntities).toStrictEqual([])
110
110
  })
111
111
 
112
+ test("getEntitiesForEvent should handle scoped events correctly", () => {
113
+ const types = {
114
+ form: { submit: () => {} },
115
+ button: { submit: () => {} },
116
+ }
117
+ const entities = {
118
+ loginForm: { type: "form" },
119
+ signupForm: { type: "form" },
120
+ submitButton: { type: "button" },
121
+ }
122
+ const eventMap = new EventMap(types, entities)
123
+
124
+ // Scoped to a type: 'form:submit' should only return form entities
125
+ expect(eventMap.getEntitiesForEvent("form:submit")).toStrictEqual([
126
+ "loginForm",
127
+ "signupForm",
128
+ ])
129
+
130
+ // Scoped to a specific entity by ID: '#loginForm:submit'
131
+ expect(eventMap.getEntitiesForEvent("#loginForm:submit")).toStrictEqual([
132
+ "loginForm",
133
+ ])
134
+
135
+ // Scoped to a specific entity by type and ID: 'form#loginForm:submit'
136
+ expect(eventMap.getEntitiesForEvent("form#loginForm:submit")).toStrictEqual([
137
+ "loginForm",
138
+ ])
139
+
140
+ // Scoped to a non-existent entity ID
141
+ expect(eventMap.getEntitiesForEvent("#nonExistent:submit")).toStrictEqual([])
142
+
143
+ // Scoped to an entity that exists but doesn't handle the event
144
+ expect(eventMap.getEntitiesForEvent("#loginForm:click")).toStrictEqual([])
145
+
146
+ // Scoped to an entity ID, but with the wrong type specified
147
+ expect(eventMap.getEntitiesForEvent("button#loginForm:submit")).toStrictEqual(
148
+ [],
149
+ )
150
+
151
+ // Broadcast event should return all entities with the handler
152
+ expect(eventMap.getEntitiesForEvent("submit")).toStrictEqual([
153
+ "loginForm",
154
+ "signupForm",
155
+ "submitButton",
156
+ ])
157
+ })
158
+
112
159
  test("EventMap provides a significant performance benefit for event handling", async () => {
113
160
  const ENTITY_COUNT = 10000
114
161
  const { entities, types } = createTestEntities(ENTITY_COUNT)