hyper-resource 1.0.0.lap58 → 1.0.0.lap62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hyper_record/class_methods.rb +56 -60
- data/lib/hyper_record/client_instance_methods.rb +47 -47
- data/lib/hyper_record/pub_sub.rb +69 -0
- data/lib/hyper_record/server_class_methods.rb +1 -1
- data/lib/hyperloop/resource/client_drivers.rb +15 -2
- data/lib/hyperloop/resource/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b68ab98c8c4f86ae7f9ef65df1e9fce88f1cf4d769f5f043da7d8798415abc9
|
4
|
+
data.tar.gz: aff99ed6aa6fc953e8e003ee98728f414ca796644cfeadd9d4ca729ba365cb69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f8c4c16583165457179a7175d00f69a98d1a3f081dbd1b87f5fff42a4a68cf4125f65eb8f7c28262160b2ab31a7af0077b9ed7ba9a612f40296947b2f3557db
|
7
|
+
data.tar.gz: d947709f378d8add9359dc2970fe7e05446dbd4e08cf9c9cfdcee55932a4ab346b8f992e6e1de5256c62c76ec1b4fa6510479141e1bb0a8c66d115b17ec40774
|
@@ -5,7 +5,7 @@ module HyperRecord
|
|
5
5
|
if record_hash.has_key?(:id)
|
6
6
|
record = _record_cache[record_hash[:id].to_s]
|
7
7
|
if record
|
8
|
-
record.instance_variable_get(:@
|
8
|
+
record.instance_variable_get(:@properties).merge!(record_hash)
|
9
9
|
return record
|
10
10
|
end
|
11
11
|
end
|
@@ -309,15 +309,15 @@ module HyperRecord
|
|
309
309
|
_property_options[name] = options
|
310
310
|
define_method(name) do
|
311
311
|
_register_observer
|
312
|
-
if @
|
313
|
-
if @
|
314
|
-
@
|
312
|
+
if @properties[:id]
|
313
|
+
if @changed_properties.has_key?(name)
|
314
|
+
@changed_properties[name]
|
315
315
|
else
|
316
|
-
@
|
316
|
+
@properties[name]
|
317
317
|
end
|
318
318
|
else
|
319
319
|
# record has not been fetched or is new and not yet saved
|
320
|
-
if @
|
320
|
+
if @properties[name].nil?
|
321
321
|
# TODO move default to initializer?
|
322
322
|
if self.class._property_options[name].has_key?(:default)
|
323
323
|
self.class._property_options[name][:default]
|
@@ -327,13 +327,13 @@ module HyperRecord
|
|
327
327
|
HyperRecord::DummyValue.new(NilClass)
|
328
328
|
end
|
329
329
|
else
|
330
|
-
@
|
330
|
+
@properties[name]
|
331
331
|
end
|
332
332
|
end
|
333
333
|
end
|
334
334
|
define_method("#{name}=") do |value|
|
335
335
|
_register_observer
|
336
|
-
@
|
336
|
+
@changed_properties[name] = value
|
337
337
|
end
|
338
338
|
end
|
339
339
|
|
@@ -342,47 +342,48 @@ module HyperRecord
|
|
342
342
|
end
|
343
343
|
|
344
344
|
def rest_class_method(name, options = { default_result: '...' })
|
345
|
-
|
346
|
-
|
347
|
-
|
345
|
+
rest_class_methods[name] = options
|
346
|
+
define_singleton_method("promise_#{name}") do |*args|
|
347
|
+
name_args = _name_args(name, *args)
|
348
|
+
_class_fetch_states[name_args] = 'i'
|
349
|
+
rest_class_methods[name_args] = { result: options[:default_result] } unless rest_class_methods.has_key?(name_args)
|
348
350
|
_promise_get_or_patch("#{resource_base_uri}/methods/#{name}.json?timestamp=#{`Date.now() + Math.random()`}", *args).then do |response_json|
|
349
|
-
|
351
|
+
rest_class_methods[name_args][:result] = response_json[:result] # result is parsed json
|
352
|
+
_class_fetch_states[name_args] = 'f'
|
350
353
|
_notify_class_observers
|
351
|
-
|
354
|
+
rest_class_methods[name_args][:result]
|
352
355
|
end.fail do |response|
|
353
356
|
error_message = "#{self.to_s}.#{name}, a rest_method, failed to execute!"
|
354
357
|
`console.error(error_message)`
|
355
358
|
response
|
356
359
|
end
|
357
360
|
end
|
358
|
-
|
361
|
+
define_singleton_method(name) do |*args|
|
362
|
+
name_args = _name_args(name, *args)
|
359
363
|
_register_class_observer
|
360
|
-
|
364
|
+
rest_class_methods[name_args] = { result: options[:default_result] } unless rest_methods.has_key?(name_args)
|
365
|
+
unless 'fi'.include?(_class_fetch_states[name_args])
|
361
366
|
self.send("promise_#{name}", *args)
|
362
367
|
end
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
end
|
368
|
+
rest_class_methods[name_args][:result]
|
369
|
+
end
|
370
|
+
define_singleton_method("update_#{name}") do |*args|
|
371
|
+
_class_fetch_states[_name_args(name, *args)] = 'u'
|
368
372
|
end
|
369
|
-
end
|
370
|
-
|
371
|
-
def rest_class_method_force_updates(method_name)
|
372
|
-
rest_methods[method_name][:force] = true
|
373
|
-
end
|
374
|
-
|
375
|
-
def rest_class_method_unforce_updates(method_name)
|
376
|
-
rest_methods[method_name][:force] = false
|
377
373
|
end
|
378
374
|
|
379
375
|
def rest_method(name, options = { default_result: '...' })
|
380
|
-
rest_methods[name] = options
|
381
376
|
define_method("promise_#{name}") do |*args|
|
377
|
+
name_args = self.class._name_args(name, *args)
|
378
|
+
@fetch_states[name_args] = 'i'
|
379
|
+
@rest_methods[name] = options unless @rest_methods.has_key?(name)
|
380
|
+
@rest_methods[name_args] = { result: options[:default_result] } unless @rest_methods.has_key?(name_args)
|
381
|
+
raise "#{self.class.to_s}[_no_id_].#{name}, can't execute instance rest_method without id!" unless self.id
|
382
382
|
self.class._promise_get_or_patch("#{resource_base_uri}/#{self.id}/methods/#{name}.json?timestamp=#{`Date.now() + Math.random()`}", *args).then do |response_json|
|
383
|
-
@
|
383
|
+
@rest_methods[name_args][:result] = response_json[:result] # result is parsed json
|
384
|
+
@fetch_states[name_args] = 'f'
|
384
385
|
_notify_observers
|
385
|
-
@
|
386
|
+
@rest_methods[name_args][:result]
|
386
387
|
end.fail do |response|
|
387
388
|
error_message = "#{self.class.to_s}[#{self.id}].#{name}, a rest_method, failed to execute!"
|
388
389
|
`console.error(error_message)`
|
@@ -391,19 +392,21 @@ module HyperRecord
|
|
391
392
|
end
|
392
393
|
define_method(name) do |*args|
|
393
394
|
_register_observer
|
394
|
-
|
395
|
+
name_args = self.class._name_args(name, *args)
|
396
|
+
@rest_methods[name] = options unless @rest_methods.has_key?(name)
|
397
|
+
@rest_methods[name_args] = { result: options[:default_result] } unless @rest_methods.has_key?(name_args)
|
398
|
+
unless 'fi'.include?(@fetch_states[name_args])
|
395
399
|
self.send("promise_#{name}", *args)
|
396
400
|
end
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
end
|
401
|
+
@rest_methods[name_args][:result]
|
402
|
+
end
|
403
|
+
define_method("update_#{name}") do |*args|
|
404
|
+
@fetch_states[self.class._name_args(name, *args)] = 'u'
|
402
405
|
end
|
403
406
|
end
|
404
407
|
|
405
|
-
def
|
406
|
-
@
|
408
|
+
def rest_class_methods
|
409
|
+
@rest_class_methods ||= {}
|
407
410
|
end
|
408
411
|
|
409
412
|
def resource_base_uri
|
@@ -412,11 +415,7 @@ module HyperRecord
|
|
412
415
|
|
413
416
|
def scope(name, options)
|
414
417
|
define_singleton_method("promise_#{name}") do |*args|
|
415
|
-
name_args =
|
416
|
-
"#{name}_#{args.to_json}"
|
417
|
-
else
|
418
|
-
name
|
419
|
-
end
|
418
|
+
name_args = _name_args(name, *args)
|
420
419
|
_class_fetch_states[name_args] = 'i'
|
421
420
|
self._promise_get_or_patch("#{resource_base_uri}/scopes/#{name}.json", *args).then do |response_json|
|
422
421
|
scopes[name_args] = _convert_array_to_collection(response_json[self.to_s.underscore][name])
|
@@ -430,27 +429,16 @@ module HyperRecord
|
|
430
429
|
end
|
431
430
|
end
|
432
431
|
define_singleton_method(name) do |*args|
|
433
|
-
name_args =
|
434
|
-
"#{name}_#{args.to_json}"
|
435
|
-
else
|
436
|
-
name
|
437
|
-
end
|
432
|
+
name_args = _name_args(name, *args)
|
438
433
|
scopes[name_args] = HyperRecord::Collection.new unless scopes.has_key?(name_args)
|
439
|
-
|
440
|
-
|
441
|
-
else
|
442
|
-
_register_class_observer
|
434
|
+
_register_class_observer
|
435
|
+
unless 'fi'.include?(_class_fetch_states[name_args])
|
443
436
|
self.send("promise_#{name}", *args)
|
444
|
-
scopes[name_args]
|
445
437
|
end
|
438
|
+
scopes[name_args]
|
446
439
|
end
|
447
440
|
define_singleton_method("update_#{name}") do |*args|
|
448
|
-
|
449
|
-
"#{name}_#{args.to_json}"
|
450
|
-
else
|
451
|
-
name
|
452
|
-
end
|
453
|
-
_class_fetch_states[name_args] = 'u'
|
441
|
+
_class_fetch_states[_name_args(name, *args)] = 'u'
|
454
442
|
end
|
455
443
|
end
|
456
444
|
|
@@ -502,6 +490,14 @@ module HyperRecord
|
|
502
490
|
@_class_state_key
|
503
491
|
end
|
504
492
|
|
493
|
+
def _name_args(name, *args)
|
494
|
+
if args.size > 0
|
495
|
+
"#{name}_#{args.to_json}"
|
496
|
+
else
|
497
|
+
name
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
505
501
|
def _promise_find(id, record_in_progress)
|
506
502
|
_class_fetch_states["record_#{id}"] = 'i'
|
507
503
|
_promise_get("#{resource_base_uri}/#{id}.json").then do |response|
|
@@ -4,13 +4,16 @@ module HyperRecord
|
|
4
4
|
def initialize(record_hash = {})
|
5
5
|
# initalize internal data structures
|
6
6
|
record_hash = {} if record_hash.nil?
|
7
|
-
@
|
8
|
-
@
|
7
|
+
@properties = {}
|
8
|
+
@changed_properties = {}
|
9
9
|
@relations = {}
|
10
|
-
@
|
11
|
-
self.class.rest_methods.keys.each { |method| @rest_methods_hash[method] = {} }
|
10
|
+
@rest_methods = {}
|
12
11
|
@destroyed = false
|
13
|
-
# for reactivity
|
12
|
+
# for reactivity, possible @fetch_states:
|
13
|
+
# n - not fetched
|
14
|
+
# f - fetched
|
15
|
+
# i - fetch in progress
|
16
|
+
# u - update needed, fetch on next usage
|
14
17
|
@fetch_states = {}
|
15
18
|
@state_key = "#{self.class.to_s}_#{self.object_id}"
|
16
19
|
@observers = Set.new
|
@@ -42,19 +45,16 @@ module HyperRecord
|
|
42
45
|
else
|
43
46
|
@relations[relation] = nil
|
44
47
|
end
|
45
|
-
@fetch_states[relation] = 'n'
|
48
|
+
@fetch_states[relation] = 'n'
|
46
49
|
end
|
47
50
|
end
|
48
51
|
record_hash.delete(relation)
|
49
52
|
end
|
50
53
|
|
51
|
-
@
|
52
|
-
|
53
|
-
# change state
|
54
|
-
_mutate_state
|
54
|
+
@properties = record_hash
|
55
55
|
|
56
56
|
# cache in global cache
|
57
|
-
self.class._record_cache[@
|
57
|
+
self.class._record_cache[@properties[:id].to_s] = self if @properties.has_key?(:id)
|
58
58
|
end
|
59
59
|
|
60
60
|
### reactive api
|
@@ -79,12 +79,12 @@ module HyperRecord
|
|
79
79
|
def method_missing(method, arg)
|
80
80
|
_register_observer
|
81
81
|
if method.end_with?('=')
|
82
|
-
@
|
82
|
+
@changed_properties[method.chop] = arg
|
83
83
|
else
|
84
|
-
if @
|
85
|
-
@
|
84
|
+
if @changed_properties.has_key?(method)
|
85
|
+
@changed_properties[method]
|
86
86
|
else
|
87
|
-
@
|
87
|
+
@properties[method]
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
@@ -95,21 +95,13 @@ module HyperRecord
|
|
95
95
|
|
96
96
|
def reset
|
97
97
|
_register_observer
|
98
|
-
@
|
98
|
+
@changed_properties = {}
|
99
99
|
end
|
100
100
|
|
101
101
|
def resource_base_uri
|
102
102
|
self.class.resource_base_uri
|
103
103
|
end
|
104
104
|
|
105
|
-
def rest_method_force_updates(method_name)
|
106
|
-
@rest_methods_hash[method_name][:force] = true
|
107
|
-
end
|
108
|
-
|
109
|
-
def rest_method_unforce_updates(method_name)
|
110
|
-
@rest_methods_hash[method_name][:force] = false
|
111
|
-
end
|
112
|
-
|
113
105
|
def save
|
114
106
|
_register_observer
|
115
107
|
promise_save.then do
|
@@ -120,14 +112,14 @@ module HyperRecord
|
|
120
112
|
|
121
113
|
def to_hash
|
122
114
|
_register_observer
|
123
|
-
res = @
|
124
|
-
res.merge!(@
|
115
|
+
res = @properties.dup
|
116
|
+
res.merge!(@changed_properties)
|
125
117
|
res
|
126
118
|
end
|
127
119
|
|
128
120
|
def to_s
|
129
121
|
_register_observer
|
130
|
-
@
|
122
|
+
@properties.to_s
|
131
123
|
end
|
132
124
|
|
133
125
|
def unlink(other_record)
|
@@ -142,7 +134,7 @@ module HyperRecord
|
|
142
134
|
|
143
135
|
def promise_destroy
|
144
136
|
_local_destroy
|
145
|
-
self.class._promise_delete("#{resource_base_uri}/#{@
|
137
|
+
self.class._promise_delete("#{resource_base_uri}/#{@properties[:id]}").then do |response|
|
146
138
|
self
|
147
139
|
end.fail do |response|
|
148
140
|
error_message = "Destroying record #{self} failed!"
|
@@ -155,15 +147,15 @@ module HyperRecord
|
|
155
147
|
called_from_collection = relation_name ? true : false
|
156
148
|
relation_name = other_record.class.to_s.underscore.pluralize unless relation_name
|
157
149
|
if reflections.has_key?(relation_name)
|
158
|
-
|
150
|
+
@relations[relation_name].push(other_record) if !called_from_collection && @fetch_states[relation_name] == 'f'
|
159
151
|
else
|
160
152
|
relation_name = other_record.class.to_s.underscore
|
161
153
|
raise "No collection for record of type #{other_record.class}" unless reflections.has_key?(relation_name)
|
162
|
-
|
154
|
+
@relations[relation_name].push(other_record) if !called_from_collection && @fetch_states[relation_name] == 'f'
|
163
155
|
end
|
164
156
|
payload_hash = other_record.to_hash
|
165
157
|
self.class._promise_post("#{resource_base_uri}/#{self.id}/relations/#{relation_name}.json", { data: payload_hash }).then do |response|
|
166
|
-
other_record.instance_variable_get(:@
|
158
|
+
other_record.instance_variable_get(:@properties).merge!(response.json[other_record.class.to_s.underscore])
|
167
159
|
self
|
168
160
|
end.fail do |response|
|
169
161
|
error_message = "Linking record #{other_record} to #{self} failed!"
|
@@ -173,14 +165,14 @@ module HyperRecord
|
|
173
165
|
end
|
174
166
|
|
175
167
|
def promise_save
|
176
|
-
payload_hash = @
|
168
|
+
payload_hash = @properties.merge(@changed_properties) # copy hash, because we need to delete some keys
|
177
169
|
(%i[id created_at updated_at] + reflections.keys).each do |key|
|
178
170
|
payload_hash.delete(key)
|
179
171
|
end
|
180
|
-
if @
|
172
|
+
if @properties[:id] && ! (@changed_properties.has_key?(:id) && @changed_properties[:id].nil?)
|
181
173
|
reset
|
182
|
-
self.class._promise_patch("#{resource_base_uri}/#{@
|
183
|
-
@
|
174
|
+
self.class._promise_patch("#{resource_base_uri}/#{@properties[:id]}", { data: payload_hash }).then do |response|
|
175
|
+
@properties.merge!(response.json[self.class.to_s.underscore])
|
184
176
|
self
|
185
177
|
end.fail do |response|
|
186
178
|
error_message = "Saving record #{self} failed!"
|
@@ -190,7 +182,7 @@ module HyperRecord
|
|
190
182
|
else
|
191
183
|
reset
|
192
184
|
self.class._promise_post(resource_base_uri, { data: payload_hash }).then do |response|
|
193
|
-
@
|
185
|
+
@properties.merge!(response.json[self.class.to_s.underscore])
|
194
186
|
self
|
195
187
|
end.fail do |response|
|
196
188
|
error_message = "Creating record #{self} failed!"
|
@@ -204,8 +196,8 @@ module HyperRecord
|
|
204
196
|
called_from_collection = collection_name ? true : false
|
205
197
|
relation_name = other_record.class.to_s.underscore.pluralize unless relation_name
|
206
198
|
raise "No relation for record of type #{other_record.class}" unless reflections.has_key?(relation_name)
|
207
|
-
|
208
|
-
self.class._promise_delete("#{resource_base_uri}/#{@
|
199
|
+
@relations[relation_name].delete_if { |cr| cr == other_record } if !called_from_collection && @fetch_states[relation_name] == 'f'
|
200
|
+
self.class._promise_delete("#{resource_base_uri}/#{@properties[:id]}/relations/#{relation_name}.json?record_id=#{other_record.id}").then do |response|
|
209
201
|
self
|
210
202
|
end.fail do |response|
|
211
203
|
error_message = "Unlinking #{other_record} from #{self} failed!"
|
@@ -219,7 +211,7 @@ module HyperRecord
|
|
219
211
|
def _local_destroy
|
220
212
|
_register_observer
|
221
213
|
@destroyed = true
|
222
|
-
self.class._record_cache.delete(@
|
214
|
+
self.class._record_cache.delete(@properties[:id].to_s)
|
223
215
|
@registered_collections.dup.each do |collection|
|
224
216
|
collection.delete(self)
|
225
217
|
end
|
@@ -267,7 +259,7 @@ module HyperRecord
|
|
267
259
|
end
|
268
260
|
if `Date.parse(#{c_record.updated_at}) >= Date.parse(#{data[:cause][:updated_at]})`
|
269
261
|
if @fetch_states[data[:relation]] == 'f'
|
270
|
-
if
|
262
|
+
if @relations[data[:relation]].include?(c_record)
|
271
263
|
return unless data[:cause][:destroyed]
|
272
264
|
end
|
273
265
|
end
|
@@ -275,7 +267,17 @@ module HyperRecord
|
|
275
267
|
end
|
276
268
|
end
|
277
269
|
@fetch_states[data[:relation]] = 'u'
|
278
|
-
send(data[:relation])
|
270
|
+
send("prommise_#{data[:relation]}").then do |collection|
|
271
|
+
_notify_observers
|
272
|
+
end.fail do |response|
|
273
|
+
error_message = "#{self}[#{self.id}].#{data[:relation]} failed to update!"
|
274
|
+
`console.error(error_message)`
|
275
|
+
end
|
276
|
+
return
|
277
|
+
end
|
278
|
+
if data.has_key?(:rest_method)
|
279
|
+
@fetch_states[data[:rest_method]] = 'u'
|
280
|
+
_notify_observers
|
279
281
|
return
|
280
282
|
end
|
281
283
|
if data[:destroyed]
|
@@ -284,18 +286,16 @@ module HyperRecord
|
|
284
286
|
_local_destroy
|
285
287
|
return
|
286
288
|
end
|
287
|
-
if @
|
288
|
-
return if `Date.parse(#{@
|
289
|
+
if @properties[:updated_at] && data[:updated_at]
|
290
|
+
return if `Date.parse(#{@properties[:updated_at]}) >= Date.parse(#{data[:updated_at]})`
|
289
291
|
end
|
290
|
-
self.class.
|
291
|
-
|
292
|
-
self._initialize_from_hash(response.json[klass_key]) if response.json[klass_key]
|
292
|
+
self.class._class_fetch_states["record_#{id}"] = 'u'
|
293
|
+
self.class._promise_find(@properties[:id], self).then do |record|
|
293
294
|
_notify_observers
|
294
295
|
self
|
295
296
|
end.fail do |response|
|
296
297
|
error_message = "#{self} failed to update!"
|
297
298
|
`console.error(error_message)`
|
298
|
-
response
|
299
299
|
end
|
300
300
|
end
|
301
301
|
end
|
data/lib/hyper_record/pub_sub.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
module HyperRecord
|
2
2
|
module PubSub
|
3
3
|
def self.included(base)
|
4
|
+
attr_accessor :current_rest_method_params
|
5
|
+
|
4
6
|
base.extend(HyperRecord::PubSub::ClassMethods)
|
5
7
|
end
|
6
8
|
|
7
9
|
module ClassMethods
|
10
|
+
attr_accessor :current_rest_method_params
|
11
|
+
|
8
12
|
def _pusher_client
|
9
13
|
Hyperloop.pusher_instance ||= Pusher::Client.new(
|
10
14
|
app_id: Hyperloop.pusher[:app_id],
|
@@ -70,6 +74,45 @@ module HyperRecord
|
|
70
74
|
Hyperloop.redis_instance.del("HRPS__#{record.class}__#{record.id}") if record.destroyed?
|
71
75
|
end
|
72
76
|
|
77
|
+
def publish_rest_class_method(record_class, rest_class_method_name)
|
78
|
+
subscribers = Hyperloop.redis_instance.hgetall("HRPS__#{record_class}__rest_class_method__#{rest_class_method_name}")
|
79
|
+
time_now = Time.now.to_f
|
80
|
+
scrub_time = time_now - 24.hours.to_f
|
81
|
+
subscribers.each do |session_id, last_requested|
|
82
|
+
if last_requested.to_f < scrub_time
|
83
|
+
Hyperloop.redis_instance.hdel("HRPS__#{record_class}__rest_class_method__#{rest_class_method_name}", session_id)
|
84
|
+
next
|
85
|
+
end
|
86
|
+
message = {
|
87
|
+
record_type: record_class.to_s,
|
88
|
+
rest_class_method: rest_class_method_name
|
89
|
+
}
|
90
|
+
if Hyperloop.resource_transport == :pusher
|
91
|
+
_pusher_client.trigger("hyper-record-update-channel-#{session_id}", 'update', message)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def publish_rest_method(record, method_name)
|
97
|
+
subscribers = Hyperloop.redis_instance.hgetall("HRPS__#{record.class}__#{record.id}__rest_method__#{method_name}")
|
98
|
+
time_now = Time.now.to_f
|
99
|
+
scrub_time = time_now - 24.hours.to_f
|
100
|
+
subscribers.each do |session_id, last_requested|
|
101
|
+
if last_requested.to_f < scrub_time
|
102
|
+
Hyperloop.redis_instance.hdel("HRPS__#{record.class}__#{record.id}__rest_method__#{method_name}", session_id)
|
103
|
+
next
|
104
|
+
end
|
105
|
+
message = {
|
106
|
+
record_type: record_class.to_s,
|
107
|
+
id: record.id,
|
108
|
+
rest_method: method_name
|
109
|
+
}
|
110
|
+
if Hyperloop.resource_transport == :pusher
|
111
|
+
_pusher_client.trigger("hyper-record-update-channel-#{session_id}", 'update', message)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
73
116
|
def publish_scope(record_class, scope_name)
|
74
117
|
subscribers = Hyperloop.redis_instance.hgetall("HRPS__#{record_class}__scope__#{scope_name}")
|
75
118
|
time_now = Time.now.to_f
|
@@ -112,6 +155,22 @@ module HyperRecord
|
|
112
155
|
Hyperloop.redis_instance.hset "HRPS__#{record.class}__#{record.id}", session.id.to_s, Time.now.to_f.to_s
|
113
156
|
end
|
114
157
|
|
158
|
+
def subscribe_rest_class_method(record_class, rest_class_method_name)
|
159
|
+
return unless session.id
|
160
|
+
time_now = Time.now.to_f.to_s
|
161
|
+
session_id = session.id.to_s
|
162
|
+
Hyperloop.redis_instance.pipelined do
|
163
|
+
Hyperloop.redis_instance.hset("HRPS__#{record_class}__rest_class_method_name__#{rest_class_method_name}", session_id, time_now)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def subscribe_rest_method(record, rest_method_name)
|
168
|
+
return unless session.id
|
169
|
+
time_now = Time.now.to_f.to_s
|
170
|
+
session_id = session.id.to_s
|
171
|
+
Hyperloop.redis_instance.hset("HRPS__#{record.class}__#{record.id}__rest_method__#{rest_method_name}", session_id, time_now)
|
172
|
+
end
|
173
|
+
|
115
174
|
def subscribe_scope(collection, record_class = nil, scope_name = nil)
|
116
175
|
return unless session.id
|
117
176
|
time_now = Time.now.to_f.to_s
|
@@ -136,6 +195,16 @@ module HyperRecord
|
|
136
195
|
publish_record(record)
|
137
196
|
end
|
138
197
|
|
198
|
+
def pub_sub_rest_class_method(record_class, rest_class_method_name)
|
199
|
+
subscribe_rest_class_method(record_class, rest_class_method_name)
|
200
|
+
publish_rest_class_method(record_class, rest_class_method_name)
|
201
|
+
end
|
202
|
+
|
203
|
+
def pub_sub_rest_method(record, rest_method_name)
|
204
|
+
subscribe_rest_method(record, rest_method_name)
|
205
|
+
publish_rest_method(record, rest_method_name)
|
206
|
+
end
|
207
|
+
|
139
208
|
def pub_sub_scope(collection, record_class, scope_name)
|
140
209
|
subscribe_scope(collection, record_class, scope_name)
|
141
210
|
publish_scope(record_class, scope_name)
|
@@ -97,11 +97,24 @@ module Hyperloop
|
|
97
97
|
if scope_params
|
98
98
|
scope_params = '[' + scope_params
|
99
99
|
record_class._class_fetch_states[data[:scope]] = 'u'
|
100
|
-
record_class.send(scope_name, *JSON.parse(scope_params))
|
100
|
+
record_class.send("promise_#{scope_name}", *JSON.parse(scope_params)).then do |collection|
|
101
|
+
record_class._notify_class_observers
|
102
|
+
end.fail do |response|
|
103
|
+
error_message = "#{record_class}.#{scope_name}(#{scope_params}), a scope failed to update!"
|
104
|
+
`console.error(error_message)`
|
105
|
+
end
|
101
106
|
else
|
102
107
|
record_class._class_fetch_states[data[:scope]] = 'u'
|
103
|
-
record_class.send(data[:scope])
|
108
|
+
record_class.send(data[:scope]).then do |collection|
|
109
|
+
record_class._notify_class_observers
|
110
|
+
end.fail do |response|
|
111
|
+
error_message = "#{record_class}.#{scope_name}, a scope failed to update!"
|
112
|
+
`console.error(error_message)`
|
113
|
+
end
|
104
114
|
end
|
115
|
+
elsif data[:rest_class_method]
|
116
|
+
record_class._class_fetch_states[data[:rest_class_method]] = 'u'
|
117
|
+
record_class._notify_class_observers
|
105
118
|
elsif record_class.record_cached?(data[:id])
|
106
119
|
record = record_class.find(data[:id])
|
107
120
|
record._update_record(data)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyper-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.lap62
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Biedermann
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|