Skip to content

Commit

Permalink
fix: multiple appends (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
npaton committed Sep 2, 2023
1 parent 2d7972a commit b6cc07f
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/fix-multi-append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@empirica/core": patch
---

Fix issue where mutliple appends were called in a row on the same key and only
the last item was added.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ test-results
e2e-tests/test-experiment-*
e2e-tests/cache

out
out
NOTES
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/charmbracelet/lipgloss v0.5.0
github.com/cortesi/moddwatch v0.0.0-20210323234936-df014e95c743
github.com/davecgh/go-spew v1.1.1
github.com/empiricaly/tajriba v1.3.0
github.com/empiricaly/tajriba v1.3.1
github.com/go-playground/validator/v10 v10.11.0
github.com/jpillora/backoff v1.0.0
github.com/json-iterator/go v1.1.12
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/empiricaly/tajriba v1.3.0 h1:HF02xD7NpGXMjQAbLvf/39ADu1WgTRYrsOQ1xBA8jRM=
github.com/empiricaly/tajriba v1.3.0/go.mod h1:ujHaXC+YBXCz5ELgYihM5F2x3LbLBM32rCyZoC31tIE=
github.com/empiricaly/tajriba v1.3.1 h1:lYfJ5h9vMJjFVQC5w/5dolvCR1KTbmVOD7yqDJLlIt8=
github.com/empiricaly/tajriba v1.3.1/go.mod h1:ujHaXC+YBXCz5ELgYihM5F2x3LbLBM32rCyZoC31tIE=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
17 changes: 16 additions & 1 deletion lib/@empirica/core/src/admin/runloop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,32 @@ export class Runloop<
// If the same key is set twice within the same loop, only send 1
// setAttribute update.
const uniqueAttrs: { [key: string]: SetAttributeInput } = {};
let appendCount = 0;
for (const attr of this.attributeInputs) {
if (!attr.nodeID) {
error(`runloop: attribute without nodeID: ${JSON.stringify(attr)}`);
continue;
}

uniqueAttrs[`${attr.nodeID}-${attr.key}`] = attr;
let key = `${attr.nodeID}-${attr.key}`;

if (attr.append) {
key += `-${appendCount++}`;
}

if (attr.index !== undefined) {
key += `-${attr.index}`;
}

console.log(key);

uniqueAttrs[key] = attr;
}

const attrs = Object.values(uniqueAttrs);

console.log(attrs);

promises.push(this.taj.setAttributes(attrs));
this.attributeInputs = [];
}
Expand Down
19 changes: 12 additions & 7 deletions lib/@empirica/core/src/shared/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,25 @@ export class Attribute {
ao?: Partial<AttributeOptions>,
item?: boolean
): SetAttributeInput {
if (!item && ao?.append && ao!.index === undefined) {
ao!.index = this.attrs?.length || 0;
if (ao?.append !== undefined && ao!.index !== undefined) {
error(`cannot set both append and index`);

throw new Error(`cannot set both append and index`);
}

if (!item && ao?.index !== undefined) {
const index = ao!.index!;
if (!item && (ao?.index !== undefined || ao?.append)) {
let index = ao!.index || 0;
if (ao?.append) {
index = this.attrs?.length || 0;
}

if (!this.attrs) {
this.attrs = [];
}

if (index + 1 > (this.attrs?.length || 0)) {
this.attrs.length = index! + 1;
}
// if (index + 1 > (this.attrs?.length || 0)) {
// this.attrs.length = index! + 1;
// }

if (!this.attrs[index]) {
this.attrs[index] = new Attribute(
Expand Down

0 comments on commit b6cc07f

Please sign in to comment.