react-native-unistyles 3.0.0-alpha.23 → 3.0.0-alpha.24

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,14 @@
1
1
  #if os(iOS)
2
2
 
3
3
  import Foundation
4
+ import Combine
4
5
  import NitroModules
5
6
 
6
7
  typealias CxxListener = (Array<UnistyleDependency>) -> Void
7
8
 
8
9
  class NativeIOSPlatform: HybridNativePlatformSpec {
9
10
  var miniRuntime: UnistylesNativeMiniRuntime?
11
+ var cancellables = Set<AnyCancellable>()
10
12
 
11
13
  var listeners: Array<CxxListener> = []
12
14
  var hybridContext = margelo.nitro.HybridContext()
@@ -2,16 +2,18 @@ import Foundation
2
2
 
3
3
  extension NativeIOSPlatform {
4
4
  func setupPlatformListeners() {
5
- NotificationCenter.default.addObserver(
6
- self,
7
- selector: #selector(onWindowChange(_:)),
8
- name: NSNotification.Name("RCTWindowFrameDidChangeNotification"),
9
- object: nil
10
- )
5
+ NotificationCenter.default.publisher(for: NSNotification.Name("RCTWindowFrameDidChangeNotification"))
6
+ // add small delay (10ms) to make sure all values are up ot date
7
+ // we MUST call it on current thread, otherwise random crashes occurs
8
+ .delay(for: .milliseconds(10), scheduler: RunLoop.current)
9
+ .sink { [weak self] notification in
10
+ self?.onWindowChange(notification)
11
+ }
12
+ .store(in: &cancellables)
11
13
  }
12
14
 
13
15
  func removePlatformListeners() {
14
- NotificationCenter.default.removeObserver(self, name: NSNotification.Name("RCTWindowFrameDidChangeNotification"), object: nil)
16
+ cancellables.removeAll()
15
17
  }
16
18
 
17
19
  func registerPlatformListener(callback: @escaping (CxxListener)) throws {
@@ -24,18 +26,16 @@ extension NativeIOSPlatform {
24
26
 
25
27
  @objc func onWindowChange(_ notification: Notification) {
26
28
  // add small delay (10ms) to make sure all values are up ot date
27
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
28
- guard let currentMiniRuntime = self.miniRuntime else {
29
- return
30
- }
31
-
32
- let newMiniRuntime = self.buildMiniRuntime()
33
- let changedDependencies = UnistylesNativeMiniRuntime.diff(lhs: currentMiniRuntime, rhs: newMiniRuntime)
34
-
35
- if (changedDependencies.count > 0) {
36
- self.miniRuntime = newMiniRuntime
37
- self.emitCxxEvent(dependencies: changedDependencies)
38
- }
29
+ guard let currentMiniRuntime = self.miniRuntime else {
30
+ return
31
+ }
32
+
33
+ let newMiniRuntime = self.buildMiniRuntime()
34
+ let changedDependencies = UnistylesNativeMiniRuntime.diff(lhs: currentMiniRuntime, rhs: newMiniRuntime)
35
+
36
+ if (changedDependencies.count > 0) {
37
+ self.miniRuntime = newMiniRuntime
38
+ self.emitCxxEvent(dependencies: changedDependencies)
39
39
  }
40
40
  }
41
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-alpha.23",
3
+ "version": "3.0.0-alpha.24",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",