我有以下课程:
class Vigil < ActiveRecord::Base
after_update :do_something_cool
private
def do_something_cool
# Sweet code here
end
end
class NewsFeedObserver < ActionController::Caching::Sweeper
observe Vigil
def after_update
# Create a news feed entry
end
end
Run Code Online (Sandbox Code Playgroud)
一切都按预期进行;但是,after_updatein the clearer 需要do_something_cool模型中的方法已经完成才能正常运行。问题是after_update在do_something_cool回调之前(或可能同时)调用了清扫器中的,这会导致问题。
有谁知道如何after_update在模型回调后强制清扫器中的 s 触发?有没有更好的方法来实现这一目标?
更新/修复:事实证明,与下面的答案不同,观察者回调实际上以正确的顺序触发(在模型回调之后)。当我发现这一点时,我意识到一定是其他地方出了问题。
该do_something_cool方法销毁所有守夜人的插槽,并用正确数量的正确时间替换它们。观察者依靠插槽的数量来确定守夜应该持续多长时间。所以,潜在的问题是所有守夜人的插槽都被销毁了,并且数据被缓存了,所以当我vigil.slots从观察者那里调用时,它正在使用缓存(销毁的插槽)数据。解决方案:只需在结束时调用 vigil.slots(true)do_something_cool即可重新加载/重新缓存新创建的插槽!
是否可以创建回调函数 ArrayList 添加/删除方法。
ArrayList 将包含图像 url,我想在添加/删除新元素后立即运行我的代码(更新 UI)。
我有一个为 onbeforechange 方法编写的回调函数。下面是代码:
introJs().onbeforechange(function() {
if($(this).is(":visible") != true)
if($(this).is("ui-tabs-panel") == true)
$('.ui-tabs-nav a[href$="' + $(this).attr('id') + '"]').click();
else
$(this).show();
}).start();
Run Code Online (Sandbox Code Playgroud)
逻辑是正确的(在没有调用 introJs().start() 的情况下测试)但是,在下一步之前仍然没有调用这个回调函数。有任何想法吗?
我正在编写一个带有串行 IO 的多线程 Python 应用程序,并且我在 IO 类中有这个构造:
def __init__(self):
# Register these with thread-safe functions having the arguments listed
self.callbacks_status = [] # args: (pod_index, message, color)
self.callbacks_conn = [] # args: (pod_index, message, color)
self.callbacks_angle = [] # args: (pod_index, angle_deg)
self.callbacks_brake = [] # args: (brake_on)
Run Code Online (Sandbox Code Playgroud)
然后,当我的一个更新线程获得新状态时,我每次都在做这样的事情:
for func in self.callbacks_conn:
func(i, "Open", "yellow")
Run Code Online (Sandbox Code Playgroud)
毋庸置疑,这很丑陋,而且感觉不是 Pythonic。有没有更优雅的方法来调用具有相同参数的函数列表?基本上我正在寻找map相反的功能。
我有一个简单的委托、事件和属性,允许我在事件上创建回调订阅:
public static class Test
{
/// <summary>Delegate for property changed event</summary>
public delegate void TestEventHandler();
/// <summary>Event called when value is changed</summary>
public static event TestEventHandler OnTestHappening;
/// <summary>Property to specify our test is happening</summary>
private static bool testHappening;
public static bool TestHappening
{
get
{
return testHappening;
}
set
{
testHappening = value;
// Notify our value has changed only if True
// ie. Only fire an event when we're ready as we'll hook methods to the Event …Run Code Online (Sandbox Code Playgroud) 注意:为了清楚起见,在下面的代码片段中省略了constuctors/destructors和includes.
我正在使用一个函数registerCallback需要以下参数的库:
void*"用户-数据"传输任何一个认为合适的因为我用C++编写代码而我希望我的回调函数调用一个类成员方法,由于registerCallback第一个参数的C样式,我不能直接做,我已经写了这个函数:
extern C {
void handler( void* userData ) {
(static_cast<Base*>(userData))->handle();
}
}
Run Code Online (Sandbox Code Playgroud)
我使用的registerCallback功能如下:
class Base {
public void handle() {}
};
...
Base b;
registerCallback( handler, &b );
Run Code Online (Sandbox Code Playgroud)
这到目前为止运作良好.
现在,让我们添加一些复杂性:
Baseclass有一个toto名为by 的纯虚函数handle(),我有Child1和Child2扩展的类Base.
class Base {
public:
void handle() { toto(); }
virtual void toto() = 0;
};
class Child1: public Base {
public: virtual void toto() …Run Code Online (Sandbox Code Playgroud) 首先来看看下面的简单代码:
function mySecondFunction(objArray,setFunc)
{
for (let i = 0; i < objArray.length; i++)
{
objArray[i].info.setTop(72);
}
}
function myFunction()
{
let myObjArray = [];
for (let i = 0; i < 10; i++)
{
myObjArray.push({
info:{topVar:0,
bottomVar:0,
get top() {return this.topVar;},
get bottom() {return this.bottomVar;},
setTop: function(input) {this.topVar = input;},
setBottom: function(input) {this.bottomVar = input; }
}
});
}
mySecondFunction(myObjArray); // This works Fine
mySecondFunction(myObjArray,setTop); // I want something like this!!!
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我想将对象的方法传递给另一个函数.我知道很多可能的解决方案来避免这种情况,但我想知道它是否可能.
interface CallableInterface
{
void callBackMethod();
}
class Worker
{
public static ArrayList<String> names = new ArrayList<String>();
static Timer t = new Timer();
public void addToList(String newAdd, CallableInterface callback)
{
names.add("BMW");
names.add("DODGE");
t.schedule(new TimerTask() {
@Override
public void run() {
names.add(newAdd);
}
}, 5000);
callback.callBackMethod();
}
public void printList(){
System.out.println(Worker.names);
}
}
class Boss implements CallableInterface
{
Worker w1 = new Worker();
public Boss(String carName)
{
w1.addToList(carName, this);
}
public void callBackMethod()
{
w1.printList();
}
}
public class IntroCallbacks
{
public …Run Code Online (Sandbox Code Playgroud) Array.prototype.ownMethod = function(x) {
x(this)
}
[1, 2, 3, 4].ownMethod(function(x) {
return x[1]
}) // This callback won't work .Run Code Online (Sandbox Code Playgroud)
我试图在我的First-class函数中传递数组值.这种方法对我不起作用.
PiGPIO库提供以下C风格的API函数:
typedef void (*gpioAlertFuncEx_t)(int, int, uint32_t, void *); // assumed
int gpioSetAlertFuncEx(unsigned user_gpio, gpioAlertFuncEx_t f, void *userdata)
Run Code Online (Sandbox Code Playgroud)
基本上它允许您通过回调函数处理引脚状态更改.
到现在为止还挺好.问题是将此回调包装到c ++类中.
我的方法如下:
class Pin
{
public:
Pin(_GpioPin)
{
gpioSetAlertFuncEx(_GpioPin, &PushButton::internal_gpio_callback, this );
}
void internal_callback_func(int pin, int level, uint32_t tick)
{
cout << "New level: " << pin << " " << level;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是回调函数类型不同(因为它是非静态的).并且提示错误:
error: cannot convert 'void (Pin::*)(int, int, uint32_t, void*) {aka void (Pin::*)(int, int, unsigned int, void*)}' to 'gpioAlertFuncEx_t {aka void (*)(int, int, unsigned int, void*)}' for …Run Code Online (Sandbox Code Playgroud)