import * as cloud from "@pulumi/cloud" ; export type PollFunction < T > = ( lastToken ? : string ) => Promise < { items : T [ ] ; nextToken : string ; } > ; let pollMarkers = new cloud . Table ( "__pollMarkers" ) ; export function poll < T > ( name , rate , poller ) { let topic = new cloud . Topic < T > ( name ) ; cloud . timer . interval ( name , rate , async ( ) => { console . log ( `Starting polling...` ) ; console . log ( `Getting pollMarker for ${ name }` ) ; let pollMarker = await pollMarkers . get ( { id : name } ) ; console . log ( `pollMarker is ${ JSON . stringify ( pollMarker , null , "" ) }` ) ; let lastToken ; if ( pollMarker !== undefined ) { lastToken = pollMarker . lastToken ; } console . log ( `lastToken is ${ lastToken }` ) ; console . log ( "Polling for results..." ) ; let results = await poller ( lastToken ) ; console . log ( `Got ${ results . items . length } results...` ) ; pollMarkers . update ( { id : name } , { lastToken : results . nextToken } ) ; console . log ( `Updating pollmarker ${ name } to ${ results . nextToken }...` ) ; console . log ( `Publishing results to topic '${ name }'...` ) ; for ( let result of results . items ) { await topic . publish ( result ) ; } console . log ( "Done publishing results..." ) ; console . log ( `Done polling...` ) ; } ) ; return { subscribe : topic . subscribe } ; }