Skip to content

Commit

Permalink
upgrade liveviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Aug 28, 2024
1 parent 3446999 commit f345ae4
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 99 deletions.
66 changes: 66 additions & 0 deletions src/happyx/private/macro_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,72 @@ proc useComponent*(statement: NimNode, inCycle, inComponent: bool,
newStmtList(newNimNode(nnkDiscardStmt).add(newEmptyNode()))
inc uniqueId
objConstr.add(stringId)
when not defined(js) and enableLiveViews:
objConstr.add(
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"urlPath"),
ident"urlPath"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"urlPath")),
newDotExpr(ident"self", ident"urlPath")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"hostname"),
ident"hostname"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"hostname")),
newDotExpr(ident"self", ident"hostname")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"query"),
ident"query"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"query")),
newDotExpr(ident"self", ident"query")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"queryArr"),
ident"queryArr"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"queryArr")),
newDotExpr(ident"self", ident"queryArr")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"reqMethod"),
ident"reqMethod"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"reqMethod")),
newDotExpr(ident"self", ident"reqMethod")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"inCookies"),
ident"inCookies"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"inCookies")),
newDotExpr(ident"self", ident"inCookies")
), newNimNode(nnkElse).add(
newLit""
)),
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"headers"),
ident"headers"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"headers")),
newDotExpr(ident"self", ident"headers")
), newNimNode(nnkElse).add(
newLit""
)),
)
if statement[1].kind == nnkCall:
for i in 1..<statement[1].len:
# call -> arg
Expand Down
92 changes: 92 additions & 0 deletions src/happyx/spa/components.nim
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,87 @@ macro component*(name, body: untyped): untyped =
),
nnkMethodDef
)
when not defined(js) and enableLiveViews:
initObjConstr.add(
newColonExpr(
ident"urlPath",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"urlPath"),
ident"urlPath"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"urlPath")),
newDotExpr(ident"self", ident"urlPath")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"hostname",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"hostname"),
ident"hostname"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"hostname")),
newDotExpr(ident"self", ident"hostname")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"query",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"query"),
ident"query"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"query")),
newDotExpr(ident"self", ident"query")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"queryArr",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"queryArr"),
ident"queryArr"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"queryArr")),
newDotExpr(ident"self", ident"queryArr")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"reqMethod",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"reqMethod"),
ident"reqMethod"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"reqMethod")),
newDotExpr(ident"self", ident"reqMethod")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"inCookies",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"inCookies"),
ident"inCookies"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"inCookies")),
newDotExpr(ident"self", ident"inCookies")
), newNimNode(nnkElse).add(
newLit""
))
), newColonExpr(
ident"headers",
newNimNode(nnkWhenStmt).add(newNimNode(nnkElifBranch).add(
newCall("declared", ident"headers"),
ident"headers"
), newNimNode(nnkElifBranch).add(
newCall("declared", newDotExpr(ident"self", ident"headers")),
newDotExpr(ident"self", ident"headers")
), newNimNode(nnkElse).add(
newLit""
))
),
)
initProc[2] = generics.copy()

var
Expand Down Expand Up @@ -422,6 +503,17 @@ macro component*(name, body: untyped): untyped =
initParams.add(genericsIdent, newIdentDefs(ident(UniqueComponentId), bindSym"string"))
else:
initParams.add(inherited, newIdentDefs(ident(UniqueComponentId), bindSym"string"))

when not defined(js) and enableLiveViews:
initParams.add(
newIdentDefs(ident"urlPath", ident"string"),
newIdentDefs(ident"hostname", ident"string"),
newIdentDefs(ident"query", ident"StringTableRef"),
newIdentDefs(ident"queryArr", newNimNode(nnkBracketExpr).add(ident"TableRef", ident"string", newNimNode(nnkBracketExpr).add(ident"seq", ident"string",))),
newIdentDefs(ident"reqMethod", ident"HttpMethod"),
newIdentDefs(ident"inCookies", ident"StringTableRef"),
newIdentDefs(ident"headers", ident"HttpHeaders"),
)

var
fields: seq[string] = @[]
Expand Down
20 changes: 16 additions & 4 deletions src/happyx/spa/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ when defined(js):
beforeUpdated*: ComponentEventHandler ## Calls before every rendering
updated*: ComponentEventHandler ## Calls after every DOM rendering
else:
import json
import
std/json,
std/httpcore

type
AppEventHandler* = proc(ev: JsonNode = newJObject()): void
Expand All @@ -98,6 +100,12 @@ else:
BaseComponent* = ref BaseComponentObj
BaseComponentObj* {.inheritable.} = object
uniqCompId*: string
urlPath*, hostname*: string
query*: StringTableRef
queryArr*: TableRef[string, seq[string]]
reqMethod*: HttpMethod
inCookies*: StringTableRef
headers*: HttpHeaders
isCreated*: bool
slot*: proc(
scopeSelf: BaseComponent, inComponent: bool, compName: string,
Expand All @@ -121,13 +129,13 @@ var
currentRoute*: cstring = "/" ## Current route path
scopedCycleCounter*: int = 0
when enableLiveViews and not defined(js):
import std/httpcore
var liveviewRoutes* = newTable[string, proc(
query: StringTableRef,
queryArr: TableRef[string, seq[string]],
reqMethod: HttpMethod,
inCookies: StringTableRef,
headers: HttpHeaders
headers: HttpHeaders,
component: BaseComponent
): TagRef]()
var components* = newTable[string, BaseComponent]()
when enableDefaultComponents:
Expand Down Expand Up @@ -211,7 +219,11 @@ else:
componentsResult[comp.uniqCompId] = "script:" & fmt"<script>{script}</script>"
proc rerender*(query, queryArr, reqMethod, inCookies, headers: auto, hostname, urlPath: string) =
requestResult[hostname] = "rerender:" & liveviewRoutes[urlPath](
query, queryArr, reqMethod, inCookies, headers
query, queryArr, reqMethod, inCookies, headers, nil
).children[1].ugly()
proc rerenderWithComponent*(comp: BaseComponent) =
componentsResult[comp.uniqCompId] = "rerender:" & liveviewRoutes[comp.urlPath](
comp.query, comp.queryArr, comp.reqMethod, comp.inCookies, comp.headers, comp
).children[1].ugly()
proc bck*(hostname, urlPath: string) =
requestResult[hostname] = "bck"
Expand Down
Loading

0 comments on commit f345ae4

Please sign in to comment.