Skip to content

Update Graph API #145

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

Merged
merged 2 commits into from
Aug 2, 2025
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
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

public import OpenGraphCxx

@_silgen_name("OGGraphMutateAttribute")
private func OGGraphMutateAttribute(
_ attribute: AnyAttribute,
type: Metadata,
invalidating: Bool,
body: (UnsafeMutableRawPointer) -> Void
)

extension AnyAttribute {
public typealias Flags = OGAttributeTypeFlags

Expand Down Expand Up @@ -46,7 +54,11 @@ extension AnyAttribute {
}

public func mutateBody<Value>(as type: Value.Type, invalidating: Bool, _ body: (inout Value) -> Void) {
AnyAttribute.mutateAttribute(self, type: Metadata(type), invalidating: invalidating) { value in
OGGraphMutateAttribute(
self,
type: Metadata(type),
invalidating: invalidating
) { value in
body(&value.assumingMemoryBound(to: Value.self).pointee)
}
}
Expand Down Expand Up @@ -87,17 +99,6 @@ extension AnyAttribute: Swift.CustomStringConvertible {

public typealias AttributeUpdateBlock = () -> (UnsafeMutableRawPointer, AnyAttribute) -> Void

// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension AnyAttribute {
@_silgen_name("OGGraphMutateAttribute")
private static func mutateAttribute(
_ attribute: AnyAttribute,
type: Metadata,
invalidating: Bool,
body: (UnsafeMutableRawPointer) -> Void
)
}

extension [AnyAttribute] {
@_transparent
public var anyInputsChanged: Bool {
Expand Down
75 changes: 50 additions & 25 deletions Sources/OpenGraph/Graph/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,62 @@ extension Graph {
}
}

@_silgen_name("OGGraphSetInvalidationCallback")
private func OGGraphSetInvalidationCallback(
graph: Graph,
_ callback: @escaping (AnyAttribute) -> Void
)

@_silgen_name("OGGraphSetUpdateCallback")
private func OGGraphSetUpdateCallback(
graph: Graph,
_ callback: @escaping () -> Void
)

extension Graph {
@_silgen_name("OGGraphStartProfiling")
public static func startProfiling(_ graph: Graph? = nil)
public static func withoutUpdate<V>(_ body: () -> V) -> V {
let update = clearUpdate()
defer { setUpdate(update) }
return body()
}

public func withoutSubgraphInvalidation<V>(_ body: () -> V) -> V {
preconditionFailure("TODO")
}

public func withDeadline<V>(
_: UInt64,
_: () -> V
) -> V {
preconditionFailure("TODO")
}

public func onInvalidation(_ callback: @escaping (AnyAttribute) -> Void) {
OGGraphSetInvalidationCallback(graph: self, callback)
}

public func onUpdate(_ callback: @escaping () -> Void) {
OGGraphSetUpdateCallback(graph: self, callback)
}

@_silgen_name("OGGraphStopProfiling")
public static func stopProfiling(_ graph: Graph? = nil)
public func withMainThreadHandler(_: (() -> Void) -> Void, do: () -> Void) {
// TODO: OGGraphWithMainThreadHandler
preconditionFailure("TODO")
}
}

// FIXME: migrate to use @_extern(c, "xx") in Swift 6
// > Also similar to @_silgen_name, but a function declared with @_extern(c) is assumed to use the C ABI, while @_silgen_name assumes the Swift ABI.
//extension OGGraph {
// @_silgen_name("OGGGraphSetInvalidationCallback") // Use Swift ABI(self: x20) ❌, we need C ABI here(self: x0).
// public func setInvalidationCallback(_ callback: ((AnyAttribute) -> Void)?)
//
// @_silgen_name("OGGGraphSetUpdateCallback")
// public func setUpdateCallback(_ callback: (() -> Void)?)
//}
extension Graph {
@_silgen_name("OGGraphSetInvalidationCallback")
public static func setInvalidationCallback(_ graph: Graph, callback: ((AnyAttribute) -> Void)?)
public static func startProfiling() {
__OGGraphStartProfiling(nil)
}

@_silgen_name("OGGraphSetUpdateCallback")
public static func setUpdateCallback(_ graph: Graph, callback: (() -> Void)?)
public static func stopProfiling() {
__OGGraphStopProfiling(nil)
}

public static func resetProfile() {
__OGGraphResetProfile(nil)
}
}

extension Graph {
Expand All @@ -51,14 +84,6 @@ extension Graph {
public var mainUpdates: Int { numericCast(counter(for: ._10)) }
}

extension Graph {
public static func withoutUpdate<Value>(_ body: () -> Value) -> Value {
let update = clearUpdate()
defer { setUpdate(update) }
return body()
}
}

extension Graph {
// NOTE: Currently Swift does not support generic computed variable
@_silgen_name("OGGraphGetOutputValue")
Expand Down
24 changes: 14 additions & 10 deletions Sources/OpenGraph/Graph/Subgraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public import OpenGraphCxx

extension Subgraph {
public func addObserver(_ observer: () -> Void) -> Int {
Subgraph.addObserver(self, observer: observer)
OGSubgraphAddObserver(self, observer: observer)
}

public func apply<Value>(_ body: () -> Value) -> Value {
Expand All @@ -28,7 +28,7 @@ extension Subgraph {
}

public func forEach(_ flags: OGAttributeFlags, _ callback: (AnyAttribute) -> Void) {
Subgraph.apply(self, flags: flags, callback: callback)
OGSubgraphApply(self, flags: flags, callback: callback)
}
}

Expand All @@ -52,11 +52,15 @@ extension Subgraph {
}
}

// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension Subgraph {
@_silgen_name("OGSubgraphApply")
private static func apply(_ graph: Subgraph, flags: OGAttributeFlags, callback: (AnyAttribute) -> Void)

@_silgen_name("OGSubgraphAddObserver")
private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int
}
@_silgen_name("OGSubgraphApply")
private func OGSubgraphApply(
_ graph: Subgraph,
flags: OGAttributeFlags,
callback: (AnyAttribute) -> Void
)

@_silgen_name("OGSubgraphAddObserver")
private func OGSubgraphAddObserver(
_ graph: Subgraph,
observer: () -> Void
) -> Int
4 changes: 4 additions & 0 deletions Sources/OpenGraphCxx/Graph/OGGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ void OGGraphStopProfiling(_Nullable OGGraphRef graph) {
graph->context.get_graph().stop_profiling();
}

void OGGraphResetProfile(_Nullable OGGraphRef graph) {
// TODO
}

const void * _Nullable OGGraphGetContext(OGGraphRef graph) {
if (graph->context.isInvalid()) {
OG::precondition_failure("invalidated graph");
Expand Down
4 changes: 4 additions & 0 deletions Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphStopProfiling(_Nullable OGGraphRef graph);

OG_EXPORT
OG_REFINED_FOR_SWIFT
void OGGraphResetProfile(_Nullable OGGraphRef graph);

OG_EXPORT
OG_REFINED_FOR_SWIFT
const void * _Nullable OGGraphGetContext(OGGraphRef graph) OG_SWIFT_NAME(getter:OGGraphRef.context(self:));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ struct GraphCompatibilityTests {
@Test
func graphCallback() {
let graph = Graph()
Graph.setUpdateCallback(graph, callback: nil)
Graph.setUpdateCallback(graph) {
graph.onUpdate {
print("Update")
}
Graph.setInvalidationCallback(graph, callback: nil)
Graph.setInvalidationCallback(graph) { attr in
graph.onInvalidation { attr in
print("Invalidate \(attr)")
}
}
Expand Down