Skip to content

Commit d2d533e

Browse files
committed
fix: add full path id for subscriptions to avoid duplicate params names
1 parent a6e2092 commit d2d533e

3 files changed

Lines changed: 30 additions & 20 deletions

File tree

src/lib/client/ecs/scene/entity/component/component-manager.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export class EntityComponentManager {
1515
public readonly entity: SceneEntityHandle;
1616
private readonly _store: Writable<Record<string, Record<string, string>>>;
1717

18+
private readonly _storageResolvable: string;
19+
1820
static reset() {
1921
_storage.set({});
2022
resetSubscriptions(_subscriptions);
@@ -24,9 +26,9 @@ export class EntityComponentManager {
2426
constructor(entity: SceneEntityHandle, components: Record<string, Record<string, string>>) {
2527
this.entity = entity;
2628

27-
const storageResolvable = `${this.entity.manager.scene.id}/${this.entity.id}`;
29+
this._storageResolvable = `${this.entity.manager.scene.id}/${this.entity.id}`;
2830

29-
this._store = resolveStore(_storage, storageResolvable, components);
31+
this._store = resolveStore(_storage, this._storageResolvable, components);
3032

3133
this._listen();
3234
}
@@ -70,10 +72,11 @@ export class EntityComponentManager {
7072
});
7173
this._store.set(newComponents);
7274

75+
const fullId = `${this._storageResolvable}/${id}`;
7376
const subscriptions = get(_subscriptions);
74-
if (subscriptions[id]) {
75-
subscriptions[id]();
76-
subscriptions[id] = null;
77+
if (subscriptions[fullId]) {
78+
subscriptions[fullId]();
79+
subscriptions[fullId] = null;
7780
_subscriptions.set(subscriptions);
7881
}
7982
}
@@ -93,9 +96,10 @@ export class EntityComponentManager {
9396

9497
private _subscribe(id: string, handle: EntityComponentHandle) {
9598
setTimeout(() => {
99+
const fullId = `${this._storageResolvable}/${id}`;
96100
const subscriptions = get(_subscriptions);
97-
if (subscriptions[id]) return;
98-
subscriptions[id] = handle.params.values.subscribe((params) => this._update(id, params));
101+
if (subscriptions[fullId]) return;
102+
subscriptions[fullId] = handle.params.values.subscribe((params) => this._update(id, params));
99103
_subscriptions.set(subscriptions);
100104
}, 0);
101105
}

src/lib/client/ecs/scene/entity/component/component-param-manager.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class ComponentParamManager {
1818
private readonly _store: Writable<ComponentParam[]>;
1919
private readonly _valuesStore: Writable<Record<string, string>>;
2020

21+
private readonly _storageResolvable: string;
22+
2123
static reset() {
2224
_storage.set({});
2325
_valueStorage.set({});
@@ -28,10 +30,10 @@ export class ComponentParamManager {
2830
constructor(component: EntityComponentHandle, params: Record<string, string>) {
2931
this.component = component;
3032

31-
const storageResolvable = `${this.component.manager.entity.manager.scene.id}/${this.component.manager.entity.id}/${this.component.id}`;
33+
this._storageResolvable = `${this.component.manager.entity.manager.scene.id}/${this.component.manager.entity.id}/${this.component.id}`;
3234

33-
this._store = resolveStore(_storage, storageResolvable, get(component.store).params);
34-
this._valuesStore = resolveStore(_valueStorage, storageResolvable, params);
35+
this._store = resolveStore(_storage, this._storageResolvable, get(component.store).params);
36+
this._valuesStore = resolveStore(_valueStorage, this._storageResolvable, params);
3537

3638
this._listen();
3739
}
@@ -62,10 +64,11 @@ export class ComponentParamManager {
6264
params.map((param) => (param.name === id ? { ...param, value: undefined } : param)),
6365
);
6466

67+
const fullId = `${this._storageResolvable}/${id}`;
6568
const subscriptions = get(_subscriptions);
66-
if (subscriptions[id]) {
67-
subscriptions[id]();
68-
subscriptions[id] = null;
69+
if (subscriptions[fullId]) {
70+
subscriptions[fullId]();
71+
subscriptions[fullId] = null;
6972
_subscriptions.set(subscriptions);
7073
}
7174
}
@@ -80,9 +83,10 @@ export class ComponentParamManager {
8083

8184
private _subscribe(id: string, handle: ComponentParamHandle) {
8285
setTimeout(() => {
86+
const fullId = `${this._storageResolvable}/${id}`;
8387
const subscriptions = get(_subscriptions);
84-
if (subscriptions[id]) return;
85-
subscriptions[id] = handle.value.subscribe((param) => this._update(id, param));
88+
if (subscriptions[fullId]) return;
89+
subscriptions[fullId] = handle.value.subscribe((param) => this._update(id, param));
8690
_subscriptions.set(subscriptions);
8791
}, 0);
8892
}

src/lib/client/ecs/scene/entity/entity-manager.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ export class SceneEntityManager {
6969
const entities = get(this._store);
7070
this._store.set(entities.filter((entity) => entity.id !== id));
7171

72+
const fullId = `${this.scene.id}/${id}`;
7273
const subscriptions = get(_subscriptions);
73-
if (subscriptions[id]) {
74-
subscriptions[id]();
75-
subscriptions[id] = null;
74+
if (subscriptions[fullId]) {
75+
subscriptions[fullId]();
76+
subscriptions[fullId] = null;
7677
_subscriptions.set(subscriptions);
7778
}
7879

@@ -83,9 +84,10 @@ export class SceneEntityManager {
8384

8485
private _subscribe(id: string, handle: SceneEntityHandle) {
8586
setTimeout(() => {
87+
const fullId = `${this.scene.id}/${id}`;
8688
const subscriptions = get(_subscriptions);
87-
if (subscriptions[id]) return;
88-
subscriptions[id] = handle.store.subscribe((entity) => this._update(id, entity));
89+
if (subscriptions[fullId]) return;
90+
subscriptions[fullId] = handle.store.subscribe((entity) => this._update(id, entity));
8991
_subscriptions.set(subscriptions);
9092
}, 0);
9193
}

0 commit comments

Comments
 (0)