@@ -7,7 +7,7 @@ import org.scalajs.jsenv.{Input, RunConfig}
7
7
8
8
import java .util
9
9
import java .util .concurrent .ConcurrentLinkedQueue
10
- import java .util .concurrent .atomic .AtomicInteger
10
+ import java .util .concurrent .atomic .{ AtomicBoolean , AtomicInteger }
11
11
import java .util .function .Consumer
12
12
import scala .annotation .tailrec
13
13
import scala .concurrent .duration .DurationInt
@@ -102,21 +102,26 @@ object ResourcesFactory {
102
102
} yield ()
103
103
104
104
val fetchCounter = new AtomicInteger (0 )
105
+
105
106
def fetchMessages (
106
107
pageInstance : Page ,
107
108
intf : String
109
+ ): java.util.Map [String , java.util.List [String ]] = {
110
+ val data =
111
+ pageInstance
112
+ .evaluate(s " $intf.fetch(); " )
113
+ .asInstanceOf [java.util.Map [String , java.util.List [String ]]]
114
+ data
115
+ }
116
+ def fetchMessagesResource (
117
+ pageInstance : Page ,
118
+ intf : String
108
119
): Resource [IO , util.Map [String , util.List [String ]]] = {
109
- Resource .make {
110
- IO {
111
- scribe.info(
112
- s " Page instance is ${pageInstance.hashCode()} fetchCounter is ${fetchCounter.incrementAndGet()}"
113
- )
114
- val data = pageInstance
115
- .evaluate(s " $intf.fetch(); " )
116
- .asInstanceOf [java.util.Map [String , java.util.List [String ]]]
117
- data
118
- }
119
- }(_ => IO .unit)
120
+ Resource .pure[IO , util.Map [String , util.List [String ]]] {
121
+ scribe.info(
122
+ s " Page instance is ${pageInstance.hashCode()} fetchCounter is ${fetchCounter.incrementAndGet()}" )
123
+ fetchMessages(pageInstance, intf)
124
+ }
120
125
}
121
126
122
127
def sendAllResource (
@@ -125,31 +130,60 @@ object ResourcesFactory {
125
130
intf : String
126
131
): Resource [IO , Unit ] = {
127
132
Resource .make {
128
- scribe.info(s " Sending all messages sendQueue size is ${sendQueue.size()} " )
133
+ scribe.info(
134
+ s " Sending all messages sendQueue size is ${sendQueue.size()} "
135
+ )
129
136
sendAll(sendQueue, pageInstance, intf)
130
137
IO .unit
131
138
}(_ => IO .unit)
132
139
}
133
140
141
+ def ProcessUntilStop (
142
+ stopSignal : AtomicBoolean ,
143
+ pageInstance : Page ,
144
+ intf : String ,
145
+ sendQueue : ConcurrentLinkedQueue [String ],
146
+ outStream : OutputStreams .Streams ,
147
+ receivedMessage : String => Unit ,
148
+ isComEnabled : Boolean
149
+ ): Resource [IO , Unit ] = {
150
+ Resource .make {
151
+ IO .unit
152
+ } { _ =>
153
+ while (! stopSignal.get() && isComEnabled) {
154
+ scribe.info(
155
+ s " Calling repeatSendUntilStopSignal with stopSignal = ${stopSignal.get()}"
156
+ )
157
+ IO .sleep(100 .milliseconds)
158
+ sendAll(sendQueue, pageInstance, intf)
159
+ val jsResponse = fetchMessages(pageInstance, intf)
160
+ streamWriter(jsResponse, outStream, Some (receivedMessage))
161
+ }
162
+ IO .unit
163
+ }
164
+ }
165
+
166
+
134
167
def fetchAndProcess (
135
168
stopSignal : Deferred [IO , Boolean ],
136
169
pageInstance : Page ,
137
170
intf : String ,
138
- runConfig : RunConfig
171
+ // runConfig: RunConfig,
172
+ outStream : OutputStreams .Streams
139
173
): Resource [IO , Unit ] = {
140
174
Resource .make {
141
- stopSignal.get.attempt. flatMap {
142
- case Left (_) =>
175
+ stopSignal.get.flatMap {
176
+ case true =>
143
177
scribe.info(" Stopping the program" )
144
178
IO .unit
145
- case Right (_) => {
179
+ case false => {
146
180
scribe.info(" Program is running" )
147
181
for {
148
182
_ <- Resource .pure(
149
183
scribe.info(s " Page instance is ${pageInstance.hashCode()}" )
150
184
)
151
- jsResponse <- fetchMessages (pageInstance, intf)
152
- _ <- streamWriter (jsResponse, runConfig )
185
+ jsResponse <- fetchMessagesResource (pageInstance, intf)
186
+ _ <- streamWriterResource (jsResponse, outStream )
153
187
} yield ()
154
188
IO .sleep(100 .milliseconds)
155
189
}
@@ -161,14 +195,15 @@ object ResourcesFactory {
161
195
pageInstance : Page ,
162
196
intf : String
163
197
): Resource [IO , Boolean ] = {
164
- Resource .make {
165
- IO {
166
- scribe.info(s " Page instance is ${pageInstance.hashCode()}" )
167
- pageInstance.evaluate(s " !! $intf; " ).asInstanceOf [Boolean ]
168
- }
169
- }(_ => IO .unit)
198
+ Resource .pure[IO , Boolean ] {
199
+ scribe.info(s " Page instance is ${pageInstance.hashCode()}" )
200
+ pageInstance.evaluate(s " !! $intf; " ).asInstanceOf [Boolean ]
201
+ }
202
+
170
203
}
171
204
205
+
206
+
172
207
def materializer (pwConfig : Config ): Resource [IO , FileMaterializer ] =
173
208
Resource .make {
174
209
IO .blocking(FileMaterializer (pwConfig.materialization)) // build
@@ -185,7 +220,7 @@ object ResourcesFactory {
185
220
/*
186
221
* Creates resource for outputStream
187
222
*/
188
- private def outputStream (
223
+ def outputStream (
189
224
runConfig : RunConfig
190
225
): Resource [IO , OutputStreams .Streams ] =
191
226
Resource .make {
@@ -202,24 +237,37 @@ object ResourcesFactory {
202
237
203
238
def streamWriter (
204
239
jsResponse : util.Map [String , util.List [String ]],
205
- runConfig : RunConfig ,
240
+ outStream : OutputStreams . Streams ,
206
241
onMessage : Option [String => Unit ] = None
207
- ): Resource [ IO , Unit ] = {
242
+ ): Unit = {
208
243
val data = jsResponse.get(" consoleLog" )
209
244
val consoleError = jsResponse.get(" consoleError" )
210
245
val error = jsResponse.get(" errors" )
211
246
onMessage match {
212
247
case Some (f) =>
213
248
val msgs = jsResponse.get(" msgs" )
249
+ // data.get("msgs").forEach(consumer(receivedMessage _))
250
+ // msgs.forEach(receivedMessage(f))
214
251
msgs.forEach(consumer(f))
215
- case None => ( )
252
+ case None => scribe.info( " No onMessage function " )
216
253
}
217
- for {
218
- out <- ResourcesFactory .outputStream(runConfig)
219
- _ <- Resource .pure(data.forEach(out.out.println _))
220
- _ <- Resource .pure(error.forEach(out.out.println _))
221
- _ <- Resource .pure(consoleError.forEach(out.out.println _))
222
- } yield ()
254
+ data.forEach(outStream.out.println _)
255
+ error.forEach(outStream.out.println _)
256
+ consoleError.forEach(outStream.out.println _)
257
+ // val errs = data.get("errors")
258
+ if (! error.isEmpty) {
259
+ // Convoluted way of writing errs.toList without JavaConverters.
260
+ val errList = error.toArray(Array [String ]()).toList
261
+ throw new WindowOnErrorException (errList)
262
+ }
263
+ }
264
+ def streamWriterResource (
265
+ jsResponse : util.Map [String , util.List [String ]],
266
+ outStream : OutputStreams .Streams ,
267
+ onMessage : Option [String => Unit ] = None
268
+ ): Resource [IO , Unit ] = {
269
+ streamWriter(jsResponse, outStream, onMessage)
270
+ Resource .pure[IO , Unit ](IO .unit)
223
271
}
224
272
225
273
@ tailrec
@@ -229,8 +277,8 @@ object ResourcesFactory {
229
277
intf : String
230
278
): Unit = {
231
279
val msg = sendQueue.poll()
232
- scribe.info(s " Sending message $msg" )
233
280
if (msg != null ) {
281
+ scribe.debug(s " Sending message $msg" )
234
282
val script = s " $intf.send(arguments[0]); "
235
283
val wrapper = s " function(arg) { $script } "
236
284
pageInstance.evaluate(s " $wrapper" , msg)
0 commit comments