Class: ZeroRuby::PushProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_ruby/push_processor.rb

Overview

Processes Zero push requests with LMID tracking, version validation, and transaction support. This implements the same protocol as Zero’s TypeScript implementation.

Examples:

Basic usage

processor = PushProcessor.new(
  schema: ZeroSchema,
  lmid_store: ZeroRuby.configuration.lmid_store_instance
)
result = processor.process(push_data, context)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema:, lmid_store:) ⇒ PushProcessor

Returns a new instance of PushProcessor.

Parameters:

  • schema (Class)

    The schema class for mutation processing

  • lmid_store (LmidStore)

    The LMID store instance



22
23
24
25
# File 'lib/zero_ruby/push_processor.rb', line 22

def initialize(schema:, lmid_store:)
  @schema = schema
  @lmid_store = lmid_store
end

Instance Attribute Details

#lmid_storeObject (readonly)

Returns the value of attribute lmid_store.



18
19
20
# File 'lib/zero_ruby/push_processor.rb', line 18

def lmid_store
  @lmid_store
end

#schemaObject (readonly)

Returns the value of attribute schema.



18
19
20
# File 'lib/zero_ruby/push_processor.rb', line 18

def schema
  @schema
end

Instance Method Details

#process(push_data, context) ⇒ Hash

Process a Zero push request

Parameters:

  • push_data (Hash)

    The parsed push request body

  • context (Hash)

    Context to pass to mutations

Returns:

  • (Hash)

    The response hash



32
33
34
35
36
37
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
65
66
67
68
# File 'lib/zero_ruby/push_processor.rb', line 32

def process(push_data, context)
  client_group_id = push_data["clientGroupID"]
  mutations = push_data["mutations"] || []
  results = []

  mutations.each_with_index do |mutation_data, index|
    if mutation_data["name"] == "_zero_cleanupResults"
      handle_cleanup_results(mutation_data)
      next
    end

    result = process_mutation_with_lmid(mutation_data, client_group_id, context)
    results << result
  rescue OutOfOrderMutationError => e
    # Return top-level PushFailedBody with all unprocessed mutation IDs
    unprocessed_ids = mutations[index..].map { |m| {id: m["id"], clientID: m["clientID"]} }
    return {
      kind: "PushFailed",
      origin: "server",
      reason: "oooMutation",
      message: e.message,
      mutationIDs: unprocessed_ids
    }
  rescue TransactionError => e
    # Database errors trigger top-level PushFailed per Zero protocol
    unprocessed_ids = mutations[index..].map { |m| {id: m["id"], clientID: m["clientID"]} }
    return {
      kind: "PushFailed",
      origin: "server",
      reason: "database",
      message: e.message,
      mutationIDs: unprocessed_ids
    }
  end

  {mutations: results}
end