Sha256: 593157cf5768036cb67a45a81f9fc14e835ddce5a5a482f3f991090cf9df8bce

Contents?: true

Size: 1.57 KB

Versions: 41

Compression:

Stored size: 1.57 KB

Contents

module Preact
  class ContextWrapper
    include ::Native::Wrapper

    def initialize(native)
      @native = native
    end

    def is_wrapped_context
      true
    end

    def Consumer(*args, &block)
      %x{
        let operabu = Opal.Preact.render_buffer;
        let props = null;

        if (args.length > 0) { props = Opal.Preact.to_native_preact_props(args[0]); }

        let preact_element = Opal.global.Preact.createElement(this.native.Consumer, props, function(value) {
          let children = null;
          if (block !== nil) {
            operabu.push([]);
            // console.log("consumer pushed", operabu, operabu.toString());
            try {
              let block_result = block.$call(value);
              if (block_result && block_result !== nil) { Opal.Preact.render_block_result(block_result); }
            } catch (e) { console.error(e.message === nil ? 'error at' : e.message, e.stack); }
            // console.log("consumer popping", operabu, operabu.toString());
            children = operabu.pop();
            if (children.length === 1) { children = children[0]; }
            else if (children.length === 0) { children = null; }
          }
          return children;
        });
        operabu[operabu.length - 1].push(preact_element);
      }
    end

    def Provider(*args, &block)
      %x{
        var props = null;
        if (args.length > 0) { props = Opal.Preact.to_native_preact_props(args[0]); }
        Opal.Preact.internal_render(this.native.Provider, props, null, block);
      }
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
isomorfeus-preact-10.9.0 lib/preact/context_wrapper.rb
isomorfeus-preact-10.8.2 lib/preact/context_wrapper.rb
isomorfeus-preact-10.8.1 lib/preact/context_wrapper.rb
isomorfeus-preact-10.8.0 lib/preact/context_wrapper.rb
isomorfeus-preact-10.7.3 lib/preact/context_wrapper.rb
isomorfeus-preact-10.7.2 lib/preact/context_wrapper.rb
isomorfeus-preact-10.7.1 lib/preact/context_wrapper.rb
isomorfeus-preact-10.7.0 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.62 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.61 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.60 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.59 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.58 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.57 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.56 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.55 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.54 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.53 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.52 lib/preact/context_wrapper.rb
isomorfeus-preact-10.6.51 lib/preact/context_wrapper.rb