Class: Preact::State
- Inherits:
-
Object
show all
- Includes:
- Native::Wrapper
- Defined in:
- lib/preact/state.rb
Instance Method Summary
collapse
Constructor Details
#initialize(native) ⇒ State
Returns a new instance of State.
5
6
7
|
# File 'lib/preact/state.rb', line 5
def initialize(native)
@native = native
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, &block) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/preact/state.rb', line 16
def method_missing(key, *args, &block)
if `args.length > 0`
new_state = `{}`
new_state.JS[(`key.endsWith('=')` ? key.chop : key)] = args[0]
if block_given?
@native.JS.setState(new_state, `function() { block.$call(); }`)
else
@native.JS.setState(new_state, `null`)
end
else
%x{
if (typeof #@native.state[key] === 'undefined') { return nil; }
return #@native.state[key];
}
end
end
|
Instance Method Details
#==(other_state) ⇒ Object
9
10
11
12
13
14
|
# File 'lib/preact/state.rb', line 9
def ==(other_state)
%x{
if (Opal.Preact.state_is_not_equal(#@native.state, #{other_state.to_raw_n})) { return false; }
return true;
}
end
|
#set_state(updater, &block) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/preact/state.rb', line 33
def set_state(updater, &block)
new_state = `{}`
updater.each do |key, value|
new_state.JS[key] = value
end
if block_given?
@native.JS.setState(new_state, `function() { block.$call(); }`)
else
@native.JS.setState(new_state, `null`)
end
end
|
45
46
47
|
# File 'lib/preact/state.rb', line 45
def size
`Object.keys(#@native.state).length`;
end
|
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/preact/state.rb', line 49
def to_n
%x{
var new_native = {};
for (var key in #@native.state) {
if (typeof #@native.state[key].$to_n !== "undefined") {
new_native[key] = #@native.state[key].$to_n();
} else {
new_native[key] = #@native.state[key];
}
}
return new_native;
}
end
|
63
64
65
|
# File 'lib/preact/state.rb', line 63
def to_raw_n
`#@native.state`
end
|