Skip to content

Commit

Permalink
fix: make rename return a promise so as not to race
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Aug 23, 2020
1 parent fd5e187 commit 712b095
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
5 changes: 1 addition & 4 deletions packages/dapp-svelte-wallet/api/src/lib-dehydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ export const makeDehydrator = (initialUnnamedCount = 0) => {
!petnameToVal.has(petname),
details`petname ${petname} is already in use`,
);
assert(
!valToPetname.has(val),
details`val ${val} already has a strong name`,
);
assert(!valToPetname.has(val), details`val ${val} already has a petname`);
petnameToVal.init(petname, val);
valToPetname.init(val, petname);

Expand Down
15 changes: 6 additions & 9 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1136,29 +1136,26 @@ export async function makeWallet({
);
}

function renameIssuer(petname, issuer) {
async function renameIssuer(petname, issuer) {
assert(
brandTable.hasByIssuer(issuer),
`issuer has not been previously added`,
);
const brandRecord = brandTable.getByIssuer(issuer);
brandMapping.renamePetname(petname, brandRecord.brand);
// We don't wait for the update before returning.
updateAllState();
await updateAllState();
return `issuer ${q(petname)} successfully renamed in wallet`;
}

function renameInstance(petname, instance) {
async function renameInstance(petname, instance) {
instanceMapping.renamePetname(petname, instance);
// We don't wait for the update before returning.
updateAllState();
await updateAllState();
return `instance ${q(petname)} successfully renamed in wallet`;
}

function renameInstallation(petname, installation) {
async function renameInstallation(petname, installation) {
installationMapping.renamePetname(petname, installation);
// We don't wait for the update before returning.
updateAllState();
await updateAllState();
return `installation ${q(petname)} successfully renamed in wallet`;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dapp-svelte-wallet/api/test/test-lib-dehydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ test('makeDehydrator', async t => {
instanceHandleMapping.addPetname('automaticRefund', handle3);
console.log(`ERROR EXPECTED 'already has a petname' >>>>`);
t.throws(() => instanceHandleMapping.addPetname('simpleExchange2', handle1), {
message: `cannot add a second petname for the same value`,
message: /val \(an object\) already has a petname/,
});
console.log(`ERROR EXPECTED 'petname simpleExchange is already in use' >>>>`);
t.throws(
() => instanceHandleMapping.addPetname('simpleExchange', harden({})),
{ message: `cannot add another value for the same petname` },
{ message: /petname \(a string\) is already in use/ },
);

// Test renaming.
Expand Down
8 changes: 3 additions & 5 deletions packages/dapp-svelte-wallet/api/test/test-lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ test('lib-wallet issuer and purse methods', async t => {
});

test('lib-wallet dapp suggests issuer, instance, installation petnames', async t => {
t.plan(14);
t.plan(13);
const {
board,
zoe,
Expand Down Expand Up @@ -438,7 +438,6 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t

// `resuggesting a petname doesn't error`
await wallet.suggestInstallation('autoswap', autoswapInstallationBoardId);

await wallet.renameInstallation('automaticRefund2', installation);

t.is(
Expand All @@ -449,7 +448,6 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t

// We need this await for the pursesStateChangeLog to be updated
// by the time we check it.
// TODO: check the pursesState changes in a less fragile way.
const currentAmount2 = await E(zoeInvitePurse).getCurrentAmount();
t.deepEqual(
currentAmount2.value,
Expand Down Expand Up @@ -490,7 +488,7 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t
{ kind: 'brand', petname: 'zoe invite' },
{ kind: 'unnamed', petname: 'unnamed-4' },
{ kind: 'instance', petname: 'automaticRefund' },
{ kind: 'installation', petname: 'automaticRefund' },
{ kind: 'installation', petname: 'automaticRefund2' },
],
},
currentAmount: {
Expand All @@ -502,7 +500,7 @@ test('lib-wallet dapp suggests issuer, instance, installation petnames', async t
instance: { kind: 'instance', petname: 'automaticRefund' },
installation: {
kind: 'installation',
petname: 'automaticRefund',
petname: 'automaticRefund2',
},
},
],
Expand Down
2 changes: 2 additions & 0 deletions packages/dapp-svelte-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "@agoric/dapp-svelte-wallet",
"version": "0.1.0",
"main": "index.js",
"license": "Apache-2.0",
"author": "Agoric",
"agoric-wallet": {
"htmlBasedir": "./ui/public/wallet",
"deploy": [
Expand Down

0 comments on commit 712b095

Please sign in to comment.