Skip to content

Commit 51ca025

Browse files
authored
Update Graph API (#145)
1 parent a711595 commit 51ca025

File tree

7 files changed

+88
-52
lines changed

7 files changed

+88
-52
lines changed

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.

Sources/OpenGraph/Attribute/Attribute/AnyAttribute.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
public import OpenGraphCxx
99

10+
@_silgen_name("OGGraphMutateAttribute")
11+
private func OGGraphMutateAttribute(
12+
_ attribute: AnyAttribute,
13+
type: Metadata,
14+
invalidating: Bool,
15+
body: (UnsafeMutableRawPointer) -> Void
16+
)
17+
1018
extension AnyAttribute {
1119
public typealias Flags = OGAttributeTypeFlags
1220

@@ -46,7 +54,11 @@ extension AnyAttribute {
4654
}
4755

4856
public func mutateBody<Value>(as type: Value.Type, invalidating: Bool, _ body: (inout Value) -> Void) {
49-
AnyAttribute.mutateAttribute(self, type: Metadata(type), invalidating: invalidating) { value in
57+
OGGraphMutateAttribute(
58+
self,
59+
type: Metadata(type),
60+
invalidating: invalidating
61+
) { value in
5062
body(&value.assumingMemoryBound(to: Value.self).pointee)
5163
}
5264
}
@@ -87,17 +99,6 @@ extension AnyAttribute: Swift.CustomStringConvertible {
8799

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

90-
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
91-
extension AnyAttribute {
92-
@_silgen_name("OGGraphMutateAttribute")
93-
private static func mutateAttribute(
94-
_ attribute: AnyAttribute,
95-
type: Metadata,
96-
invalidating: Bool,
97-
body: (UnsafeMutableRawPointer) -> Void
98-
)
99-
}
100-
101102
extension [AnyAttribute] {
102103
@_transparent
103104
public var anyInputsChanged: Bool {

Sources/OpenGraph/Graph/Graph.swift

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,62 @@ extension Graph {
2020
}
2121
}
2222

23+
@_silgen_name("OGGraphSetInvalidationCallback")
24+
private func OGGraphSetInvalidationCallback(
25+
graph: Graph,
26+
_ callback: @escaping (AnyAttribute) -> Void
27+
)
28+
29+
@_silgen_name("OGGraphSetUpdateCallback")
30+
private func OGGraphSetUpdateCallback(
31+
graph: Graph,
32+
_ callback: @escaping () -> Void
33+
)
34+
2335
extension Graph {
24-
@_silgen_name("OGGraphStartProfiling")
25-
public static func startProfiling(_ graph: Graph? = nil)
36+
public static func withoutUpdate<V>(_ body: () -> V) -> V {
37+
let update = clearUpdate()
38+
defer { setUpdate(update) }
39+
return body()
40+
}
41+
42+
public func withoutSubgraphInvalidation<V>(_ body: () -> V) -> V {
43+
preconditionFailure("TODO")
44+
}
45+
46+
public func withDeadline<V>(
47+
_: UInt64,
48+
_: () -> V
49+
) -> V {
50+
preconditionFailure("TODO")
51+
}
52+
53+
public func onInvalidation(_ callback: @escaping (AnyAttribute) -> Void) {
54+
OGGraphSetInvalidationCallback(graph: self, callback)
55+
}
56+
57+
public func onUpdate(_ callback: @escaping () -> Void) {
58+
OGGraphSetUpdateCallback(graph: self, callback)
59+
}
2660

27-
@_silgen_name("OGGraphStopProfiling")
28-
public static func stopProfiling(_ graph: Graph? = nil)
61+
public func withMainThreadHandler(_: (() -> Void) -> Void, do: () -> Void) {
62+
// TODO: OGGraphWithMainThreadHandler
63+
preconditionFailure("TODO")
64+
}
2965
}
3066

31-
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
32-
// > 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.
33-
//extension OGGraph {
34-
// @_silgen_name("OGGGraphSetInvalidationCallback") // Use Swift ABI(self: x20) ❌, we need C ABI here(self: x0).
35-
// public func setInvalidationCallback(_ callback: ((AnyAttribute) -> Void)?)
36-
//
37-
// @_silgen_name("OGGGraphSetUpdateCallback")
38-
// public func setUpdateCallback(_ callback: (() -> Void)?)
39-
//}
4067
extension Graph {
41-
@_silgen_name("OGGraphSetInvalidationCallback")
42-
public static func setInvalidationCallback(_ graph: Graph, callback: ((AnyAttribute) -> Void)?)
68+
public static func startProfiling() {
69+
__OGGraphStartProfiling(nil)
70+
}
4371

44-
@_silgen_name("OGGraphSetUpdateCallback")
45-
public static func setUpdateCallback(_ graph: Graph, callback: (() -> Void)?)
72+
public static func stopProfiling() {
73+
__OGGraphStopProfiling(nil)
74+
}
75+
76+
public static func resetProfile() {
77+
__OGGraphResetProfile(nil)
78+
}
4679
}
4780

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

54-
extension Graph {
55-
public static func withoutUpdate<Value>(_ body: () -> Value) -> Value {
56-
let update = clearUpdate()
57-
defer { setUpdate(update) }
58-
return body()
59-
}
60-
}
61-
6287
extension Graph {
6388
// NOTE: Currently Swift does not support generic computed variable
6489
@_silgen_name("OGGraphGetOutputValue")

Sources/OpenGraph/Graph/Subgraph.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public import OpenGraphCxx
99

1010
extension Subgraph {
1111
public func addObserver(_ observer: () -> Void) -> Int {
12-
Subgraph.addObserver(self, observer: observer)
12+
OGSubgraphAddObserver(self, observer: observer)
1313
}
1414

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

3030
public func forEach(_ flags: OGAttributeFlags, _ callback: (AnyAttribute) -> Void) {
31-
Subgraph.apply(self, flags: flags, callback: callback)
31+
OGSubgraphApply(self, flags: flags, callback: callback)
3232
}
3333
}
3434

@@ -52,11 +52,15 @@ extension Subgraph {
5252
}
5353
}
5454

55-
// FIXME: migrate to use @_extern(c, "xx") in Swift 6
56-
extension Subgraph {
57-
@_silgen_name("OGSubgraphApply")
58-
private static func apply(_ graph: Subgraph, flags: OGAttributeFlags, callback: (AnyAttribute) -> Void)
59-
60-
@_silgen_name("OGSubgraphAddObserver")
61-
private static func addObserver(_ graph: Subgraph, observer: () -> Void) -> Int
62-
}
55+
@_silgen_name("OGSubgraphApply")
56+
private func OGSubgraphApply(
57+
_ graph: Subgraph,
58+
flags: OGAttributeFlags,
59+
callback: (AnyAttribute) -> Void
60+
)
61+
62+
@_silgen_name("OGSubgraphAddObserver")
63+
private func OGSubgraphAddObserver(
64+
_ graph: Subgraph,
65+
observer: () -> Void
66+
) -> Int

Sources/OpenGraphCxx/Graph/OGGraph.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ void OGGraphStopProfiling(_Nullable OGGraphRef graph) {
9292
graph->context.get_graph().stop_profiling();
9393
}
9494

95+
void OGGraphResetProfile(_Nullable OGGraphRef graph) {
96+
// TODO
97+
}
98+
9599
const void * _Nullable OGGraphGetContext(OGGraphRef graph) {
96100
if (graph->context.isInvalid()) {
97101
OG::precondition_failure("invalidated graph");

Sources/OpenGraphCxx/include/OpenGraph/OGGraph.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ OG_EXPORT
4949
OG_REFINED_FOR_SWIFT
5050
void OGGraphStopProfiling(_Nullable OGGraphRef graph);
5151

52+
OG_EXPORT
53+
OG_REFINED_FOR_SWIFT
54+
void OGGraphResetProfile(_Nullable OGGraphRef graph);
55+
5256
OG_EXPORT
5357
OG_REFINED_FOR_SWIFT
5458
const void * _Nullable OGGraphGetContext(OGGraphRef graph) OG_SWIFT_NAME(getter:OGGraphRef.context(self:));

Tests/OpenGraphCompatibilityTests/Graph/GraphCompatibilityTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ struct GraphCompatibilityTests {
7070
@Test
7171
func graphCallback() {
7272
let graph = Graph()
73-
Graph.setUpdateCallback(graph, callback: nil)
74-
Graph.setUpdateCallback(graph) {
73+
graph.onUpdate {
7574
print("Update")
7675
}
77-
Graph.setInvalidationCallback(graph, callback: nil)
78-
Graph.setInvalidationCallback(graph) { attr in
76+
graph.onInvalidation { attr in
7977
print("Invalidate \(attr)")
8078
}
8179
}

0 commit comments

Comments
 (0)