Skip to content

Commit

Permalink
feat: combine-invitation-makers exo
Browse files Browse the repository at this point in the history
- Takes two or more InvitationMaker exos and combines them into a new one.
- Useful for combining exos that are already instantiated
  • Loading branch information
0xpatrickdev authored and turadg committed Sep 6, 2024
1 parent 0a7338b commit 19fbaf1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/orchestration/src/exos/combine-invitation-makers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { M } from '@endo/patterns';
import {
prepareGuardedAttenuator,
makeSyncMethodCallback,
} from '@agoric/internal/src/callback.js';
import { getMethodNames } from '@agoric/internal';

/**
* @import {InvitationMakers} from '@agoric/smart-wallet/src/types.js';
* @import {Zone} from '@agoric/zone';
*/

// TODO use a helper from Endo https://github.com/endojs/endo/issues/2448
/**
* Takes two or more InvitationMaker exos and combines them into a new one.
*
* @param {Zone} zone
* @param {import('@endo/patterns').InterfaceGuard[]} interfaceGuards
*/
export const prepareCombineInvitationMakers = (zone, ...interfaceGuards) => {
const methodGuards = interfaceGuards.map(ig => ig.payload.methodGuards);
const CombinedInterfaceGuard = M.interface(
'CombinedInvitationMakers interface',
Object.assign({}, ...methodGuards),
);

const mixin = prepareGuardedAttenuator(zone, CombinedInterfaceGuard, {
tag: 'CombinedInvitationMakers',
});

/**
* @template {InvitationMakers[]} IM
* @param {IM} invitationMakers
* @returns {IM[number]}
*/
const combineInvitationMakers = (...invitationMakers) => {
const overrides = {};
for (const invMakers of invitationMakers) {
// remove '__getInterfaceGuard__', '__getMethodNames__'
const names = getMethodNames(invMakers).filter(n => !n.startsWith('__'));
for (const key of names) {
overrides[key] = makeSyncMethodCallback(invMakers, key);
}
}
return mixin({
overrides,
});
};

return combineInvitationMakers;
};

/** @typedef {ReturnType<typeof prepareCombineInvitationMakers>} MakeCombineInvitationMakers */

0 comments on commit 19fbaf1

Please sign in to comment.