gpt4 book ai didi

ruby - MacRuby 中的 FSEvents : wrong callback is executed

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:46 26 4
gpt4 key购买 nike

我正在 MacRuby 中开发 GUI 应用程序,我需要使用 FSEvents。我正在为不同的目录注册几个流。任何这些目录中的更改都会导致运行回调,但存在一个大问题:无论哪个目录发生更改,都会执行最后注册的回调。

下面是独立的测试脚本:

framework 'Cocoa'
framework 'CoreServices'

class Monitor
def initialize(dir)
@dir = dir
end

def start(&block)
callback = Proc.new do |stream, context, count, paths, flags, ids|
p @dir
block.call
end

flags = KFSEventStreamCreateFlagUseCFTypes

@stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, [@dir], KFSEventStreamEventIdSinceNow, 0.0, flags)
FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)
FSEventStreamStart(@stream)
end
end

Monitor.new(Dir.pwd + "/dir1").start { p "dir1" }
Monitor.new(Dir.pwd + "/dir2").start { p "dir2" }
Monitor.new(Dir.pwd + "/dir3").start { p "dir3" }

app = NSApplication.sharedApplication
app.run

当我运行它并开始修改这些目录时:

~/tmp/fsevents $ touch dir1/test
~/tmp/fsevents $ touch dir2/test
~/tmp/fsevents $ touch dir3/test

输出是:

"/Users/janek/tmp/fsevents/dir3"
"dir3"
"/Users/janek/tmp/fsevents/dir3"
"dir3"
"/Users/janek/tmp/fsevents/dir3"
"dir3"

我更希望的是:

"/Users/janek/tmp/fsevents/dir1"
"dir1"
"/Users/janek/tmp/fsevents/dir2"
"dir2"
"/Users/janek/tmp/fsevents/dir3"
"dir3"

也许我可以通过上下文参数提供我需要的数据来解决这个问题(因为在回调中检查 paths 会显示实际更改的目录),但是,当前的行为仍然是我完全出乎意料的.

我使用的是 OS X 10.8.2 (12C60) 和 MacRuby 0.12 (ruby 1.9.2) [universal-darwin10.0, x86_64]。

最佳答案

是的,这真的很奇怪。我也有这种行为。似乎总是调用最新注册的回调。但从好的方面来说,从第四个参数到回调,有可能获得实际调用的目录的路径。我不得不使用这样的结构。

我不是 ruby​​ 爱好者,但这段代码适合我。

framework 'Cocoa'
framework 'CoreServices'


class Monitor
@@registry = {}
def self.register(dir, other_data)
@@registry[dir] = other_data
end

def initialize(dir, other_data)
@dir = dir

self.class.register(dir, other_data)
callback = Proc.new do |stream, context, count, paths, flags, ids|
paths.cast!('*')

p "the callback that triggered has closure variable @dir=#{@dir}"
p "but the actual callback said the dir was #{paths[0]}"
p "the metadata that I stored associated with that directory is #{@@registry[paths[0]]}"
end


@stream = FSEventStreamCreate(KCFAllocatorDefault, callback, nil, [@dir], KFSEventStreamEventIdSinceNow, 0.0, 0)
FSEventStreamScheduleWithRunLoop(@stream, CFRunLoopGetCurrent(), KCFRunLoopDefaultMode)
FSEventStreamStart(@stream)
end
end

Monitor.new(Dir.pwd + "/dir1/", 'dir1 data')
Monitor.new(Dir.pwd + "/dir2/", 'dir2 data')
Monitor.new(Dir.pwd + "/dir3/", 'dir3 data')

app = NSApplication.sharedApplication
app.run

这是我看到的输出:

rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ macruby fsevents.rb &
[1] 14638

rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ touch dir1/mao

rmcgibbo@Roberts-MacBook-Pro-2 ~/local/fsync
$ "the callback that triggered has closure variable @dir=/Users/rmcgibbo/local/fsync/dir3/"
"but the actual callback said the dir was /Users/rmcgibbo/local/fsync/dir1/"
"the metadata that I stored associated with that directory is dir1 data"

关于ruby - MacRuby 中的 FSEvents : wrong callback is executed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com