Class: Cline::Task

Inherits:
Object
  • Object
show all
Includes:
Serializable::Dir
Defined in:
lib/cline/task.rb

Overview

A task defined in a directory

Instance Attribute Summary

Attributes included from Serializable::Dir

#create, #dir

Internal collapse

Methods included from Serializable::Dir

included, #initialize_from_dir, #subpath

Instance Method Summary collapse

Constructor Details

#initialize(cline_models:) ⇒ Task

Constructor

Parameters:

  • cline_models (Models)

    The Cline models used to interpret the tasks' messages



71
72
73
# File 'lib/cline/task.rb', line 71

def initialize(cline_models:)
  @cline_models = cline_models
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



20
21
22
23
# File 'lib/cline/task.rb', line 20

def ==(other)
  other.is_a?(Task) &&
    other.messages == messages
end

#messages(create: self.create) ⇒ TaskMessages?

Get the task's messages

Parameters:

  • create (Boolean) (defaults to: self.create)

    Should the data be created if it does not exist?

Returns:

  • (TaskMessages, nil)

    The task's messages, or nil if none



12
13
14
# File 'lib/cline/task.rb', line 12

def messages(create: self.create)
  @messages ||= TaskMessages.from_cline_data(dir, cline_models: @cline_models, create:)
end

#monitor_messages(on_message:, ignore_partials: false, monitoring_interval_secs: 1) { ... } ⇒ FileMonitor?

Monitor messages with a callback called when new or updated messages arrive

Parameters:

  • on_message (#call)

    Block called each time there is a new or updated message.

    • Param message [TaskMessage] Message that has happened
    • Param last [Boolean] Is this the last message fetched from the list of messages?
    • Param previous_version [TaskMessage or nil] Previous version of this message if it got updated, or nil if it is a new one
  • ignore_partials (Boolean) (defaults to: false)

    Should we ignore partial messages? If true, then on_message will only be called for messages that have been fully received.

  • monitoring_interval_secs (Float) (defaults to: 1)

    The monitoring interval in seconds

Yields:

  • Optional code called while monitoring is in place. If used then monitoring is stopped at the end of the block's execution.

Returns:

  • (FileMonitor, nil)

    If no block has been given, return the monitor that needs to be stopped by the caller when monitoring should end.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cline/task.rb', line 38

def monitor_messages(on_message:, ignore_partials: false, monitoring_interval_secs: 1, &)
  # Keep messages per timestamp to detect updates
  messages = {}
  TaskMessages.monitor_cline_data_changes(
    dir,
    cline_models: @cline_models,
    on_change: proc do |new_messages|
      # Update the messages we have
      @messages = new_messages
      if new_messages && !new_messages.empty?
        # Filter unwanted messages
        new_messages = new_messages.reject(&:partial) if ignore_partials
        # Check for updates in all messages
        last_idx = new_messages.size - 1
        new_messages.each.with_index do |message, idx|
          ts = message.ts
          if message != messages[ts]
            on_message.call(message, idx == last_idx, messages[ts])
            messages[ts] = message
          end
        end
      end
    end,
    monitoring_interval_secs:,
    &
  )
end