-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Takes two or more InvitationMaker exos and combines them into a new one. - Useful for combining exos that are already instantiated
- Loading branch information
1 parent
0a7338b
commit 19fbaf1
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/orchestration/src/exos/combine-invitation-makers.js
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,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 */ |