Skip to content

Commit

Permalink
refactor: StoredSubscribers on state
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jan 28, 2023
1 parent bee26f9 commit 42c9f2d
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
makeKindHandle,
makeScalarBigMapStore,
} from '@agoric/vat-data';
import { makeEphemeraProvider } from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/far';
import { makeInvitationsHelper } from './invitations.js';
import { makeOfferExecutor } from './offers.js';
Expand Down Expand Up @@ -129,22 +128,14 @@ const mapToRecord = map => Object.fromEntries(map.entries());
* purseBalances: MapStore<RemotePurse, Amount>,
* updatePublishKit: PublishKit<UpdateRecord>,
* currentPublishKit: PublishKit<CurrentWalletRecord>,
* storedUpdateSubscriber: StoredSubscriber<UpdateRecord>,
* storedCurrentSubscriber: StoredSubscriber<CurrentWalletRecord>,
* }>} ImmutableState
*
* @typedef {{
* }} MutableState
*/

/**
* Ephemera are the elements of state that cannot (or need not) be durable.
*
* @type {(storageNode: StorageNode) => {
* storedUpdateSubscriber: StoredSubscriber<UpdateRecord>,
* storedCurrentSubscriber: StoredSubscriber<CurrentWalletRecord>,
* }} */
// @ts-expect-error properties not defined until finish()
const provideEphemera = makeEphemeraProvider(() => ({}));

/**
*
* @param {import('@agoric/vat-data').Baggage} baggage
Expand Down Expand Up @@ -225,6 +216,17 @@ export const prepareSmartWallet = (baggage, shared) => {
/** @type {PublishKit<CurrentWalletRecord>} */
const currentPublishKit = makeWalletPublishKit();

const storedUpdateSubscriber = makeStoredSubscriber(
updatePublishKit.subscriber,
unique.walletStorageNode,
shared.publicMarshaller,
);
const storedCurrentSubscriber = makeStoredSubscriber(
currentPublishKit.subscriber,
E(unique.walletStorageNode).makeChildNode('current'),
shared.publicMarshaller,
);

const { walletStorageNode } = unique;
const nonpreciousState = {
// What purses have reported on construction and by getCurrentAmountNotifier updates.
Expand All @@ -234,6 +236,8 @@ export const prepareSmartWallet = (baggage, shared) => {
/** @type {PublishKit<CurrentWalletRecord>} */
currentPublishKit,
walletStorageNode,
storedUpdateSubscriber,
storedCurrentSubscriber,
};

return {
Expand Down Expand Up @@ -530,13 +534,11 @@ export const prepareSmartWallet = (baggage, shared) => {
},
/** @returns {StoredSubscriber<CurrentWalletRecord>} */
getCurrentSubscriber() {
const ephemera = provideEphemera(this.state.walletStorageNode);
return ephemera.storedCurrentSubscriber;
return this.state.storedCurrentSubscriber;
},
/** @returns {StoredSubscriber<UpdateRecord>} */
getUpdatesSubscriber() {
const ephemera = provideEphemera(this.state.walletStorageNode);
return ephemera.storedUpdateSubscriber;
return this.state.storedUpdateSubscriber;
},
},
},
Expand All @@ -545,18 +547,6 @@ export const prepareSmartWallet = (baggage, shared) => {
const { invitationBrand, invitationDisplayInfo, invitationIssuer } =
shared;

const ephemera = provideEphemera(state.walletStorageNode);
ephemera.storedUpdateSubscriber = makeStoredSubscriber(
state.updatePublishKit.subscriber,
state.walletStorageNode,
shared.publicMarshaller,
);
ephemera.storedCurrentSubscriber = makeStoredSubscriber(
state.currentPublishKit.subscriber,
E(state.walletStorageNode).makeChildNode('current'),
shared.publicMarshaller,
);

const { helper } = facets;

// Ensure a purse for each issuer
Expand Down

0 comments on commit 42c9f2d

Please sign in to comment.