nodo 1.6.2 → 1.6.3
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/README.md +48 -0
- data/lib/nodo/core.rb +5 -7
- data/lib/nodo/function.rb +8 -3
- data/lib/nodo/script.rb +5 -3
- data/lib/nodo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42cd6c0e3e07674ceb2f10dd670c490b7f23ed6534f22f978a40e0b91d529570
|
4
|
+
data.tar.gz: b560df0c25e2b72842e6d6d5be610503252069bfd1741fb2cd9228066d28f91e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 012ed2197c0ea3e519884b3295a62986158326027adb08c484fbf87903874763594fe72236bebcc01f47a919a4893ef92cdc7a14b1f8ad02da7ea527ae5eab38
|
7
|
+
data.tar.gz: 6a953be3a789671c966248dcc385740c5964ad0bbb370411d93cdc44ee7ea129c14537d73c08e7a44eb4c19ca1bc2b5465873bec65fd4d56884ba9ed339981d6
|
data/README.md
CHANGED
@@ -131,6 +131,23 @@ class BarFoo < Nodo::Core
|
|
131
131
|
end
|
132
132
|
```
|
133
133
|
|
134
|
+
With the above syntax, the script code will be generated during class definition
|
135
|
+
time. In order to have the code generated when the first instance is created, the
|
136
|
+
code can be defined inside a block:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
class Foo < Nodo::Core
|
140
|
+
script do
|
141
|
+
<<~JS
|
142
|
+
var definitionTime = #{Time.now.to_json};
|
143
|
+
JS
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
Note that the script will still be executed only once, when the first instance
|
149
|
+
of class is created.
|
150
|
+
|
134
151
|
### Inheritance
|
135
152
|
|
136
153
|
Subclasses will inherit functions, constants, dependencies and scripts from
|
@@ -168,6 +185,37 @@ class SyncFoo < Nodo::Core
|
|
168
185
|
end
|
169
186
|
```
|
170
187
|
|
188
|
+
### Deferred function definition
|
189
|
+
|
190
|
+
By default, the function code string literal is created when the class
|
191
|
+
is defined. Therefore any string interpolation inside the code will take
|
192
|
+
place at definition time.
|
193
|
+
|
194
|
+
In order to defer the code generation until the first object instantiation,
|
195
|
+
the function code can be given inside a block:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
class Deferred < Nodo::Core
|
199
|
+
function :now, <<~JS
|
200
|
+
() => { return #{Time.now.to_json}; }
|
201
|
+
JS
|
202
|
+
|
203
|
+
function :later do
|
204
|
+
<<~JS
|
205
|
+
() => { return #{Time.now.to_json}; }
|
206
|
+
JS
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
instance = Deferred.new
|
211
|
+
sleep 5
|
212
|
+
instance.now => "2021-10-28 20:30:00 +0200"
|
213
|
+
instance.later => "2021-10-28 20:30:05 +0200"
|
214
|
+
```
|
215
|
+
|
216
|
+
The block will be invoked when the first instance is created. As with deferred
|
217
|
+
scripts, it will only be invoked once.
|
218
|
+
|
171
219
|
### Limiting function execution time
|
172
220
|
|
173
221
|
The default timeout for a single JS function call is 60 seconds and can be
|
data/lib/nodo/core.rb
CHANGED
@@ -111,13 +111,11 @@ module Nodo
|
|
111
111
|
self.dependencies = dependencies + mods.merge(deps).map { |name, package| Dependency.new(name, package) }
|
112
112
|
end
|
113
113
|
|
114
|
-
def function(name, _code = nil, timeout: Nodo.timeout, code: nil)
|
114
|
+
def function(name, _code = nil, timeout: Nodo.timeout, code: nil, &block)
|
115
115
|
raise ArgumentError, "reserved method name #{name.inspect}" if reserved_method_name?(name)
|
116
|
-
code = (code ||= _code).strip
|
117
|
-
raise ArgumentError, 'function code is required' if '' == code
|
118
116
|
loc = caller_locations(1, 1)[0]
|
119
117
|
source_location = "#{loc.path}:#{loc.lineno}: in `#{name}'"
|
120
|
-
self.functions = functions.merge(name => Function.new(name, _code || code, source_location, timeout))
|
118
|
+
self.functions = functions.merge(name => Function.new(name, _code || code, source_location, timeout, &block))
|
121
119
|
define_method(name) { |*args| call_js_method(name, args) }
|
122
120
|
end
|
123
121
|
|
@@ -129,8 +127,8 @@ module Nodo
|
|
129
127
|
self.constants = constants + [Constant.new(name, value)]
|
130
128
|
end
|
131
129
|
|
132
|
-
def script(code)
|
133
|
-
self.scripts = scripts + [Script.new(code)]
|
130
|
+
def script(code = nil, &block)
|
131
|
+
self.scripts = scripts + [Script.new(code, &block)]
|
134
132
|
end
|
135
133
|
|
136
134
|
def nodo_js
|
@@ -246,7 +244,7 @@ module Nodo
|
|
246
244
|
request = Net::HTTP::Post.new("/#{clsid}/#{context_id}/#{method}", 'Content-Type': 'application/json')
|
247
245
|
request.body = JSON.dump(args)
|
248
246
|
client = Client.new("unix://#{socket_path}")
|
249
|
-
client.read_timeout = function
|
247
|
+
client.read_timeout = function&.timeout || Nodo.timeout
|
250
248
|
response = client.request(request)
|
251
249
|
if response.is_a?(Net::HTTPOK)
|
252
250
|
parse_response(response)
|
data/lib/nodo/function.rb
CHANGED
@@ -2,12 +2,17 @@ module Nodo
|
|
2
2
|
class Function
|
3
3
|
attr_reader :name, :code, :source_location, :timeout
|
4
4
|
|
5
|
-
def initialize(name, code, source_location, timeout)
|
6
|
-
|
5
|
+
def initialize(name, code, source_location, timeout, &block)
|
6
|
+
raise ArgumentError, 'cannot give code when block is given' if code && block
|
7
|
+
code = code.strip if code
|
8
|
+
raise ArgumentError, 'function code is required' if '' == code
|
9
|
+
raise ArgumentError, 'code is required' unless code || block
|
10
|
+
@name, @code, @source_location, @timeout = name, code || block, source_location, timeout
|
7
11
|
end
|
8
12
|
|
9
13
|
def to_js
|
10
|
-
|
14
|
+
js = code.respond_to?(:call) ? code.call.strip : code
|
15
|
+
"const #{name} = __nodo_klass__.#{name} = (#{js});\n"
|
11
16
|
end
|
12
17
|
end
|
13
18
|
end
|
data/lib/nodo/script.rb
CHANGED
@@ -2,12 +2,14 @@ module Nodo
|
|
2
2
|
class Script
|
3
3
|
attr_reader :code
|
4
4
|
|
5
|
-
def initialize(code)
|
6
|
-
|
5
|
+
def initialize(code = nil, &block)
|
6
|
+
raise ArgumentError, 'cannot give code when block is given' if code && block
|
7
|
+
@code = code || block
|
7
8
|
end
|
8
9
|
|
9
10
|
def to_js
|
10
|
-
|
11
|
+
js = code.respond_to?(:call) ? code.call : code
|
12
|
+
"#{js}\n"
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
data/lib/nodo/version.rb
CHANGED