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

feat: update to v1.1.3 #3

Merged
merged 13 commits into from
Nov 28, 2024
Merged
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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ if ProcessInfo.processInfo.environment["LOCAL_BUILD"] != nil {
}else {
FFIbinaryTarget = .binaryTarget(
name: "LoroFFI",
url: "https://github.com/loro-dev/loro-swift/releases/download/0.16.2-alpha.3/loroFFI.xcframework.zip",
checksum: "9475660c4fcee609a498212b8ca038278b34c1325b30fece0ce5740d4025377a"
url: "https://github.com/loro-dev/loro-swift/releases/download/1.0.0-alpha.5/loroFFI.xcframework.zip",
checksum: "2b9c11aecf4f90ead28e12c8487b072e3fc406885e91ab2d1999b8c83040bd6c"
)
}

Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ If you have any suggestions for API, please feel free to create an issue or join
- [x] Checkout
- [x] Subscribe Event
- [x] UndoManager
- [ ] Bindings for all types in Loro
- [x] Bindings for all types in Loro
- [ ] Tests
- [ ] Benchmarks

Expand All @@ -37,7 +37,7 @@ let package = Package(
products: [......],
dependencies:[
...,
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "0.16.2-alpha.3")
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "1.0.0-alpha.5")
],
targets:[
.executableTarget(
Expand Down Expand Up @@ -66,20 +66,18 @@ let s = text.toString()
// XCTAssertEqual(s, "bc")

// subscribe the event
let subId = doc.subscribeRoot{ diffEvent in
let sub = doc.subscribeRoot{ diffEvent in
print(diffEvent)
}
// unsubscribe
doc.unsubscribe(subId: id)

// export updates or snapshot
let doc2 = LoroDoc()
let snapshot = doc.exportSnapshot()
let updates = doc.exportFrom(vv: VersionVector())

// import updates or snapshot
try! doc2.import(snapshot)
try! doc2.import(updates)
let status = try! doc2.import(snapshot)
let status2 = try! doc2.import(updates)
// import batch of updates or snapshot
try! doc2.importBatch(bytes: [snapshot, updates])

Expand Down
34 changes: 30 additions & 4 deletions Sources/Loro/Event.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

import Foundation

class ClosureSubscriber: Subscriber {
private let closure: (DiffEvent) -> Void
Expand All @@ -12,13 +12,25 @@ class ClosureSubscriber: Subscriber {
}
}

class ClosureLocalUpdate: LocalUpdateCallback{
private let closure: (Data) -> Void

public init(closure: @escaping (Data) -> Void) {
self.closure = closure
}

public func onLocalUpdate(update: Data) {
closure(update)
}
}

extension LoroDoc{
/** Subscribe all the events.
*
* The callback will be invoked when any part of the [loro_internal::DocState] is changed.
* The callback will be invoked when any part of the [DocState] is changed.
* Returns a subscription id that can be used to unsubscribe.
*/
public func subscribeRoot(callback: @escaping (DiffEvent)->Void) -> SubId {
public func subscribeRoot(callback: @escaping (DiffEvent)->Void) -> Subscription {
let closureSubscriber = ClosureSubscriber(closure: callback)
return self.subscribeRoot(subscriber: closureSubscriber)
}
Expand All @@ -27,9 +39,23 @@ extension LoroDoc{
*
* The callback will be invoked when the container is changed.
* Returns a subscription id that can be used to unsubscribe.
*
* The events will be emitted after a transaction is committed. A transaction is committed when:
* - `doc.commit()` is called.
* - `doc.exportFrom(version)` is called.
* - `doc.import(data)` is called.
* - `doc.checkout(version)` is called.
*/
public func subscribe(containerId: ContainerId, callback: @escaping (DiffEvent)->Void) -> SubId {
public func subscribe(containerId: ContainerId, callback: @escaping (DiffEvent)->Void) -> Subscription {
let closureSubscriber = ClosureSubscriber(closure: callback)
return self.subscribe(containerId: containerId, subscriber: closureSubscriber)
}

/**
* Subscribe the local update of the document.
*/
public func subscribeLocalUpdate(callback: @escaping (Data)->Void)->Subscription{
let closureLocalUpdate = ClosureLocalUpdate(closure: callback)
return self.subscribeLocalUpdate(callback: closureLocalUpdate)
}
}
20 changes: 20 additions & 0 deletions Sources/Loro/Loro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ extension UndoManager{
}
}
}


class ChangeAncestorsTravel: ChangeAncestorsTraveler{
private let closure: (ChangeMeta)->Bool

public init(closure: @escaping (ChangeMeta)->Bool) {
self.closure = closure
}

func travel(change: ChangeMeta) -> Bool {
closure(change)
}
}

extension LoroDoc{
public func travelChangeAncestors(ids: [Id], f: @escaping (ChangeMeta)->Bool) throws {
let closureSubscriber = ChangeAncestorsTravel(closure: f)
try self.travelChangeAncestors(ids: ids, f: closureSubscriber)
}
}
Loading
Loading