async.js
1 /** 2 * @file shared functions for handling async behaviors 3 */ 4 import { AmountMath } from '@agoric/ertp'; 5 import { observeIteration } from '@agoric/notifier'; 6 import { lensProp, trace, view } from './utils/general.js'; 7 import { ADD_ISSUER_REFERENCE, onAddDebtTokenMint } from './state/index.js'; 8 9 const poolManagerHelpers = { 10 getSubscriber: async (keyword, adminStore) => { 11 const currentState = await view(lensProp(keyword), adminStore.getState()); 12 const observer = harden({ 13 updateState: val => 14 void adminStore.dispatch({ 15 type: val.type, 16 payload: harden(val.payload) 17 }), 18 finish: val => 19 void adminStore.dispatch({ 20 type: val.type, 21 payload: harden(val.payload) 22 }), 23 fail: reason => 24 void adminStore.dispatch({ 25 type: val.type, 26 payload: harden(val.payload) 27 }) 28 }); 29 void observeIteration( 30 Promise.resolve(currentState.marketSubscription), 31 observer 32 ); 33 } 34 }; 35 36 const yieldMints = (privateState, publicState) => async (fn, keys) => { 37 for await (const mintObject of fn(keys)) { 38 privateState.dispatch(onAddDebtTokenMint(mintObject)); 39 publicState.dispatch({ 40 type: ADD_ISSUER_REFERENCE, 41 payload: { issuer: mintObject.issuer, keyword: mintObject.keyword } 42 }); 43 } 44 return privateState.getState(); 45 }; 46 47 const makeMint = zcf => 48 async function* makeDebtMints(debtKeywords) { 49 // TODO: Refactor this into using a Task 50 for await (const name of debtKeywords) { 51 const mint = await zcf.makeZCFMint(name); 52 const details = mint.getIssuerRecord(); 53 yield { 54 mintDetails: { 55 ...details, 56 mint, 57 makeAmount: x => AmountMath.make(details.brand, x) 58 }, 59 issuer: details.issuer, 60 keyword: name, 61 collateralKeyword: name.slice(2) 62 }; 63 } 64 }; 65 export { yieldMints, makeMint, poolManagerHelpers };