diff --git a/.changeset/attribute-equality.md b/.changeset/attribute-equality.md new file mode 100644 index 00000000..574cdc38 --- /dev/null +++ b/.changeset/attribute-equality.md @@ -0,0 +1,14 @@ +--- +"@empirica/core": patch +--- + +Fix setting attributes with the same mutated object as the current one. + +For example, before this patch, the value would not be saved, since we are +reusing the same object, which we've only mutated in place: + +```js +const value = player.get("myobject"); +value["mykey"] = "myvalue"; +player.set("myobject", value); +``` diff --git a/lib/@empirica/core/src/shared/attributes.ts b/lib/@empirica/core/src/shared/attributes.ts index 26be6ee0..ed5ece31 100644 --- a/lib/@empirica/core/src/shared/attributes.ts +++ b/lib/@empirica/core/src/shared/attributes.ts @@ -236,6 +236,7 @@ export class Attribute { private attrs?: Attribute[]; private val = new BehaviorSubject(undefined); + private serVal?: string; constructor( private setAttributes: (input: SetAttributeInput[]) => Promise, @@ -294,6 +295,8 @@ export class Attribute { throw new Error(`cannot set both append and index`); } + const serVal = JSON.stringify(value); + if (!item && (ao?.index !== undefined || ao?.append)) { let index = ao!.index || 0; if (ao?.append) { @@ -316,7 +319,7 @@ export class Attribute { ); } else { const existing = this.attrs[index]; - if (existing && existing.value === value) { + if (existing && existing.serVal === serVal) { return; } } @@ -325,17 +328,19 @@ export class Attribute { const v = this._recalcVectorVal(); this.val.next(v); } else { - if (this.value === value) { + if (this.serVal === serVal) { return; } this.val.next(value); } + this.serVal = serVal; + const attrProps: SetAttributeInput = { key: this.key, nodeID: this.scopeID, - val: JSON.stringify(value), + val: serVal, }; if (ao) { diff --git a/lib/admin-ui/src/components/batches/NewBatch.svelte b/lib/admin-ui/src/components/batches/NewBatch.svelte index 1c4bcf21..5851e1e3 100644 --- a/lib/admin-ui/src/components/batches/NewBatch.svelte +++ b/lib/admin-ui/src/components/batches/NewBatch.svelte @@ -14,10 +14,10 @@