Skip to content

Instantly share code, notes, and snippets.

@anilhelvaci
Last active February 20, 2024 11:56
Show Gist options
  • Save anilhelvaci/966f00260e069e55e276937dd232a4a1 to your computer and use it in GitHub Desktop.
Save anilhelvaci/966f00260e069e55e276937dd232a4a1 to your computer and use it in GitHub Desktop.
Mutate Exo Class Stete

Mutating Exo Class State

I'm confused about how to mutate the state of an exo class. The test above reproduces the prpblem I'm facing. The part that confuses me the most is that we're able to mutate the state object in contracts like vaultManager. The reason why I'm vaultManager confuses me is that zone API and prepareExoClassKit(what vaultManager uses) use the same code underneeth, as far as I know. Could use a clarification here.

import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { heapZone } from '@agoric/zone';
const prepareTestExo = zone => {
const makeTestExo = zone.exoClass(
'TestExo',
undefined,
() => {
const wantToMutate = harden({
a: '1',
b: 2,
});
return harden({ wantToMutate });
},
{
mutate(newVal) {
const { state } = this;
const { wantToMutate: oldVal } = state;
state.wantToMutate = harden({
...oldVal,
...newVal,
});
},
getState() {
console.log('this', this);
const { state } = this;
return harden({ ...state });
},
},
);
return makeTestExo;
};
test('initial', t => {
const makeTestExo = prepareTestExo(heapZone);
const testExo = makeTestExo();
const stateOne = testExo.getState();
t.deepEqual(stateOne, {
wantToMutate: {
a: '1',
b: 2,
},
});
t.throws(() =>
testExo.mutate({
a: 4,
}),
);
const stateTwo = testExo.getState();
t.notDeepEqual(stateTwo, {
wantToMutate: {
a: 4,
b: 2,
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment