-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
4 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
Sources/OpenSwiftUI/DataAndStorage/Internal/Location/ObservableObjectLocation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// ObservableObjectLocation.swift | ||
// OpenSwiftUI | ||
// | ||
// Created by Kyle on 2023/11/2. | ||
// Lastest Version: iOS 15.5 | ||
// Status: Blocked by PropertyList.Element | ||
|
||
#if OPENSWIFTUI_USE_COMBINE | ||
import Combine | ||
#else | ||
import OpenCombine | ||
#endif | ||
internal import OpenSwiftUIShims | ||
|
||
struct ObservableObjectLocation<Root, Value>: Location where Root: ObservableObject { | ||
let base: Root | ||
let keyPath: ReferenceWritableKeyPath<Root, Value> | ||
|
||
var wasRead: Bool { | ||
get { true } | ||
set {} | ||
} | ||
func get() -> Value { base[keyPath: keyPath] } | ||
// FIXME | ||
func set(_ value: Value, transaction: Transaction) { | ||
let element = _threadTransactionData().map { Unmanaged<PropertyList.Element>.fromOpaque($0).takeRetainedValue() } | ||
let newElement: PropertyList.Element? | ||
if let element = transaction.plist.elements { | ||
newElement = element.byPrepending(element) | ||
} else { | ||
newElement = element | ||
} | ||
|
||
let data = _threadTransactionData() | ||
defer { _setThreadTransactionData(data) } | ||
_setThreadTransactionData(newElement.map { Unmanaged.passUnretained($0).toOpaque() }) | ||
base[keyPath: keyPath] = value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Sources/OpenSwiftUI/DataAndStorage/ModelData/State/ObservedObject.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// ObservedObject.swift | ||
// OpenSwiftUI | ||
// | ||
// Created by Kyle on 2023/11/5. | ||
// Lastest Version: iOS 15.5 | ||
// Status: Blocked by DynamicProperty | ||
|
||
#if OPENSWIFTUI_USE_COMBINE | ||
import Combine | ||
#else | ||
import OpenCombine | ||
#endif | ||
internal import OpenSwiftUIShims | ||
|
||
@propertyWrapper | ||
@frozen | ||
public struct ObservedObject<ObjectType> where ObjectType: ObservableObject { | ||
@dynamicMemberLookup | ||
@frozen | ||
public struct Wrapper { | ||
let root: ObjectType | ||
public subscript<Subject>(dynamicMember keyPath: ReferenceWritableKeyPath<ObjectType, Subject>) -> Binding<Subject> { | ||
Binding(root, keyPath: keyPath) | ||
} | ||
} | ||
|
||
@usableFromInline | ||
var _seed = 0 | ||
|
||
public var wrappedValue: ObjectType | ||
|
||
@_alwaysEmitIntoClient | ||
public init(initialValue: ObjectType) { | ||
self.init(wrappedValue: initialValue) | ||
} | ||
|
||
public init(wrappedValue: ObjectType) { | ||
self.wrappedValue = wrappedValue | ||
} | ||
|
||
public var projectedValue: ObservedObject<ObjectType>.Wrapper { | ||
.init(root: wrappedValue) | ||
} | ||
} | ||
|
||
extension ObservedObject: DynamicProperty { | ||
public static func _makeProperty(in _: inout _DynamicPropertyBuffer, container _: _GraphValue<some Any>, fieldOffset _: Int, inputs _: inout _GraphInputs) { | ||
// TODO | ||
} | ||
|
||
public static var _propertyBehaviors: UInt32 { 2 } | ||
} | ||
|
||
extension Binding { | ||
init<ObjectType: ObservableObject>(_ root: ObjectType, keyPath: ReferenceWritableKeyPath<ObjectType, Value>) { | ||
let location = ObservableObjectLocation(base: root, keyPath: keyPath) | ||
let box = LocationBox(location: location) | ||
self.init(value: location.get(), location: box) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// TLS.c | ||
// | ||
// | ||
// Created by Kyle on 2023/11/5. | ||
// | ||
|
||
#include "TLS.h" | ||
|
||
static _Thread_local int64_t _perThreadUpdateCount = 0; | ||
static _Thread_local void * _perThreadTransactionData = NULL; | ||
static _Thread_local void * _perThreadGeometryProxyData = NULL; | ||
|
||
void _setThreadTransactionData(void * data) { | ||
_perThreadTransactionData = data; | ||
} | ||
|
||
void * _threadTransactionData(void) { | ||
return _perThreadTransactionData; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// TLS.h | ||
// | ||
// | ||
// Created by Kyle on 2023/11/5. | ||
// | ||
|
||
#ifndef TLS_h | ||
#define TLS_h | ||
|
||
#include <OpenFoundation/OpenFoundation.h> | ||
#include <stdio.h> | ||
|
||
OF_EXPORT | ||
void _setThreadTransactionData(void * _Nullable data); | ||
|
||
OF_EXPORT | ||
void * _Nullable _threadTransactionData(void); | ||
|
||
#endif /* TLS_h */ |