Class: Rufio::AsyncScannerPromise

Inherits:
Object
  • Object
show all
Defined in:
lib/rufio/async_scanner_promise.rb

Overview

Promise風インターフェースで非同期スキャンを扱うクラス

使用例:

scanner = NativeScannerRubyCore.new
AsyncScannerPromise.new(scanner)
  .scan_async('/path')
  .then { |entries| entries.select { |e| e[:type] == 'file' } }
  .then { |files| files.map { |f| f[:name] } }
  .wait

Instance Method Summary collapse

Constructor Details

#initialize(scanner) ⇒ AsyncScannerPromise

Returns a new instance of AsyncScannerPromise.



15
16
17
18
# File 'lib/rufio/async_scanner_promise.rb', line 15

def initialize(scanner)
  @scanner = scanner
  @callbacks = []
end

Instance Method Details

#scan_async(path) ⇒ AsyncScannerPromise

非同期スキャンを開始

Parameters:

  • path (String)

    スキャンするディレクトリのパス

Returns:



24
25
26
27
# File 'lib/rufio/async_scanner_promise.rb', line 24

def scan_async(path)
  @scanner.scan_async(path)
  self
end

#scan_fast_async(path, max_entries) ⇒ AsyncScannerPromise

高速スキャンを開始(エントリ数制限付き)

Parameters:

  • path (String)

    スキャンするディレクトリのパス

  • max_entries (Integer)

    最大エントリ数

Returns:



34
35
36
37
# File 'lib/rufio/async_scanner_promise.rb', line 34

def scan_fast_async(path, max_entries)
  @scanner.scan_fast_async(path, max_entries)
  self
end

#then {|result| ... } ⇒ AsyncScannerPromise

コールバックを登録

Yields:

  • (result)

    前のステップの結果を受け取るブロック

Returns:



43
44
45
46
# File 'lib/rufio/async_scanner_promise.rb', line 43

def then(&block)
  @callbacks << block if block_given?
  self
end

#wait(timeout: nil) ⇒ Object

スキャン完了を待ち、コールバックを順次実行

Parameters:

  • timeout (Integer, nil) (defaults to: nil)

    タイムアウト秒数(オプション)

Returns:

  • (Object)

    最後のコールバックの戻り値、またはスキャン結果



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rufio/async_scanner_promise.rb', line 52

def wait(timeout: nil)
  result = @scanner.wait(timeout: timeout)

  # 登録されたコールバックを順次実行
  @callbacks.each do |callback|
    result = callback.call(result)
  end

  result
ensure
  # 完了後はスキャナーを自動的にクローズ
  @scanner.close
end