dhenning
3/9/2020 - 5:00 PM

Generated by XState Viz: https://xstate.js.org/viz

Generated by XState Viz: https://xstate.js.org/viz


  // Available variables:
  // - Machine
  // - interpret
  // - assign
  // - send
  // - sendParent
  // - spawn
  // - raise
  // - actions
  // - XState (all XState exports)
  
  const states_getInfo_A = {
    initial: 'start',
    states: {
      start: {
        entry: ['aSendReqInfo_A'],
        on: {
          tInputReady:   {actions:'aReadToken'},
          tRcvdSrvReqId: {target: 'SendId'},
        }},
      SendId: {
        entry: ['aSendId'],
        on: {
          tInputReady: {actions: 'aReadToken'},
          tRcvdInfo_A: {
            actions: ['aSaveInfo_A'],
            target: 'end',
          }
        }},
      end: {
        entry: 'Push_tGotInfo',
        type: 'final',
      }
    }
  };
  
  const states_getInfo_B = {
    initial: 'start',
    states: {
      start: {
        entry: ['aSendReqInfo_B'],
        on: {
          tInputReady: {actions:'aReadToken'},
          tRcvdInfo_C: {
            actions: ['aSaveInfo_B'],
            target: 'end',
          }
        }},
      end: {
        entry: 'Push_tGotInfo',
        type: 'final',
      }
    }
  };
  
  const goalMachine = Machine({
    id: 'serverInfoProvider',
    initial: 'disconnected',
    context: { retries: 0 },
    states: {
      disconnected: {
        on: {
          tGetInfo_A: {
            target: 'Open',
            actions: 'aPushGoal_GetInfo_A'
          },
          tGetInfo_B: {
            target: 'Open',
            actions: 'aPushGoal_GetInfo_B'
          },
        },
      },
      
      Open: {
        entry: ['aSocketOpen'],
        on: {
          tOpenSuccess: {
            target: 'Opened',
            actions: 'aOpened'
          },
          tRetry: [
            {
              actions: 'aOpenRetry',
              cond: 'retryValid',
            },
            {
              actions: 'aOpenTimeout',
              target: 'End',
            }
          ],
          tNetError: {
            target: 'End',
            actions: 'aOpenError'
          },
        },
      },

      Opened: {
        on: {
          tInputReady: {actions:'aReadToken'},
          tRcvdSrvInfo:{actions:'aSaveSrvInfo'},
          tRcvdSrvReqClientId: {
            actions: 'aSendClientId'
          },
          tSendSuccess:{target: 'ClientIdSent'},
          tNetError: {
            target: 'End',
            actions: 'aOpenedError'
          },
        }
      },

      ClientIdSent: {
        //entry: ['aSendTermId'],
        on: {
          tInputReady: {actions:'aReadToken'},
          tRcvdSrvReqClientVer: {
            actions: ['aSendClientVersion'],
          },
          tSendSuccess:{target: 'ClientVerSent'},
          tNetError: {
            target: 'End',
            actions: 'aClientIdError'
          },
        }
      },

      ClientVerSent: {
        on: {
          tInputReady: {actions:'aReadToken'},
          tRcvdSessionId: {
            actions: ['aSaveSessId'],
            target: 'Connected',
          },
          tNetError: {
            target: 'End',
            actions: 'aClientVerError'
          },
        }        
      },
      
      Connected: {
        entry: ['aPopGoal'],
        on: {
          tGetInfo_A: 'GetInfo_A',
          tGetInfo_B: 'GetInfo_B',
        }
      },
      
      GetInfo_A: {
        on: {
          tGotInfo: {
            target: 'Connected',
          },
          tNetError: {
            target: 'End',
            actions: 'aGetInfoAError'
          },
        },
        ...states_getInfo_A
      },
      
      GetInfo_B: {
        on: {
          tGotTracks: {
            target: 'Connected',
          },
          tNetError: {
            target: 'End',
            actions: 'aGetInfoBError'
          },
        },
        ...states_getInfo_B
      },

      End: {
        type: 'final'
      },
      

      
    }
  },
  {
    guards: {
      retryValid: (context, event) => {
        return true;
      }
    }
  },
  {
    actions: {
      // action implementations
      commStart: (context, event) => {
        console.log('commStarting...');
        socket.open("1.2.3.4", 1234,
          ()=> { //success callback
          },
          (err)=> { //error callback
          }
        );
      },
      activate: (context, event) => {
        console.log('activating...');
      },
      notifyActive: (context, event) => {
        console.log('active!');
      },
      notifyInactive: (context, event) => {
        console.log('inactive!');
      },
      sendTelemetry: (context, event) => {
        console.log('time:', Date.now());
      }
    }
  }
    
);