Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

Commit 87b9188

Browse files
author
Barrie Hadfield
committed
with state patch
1 parent 3440b9c commit 87b9188

File tree

6 files changed

+167
-38
lines changed

6 files changed

+167
-38
lines changed

README.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Hyperloop JS
22

3-
TODO: Docs about router
4-
53
### Hyperloop.js
64

75
Hyperloop client-side including:
@@ -22,9 +20,9 @@ Compiles code in your browser. Client-side Hyperloop and Opal for static sites o
2220
+ You can work with Hyperloop Components, Stores, Operations and HyperRouter
2321
+ No backend dependency or setup required
2422

25-
<!-- ### Opal.js
23+
### Hyper-router.js
2624

27-
+ Latest build of Opal. Include this file or bring your own. -->
25+
Ruby DSL wrapper of React Router V4. Requires ReactRouter and ReactRouterDOM.
2826

2927
## Documentation and Help
3028

@@ -35,32 +33,38 @@ See [VERSIONS](https://github.com/ruby-hyperloop/hyperloop-js/blob/master/VERSIO
3533

3634
## How it works
3735

38-
#### Hyperloop.js
39-
4036
##### Front-end
4137

42-
Hyperloop-js includes Hyperloop's client side Components, Operations, Stores and Hyper-Router.
38+
+ Hyperloop.js includes Hyperloop's client side Components, Operations and Stores.
39+
+ Hyperloop-compiler.js includes Opal Compiler for compiling Ruby Hyperloop code in your browser.
40+
+ Hyper-router includes a client-side build of the Hyper-router gem
4341

44-
##### Back-end
42+
##### No back-end (compile Ruby code in your browser)
4543

46-
You will need a backend to build the Hyperloop code you write.
44+
If you do not want a backend then you can use Hyperloop-compiler.js to compile code in your browser. Your ruby code will be compiled by the browser into JavaScript and executed. Any compilation or runtime errors will be briefly reported to the console.
4745

48-
+ Hyperloop Gem for Rails integration
49-
+ Opal Sprockets for Rack backend
50-
+ Simple Opal Rake rask
5146

52-
See Installation options on http://ruby-hyperloop.io/
47+
##### Back-end (compile Ruby code on the server)
5348

54-
##### No back-end
49+
To compile your code server-side, you will need a backend integrated with Opal. Your choices are:
5550

56-
If you do not want a backend then you can use Hyperloop-compiler.js to compile code in your browser.
51+
+ Hyperloop Gem for Rails integration (including Isomprphic Models & Operations)
52+
+ Opal Sprockets for Rack backend for a simple build process
53+
+ Minimal Rake task to compile with Opal
54+
+ Node.js using Webpack to compile Opal code
5755

58-
#### Hyperloop-Compiler.js
59-
60-
Your ruby code will be compiled by the browser into JavaScript and executed. Any compilation or runtime errors will be briefly reported to the console.
56+
See all the installation options on http://ruby-hyperloop.io/
6157

6258
## How to use
6359

60+
The simplest way to install is with NPM.
61+
62+
```
63+
npm install hyperloop-js --save
64+
```
65+
66+
TODO: requires and how they work....
67+
6468
### Hyperloop-JS
6569

6670
Add the following to your HTML page:

Rakefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ task :build do
2020
File.binwrite 'hyperloop-compiler.js', Opal::Builder.build('hyperloop-compiler').to_s
2121
puts "done"
2222

23-
# puts "About to build hyper-router.js"
24-
# File.binwrite 'hyper-router.js', Opal::Builder.build('hyper-router').to_s
25-
# puts "done"
23+
puts "About to build hyper-router.js"
24+
File.binwrite 'hyper-router.js', Opal::Builder.build('hyper-router').to_s
25+
puts "done"
2626

2727
puts "About to build opal.js"
2828
File.binwrite 'opal.js', Opal::Builder.build('opal').to_s
@@ -46,11 +46,11 @@ task :minify do
4646
File.open(js_min_file, "w").write(Uglifier.new.compile(File.read(js_file)))
4747
puts "done"
4848

49-
# puts "About to build hyper-router.min.js"
50-
# js_file = "hyper-router.js"
51-
# js_min_file = "./dist/hyper-router.min.js"
52-
# File.open(js_min_file, "w").write(Uglifier.new.compile(File.read(js_file)))
53-
# puts "done"
49+
puts "About to build hyper-router.min.js"
50+
js_file = "hyper-router.js"
51+
js_min_file = "./dist/hyper-router.min.js"
52+
File.open(js_min_file, "w").write(Uglifier.new.compile(File.read(js_file)))
53+
puts "done"
5454

5555
puts "About to build opal.min.js"
5656
js_file = "opal.js"

dist/hyperloop.min.js

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hyperloop.js

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14995,9 +14995,9 @@ Opal.modules["react/top_level_render"] = function(Opal) {
1499514995

1499614996
/* Generated by Opal 0.10.5 */
1499714997
(function(Opal) {
14998-
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $module = Opal.module, $klass = Opal.klass;
14998+
var self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice, $module = Opal.module, $klass = Opal.klass, $hash2 = Opal.hash2;
1499914999

15000-
Opal.add_stubs(['$require']);
15000+
Opal.add_stubs(['$require', '$[]=', '$[]', '$states', '$new', '$after', '$structure_check', '$each', '$set_state2', '$key?', '$current_observers', '$update_react_js_state', '$==']);
1500115001
self.$require("reactrb/auto-import");
1500215002
self.$require("active_support");
1500315003
self.$require("browser/interval");
@@ -15020,5 +15020,69 @@ Opal.modules["react/top_level_render"] = function(Opal) {
1502015020
self.$require("hyper-component");
1502115021
self.$require("hyper-operation");
1502215022
self.$require("hyper-store");
15023-
return self.$require("react/top_level_render");
15023+
self.$require("react/top_level_render");
15024+
return (function($base) {
15025+
var $React, self = $React = $module($base, 'React');
15026+
15027+
var def = self.$$proto, $scope = self.$$scope;
15028+
15029+
(function($base, $super) {
15030+
function $State(){};
15031+
var self = $State = $klass($base, $super, 'State', $State);
15032+
15033+
var def = self.$$proto, $scope = self.$$scope, TMP_10;
15034+
15035+
return (Opal.defs(self, '$set_state', TMP_10 = function $$set_state(object, name, value, delay) {
15036+
var $a, $b, $c, TMP_1, $d, TMP_2, TMP_8, $e, TMP_9, self = this, updates = nil;
15037+
if (self.bulk_update_flag == null) self.bulk_update_flag = nil;
15038+
if (self.delayed_updates == null) self.delayed_updates = nil;
15039+
if (self.delayed_updater == null) self.delayed_updater = nil;
15040+
if (self.rendering_level == null) self.rendering_level = nil;
15041+
15042+
if (delay == null) {
15043+
delay = $scope.get('ALWAYS_UPDATE_STATE_AFTER_RENDER');
15044+
}
15045+
self.$states()['$[]'](object)['$[]='](name, value);
15046+
if ((($a = ((($b = delay) !== false && $b !== nil && $b != null) ? $b : self.bulk_update_flag)) !== nil && $a != null && (!$a.$$is_boolean || $a == true))) {
15047+
((($a = self.delayed_updates) !== false && $a !== nil && $a != null) ? $a : self.delayed_updates = ($b = ($c = $scope.get('Hash')).$new, $b.$$p = (TMP_1 = function(h, k){var self = TMP_1.$$s || this;
15048+
if (h == null) h = nil;if (k == null) k = nil;
15049+
return h['$[]='](k, $hash2([], {}))}, TMP_1.$$s = self, TMP_1.$$arity = 2, TMP_1), $b).call($c));
15050+
self.delayed_updates['$[]'](object)['$[]='](name, [value, $scope.get('Set').$new()]);
15051+
((($a = self.delayed_updater) !== false && $a !== nil && $a != null) ? $a : self.delayed_updater = ($b = ($d = self).$after, $b.$$p = (TMP_2 = function(){var self = TMP_2.$$s || this, $e, $f, TMP_3, $g, TMP_4, $h, TMP_5, $i, TMP_7, delayed_updates = nil, updates = nil;
15052+
if (self.delayed_updates == null) self.delayed_updates = nil;
15053+
15054+
self.$structure_check("delayed set state");
15055+
delayed_updates = self.delayed_updates;
15056+
self.delayed_updates = ($e = ($f = $scope.get('Hash')).$new, $e.$$p = (TMP_3 = function(h, k){var self = TMP_3.$$s || this;
15057+
if (h == null) h = nil;if (k == null) k = nil;
15058+
return h['$[]='](k, $hash2([], {}))}, TMP_3.$$s = self, TMP_3.$$arity = 2, TMP_3), $e).call($f);
15059+
self.delayed_updater = nil;
15060+
updates = ($e = ($g = $scope.get('Hash')).$new, $e.$$p = (TMP_4 = function(hash, key){var self = TMP_4.$$s || this;
15061+
if (hash == null) hash = nil;if (key == null) key = nil;
15062+
return hash['$[]='](key, $scope.get('Array').$new())}, TMP_4.$$s = self, TMP_4.$$arity = 2, TMP_4), $e).call($g);
15063+
($e = ($h = delayed_updates).$each, $e.$$p = (TMP_5 = function(object, name_hash){var self = TMP_5.$$s || this, $i, $j, TMP_6;
15064+
if (object == null) object = nil;if (name_hash == null) name_hash = nil;
15065+
return ($i = ($j = name_hash).$each, $i.$$p = (TMP_6 = function(name, value_and_set){var self = TMP_6.$$s || this;
15066+
if (name == null) name = nil;if (value_and_set == null) value_and_set = nil;
15067+
return self.$set_state2(object, name, value_and_set['$[]'](0), updates, value_and_set['$[]'](1))}, TMP_6.$$s = self, TMP_6.$$arity = 2, TMP_6), $i).call($j)}, TMP_5.$$s = self, TMP_5.$$arity = 2, TMP_5), $e).call($h);
15068+
($e = ($i = updates).$each, $e.$$p = (TMP_7 = function(observer, args){var self = TMP_7.$$s || this, $j;
15069+
if (observer == null) observer = nil;if (args == null) args = nil;
15070+
if ((($j = self.$current_observers()['$key?'](observer)) !== nil && $j != null && (!$j.$$is_boolean || $j == true))) {
15071+
return ($j = observer).$update_react_js_state.apply($j, Opal.to_a(args))
15072+
} else {
15073+
return nil
15074+
}}, TMP_7.$$s = self, TMP_7.$$arity = 2, TMP_7), $e).call($i);
15075+
return updates = nil;}, TMP_2.$$s = self, TMP_2.$$arity = 0, TMP_2), $b).call($d, 0.001));
15076+
} else if (self.rendering_level['$=='](0)) {
15077+
updates = ($a = ($b = $scope.get('Hash')).$new, $a.$$p = (TMP_8 = function(hash, key){var self = TMP_8.$$s || this;
15078+
if (hash == null) hash = nil;if (key == null) key = nil;
15079+
return hash['$[]='](key, $scope.get('Array').$new())}, TMP_8.$$s = self, TMP_8.$$arity = 2, TMP_8), $a).call($b);
15080+
self.$set_state2(object, name, value, updates);
15081+
($a = ($e = updates).$each, $a.$$p = (TMP_9 = function(observer, args){var self = TMP_9.$$s || this, $f;
15082+
if (observer == null) observer = nil;if (args == null) args = nil;
15083+
return ($f = observer).$update_react_js_state.apply($f, Opal.to_a(args))}, TMP_9.$$s = self, TMP_9.$$arity = 2, TMP_9), $a).call($e);};
15084+
return value;
15085+
}, TMP_10.$$arity = -4), nil) && 'set_state'
15086+
})($scope.base, null)
15087+
})($scope.base);
1502415088
})(Opal);

hyperloop/hyperloop.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,40 @@ class Component
1515
require 'hyper-store'
1616
# require 'hyper-router'
1717
require 'react/top_level_render'
18+
19+
20+
# state patch
21+
module React
22+
class State
23+
def self.set_state(object, name, value, delay=ALWAYS_UPDATE_STATE_AFTER_RENDER)
24+
states[object][name] = value
25+
if delay || @bulk_update_flag
26+
@delayed_updates ||= Hash.new { |h, k| h[k] = {} }
27+
@delayed_updates[object][name] = [value, Set.new]
28+
@delayed_updater ||= after(0.001) do
29+
structure_check('delayed set state')
30+
delayed_updates = @delayed_updates
31+
@delayed_updates = Hash.new { |h, k| h[k] = {} }
32+
@delayed_updater = nil
33+
updates = Hash.new { |hash, key| hash[key] = Array.new }
34+
delayed_updates.each do |object, name_hash|
35+
name_hash.each do |name, value_and_set|
36+
set_state2(object, name, value_and_set[0], updates, value_and_set[1])
37+
end
38+
end
39+
updates.each { |observer, args|
40+
# -------> CHECK to make sure observer is still in current_observers hash
41+
observer.update_react_js_state(*args) if current_observers.key?(observer)
42+
# ------->
43+
}
44+
updates = nil
45+
end
46+
elsif @rendering_level == 0
47+
updates = Hash.new { |hash, key| hash[key] = Array.new }
48+
set_state2(object, name, value, updates)
49+
updates.each { |observer, args| observer.update_react_js_state(*args) }
50+
end
51+
value
52+
end
53+
end
54+
end

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "hyperloop-js",
3+
"version": "1.0.0",
4+
"description": "Ruby Hyperloop client-side components",
5+
"main": "./dist/hyperloop.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/ruby-hyperloop/hyperloop-js.git"
12+
},
13+
"keywords": [
14+
"ruby",
15+
"compiler",
16+
"opal"
17+
],
18+
"author": "Ruby Hyperloop team",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/ruby-hyperloop/hyperloop-js/issues"
22+
},
23+
"homepage": "http://ruby-hyperloop.io/"
24+
}

0 commit comments

Comments
 (0)