Class: Pakyow::Data::Subscribers::Adapters::Memory Private

Inherits:
Object
  • Object
show all
Extended by:
Support::DeepFreeze
Defined in:
lib/pakyow/data/subscribers/adapters/memory.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Manages data subscriptions in memory.

Great for development, not for use in production!

API:

  • private

Constant Summary collapse

SERIALIZABLE_IVARS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

API:

  • private

i(
  @subscriptions_by_id
  @subscription_ids_by_source
  @subscribers_by_subscription_id
  @expirations_for_subscriber
).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemory

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Memory.

API:

  • private



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 33

def initialize(*)
  @subscriptions_by_id = Concurrent::Hash.new
  @subscription_ids_by_source = Concurrent::Hash.new
  @subscribers_by_subscription_id = Concurrent::Hash.new
  @subscription_ids_by_subscriber = Concurrent::Hash.new
  @expirations_for_subscriber = Concurrent::Hash.new

  Concurrent::TimerTask.new(execution_interval: 10, timeout_interval: 10) {
    @expirations_for_subscriber.each do |subscriber, timeout|
      if timeout < Time.now
        unsubscribe(subscriber)
      end
    end
  }.execute
end

Class Method Details

.generate_subscription_id(subscription) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



23
24
25
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 23

def generate_subscription_id(subscription)
  Digest::SHA1.hexdigest(Marshal.dump(subscription))
end

Instance Method Details

#expire(subscriber, seconds) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



71
72
73
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 71

def expire(subscriber, seconds)
  @expirations_for_subscriber[subscriber] = Time.now + seconds
end

#expiring?(subscriber) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



79
80
81
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 79

def expiring?(subscriber)
  @expirations_for_subscriber.key?(subscriber)
end

#persist(subscriber) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



75
76
77
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 75

def persist(subscriber)
  @expirations_for_subscriber.delete(subscriber)
end

#register_subscriptions(subscriptions, subscriber: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



49
50
51
52
53
54
55
56
57
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 49

def register_subscriptions(subscriptions, subscriber: nil)
  subscriptions.map { |subscription|
    self.class.generate_subscription_id(subscription).tap do |subscription_id|
      register_subscription_with_subscription_id(subscription, subscription_id)
      register_subscription_id_for_source(subscription_id, subscription[:source])
      register_subscriber_for_subscription_id(subscriber, subscription_id)
    end
  }
end

#serializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



94
95
96
97
98
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 94

def serialize
  SERIALIZABLE_IVARS.each_with_object({}) do |ivar, hash|
    hash[ivar] = instance_variable_get(ivar)
  end
end

#subscribers_for_subscription_id(subscription_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



83
84
85
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 83

def subscribers_for_subscription_id(subscription_id)
  @subscribers_by_subscription_id[subscription_id] || []
end

#subscriptions_for_source(source) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



59
60
61
62
63
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 59

def subscriptions_for_source(source)
  subscription_ids_for_source(source).map { |subscription_id|
    subscription_with_id(subscription_id)
  }
end

#unsubscribe(subscriber) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



65
66
67
68
69
# File 'lib/pakyow/data/subscribers/adapters/memory.rb', line 65

def unsubscribe(subscriber)
  subscription_ids_for_subscriber(subscriber).dup.each do |subscription_id|
    unsubscribe_subscriber_from_subscription_id(subscriber, subscription_id)
  end
end