简体   繁体   English

Rails-单击事件-如何处理2种Ruby方法之间的竞争情况?

[英]Rails - on click event - how to deal with race condition between 2 Ruby methods?

On my view I have a link_to element "Start Quiz!" 在我看来,我有一个link_to元素“开始测验!” that calls a controller method (method A) that sets a variable "Phase" and redirects the user to a new static page. 调用控制器方法(方法A),该方法设置变量“阶段”并将用户重定向到新的静态页面。 In my CoffeeScript I have an event listener listening for clicks on this link_to element. 在我的CoffeeScript中,我有一个事件侦听器,侦听此link_to元素的单击。 The listener calls a different method (method B) on the backend that uses the "Phase" variable to do sth. 侦听器在后端使用“ Phase”变量执行其他操作(方法B)。

The problem: sometimes method B tries to use the "Phase" variable before it has been updated by the controller method A! 问题:有时方法B会在控制器方法A更新“ Phase”变量之前尝试使用它! Does anyone have a suggestion what the best way would be to ensure that the "Phase" variable was updated before using it for anything else? 是否有人建议最好的方法是确保将“ Phase”变量更新后再用于其他用途?

I'm a Rails beginner and would appreciate any pointers. 我是Rails的初学者,不胜感激。 :) :)


EDIT: "Phase" is a variable I use to track the current question of my quiz. 编辑:“阶段”是一个变量,我用来跟踪我的测验的当前问题。 It's not dependent on any input, but it needs increment whenever a new page is rendered (which is triggered by the button click). 它不依赖于任何输入,但是无论何时呈现新页面(由按钮单击触发),它都需要增加。


EDIT2: I've now tried to solve this by introducing an instance variable flag "is_changed" to my controller and letting the method B sleep until the flag is set to true: EDIT2:我现在尝试通过向我的控制器引入实例变量标志“ is_changed”并让方法B睡眠直到标志设置为true来解决此问题:

#method B:
def send_answers
   until controller.index_changed?
     sleep(0.5)
   end
   ...
   do sth.
end

#method A (in controller):
def initialize
   @index_changed = false
end

def index_changed?
   @index_changed
end

def set_index_changed!
   @index_changed = false
end

def switch_to_next_question
   ...
   self.phase += 1
   if self.save
      Thread.new do
         @index_changed = true
         ActiveRecord::Base.connection.close
      end
   else
      ...
   end
end

It's not working. 没用 Method A gets stuck sleeping. 方法A陷入睡眠状态。 How do I fix this? 我该如何解决? And is this even the right approach? 这甚至是正确的方法吗?

Is it a problem with the scope of my @index_changed variable? 我的@index_changed变量的范围是否有问题? I'm trying to create an instance variable that is accessible from multiple methods within the class, but individual to each instance of this class. 我正在尝试创建一个实例变量,该实例变量可从该类中的多个方法访问,但对于该类的每个实例都是单独的。

Ok, I solved it by finding a way to call the 2 methods sequentially. 好的,我通过找到一种顺序调用两种方法的方法解决了这一问题。 I ended up eliminating the on click listener completely and calling the ActionCable backend method directly , which was possible by turning it into a class method: 我最终完全消除了on click监听器,并直接调用ActionCable后端方法 ,可以通过将其转换为类方法来实现:

class QuizDataChannel < ApplicationCable::Channel
  #method B:
  def self.send_answers #self. makes it a class method
    #...
    #do something with phase
  end
end

And in my controller: 在我的控制器中:

#method A:
def switch_to_next_question
   #...
   self.phase += 1
   if self.save
      QuizDataChannel.send_answers
   else
      #...
   end
end

As you can see in the end I got rid of all of these: sleep , new threads and the index_changed flag. 如您所见,最后我摆脱了所有这些问题: sleep ,新线程和index_changed标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何应对比赛条件 - How to deal with a race condition 如何处理代码中的竞争条件? - How can I deal with the race condition in the code? 如何处理事件监听器和共享 state 中的竞争条件? - How to deal with race conditions in event listeners and shared state? 如何在唤醒事件页面时防止/检测处理和恢复存储数据之间的竞争条件 - How to prevent/detect race condition between processing and restoring store data when waking up an Event page 新窗口对象和链接单击之间的竞争条件 - A race condition between new window object and link click JavaScript ES6 - 承诺解决和事件之间可能的竞争条件? - JavaScript ES6 - Possible race condition between promise resolution and event? 如何在javascript中处理竞争条件? - How to deal with race conditions in javascript? 如何处理nodejs中的竞争条件 - How to deal with race conditions in nodejs 如何解决Win.loadFile()和app.on(&#39;browser-window-created&#39;)事件中Promise解析之间的竞争条件 - How to fix race condition between Promise resolving on win.loadFile() and app.on('browser-window-created') event JavaScript中带有加载事件的竞争条件 - Race condition with load event in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM