Edits history of script submission #175 for ' Watch for new flows (windmillhub)'

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts"
    
    export async function main() {
      const state = await wmill.getInternalState();
      const previousLatestId = state?.latestId ?? 0;
      
      // Fetch and validate
      const data = await (await fetch('https://hub.windmill.dev/searchFlowData')).json();
      const flows = assertFlows(data.flows);
      const newFlows = flows.filter(flow => flow.flow_id > previousLatestId);
    
      // Update state
      const latestId: number = flows.reduce((prevId, flow) => {
        return Math.max(prevId, flow.flow_id);
      }, previousLatestId);
      const newState = {
        ...state,
        latestId,
      };
      await wmill.setInternalState(newState);
      console.info('old state', state);
      console.info('new state', newState);
      
      // Return new flows
      return newFlows;
    }
    
    function assertFlows(items: any): { flow_id: number }[] {
      if (!Array.isArray(items)) {
        throw Error('"flows" must be an array');
      }
      if (!items.every((item: any) => !!item && Number.isInteger(item.flow_id))) {
        throw Error('Not all flows have an integer "flow_id"');
      }
      return items;
    }

    Submitted by jaller94 1327 days ago