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

Update SVG with AJAX incrementally #81

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/valeriyvan/swift-geometrize.git",
"state" : {
"revision" : "b874832b6824922f877664a78fccee7a7d258705",
"version" : "1.1.1"
"branch" : "feature/asynciterator",
"revision" : "c82b7e104ce6ed94f55530a6ef1351ae4ad23ab9"
}
},
{
Expand Down Expand Up @@ -284,17 +284,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/nerzh/telegram-vapor-bot",
"state" : {
"revision" : "0c3469a60bee8170765ab9f108263e76cd065441",
"version" : "2.4.3"
"revision" : "1e7a08260f354c5e131bac8722494f2f61d786c6",
"version" : "2.5.0"
}
},
{
"identity" : "vapor",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/vapor.git",
"state" : {
"revision" : "67fe736c37b0ad958b9d248f010cff6c1baa5c3a",
"version" : "4.89.3"
"revision" : "6db3d917b5ce5024a84eb265ef65691383305d70",
"version" : "4.90.0"
}
},
{
Expand Down
12 changes: 9 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ let package = Package(
.macOS(.v12)
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.76.0"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.90.0"),
.package(url: "https://github.com/vapor/leaf", from: "4.2.4"),
.package(url: "https://github.com/vapor/leaf-kit", from: "1.10.2"),
.package(url: "https://github.com/nerzh/telegram-vapor-bot", from: "2.4.3"),
.package(url: "https://github.com/valeriyvan/swift-geometrize.git", from: "1.1.1"),
.package(url: "https://github.com/nerzh/telegram-vapor-bot", from: "2.5.0"),
// As remainder how to add local package as dependency:
// https://stackoverflow.com/questions/49819552/swift-4-local-dependencies-with-swift-package-manager
// .package(name: "MyPackage", path: "/local/path/to/package"),
// .package(path: "../Modules/MySwiftLib"),
// .package(url: "file:///path/to/MySwiftLib", from: "1.0.0"),
// .package(url: "../swift-geometrize/", branch: "feature/asynciterator"),
.package(url: "https://github.com/valeriyvan/swift-geometrize.git", branch: "feature/asynciterator"),
.package(url: "https://github.com/valeriyvan/jpeg.git", from: "1.0.2"),
.package(url: "https://github.com/kelvin13/swift-png.git", from: "4.0.2"),
.package(url: "https://github.com/awslabs/aws-sdk-swift", exact: "0.17.0")
Expand Down
19 changes: 10 additions & 9 deletions Sources/App/DefaultHandlers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ final class DefaultBotHandlers {
chatId: .chat(message.chat.id),
messageThreadId: nil, // TODO: ???
text: "How would you like your image to be geometrized?",
replyToMessageId: message.messageId,
replyParameters: TGReplyParameters(messageId: message.messageId),
replyMarkup: .replyKeyboardMarkup(keyboard)
)
try await bot.sendMessage(params: params)
Expand Down Expand Up @@ -143,7 +143,7 @@ final class DefaultBotHandlers {
chatId: .chat(message.chat.id),
messageThreadId: nil, // TODO: ???
text: "What's stroke width?",
replyToMessageId: message.messageId,
replyParameters: TGReplyParameters(messageId: message.messageId),
replyMarkup: .replyKeyboardMarkup(keyboard)
)
try await bot.sendMessage(params: params)
Expand Down Expand Up @@ -171,7 +171,7 @@ final class DefaultBotHandlers {
chatId: .chat(message.chat.id),
messageThreadId: nil, // TODO: ???
text: "How many shapes?",
replyToMessageId: message.messageId,
replyParameters: TGReplyParameters(messageId: message.messageId),
replyMarkup: .replyKeyboardMarkup(keyboard)
)
try await bot.sendMessage(params: params)
Expand Down Expand Up @@ -205,7 +205,7 @@ final class DefaultBotHandlers {
chatId: .chat(message.chat.id),
messageThreadId: nil, // TODO: ???
text: "How many shapes?",
replyToMessageId: message.messageId,
replyParameters: TGReplyParameters(messageId: message.messageId),
replyMarkup: .replyKeyboardMarkup(keyboard)
)
try await bot.sendMessage(params: params)
Expand Down Expand Up @@ -236,16 +236,17 @@ final class DefaultBotHandlers {
" Will post here \(iterations - 1) intermediary geometrizing results and then final one." :
""
),
replyToMessageId: message.messageId
replyParameters: TGReplyParameters(messageId: message.messageId)
)
try await bot.sendMessage(params: params)

