Skip to content

Commit

Permalink
Updated examples to use 0.3 features (#10)
Browse files Browse the repository at this point in the history
* added environment and async/awailt in hummingbird demo

* added async to vapor examples
  • Loading branch information
sliemeobn authored Sep 14, 2024
1 parent 85f61a0 commit f20ae43
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 15 deletions.
13 changes: 4 additions & 9 deletions Examples/HummingbirdDemo/Sources/App/Routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import HummingbirdElementary

func addRoutes(to router: Router<some RequestContext>) {
router.get("") { _, _ in
let model = await Database.shared.model

return HTMLResponse {
MainPage(model: model)
HTMLResponse {
MainPage()
}
}

Expand All @@ -27,10 +25,9 @@ func addRoutes(to router: Router<some RequestContext>) {
router.post("/items") { request, context in
let body = try await request.decode(as: AddItemRequest.self, context: context)
await Database.shared.addItem(body.item)
let items = await Database.shared.model.items

return HTMLResponse {
ItemList(items: items)
ItemList()
}
}

Expand All @@ -45,11 +42,9 @@ func addRoutes(to router: Router<some RequestContext>) {
throw HTTPError(.notFound)
}

let items = await Database.shared.model.items

return HTMLResponse {
// exmple of using OOB swaps
ItemList(items: items)
ItemList()
.attributes(.hx.swapOOB(.outerHTML, "#list"))
}
}
Expand Down
14 changes: 9 additions & 5 deletions Examples/HummingbirdDemo/Sources/App/Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import Foundation
struct MainPage: HTMLDocument {
var title: String { "Hummingbird + Elementary + HTMX" }

var model: Model

var head: some HTML {
meta(.charset(.utf8))
script(.src("/htmx.min.js")) {}
Expand All @@ -31,20 +29,22 @@ struct MainPage: HTMLDocument {
}
}
}
ItemList(items: model.items)
ItemList()
}
}
}

struct ItemList: HTML {
var items: [String]
@Environment(EnvironmentValues.$database) var database

var content: some HTML<HTMLTag.div> {
div(.id("list")) {
let items = await database.model.items

h4 { "Items" }
p { "Count: \(items.count)" }

for (index, item) in items.enumerated() {
ForEach(items.enumerated()) { index, item in
div {
// this hx-delete will use OOB swap
button(.hx.delete("items/\(index)")) { "X" }
Expand All @@ -63,3 +63,7 @@ struct TimeHeading: HTML {
}
}
}

enum EnvironmentValues {
@TaskLocal static var database: Database = .shared
}
19 changes: 19 additions & 0 deletions Examples/VaporDemo/Sources/App/BonusFacts.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
actor BonusFactStore {
// courtesy of chatchipitty
let bonusFacts = [
"Writing code is 10% typing and 90% figuring out why you typed it that way in the first place.",
"The hardest part of programming isn't coding — it's naming variables without sounding ridiculous.",
"If code comments are the breadcrumbs, then debugging is following a very messy trail of pizza crumbs.",
"Optionals in Swift are a nice way of saying, 'Yeah, I have no idea what might happen here.'",
"You can fix 99% of your bugs by turning it off and back on. The other 1%? Crying helps.",
"Programmers don't sleep. They just enter a low-power mode with their eyes open.",
"In the world of code, everything is either a `true`, `false`, or 'maybe, if you handle this optional safely.'",
"Any code that hasn't been touched in more than six months is officially considered 'legacy code.'",
]

func calculateBonusFact() async -> String {
return bonusFacts.randomElement()!
}

@TaskLocal static var current: BonusFactStore?
}
1 change: 1 addition & 0 deletions Examples/VaporDemo/Sources/App/Routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func addRoutes(to app: Application) {
let y = try request.query.get(Int.self, at: "y")
return HTMLResponse {
ResultView(x: x, y: y)
.environment(BonusFactStore.$current, BonusFactStore())
}
}

Expand Down
7 changes: 7 additions & 0 deletions Examples/VaporDemo/Sources/App/Views.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ struct ResultView: HTML {
let x: Int
let y: Int

@Environment(requiring: BonusFactStore.$current) var bonusFacts

var content: some HTML {
p {
"\(x) + \(y) = "
b { "\(x + y)" }
}
p {
i {
await bonusFacts.calculateBonusFact()
}
}
}
}
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/sliemeobn/elementary.git", from: "0.1.1"),
.package(url: "https://github.com/sliemeobn/elementary.git", from: "0.3.0"),
],
targets: [
.target(
Expand Down

0 comments on commit f20ae43

Please sign in to comment.