Skip to content

Commit

Permalink
chore: durable state snippet w/interface guard
Browse files Browse the repository at this point in the history
 - regions for snippets
 - more concise

publicFacet was a poor example of a baggage key
  • Loading branch information
dckc committed Feb 9, 2024
1 parent 11a71dc commit 1acd85d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions snippets/zoe/src/02-state.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Far } from '@endo/far';

// #region startfn
// #region heap-state
export const start = () => {
let value = 0;
// #endregion heap-state
const get = () => value;
const set = v => (value = v);

// #region fresh-export
return {
publicFacet: Far('ValueCell', { get, set }),
};
// #endregion fresh-export
};
// #endregion startfn
57 changes: 57 additions & 0 deletions snippets/zoe/src/02b-state-durable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @ts-check
// #region contract
import { M } from '@endo/patterns';
// #region import-zone
import { makeDurableZone } from '@agoric/zone/durable.js';
// #endregion import-zone

// #region interface-guard
const RoomI = M.interface('Room', {
getId: M.call().returns(M.number()),
incr: M.call().returns(M.number()),
decr: M.call().returns(M.number()),
});

const RoomMakerI = M.interface('RoomMaker', {
makeRoom: M.call().returns(M.remotable()),
});
// #endregion interface-guard

// #region export-prepare
export const prepare = (_zcf, _privateArgs, baggage) => {
// #endregion export-prepare
// #region zone1
const zone = makeDurableZone(baggage);
const rooms = zone.mapStore('rooms');
// #endregion zone1

// #region exoclass
const makeRoom = zone.exoClass('Room', RoomI, (id) => ({ id, count: 0 }), {
getId() {
return this.state.id;
},
incr() {
this.state.count += 1;
return this.state.count;
},
decr() {
this.state.count -= 1;
return this.state.count;
},
});
// #endregion exoclass

// #region exo
const publicFacet = zone.exo('RoomMaker', RoomMakerI, {
makeRoom() {
const room = makeRoom();
const id = rooms.size;
rooms.init(id, room);
return room;
},
});

return { publicFacet };
// #endregion exo
};
// #endregion contract

0 comments on commit 1acd85d

Please sign in to comment.