Sha256: d70aefca4e72b9346bd1ac7211ea970a42ca3f857f61fe0ce5fc466b4b7e4bc5

Contents?: true

Size: 1.71 KB

Versions: 9

Compression:

Stored size: 1.71 KB

Contents

module Preact
  class Context
    def self.create_application_contexts
      Preact.create_context('LucidApplicationContext', { iso_location: "/", iso_store: {}, iso_theme: {} })
      Preact.create_context('RouterContext', {})
    end

    class Provider < Preact::Component
      attr_reader :context_id
      attr_reader :subs

      def initialize(props, context)
        unless props.key?(:value)
          props = props.merge({ value: get_default_value })
        end
        super(props, context)
        @context_id = get_context_id
        @child_context = { @context_id => self }
        @getChildContext = true
        @subs = []
      end

      def get_child_context
        @child_context
      end

      def render
        @props[:children]
      end

      def should_component_update?(next_props, _next_state, _context)
        if @props[:value] != next_props[:value]
          @subs.each do |subc|
            Preact._enqueue_render(subc)
          end
        end
        true
      end

      def sub(component)
        @subs.push(component)
        component.provider_subs = @subs
      end
    end

    attr_reader :context_id
    attr_reader :Provider
    attr_accessor :value

    def initialize(default_value = nil)
      @Provider = Class.new(Provider)
      @value = default_value
      @context_id = Preact._context_id
      ctx = self
      @Provider.define_method(:get_default_value) do
        ctx.value
      end
      @Provider.define_method(:get_context_id) do
        ctx.context_id
      end
    end

    def Consumer(&block)
      block_result = block.call(@value)
      Preact.render_buffer.last.push(block_result) if block_result
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
isomorfeus-preact-22.9.0.rc9 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc8 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc7 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc6 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc5 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc4 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc3 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc2 lib/preact/context.rb
isomorfeus-preact-22.9.0.rc1 lib/preact/context.rb