diff --git a/src/tim.nim b/src/tim.nim index 03625a6..d7c468b 100755 --- a/src/tim.nim +++ b/src/tim.nim @@ -204,40 +204,6 @@ proc compileCode(engine: TimEngine, tpl: TimTemplate, else: c.logger.displayErrors() else: p.logger.displayErrors() -var sync: Thread[(Port, int)] -var lastModified, prevModified: Time - -proc browserSync(x: (Port, int)) {.thread.} = - proc onRequest(req: Request) {.async.} = - if req.httpMethod == some(HttpGet): - case req.path.get() - of "/": - req.send("Hello, Hello!") # todo a cool page here? - of "/ws": - try: - var ws = await newWebSocket(req) - while ws.readyState == Open: - if lastModified > prevModified: - # our JS snippet listens on `message`, so once - # we have an update we can just send an empty string. - # connecting to Tim's WebSocket via JS: - # const watchout = new WebSocket('ws://127.0.0.1:6502/ws'); - # watchout.addEventListener('message', () => location.reload()); - await ws.send("") - ws.close() - prevModified = lastModified - sleep(x[1]) - except WebSocketClosedError: - echo "Socket closed" - except WebSocketProtocolMismatchError: - echo "Socket tried to use an unknown protocol: ", getCurrentExceptionMsg() - except WebSocketError: - req.send(Http404) - else: req.send(Http404) - else: req.send(Http503) - let settings = initSettings(x[0], numThreads = 1) - httpx.run(onRequest, settings) - proc resolveDependants(engine: TimEngine, x: seq[string]) = for path in x: let tpl = engine.getTemplateByPath(path) @@ -251,11 +217,10 @@ proc resolveDependants(engine: TimEngine, x: seq[string]) = # for err in engine.errors: # echo err # else: - lastModified = getTime() proc precompile*(engine: TimEngine, callback: TimCallback = nil, flush = true, waitThread = false, browserSyncPort = Port(6502), - browserSyncDelay = 550, global: JsonNode = newJObject(), watchoutNotify = true) = + browserSyncDelay = 200, global: JsonNode = newJObject(), watchoutNotify = true) = ## Precompiles available templates inside `layouts` and `views` ## directories to either static `.html` or binary `.ast`. ## @@ -293,7 +258,6 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil, case tpl.getType() of ttView, ttLayout: engine.compileCode(tpl) - lastModified = getTime() else: engine.resolveDependants(tpl.getDeps.toSeq) @@ -303,10 +267,16 @@ proc precompile*(engine: TimEngine, callback: TimCallback = nil, notify("✨ Deleted", file.getName()) engine.clearTemplateByPath(file.getPath()) - var w = newWatchout(@[engine.getSourcePath() / "*"], onChange, - onFound, onDelete, recursive = true, ext = @["timl"], delay = 200) - # start browser sync server in a separate thread - createThread(sync, browserSync, (browserSyncPort, browserSyncDelay)) + var w = + newWatchout( + @[engine.getSourcePath() / "*"], + onChange, onFound, onDelete, + recursive = true, + ext = @["timl"], delay = 200, + browserSync = + WatchoutBrowserSync(port: browserSyncPort, + delay: browserSyncDelay) + ) # start filesystem monitor in a separate thread w.start(waitThread) else: diff --git a/src/tim/engine/compilers/html.nim b/src/tim/engine/compilers/html.nim index 5157bda..c773c26 100755 --- a/src/tim/engine/compilers/html.nim +++ b/src/tim/engine/compilers/html.nim @@ -356,26 +356,33 @@ proc walkAccessorStorage(c: var HtmlCompiler, of ntDotExpr: let lhs = c.walkAccessorStorage(lhs.lhs, lhs.rhs, scopetables) notnil lhs: - case rhs.nt - of ntIdent: - rhs.identArgs.insert(lhs, 0) - result = c.fnCall(rhs, scopetables) - rhs.identArgs.del(0) - else: + case lhs.nt + of ntLitObject: return c.walkAccessorStorage(lhs, rhs, scopetables) - of ntIdent: - let lhs = c.getValue(lhs, scopetables) - notnil lhs: - case rhs.nt - of ntIdent: - let some = c.getScope(rhs.identName, scopetables) - if likely(some.scopeTable != nil): + else: + case rhs.nt + of ntIdent: rhs.identArgs.insert(lhs, 0) result = c.fnCall(rhs, scopetables) rhs.identArgs.del(0) else: return c.walkAccessorStorage(lhs, rhs, scopetables) + of ntIdent: + let lhs = c.getValue(lhs, scopetables) + notnil lhs: + case lhs.nt + of ntLitObject: + return c.walkAccessorStorage(lhs, rhs, scopetables) else: + case rhs.nt + of ntIdent: + let some = c.getScope(rhs.identName, scopetables) + if likely(some.scopeTable != nil): + rhs.identArgs.insert(lhs, 0) + result = c.fnCall(rhs, scopetables) + rhs.identArgs.del(0) + else: discard + else: discard return c.walkAccessorStorage(lhs, rhs, scopetables) of ntBracketExpr: let lhs = c.bracketEvaluator(lhs, scopetables) @@ -711,8 +718,6 @@ proc getValue(c: var HtmlCompiler, node: Node, if likely(some.scopeTable != nil): case some.scopeTable[node.identName].nt of ntFunction: - # evaluate a function call and return the result - # if the retun type is not void, otherwise nil return c.unsafeCall(node, some.scopeTable[node.identName], scopetables) of ntVariableDef: return c.getValue(some.scopeTable[node.identName].varValue, scopetables)