-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: durable state snippet w/interface guard
- regions for snippets - more concise publicFacet was a poor example of a baggage key
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |