Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Mar 30, 2024
1 parent 573b03c commit 6bf9f5f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 56 deletions.
52 changes: 11 additions & 41 deletions src/tim.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`.
##
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand Down
35 changes: 20 additions & 15 deletions src/tim/engine/compilers/html.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6bf9f5f

Please sign in to comment.