Class: Browser::DOM::MutationObserver

Inherits:
Object
  • Object
show all
Includes:
Native
Defined in:
opal/browser/dom/mutation_observer.rb

Overview

A MutationObserver is a performant way to observe changes in the DOM, either on the tree, the attributes or data.

Defined Under Namespace

Classes: Record

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|records| ... } ⇒ MutationObserver

Create a new MutationObserver with the given block.

Yield Parameters:

  • records (Array<Record>)

    the recorded changes


89
90
91
92
93
94
95
96
97
# File 'opal/browser/dom/mutation_observer.rb', line 89

def initialize(&block)
  %x{
    var func = function(records) {
      return #{block.call(`records`.map { |r| Browser::DOM::MutationObserver::Record.new(r) })};
    }
  }

  super(`new window.MutationObserver(func)`)
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)

8
9
10
# File 'opal/browser/dom/mutation_observer.rb', line 8

def self.supported?
  Browser.supports? :MutationObserver
end

Instance Method Details

#disconnectObject

Disconnect the observer, thus stopping observing any changes.


138
139
140
# File 'opal/browser/dom/mutation_observer.rb', line 138

def disconnect
  `#@native.disconnect()`
end

#observe(target, options = nil) ⇒ Object

Observe the given target with the given options.

The supported options are:

  • children - whether to observe changes on the children of the target or not
  • tree - whether to observe changes on the whole subtree or not
  • attributes - whether to observe changes to attributes or not, if the value is :old the old value will be saved
  • cdata - whether to observe changes to CDATA sections or not, if the value is :old the old value will be saved
  • filter - array of attribute names to observe

Parameters:

  • target (DOM::Node, native)

    the node to observe

  • options (Hash?) (defaults to: nil)

    the options


115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'opal/browser/dom/mutation_observer.rb', line 115

def observe(target, options = nil)
  unless options
    options = {
      children:   true,
      tree:       true,
      attributes: :old,
      cdata:      :old
    }
  end

  `#@native.observe(#{Native.convert(target)}, #{convert(options)})`

  self
end

#takeArray<Record>

Empty the observer queue and return its contents.

Returns:


133
134
135
# File 'opal/browser/dom/mutation_observer.rb', line 133

def take
  `#@native.takeRecords()`.map { |r| Record.new(r) }
end