apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Stop Polling Programatically

Open Maxondria opened this issue 7 years ago • 6 comments

Hullo,

How do I stop polling when I change a component? Or after receiving a certain result?

 apollo: {
        patientAppointmentStatus: {
            query: AppStatusChecker,
            skip: true,
            pollInterval: 300, // ms
            result (data){
              if(data == x){
                 //STOP POLLING HERE,
                setting the skip option to false never seems to work 
              }
           }

thank you

Maxondria avatar Apr 15 '19 20:04 Maxondria

Generally the answer with vue-apollo is "turn it into a function". This gives you access to the component scope, and then you can do whatever you want from there.

Most of the properties also can become a function (like query or variables), but pollInterval is not one of them, so you have to turn the whole smart query into a function. Here you go:

apollo: {
  patientAppointmentStatus () {
    return {
      query: AppStatusChecker,
      skip: true,
      pollInterval: this.shouldPoll ? 300 : null, // ms
      result (data) {
        if (data == x) {
          this.shouldPoll = false
        }
      }

    }
  }
}

bbugh avatar Apr 18 '19 20:04 bbugh

Awesome! Great... Let me try this!

Maxondria avatar Apr 18 '19 20:04 Maxondria

@Maxondria Did you get this to work? I did some experiments with this but the query does not seem to respond to this.shouldPoll being set to false, instead it keeps polling. Any ideas?

pwgn avatar Jun 28 '19 20:06 pwgn

this.$apollo.queries.patientAppointmentStatus.stopPolling() solved it. Call it anywhere you would want to stop the polling

Maxondria avatar Jun 28 '19 21:06 Maxondria

Thx for the response! Actually I got it to work with stopPolling(), however it seems to only work within the query function itself. Eg:

apollo: {
  patientAppointmentStatus () {
    return {
      query: AppStatusChecker,
      skip: true,
      pollInterval: 300, // ms
      result (data) {
        if (data == x) {
          this.$apollo.queries.patientAppointmentStatus.stopPolling()
        }
      }

    }
  }
}

But I would like to call stopPolling() from one of the methods:

  methods: {
    stopPoll () {
      this.$apollo.queries.thread.stopPolling()
    }
  },

After stopPoll is called the polling continues anyway. Guess Ill have a look at subscriptions instead :)

pwgn avatar Jun 29 '19 07:06 pwgn

Thx for the response! Actually I got it to work with stopPolling(), however it seems to only work within the query function itself. Eg:

apollo: {
  patientAppointmentStatus () {
    return {
      query: AppStatusChecker,
      skip: true,
      pollInterval: 300, // ms
      result (data) {
        if (data == x) {
          this.$apollo.queries.patientAppointmentStatus.stopPolling()
        }
      }

    }
  }
}

But I would like to call stopPolling() from one of the methods:

  methods: {
    stopPoll () {
      this.$apollo.queries.thread.stopPolling()
    }
  },

After stopPoll is called the polling continues anyway. Guess Ill have a look at subscriptions instead :)

Oh, I really can see that in this this.$apollo.queries.thread.stopPolling(), you're calling the thread query to stop polling. is it the right query? Because this should really work. I can call that line of code in any of the component methods. It can only poll again after you reload the component, or leave it and come back to it later in routing

Maxondria avatar Jun 29 '19 07:06 Maxondria