<commit_before>module RMXViewControllerPresentation

  def self.included(klass)
    klass.send(:include, InstanceMethods)
  end

  module FactoryMethods

    # presentViewController should always be called on the next runloop to avoid quirks.
    # this just wraps that behavior
    def present(opts)
      unless [ :origin, :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
        raise "Missing RMXViewControllerPresentation.present opts: #{opts.inspect}"
      end
      RACScheduler.mainThreadScheduler.schedule(-> {
        opts[:origin].presentViewController(opts[:view_controller], animated:opts[:animated], completion:opts[:completion])
      })
    end

    # remove the controller from the display heirarchy, taking into account how it is
    # currently presented.
    def dismiss(opts)
      unless [ :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
        raise "Missing RMXViewControllerPresentation.dismiss opts: #{opts.inspect}"
      end
      animated = opts[:animated]
      block = opts[:completion]
      view_controller = opts[:view_controller]
      navigationController = view_controller.navigationController


      if view_controller.presentingViewController
        RACScheduler.mainThreadScheduler.schedule(-> {
          view_controller.dismissViewControllerAnimated(animated, completion:block)
        })
      elsif navigationController
        if index = navigationController.viewControllers.index(view_controller)
          before_index = index - 1
          before_index = 0 if index < 0
          pop_to_controller = navigationController.viewControllers[before_index]
          if pop_to_controller && pop_to_controller != navigationController.viewControllers.last
            # p "pop_to_controller", pop_to_controller
            # p "navigationController.popToViewController(pop_to_controller, animated:animated)"
            RACScheduler.mainThreadScheduler.schedule(-> {
              if block
                pop_to_controller.rac_signalForSelector('viewDidAppear:')
                .timeout(2, onScheduler:RACScheduler.mainThreadScheduler)
                .take(1)
                .subscribeNext(->(args) {
                  block.call
                }, error:->(error) {
                })
              end
              navigationController.popToViewController(pop_to_controller, animated:animated)
            })
          end
        end
      end
    end

  end
  extend FactoryMethods

  module InstanceMethods

    def viewStateSignal
      @viewStateSignal ||= begin
        sub = RACReplaySubject.replaySubjectWithCapacity(1)

        RACSignal.merge([
          rac_signalForSelector('viewWillAppear:').map(->(tuple) { [ :viewWillAppear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewDidAppear:').map(->(tuple) { [ :viewDidAppear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewWillDisappear:').map(->(tuple) { [ :viewWillDisappear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewDidDisappear:').map(->(tuple) { [ :viewDidDisappear, tuple.first ] }.rmx_unsafe!)
        ])
        .takeUntil(rac_willDeallocSignal)
        .subscribeNext(->(v) {
          sub.sendNext(v)
        }.rmx_unsafe!)

        rac_signalForSelector('viewWillAppear:').subscribeNext(->(tuple) { appearing(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewDidAppear:').subscribeNext(->(tuple) { appeared(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewWillDisappear:').subscribeNext(->(tuple) { disappearing(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewDidDisappear:').subscribeNext(->(tuple) { disappeared(tuple.first) }.rmx_unsafe!)

        sub.takeUntil(rac_willDeallocSignal).subscribeOn(RACScheduler.mainThreadScheduler).deliverOn(RACScheduler.mainThreadScheduler)
      end
    end

    def viewStateFilteredSignal(state)
      viewStateSignal
      .filter(->((_state, animated)) {
        state == _state
      }.rmx_weak!)
    end

    def whenOrIfViewState(viewState, &block)
      viewStateFilteredSignal(viewState)
      .take(1)
      .subscribeNext(->(v) {
        block.call
      })
    end

    def appearing(animated)
    end

    def appeared(animated)
    end

    def disappearing(animated)
    end

    def disappeared(animated)
    end

    def present(vc, animated=false, &block)
      RMXViewControllerPresentation.present({
        :origin => self,
        :view_controller => vc,
        :animated => animated,
        :completion => block
      })
    end
    
    def dismiss(animated=false, &block)
      RMXViewControllerPresentation.dismiss({
        :view_controller => self,
        :animated => animated,
        :completion => block
      })
    end
  end
end
<commit_msg>move takeUntil after subscribeOn/deliverOn so it works<commit_after>module RMXViewControllerPresentation

  def self.included(klass)
    klass.send(:include, InstanceMethods)
  end

  module FactoryMethods

    # presentViewController should always be called on the next runloop to avoid quirks.
    # this just wraps that behavior
    def present(opts)
      unless [ :origin, :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
        raise "Missing RMXViewControllerPresentation.present opts: #{opts.inspect}"
      end
      RACScheduler.mainThreadScheduler.schedule(-> {
        opts[:origin].presentViewController(opts[:view_controller], animated:opts[:animated], completion:opts[:completion])
      })
    end

    # remove the controller from the display heirarchy, taking into account how it is
    # currently presented.
    def dismiss(opts)
      unless [ :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
        raise "Missing RMXViewControllerPresentation.dismiss opts: #{opts.inspect}"
      end
      animated = opts[:animated]
      block = opts[:completion]
      view_controller = opts[:view_controller]
      navigationController = view_controller.navigationController


      if view_controller.presentingViewController
        RACScheduler.mainThreadScheduler.schedule(-> {
          view_controller.dismissViewControllerAnimated(animated, completion:block)
        })
      elsif navigationController
        if index = navigationController.viewControllers.index(view_controller)
          before_index = index - 1
          before_index = 0 if index < 0
          pop_to_controller = navigationController.viewControllers[before_index]
          if pop_to_controller && pop_to_controller != navigationController.viewControllers.last
            # p "pop_to_controller", pop_to_controller
            # p "navigationController.popToViewController(pop_to_controller, animated:animated)"
            RACScheduler.mainThreadScheduler.schedule(-> {
              if block
                pop_to_controller.rac_signalForSelector('viewDidAppear:')
                .timeout(2, onScheduler:RACScheduler.mainThreadScheduler)
                .take(1)
                .subscribeNext(->(args) {
                  block.call
                }, error:->(error) {
                })
              end
              navigationController.popToViewController(pop_to_controller, animated:animated)
            })
          end
        end
      end
    end

  end
  extend FactoryMethods

  module InstanceMethods

    def viewStateSignal
      @viewStateSignal ||= begin
        sub = RACReplaySubject.replaySubjectWithCapacity(1)

        RACSignal.merge([
          rac_signalForSelector('viewWillAppear:').map(->(tuple) { [ :viewWillAppear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewDidAppear:').map(->(tuple) { [ :viewDidAppear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewWillDisappear:').map(->(tuple) { [ :viewWillDisappear, tuple.first ] }.rmx_unsafe!),
          rac_signalForSelector('viewDidDisappear:').map(->(tuple) { [ :viewDidDisappear, tuple.first ] }.rmx_unsafe!)
        ])
        .takeUntil(rac_willDeallocSignal)
        .subscribeNext(->(v) {
          sub.sendNext(v)
        }.rmx_unsafe!)

        rac_signalForSelector('viewWillAppear:').subscribeNext(->(tuple) { appearing(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewDidAppear:').subscribeNext(->(tuple) { appeared(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewWillDisappear:').subscribeNext(->(tuple) { disappearing(tuple.first) }.rmx_unsafe!)
        rac_signalForSelector('viewDidDisappear:').subscribeNext(->(tuple) { disappeared(tuple.first) }.rmx_unsafe!)

        sub.subscribeOn(RACScheduler.mainThreadScheduler).deliverOn(RACScheduler.mainThreadScheduler).takeUntil(rac_willDeallocSignal)
      end
    end

    def viewStateFilteredSignal(state)
      viewStateSignal
      .filter(->((_state, animated)) {
        state == _state
      }.rmx_weak!)
    end

    def whenOrIfViewState(viewState, &block)
      viewStateFilteredSignal(viewState)
      .take(1)
      .subscribeNext(->(v) {
        block.call
      })
    end

    def appearing(animated)
    end

    def appeared(animated)
    end

    def disappearing(animated)
    end

    def disappeared(animated)
    end

    def present(vc, animated=false, &block)
      RMXViewControllerPresentation.present({
        :origin => self,
        :view_controller => vc,
        :animated => animated,
        :completion => block
      })
    end
    
    def dismiss(animated=false, &block)
      RMXViewControllerPresentation.dismiss({
        :view_controller => self,
        :animated => animated,
        :completion => block
      })
    end
  end
end

{'fraction_non_alphanumeric': 0.10454592738999272, 'fraction_numerical': 0.0014563611775720378, 'mean_word_length': 2.9017857142857144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3302740', 'n_tokens_mistral': 2804, 'n_tokens_neox': 2684, 'n_words': 476}