let svgSequence = try await Geometrizer.geometrize(
let svgSequence = try await SVGAsyncGeometrizer.geometrize(
bitmap: imageData.bitmap,
shapeTypes: types,
strokeWidth: strokeWidths[userId] ?? 1,
iterations: iterations,
shapesPerIteration: shapesPerIteration
shapesPerIteration: shapesPerIteration,
iterationOptions: .completeSVGEachIteration
)
var shapesCounter = shapesPerIteration
var iteration = 0
Expand Down Expand Up @@ -279,7 +280,7 @@ final class DefaultBotHandlers {
document: .file(file),
thumbnail: .file(thumbnail),
caption: iterations > 1 ? "\(iteration + 1)/\(iterations)" : nil,
replyToMessageId: imageData.messageId
replyParameters: TGReplyParameters(messageId: message.messageId)
)
)
shapesCounter += shapesPerIteration
Expand Down Expand Up @@ -327,7 +328,7 @@ final class DefaultBotHandlers {
chatId: .chat(message.chat.id),
messageThreadId: nil, // TODO: ???
text: "Try send an image...",
replyToMessageId: message.messageId
replyParameters: TGReplyParameters(messageId: message.messageId)
)
state[userId] = .waitImageFromUser
try await connection.bot.sendMessage(params: params)
Expand Down
164 changes: 0 additions & 164 deletions Sources/App/Geometrizer.swift

This file was deleted.

32 changes: 23 additions & 9 deletions Sources/App/routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Vapor
import Geometrize
import Leaf

var cache: [String: (date: Date, iterator: SVGIterator)] = [:]
var cache: [String: (date: Date, iterator: SVGAsyncIterator)] = [:]

var iterators: [UUID: (date: Date, iterator: SVGIterator)] = [:]
var iterators: [UUID: (date: Date, iterator: SVGAsyncIterator)] = [:]

func routes(_ app: Application) throws {
let updateMarker = "<!-- insert here next shapes -->\n"

app.get { req in
cleanup()
return req.leaf.render("index")
Expand Down Expand Up @@ -47,12 +49,13 @@ func routes(_ app: Application) throws {
throw "Cannot process file \(input.file.filename)"
}

let svgSequence: SVGAsyncSequence = try await Geometrizer.geometrize(
let svgSequence: SVGAsyncSequence = try await SVGAsyncGeometrizer.geometrize(
bitmap: bitmap,
shapeTypes: [selectedShape],
strokeWidth: 1,
iterations: steps,
shapesPerIteration: shapeCount / steps
shapesPerIteration: shapeCount / steps,
iterationOptions: .completeSVGEachIteration
)
var asyncIterator = svgSequence.makeAsyncIterator()
cache[id] = (date: Date(), iterator: asyncIterator)
Expand Down Expand Up @@ -112,12 +115,13 @@ func routes(_ app: Application) throws {
throw "Cannot process file \(input.file.filename)"
}

let svgSequence: SVGAsyncSequence = try await Geometrizer.geometrize(
let svgSequence: SVGAsyncSequence = try await SVGAsyncGeometrizer.geometrize(
bitmap: bitmap,
shapeTypes: [selectedShape],
strokeWidth: 1,
iterations: shapeCount,
shapesPerIteration: 1
shapesPerIteration: 1,
iterationOptions: .completeSVGFirstIterationThenDeltas(updateMarker: updateMarker)
)
let asyncIterator = svgSequence.makeAsyncIterator()
let uuid = UUID()
Expand All @@ -135,10 +139,20 @@ func routes(_ app: Application) throws {
try? await ws.close(code: .unacceptableData)
return
}
var firstSVG: String? = nil
var svgAdOns: String = ""
// for try await result in svgSequence { TODO: do this!
while let result = try? await iterator.next() {
let svgLines = result.svg.components(separatedBy: .newlines)
let svg = svgLines.dropFirst(2).joined(separator: "\n")
try? await ws.send(svg)
if let firstSVG {
svgAdOns += result.svg
let fullSVG = firstSVG.replacingOccurrences(of: updateMarker, with: svgAdOns)
try? await ws.send(fullSVG)
} else {
let svgLines = result.svg.components(separatedBy: .newlines)
let firstSVG_ = svgLines.dropFirst(2).joined(separator: "\n")
try? await ws.send(firstSVG_)
firstSVG = firstSVG_
}
}
try? await ws.close()
}
Expand Down