Skip to content

Commit

Permalink
iOS: remove strong self references in plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
hborawski committed Oct 27, 2023
1 parent 80a0ff7 commit 33664b3
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ios/plugins/BaseBeaconPlugin/Sources/BaseBeaconPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ open class BaseBeaconPlugin<BeaconStruct: Decodable>: JSBasePlugin {
for plugin in plugins {
plugin.context = self.context
}
let callback: @convention(block) (JSValue?) -> Void = { rawBeacon in
let callback: @convention(block) (JSValue?) -> Void = { [weak self] rawBeacon in
guard
let object = rawBeacon?.toObject(),
let data = try? JSONSerialization.data(withJSONObject: object),
let beacon = try? AnyTypeDecodingContext(rawData: data)
.inject(to: JSONDecoder())
.decode(BeaconStruct.self, from: data)
else { return }
self.callback?(beacon)
self?.callback?(beacon)
}
let jsCallback = JSValue(object: callback, in: context) as Any
return [["callback": jsCallback, "plugins": plugins.map { $0.pluginRef }]]
Expand Down
3 changes: 2 additions & 1 deletion ios/plugins/BeaconPlugin/Sources/SwiftUIBeaconPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public class BeaconPlugin<BeaconStruct: Decodable>: BaseBeaconPlugin<BeaconStruc

public func apply<P>(player: P) where P: HeadlessPlayer {
guard let player = player as? SwiftUIPlayer else { return }
let beacon = self.beacon(assetBeacon:)
player.hooks?.view.tap(name: "BeaconPlugin") { view in
AnyView(view.environment(\.beaconContext, BeaconContext(self.beacon(assetBeacon:))))
AnyView(view.environment(\.beaconContext, BeaconContext(beacon)))
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions ios/plugins/ExpressionPlugin/Sources/ExpressionPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public class ExpressionPlugin: JSBasePlugin, NativePlugin {
override public func getArguments() -> [Any] {
guard
let map = context?.evaluateScript("Map")?.construct(withArguments: []),
let restWrapper = context?.evaluateScript("(fn) => (context, ...args) => fn(context, args)")
let restWrapper = context?.evaluateScript("(fn) => (context, ...args) => fn(context, args)"),
let context = self.context
else { return [] }
for (key, value) in expressions {
let callback: @convention(block) (JSValue?, JSValue?) -> JSValue? = { (context, params) in
let callback: @convention(block) (JSValue?, JSValue?) -> JSValue? = { (_, params) in
let args = params?.toObject() as? [Any] ?? []
return JSValue(object: value(args), in: self.context)
return JSValue(object: value(args), in: context)
}
let jsCallback = JSValue(object: callback, in: context)
map.invokeMethod("set", withArguments: [key, restWrapper.call(withArguments: [jsCallback as Any]) as Any])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public class ExternalActionPlugin: JSBasePlugin, NativePlugin {
- returns: An array of arguments to construct the plugin
*/
override public func getArguments() -> [Any] {
let callback: @convention(block) (JSValue, JSValue) -> JSValue? = { (state, options) in
let callback: @convention(block) (JSValue, JSValue) -> JSValue? = { [weak self] (state, options) in
guard
let context = self.context,
let context = self?.context,
let controllers = PlayerControllers(from: options),
let promise = JSUtilities.createPromise(context: context, handler: { (resolve, reject) in
do {
try self.handler?(NavigationFlowExternalState(state), controllers) { transition in
try self?.handler?(NavigationFlowExternalState(state), controllers) { transition in
resolve(transition)
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ open class ExternalActionViewModifierPlugin<ModifierType: ExternalStateViewModif
- returns: An array of arguments to construct the plugin
*/
override open func getArguments() -> [Any] {
let callback: @convention(block) (JSValue, JSValue) -> JSValue? = { (state, options) in
let callback: @convention(block) (JSValue, JSValue) -> JSValue? = { [weak self] (state, options) in
guard
let context = self.context,
let context = self?.context,
let controllers = PlayerControllers(from: options),
let promise = JSUtilities.createPromise(context: context, handler: { (resolve, reject) in
self.isExternalState = true
self?.isExternalState = true
let state = NavigationFlowExternalState(state)
self.state = state
self?.state = state
do {
self.content = try self.handler?(state, controllers) { transition in
self?.content = try self?.handler?(state, controllers) { transition in
resolve(transition)
withAnimation {
self.isExternalState = false
self.state = nil
self?.isExternalState = false
self?.state = nil
}
self.content = nil
self?.content = nil
}
} catch {
reject(JSValue(newErrorFromMessage: error.playerDescription, in: context) as Any)
Expand Down
7 changes: 4 additions & 3 deletions ios/plugins/MetricsPlugin/Sources/MetricsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class RequestTimePlugin: NativePlugin {

public func apply<P>(player: P) where P: HeadlessPlayer {
requestTimeWebPlugin.context = player.jsPlayerReference?.context
player.applyTo(MetricsPlugin.self) { plugin in
self.requestTimeWebPlugin.pluginRef?.invokeMethod("apply", withArguments: [plugin])
player.applyTo(MetricsPlugin.self) { [weak self] plugin in
self?.requestTimeWebPlugin.pluginRef?.invokeMethod("apply", withArguments: [plugin])
}
}
}
Expand Down Expand Up @@ -74,9 +74,10 @@ public class MetricsPlugin: JSBasePlugin, NativePlugin, WithSymbol {

public func apply<P>(player: P) where P: HeadlessPlayer {
guard trackRenderTime, let player = player as? SwiftUIPlayer else { return }
let renderEnd = self.renderEnd
player.hooks?.view.tap(name: pluginName, { (view) -> AnyView in
AnyView(view.onAppear {
self.renderEnd()
renderEnd()
})
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class SwiftUICheckPathPlugin: BaseCheckPathPlugin, NativePlugin {

public func apply<P>(player: P) where P: HeadlessPlayer {
guard let player = player as? SwiftUIPlayer else { return }
player.hooks?.view.tap(name: self.pluginName) { view in
player.hooks?.view.tap(name: self.pluginName) { [weak self] view in
AnyView(view.environment(\.checkPath, self))
}
}
Expand Down

0 comments on commit 33664b3

Please sign in to comment.