rm-extensions 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/motion/events.rb +28 -32
- data/lib/motion/util.rb +33 -11
- data/lib/rm-extensions/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cbe483d3f68986bb4f011ecdd231cfc489a706c
|
4
|
+
data.tar.gz: 9900e1371f808b29a274faf967ab1f72310c407f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e81aba29549b3b3059bddb68a697c1946fce35aa4624a6fd5228f8450db7c0ebec6a2283f1ad34933368085027398be17e2d930db2912d4d9b519056f404e5c
|
7
|
+
data.tar.gz: c14c74e85f4f9c86903312ba1b29d5ba387f16123b330ba0319442eee1f6beadc221b73a091dd4ee7cd00a16259c4d9dfab5ce37b1b734f33defed2a565e50f6
|
data/lib/motion/events.rb
CHANGED
@@ -10,7 +10,7 @@ module RMExtensions
|
|
10
10
|
|
11
11
|
# register a callback when an event is triggered on this object.
|
12
12
|
def rmext_on(object, event, &block)
|
13
|
-
object.rmext_events_proxy.on(event, inContext:self, withBlock:block)
|
13
|
+
object.rmext_events_proxy.on(event, limit:-1, inContext:self, withBlock:block)
|
14
14
|
end
|
15
15
|
|
16
16
|
def rmext_now_and_on(object, event, &block)
|
@@ -19,7 +19,7 @@ module RMExtensions
|
|
19
19
|
|
20
20
|
# register a callback when an event is triggered on this object and remove it after it fires once
|
21
21
|
def rmext_once(object, event, &block)
|
22
|
-
object.rmext_events_proxy.
|
22
|
+
object.rmext_events_proxy.on(event, limit:1, inContext:self, withBlock:block)
|
23
23
|
end
|
24
24
|
|
25
25
|
# remove a specific callback for an event on object
|
@@ -68,11 +68,10 @@ module RMExtensions
|
|
68
68
|
|
69
69
|
def initialize(obj)
|
70
70
|
@weak_object = WeakRef.new(obj)
|
71
|
-
@desc = obj.inspect
|
72
71
|
@events = NSMapTable.weakToStrongObjectsMapTable
|
73
72
|
@listenings = NSHashTable.weakObjectsHashTable
|
74
73
|
if ::RMExtensions.debug?
|
75
|
-
p "
|
74
|
+
p "CREATED EventsProxy: #{@weak_object.rmext_object_desc}"
|
76
75
|
end
|
77
76
|
end
|
78
77
|
|
@@ -80,7 +79,7 @@ module RMExtensions
|
|
80
79
|
@did_dealloc = true
|
81
80
|
cleanup
|
82
81
|
if ::RMExtensions.debug?
|
83
|
-
p "
|
82
|
+
p "DEALLOC EventsProxy: #{@weak_object.rmext_object_desc}"
|
84
83
|
end
|
85
84
|
super
|
86
85
|
end
|
@@ -91,7 +90,7 @@ module RMExtensions
|
|
91
90
|
true
|
92
91
|
end
|
93
92
|
|
94
|
-
def on(event, inContext:context, withBlock:block)
|
93
|
+
def on(event, limit:limit, inContext:context, withBlock:block)
|
95
94
|
return if event.nil? || block.nil?
|
96
95
|
event = event.to_s
|
97
96
|
context ||= self.class
|
@@ -100,18 +99,19 @@ module RMExtensions
|
|
100
99
|
@events.setObject(context_events, forKey:context)
|
101
100
|
end
|
102
101
|
unless context_event_blocks = context_events.objectForKey(event)
|
103
|
-
context_event_blocks =
|
102
|
+
context_event_blocks = {}
|
104
103
|
context_events.setObject(context_event_blocks, forKey:event)
|
105
104
|
end
|
106
105
|
block.weak!
|
107
|
-
context_event_blocks
|
106
|
+
context_event_blocks[block] = limit
|
108
107
|
# i.e.: controller/view listening_to model
|
109
108
|
context.rmext_events_proxy.listening_to(@weak_object)
|
110
109
|
end
|
111
110
|
|
111
|
+
# this is called in the reverse direction than normal
|
112
112
|
def listening_to(object)
|
113
113
|
if ::RMExtensions.debug?
|
114
|
-
p "
|
114
|
+
p "CONTEXT:", @weak_object.rmext_object_desc, "LISTENING TO:", object.rmext_object_desc
|
115
115
|
end
|
116
116
|
@listenings.addObject(object)
|
117
117
|
end
|
@@ -125,7 +125,7 @@ module RMExtensions
|
|
125
125
|
res.event = event
|
126
126
|
block.call(res)
|
127
127
|
end
|
128
|
-
on(event, inContext:context, withBlock:block)
|
128
|
+
on(event, limit:-1, inContext:context, withBlock:block)
|
129
129
|
end
|
130
130
|
|
131
131
|
def off(event, inContext:context, withBlock:block)
|
@@ -134,19 +134,10 @@ module RMExtensions
|
|
134
134
|
context ||= self.class
|
135
135
|
return unless context_events = @events.objectForKey(context)
|
136
136
|
return unless context_event_blocks = context_events.objectForKey(event)
|
137
|
-
context_event_blocks.
|
137
|
+
context_event_blocks.delete block
|
138
138
|
nil
|
139
139
|
end
|
140
140
|
|
141
|
-
def once(event, inContext:context, withBlock:block)
|
142
|
-
block.weak!
|
143
|
-
once_block = lambda do |opts|
|
144
|
-
off(event, inContext:context, withBlock:once_block)
|
145
|
-
block.call(opts)
|
146
|
-
end
|
147
|
-
on(event, inContext:context, withBlock:once_block)
|
148
|
-
end
|
149
|
-
|
150
141
|
def off_all
|
151
142
|
@events.removeAllObjects
|
152
143
|
end
|
@@ -158,7 +149,7 @@ module RMExtensions
|
|
158
149
|
def off_all_context
|
159
150
|
while object = @listenings.anyObject
|
160
151
|
if ::RMExtensions.debug?
|
161
|
-
p "
|
152
|
+
p "CONTEXT:", @weak_object.rmext_object_desc, "UNLISTENING TO:", object.rmext_object_desc
|
162
153
|
end
|
163
154
|
@listenings.removeObject(object)
|
164
155
|
object.rmext_events_proxy.off_context(@weak_object)
|
@@ -166,11 +157,6 @@ module RMExtensions
|
|
166
157
|
end
|
167
158
|
|
168
159
|
def trigger(event, value)
|
169
|
-
# m_desc = nil
|
170
|
-
# if ::RMExtensions.debug?
|
171
|
-
# m_desc = "~~> EventsProxy(#{@desc})#trigger(#{event}, #{value.inspect.split(" ").first }>)"
|
172
|
-
# p "called", m_desc
|
173
|
-
# end
|
174
160
|
rmext_inline_or_on_main_q do
|
175
161
|
next if @did_dealloc
|
176
162
|
next if event.nil?
|
@@ -183,17 +169,27 @@ module RMExtensions
|
|
183
169
|
while context = contexts.pop
|
184
170
|
if context_events = @events.objectForKey(context)
|
185
171
|
if event_blocks = context_events[event]
|
186
|
-
blocks =
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
while
|
172
|
+
blocks = event_blocks.keys
|
173
|
+
if ::RMExtensions.debug?
|
174
|
+
p "TRIGGER:", event, "OBJECT:", @weak_object.rmext_object_desc, "CONTEXT:", context.rmext_object_desc, "BLOCKS SIZE:", blocks.size
|
175
|
+
end
|
176
|
+
while block = blocks.pop
|
177
|
+
limit = event_blocks[block]
|
191
178
|
res = EventResponse.new
|
192
179
|
res.context = context
|
193
180
|
res.value = value
|
194
181
|
res.target = @weak_object
|
195
182
|
res.event = event
|
196
|
-
|
183
|
+
block.call(res)
|
184
|
+
if limit == 1
|
185
|
+
# off
|
186
|
+
if ::RMExtensions.debug?
|
187
|
+
p "LIMIT REACHED:", event, "OBJECT:", @weak_object.rmext_object_desc, "CONTEXT:", context.rmext_object_desc
|
188
|
+
end
|
189
|
+
off(event, inContext:context, withBlock:block)
|
190
|
+
elsif limit > 1
|
191
|
+
context_events[block] -= 1
|
192
|
+
end
|
197
193
|
end
|
198
194
|
end
|
199
195
|
end
|
data/lib/motion/util.rb
CHANGED
@@ -25,35 +25,57 @@ module RMExtensions
|
|
25
25
|
class LongTask
|
26
26
|
attr_accessor :bgTask, :desc
|
27
27
|
|
28
|
+
def self.time_remaining
|
29
|
+
UIApplication.sharedApplication.backgroundTimeRemaining
|
30
|
+
end
|
31
|
+
|
28
32
|
# RMExtensions::BackgroundTask.new("my long task") { |task| task.end! }
|
29
|
-
def
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
def self.create(desc=nil, &block)
|
34
|
+
x = new(desc)
|
35
|
+
block.weak!.call(x)
|
36
|
+
x
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(desc=nil)
|
40
|
+
@desc = "#{rmext_object_desc} #{desc}"
|
41
|
+
@bgTask = UIApplication.sharedApplication.beginBackgroundTaskWithName(@desc, expirationHandler:lambda do
|
42
|
+
p "ERROR: #{@desc} didn't call #end! in time!"
|
43
|
+
__end!
|
44
|
+
end)
|
38
45
|
self
|
39
46
|
end
|
40
47
|
|
41
48
|
def end!
|
42
49
|
if ::RMExtensions.debug?
|
43
|
-
p "SUCCESS: #{
|
50
|
+
p "SUCCESS: #{@desc} ended successfully."
|
44
51
|
end
|
45
|
-
|
52
|
+
__end!
|
53
|
+
end
|
54
|
+
|
55
|
+
def __end!
|
56
|
+
if @bgTask && @bgTask != UIBackgroundTaskInvalid
|
46
57
|
UIApplication.sharedApplication.endBackgroundTask(@bgTask)
|
47
58
|
@bgTask = UIBackgroundTaskInvalid
|
48
59
|
end
|
49
60
|
end
|
50
61
|
|
62
|
+
def dealloc
|
63
|
+
if ::RMExtensions.debug?
|
64
|
+
p "DEALLOC: #{@desc}"
|
65
|
+
end
|
66
|
+
super
|
67
|
+
end
|
68
|
+
|
51
69
|
end
|
52
70
|
|
53
71
|
module ObjectExtensions
|
54
72
|
|
55
73
|
module Util
|
56
74
|
|
75
|
+
def rmext_object_desc
|
76
|
+
"#{self.className}:#{'%x' % (self.object_id)}"
|
77
|
+
end
|
78
|
+
|
57
79
|
# Raises an exception when called from a thread other than the main thread.
|
58
80
|
# Good for development and experimenting.
|
59
81
|
def rmext_assert_main_thread!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rm-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Noon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Extensions and helpers for dealing with various areas of rubymotion
|
14
14
|
email:
|