Sha256: ec72b2f671244449719fa90c29bda604cbcc688b91bcd329d30b3c4bd620642c

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

module Hyperstack
  module Internal
    module AutoUnmount
      def self.included(base)
        base.include(Hyperstack::Internal::Callbacks)
        base.class_eval do
          define_callback :before_unmount
        end
      end

      def unmounted?
        @__hyperstack_internal_auto_unmount_unmounted
      end

      def unmount
        run_callback(:before_unmount)
        AutoUnmount.objects_to_unmount[self].each(&:unmount)
        AutoUnmount.objects_to_unmount.delete(self)
        instance_variables.each do |var|
          val = instance_variable_get(var)
          begin
            val.unmount if val.respond_to?(:unmount)
          rescue RUBY_ENGINE == 'opal' ? JS::Error : nil
            nil
          end
        end
        @__hyperstack_internal_auto_unmount_unmounted = true
      end

      def every(*args, &block)
        return if unmounted?
        super.tap do |id|
          sself = self
          id.define_singleton_method(:unmount) { abort }
          AutoUnmount.objects_to_unmount[self] << id
        end
      end

      def after(*args, &block)
        return if unmounted?
        super.tap do |id|
          sself = self
          id.define_singleton_method(:unmount) { abort }
          AutoUnmount.objects_to_unmount[self] << id
        end
      end

      class << self
        def objects_to_unmount
          @objects_to_unmount ||= Hash.new { |h, k| h[k] = Set.new }
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hyper-state-1.0.alpha1.8 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.7 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.6 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.5 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.4 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.3 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.2 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1.1 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.0.pre.alpha1 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.alpha1 lib/hyperstack/internal/auto_unmount.rb
hyper-state-1.0.pre.alpha1 lib/hyperstack/internal/auto_unmount.rb
hyper-state-0.1 lib/hyperstack/internal/auto_unmount.rb