Class: RocketChat::Realtime::AsyncTask

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Concurrent::Promises::FactoryMethods, Singleton
Defined in:
lib/rocket_chat/realtime/async_task.rb

Overview

AsyncTask resolver

Since:

  • 0.1.0

Constant Summary collapse

TASK_TIMEOUT =

Since:

  • 0.1.0

60

Instance Method Summary collapse

Constructor Details

#initializeAsyncTask

Returns a new instance of AsyncTask.

Since:

  • 0.1.0



27
28
29
# File 'lib/rocket_chat/realtime/async_task.rb', line 27

def initialize
  @tasks = Concurrent::Map.new
end

Instance Method Details

#resolve(id, result = nil) ⇒ Concurrent::Promises::ResolvableFuture

Resolve task

Parameters:

  • result (RocketChat::Realtime::ReslutMessage) (defaults to: nil)

Returns:

  • (Concurrent::Promises::ResolvableFuture)

Since:

  • 0.1.0



53
54
55
56
57
# File 'lib/rocket_chat/realtime/async_task.rb', line 53

def resolve(id, result = nil)
  task = @tasks.delete(id)
  task&.resolve true, result
  task
end

#start(id) ⇒ Concurrent::Promises::ResolvableFuture

Register a new task

Parameters:

  • id (String)

    task id

  • block (Proc)

    async taks to execute

Returns:

  • (Concurrent::Promises::ResolvableFuture)

Since:

  • 0.1.0



39
40
41
42
43
44
# File 'lib/rocket_chat/realtime/async_task.rb', line 39

def start(id)
  # TODO: check for atomic
  yield if block_given?
  Concurrent::ScheduledTask.execute(TASK_TIMEOUT) { @tasks.delete(id)&.reject(:timeout) }
  @tasks.fetch_or_store(id, resolvable_future)
end