Skip to content

Commit 85e77ac

Browse files
committed
Get basic sqlite working with updates
1 parent 14a5a87 commit 85e77ac

File tree

19 files changed

+494
-5
lines changed

19 files changed

+494
-5
lines changed

.buildkite/swift-test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function build_for_real_device() {
1818
echo "--- :swift: Building for $platform device"
1919
export NSUnbufferedIO=YES
2020
xcodebuild -destination "generic/platform=$platform" \
21-
-scheme WordPressAPI \
21+
-scheme WordPressAPI-Package \
2222
-derivedDataPath DerivedData \
2323
-skipPackagePluginValidation \
2424
build | xcbeautify

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
/docs
77
/docs.tar.gz
88

9+
# Test DB Files
10+
kotlin.db
11+
swift.db
12+
913
# Ignore Gradle project-specific cache directory
1014
.gradle
1115

@@ -33,7 +37,6 @@ fastlane/report.xml
3337
libwordPressFFI.xcframework*
3438
/swift-docs.tar.gz
3539

36-
3740
# Auto-generated Swift Files
3841
native/swift/Sources/wordpress-api-wrapper/*.swift
3942

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ _build-apple-%-tvos _build-apple-%-tvos-sim _build-apple-%-watchos _build-apple-
102102

103103
# Build the library for a specific target
104104
_build-apple-%:
105-
cargo $(CARGO_OPTS) $(cargo_config_library) build --target $* --package wp_api --profile $(CARGO_PROFILE)
105+
cargo $(CARGO_OPTS) $(cargo_config_library) build --target $* --package wp_api --profile $(CARGO_PROFILE) --no-default-features
106106
./scripts/swift-bindings.sh target/$*/$(CARGO_PROFILE_DIRNAME)/libwp_api.a
107107

108108
# Build the library for one single platform, including real device and simulator.

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ var package = Package(
2626
.library(
2727
name: "WordPressAPI",
2828
targets: ["WordPressAPI"]
29+
),
30+
.library(
31+
name: "WordPressApiCache",
32+
targets: ["WordPressApiCache"]
2933
)
3034
],
3135
dependencies: [
@@ -56,6 +60,13 @@ var package = Package(
5660
.swiftLanguageMode(.v5)
5761
]
5862
),
63+
.target(
64+
name: "WordPressApiCache",
65+
dependencies: [
66+
.target(name: "WordPressAPIInternal")
67+
],
68+
path: "native/swift/Sources/wordpress-api-cache"
69+
),
5970
libwordpressFFI,
6071
.testTarget(
6172
name: "WordPressAPITests",
@@ -68,6 +79,14 @@ var package = Package(
6879
swiftSettings: [
6980
.define("PROGRESS_REPORTING_ENABLED", .when(platforms: [.iOS, .macOS, .tvOS, .watchOS]))
7081
]
82+
),
83+
.testTarget(
84+
name: "WordPressApiCacheTests",
85+
dependencies: [
86+
.target(name: "WordPressApiCache"),
87+
.target(name: "WordPressAPIInternal")
88+
],
89+
path: "native/swift/Tests/wordpress-api-cache"
7190
)
7291
].addingIntegrationTests()
7392
)

native/kotlin/api/android/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import org.jetbrains.kotlin.konan.target.linker
2+
import kotlin.system.exitProcess
3+
14
plugins {
25
id("com.android.library")
36
id("org.jetbrains.kotlin.android")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package rs.wordpress.api.cache.kotlin
2+
3+
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.api.parallel.Execution
5+
import org.junit.jupiter.api.parallel.ExecutionMode
6+
import kotlin.test.assertEquals
7+
8+
@Execution(ExecutionMode.CONCURRENT)
9+
class WordPressApiCache {
10+
11+
@Test
12+
fun testThatMigrationsWork() {
13+
assertEquals(2, WordPressApiCache().performMigrations())
14+
}
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package rs.wordpress.cache.kotlin
2+
3+
import kotlinx.coroutines.asCoroutineDispatcher
4+
import uniffi.wp_api.DatabaseDelegate
5+
import uniffi.wp_api.UpdateHook
6+
import uniffi.wp_api.WpApiCache
7+
import uniffi.wp_api.setGlobalDelegate
8+
import java.nio.file.Path
9+
import java.util.concurrent.Executors
10+
11+
class WordPressApiCacheDelegate : DatabaseDelegate {
12+
override fun didUpdate(updateHook: UpdateHook) {
13+
println("Received update: $updateHook")
14+
}
15+
}
16+
17+
class WordPressApiCache {
18+
private val cache: WpApiCache
19+
private val internalDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
20+
private val delegate: DatabaseDelegate = WordPressApiCacheDelegate()
21+
22+
// Creates a new in-memory cache
23+
constructor() : this(":memory:")
24+
25+
// Creates a new cache at the specified file system URL
26+
constructor(path: Path) : this(path.toString())
27+
28+
// Creates a new cache at the specified path
29+
constructor(string: String) {
30+
this.cache = WpApiCache(string)
31+
}
32+
33+
fun performMigrations(): Int {
34+
internalDispatcher.run {
35+
return this@WordPressApiCache.cache.performMigrations().toInt()
36+
}
37+
}
38+
39+
fun startListeningForUpdates() {
40+
setGlobalDelegate(delegate)
41+
this.cache.startListeningForUpdates()
42+
}
43+
44+
fun stopListeningForUpdates() {
45+
this.cache.stopListeningForUpdates()
46+
}
47+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Foundation
2+
import WordPressAPIInternal
3+
4+
public actor WordPressApiCache {
5+
6+
private let cache: WpApiCache
7+
private let delegate: any DatabaseDelegate
8+
9+
public struct Notifications {
10+
public static let cacheDidUpdate = Notification.Name("WordPressApiCache.cacheDidUpdate")
11+
12+
public static func name(for table: String) -> Notification.Name {
13+
Notification.Name(rawValue: "WordPressApiCachce.cacheDidUpdate.\(table)")
14+
}
15+
}
16+
17+
final public class ApiCacheDelegate: DatabaseDelegate {
18+
public init() {}
19+
20+
public func didUpdate(updateHook: WordPressAPIInternal.UpdateHook) {
21+
let name = Notifications.name(for: updateHook.tableName)
22+
NotificationCenter.default.post(name: name, object: updateHook)
23+
}
24+
}
25+
26+
/// Creates a new in-memory cache
27+
public init(delegate: DatabaseDelegate = ApiCacheDelegate()) throws {
28+
try self.init(path: ":memory:", delegate: delegate)
29+
}
30+
31+
/// Creates a new cache at the specified file system URL
32+
public init(url: URL, delegate: DatabaseDelegate = ApiCacheDelegate()) throws {
33+
try self.init(path: url.absoluteString, delegate: delegate)
34+
}
35+
36+
/// Creates a new cache at the specified path
37+
public init(path: String, delegate: DatabaseDelegate = ApiCacheDelegate()) throws {
38+
self.cache = try WpApiCache(path: path)
39+
self.delegate = delegate
40+
}
41+
42+
public func performMigrations() async throws -> Int {
43+
return Int(try self.cache.performMigrations())
44+
}
45+
46+
public func startListeningForUpdates() {
47+
setGlobalDelegate(delegate: delegate)
48+
self.cache.startListeningForUpdates()
49+
}
50+
51+
public func stopListeningForUpdates() {
52+
self.cache.stopListeningForUpdates()
53+
}
54+
55+
deinit {
56+
self.cache.stopListeningForUpdates()
57+
}
58+
}

0 commit comments

Comments
 (0)