Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust the vapor provider #102

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adjust the provider to the changes of the latest pull request
mattesmohr committed Oct 17, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4a372a90e45b4bf31b66bcb236d73ca8964f28a3
13 changes: 7 additions & 6 deletions Sources/HTMLKitVaporProvider/Extensions/Vapor+HTMLKit.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Abstract:
The file contains the extensions of some Vapor directives.
*/

import HTMLKit
import Vapor

@@ -45,12 +50,8 @@ extension Application {
}
}

public func add<T: HTMLKit.Page>(page: T) {
self.renderer.add(page: page)
}

public func add<T: HTMLKit.View>(view: T) {
self.renderer.add(view: view)
public func add<T: HTMLKit.AnyLayout>(layout: T) {
self.renderer.add(layout: layout)
}
}
}
21 changes: 13 additions & 8 deletions Sources/HTMLKitVaporProvider/VaporCache.swift
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
/*
Abstract:
The file contains the cache of the Vapor renderer.
*/

import HTMLKit
import Vapor

public class VaporCache {

private var cache: [String: HTMLKit.Renderer.Formula]
private var storage: [String: HTMLKit.Formula]

public var count: Int {
return self.cache.keys.count
return self.storage.keys.count
}

public init() {
self.cache = .init()
self.storage = [:]
}

public func retrieve(name: String, on loop: EventLoop) -> EventLoopFuture<HTMLKit.Renderer.Formula?> {
public func retrieve(name: String, on loop: EventLoop) -> EventLoopFuture<HTMLKit.Formula?> {

if let cache = self.cache[name] {
if let cache = self.storage[name] {
return loop.makeSucceededFuture(cache)

} else {
return loop.makeSucceededFuture(nil)
}
}

public func upsert(name: String, formula: HTMLKit.Renderer.Formula) {
self.cache.updateValue(formula, forKey: name)
public func upsert(name: String, formula: HTMLKit.Formula) {
self.storage.updateValue(formula, forKey: name)
}

public func remove(name: String) {
self.cache.removeValue(forKey: name)
self.storage.removeValue(forKey: name)
}
}
26 changes: 11 additions & 15 deletions Sources/HTMLKitVaporProvider/VaporRenderer.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Abstract:
The file contains the Vapor renderer.
*/

import HTMLKit
import Vapor

@@ -39,9 +44,9 @@ public class VaporRenderer {

var buffer = ByteBufferAllocator().buffer(capacity: 4096)

let manager = HTMLKit.Renderer.ContextManager(rootContext: context)
let manager = HTMLKit.ContextManager(context: context)

for ingredient in formula.ingredient {
for ingredient in formula.ingredients {

if let value = try? ingredient.render(with: manager) {
buffer.writeString(value)
@@ -52,22 +57,13 @@ public class VaporRenderer {
}
}

public func add<T:HTMLKit.Page>(page: T) {

let formula = HTMLKit.Renderer.Formula()

try? page.prerender(formula)

self.cache.upsert(name: String(describing: type(of: page)), formula: formula)
}

public func add<T:HTMLKit.View>(view: T) {
public func add<T:HTMLKit.AnyLayout>(layout: T) {

let formula = HTMLKit.Renderer.Formula()
let formula = HTMLKit.Formula()

try? view.prerender(formula)
try? layout.prerender(formula)

self.cache.upsert(name: String(describing: type(of: view)), formula: formula)
self.cache.upsert(name: String(describing: type(of: layout)), formula: formula)
}
}

13 changes: 9 additions & 4 deletions Tests/HTMLKitVaporProviderTests/ProviderTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Abstract:
The file tests the provider.
*/

import XCTVapor
import HTMLKit
import HTMLKitVaporProvider
@@ -14,7 +19,7 @@ final class ProviderTests: XCTestCase {
var context

var body: AnyContent {
Document(type: .html5)
Document(.html5)
Html {
Head {
Title {
@@ -38,7 +43,7 @@ final class ProviderTests: XCTestCase {

app.views.use(.htmlkit)

app.htmlkit.add(view: TestView())
app.htmlkit.add(layout: TestView())

app.get("test") { request -> EventLoopFuture<Vapor.View> in
return request.view.render("TestView", TestContext(greeting: "hello world"))
@@ -70,7 +75,7 @@ final class ProviderTests: XCTestCase {

defer { app.shutdown() }

app.htmlkit.add(view: TestView())
app.htmlkit.add(layout: TestView())

app.get("test") { request -> EventLoopFuture<Vapor.View> in
return request.htmlkit.render("TestView", TestContext(greeting: "hello world"))
@@ -103,7 +108,7 @@ final class ProviderTests: XCTestCase {

defer { app.shutdown() }

app.htmlkit.add(view: TestView())
app.htmlkit.add(layout: TestView())

app.get("test") { request async throws -> Vapor.View in
return try await request.htmlkit.render("TestView", TestContext(greeting: "hello world"))