diff --git a/Tools/Sourcery/AutoMockable.stencil b/Tools/Sourcery/AutoMockable.stencil index d9e70b085..1b1390b87 100644 --- a/Tools/Sourcery/AutoMockable.stencil +++ b/Tools/Sourcery/AutoMockable.stencil @@ -36,15 +36,11 @@ import {{ import }} {% endset %} {% if method.parameters.count == 1 and not hasNonEscapingClosures %} {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name | replace:"`","" | upperFirstLetter }} = {{ param.name | replace:"`","" }}{% endfor %} - DispatchQueue.main.async { - self.{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %} - } + {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %} } {% else %} {% if not method.parameters.count == 0 and not hasNonEscapingClosures %} {% call swiftifyMethodName method.selectorName %}ReceivedArguments = ({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %}) - DispatchQueue.main.async { - self.{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append(({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %})) - } + {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations.append(({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %})) } {% endif %} {% endif %} {% endmacro %} @@ -61,29 +57,11 @@ import {{ import }} {% call methodThrowableErrorDeclaration method %} {% endif %} {% if not method.isInitializer %} - var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0 + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}CallsCountLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0 {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}CallsCount: Int { - get { - if Thread.isMainThread { - return {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount - } else { - var returnValue: Int? = nil - DispatchQueue.main.sync { - returnValue = {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount - } - - return returnValue! - } - } - set { - if Thread.isMainThread { - {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue - } else { - DispatchQueue.main.sync { - {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue - } - } - } + get { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount } } + set { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue } } } {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Called: Bool { return {% call swiftifyMethodName method.selectorName %}CallsCount > 0 @@ -95,37 +73,39 @@ import {{ import }} {% endfor -%} {% endset %} {% if method.parameters.count == 1 and not hasNonEscapingClosures %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = [] + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}Lock = NSLock(){% endfor %} + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceived{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} { + get { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} } } + set { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} = newValue } } + } + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = [] + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} { + get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } } + } {% elif not method.parameters.count == 0 and not hasNonEscapingClosures %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = [] + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? { + get { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments = newValue } } + } + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = [] + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] { + get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } } + } {% endif %} {% if not method.returnTypeName.isVoid and not method.isInitializer %} - var {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReturnValueLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} { - get { - if Thread.isMainThread { - return {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue - } else { - var returnValue: {{ method.returnTypeName }}? = nil - DispatchQueue.main.sync { - returnValue = {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue - } - - return returnValue! - } - } - set { - if Thread.isMainThread { - {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue - } else { - DispatchQueue.main.sync { - {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue - } - } - } + get { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue } } + set { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue } } } {% endif %} {% call methodClosureDeclaration method %} @@ -145,7 +125,7 @@ import {{ import }} {% if method.throws %} {% call methodThrowableErrorUsage method %} {% endif %} - {% call swiftifyMethodName method.selectorName %}CallsCount += 1 + {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount += 1 } {% call methodReceivedParameters method %} {% if method.returnTypeName.isVoid %} {% if method.throws %}try {% endif %}{% if method.isAsync %}await {% endif %}{% call methodClosureName method %}?({% call methodClosureCallParameters method %}) diff --git a/Tools/Sourcery/SDKAutoMockable.stencil b/Tools/Sourcery/SDKAutoMockable.stencil index 98296d64e..e54764a6d 100644 --- a/Tools/Sourcery/SDKAutoMockable.stencil +++ b/Tools/Sourcery/SDKAutoMockable.stencil @@ -28,15 +28,11 @@ import {{ import }} {% endset %} {% if method.parameters.count == 1 and not hasNonEscapingClosures %} {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name | replace:"`","" | upperFirstLetter }} = {{ param.name | replace:"`","" }}{% endfor %} - DispatchQueue.main.async { - self.{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %} - } + {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations.append({% for param in method.parameters %}{{ param.name }}){% endfor %} } {% else %} {% if not method.parameters.count == 0 and not hasNonEscapingClosures %} {% call swiftifyMethodName method.selectorName %}ReceivedArguments = ({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %}) - DispatchQueue.main.async { - self.{% call swiftifyMethodName method.selectorName %}ReceivedInvocations.append(({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %})) - } + {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations.append(({% for param in method.parameters %}{{ param.name | replace:"`","" }}: {{ param.name | replace:"`","" }}{% if not forloop.last%}, {% endif %}{% endfor %})) } {% endif %} {% endif %} {% endmacro %} @@ -54,29 +50,11 @@ import {{ import }} {% call methodThrowableErrorDeclaration method %} {% endif %} {% if not method.isInitializer %} - {% call accessLevel method.accessLevel %}var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0 + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}CallsCountLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = 0 {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}CallsCount: Int { - get { - if Thread.isMainThread { - return {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount - } else { - var returnValue: Int? = nil - DispatchQueue.main.sync { - returnValue = {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount - } - - return returnValue! - } - } - set { - if Thread.isMainThread { - {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue - } else { - DispatchQueue.main.sync { - {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue - } - } - } + get { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount } } + set { {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount = newValue } } } {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Called: Bool { return {% call swiftifyMethodName method.selectorName %}CallsCount > 0 @@ -88,37 +66,39 @@ import {{ import }} {% endfor -%} {% endset %} {% if method.parameters.count == 1 and not hasNonEscapingClosures %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = [] + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %}Lock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceived{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}Received{% for param in method.parameters %}{{ param.name| replace:"`","" | upperFirstLetter }}: {{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}?{% endfor %} { + get { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} } } + set { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}Received{{ param.name| replace:"`","" | upperFirstLetter }}Lock{% endfor %}.withLock { {% for param in method.parameters %}{% call swiftifyMethodName method.selectorName %}UnderlyingReceived{{ param.name| replace:"`","" | upperFirstLetter }}{% endfor %} = newValue } } + } + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} = [] + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations{% for param in method.parameters %}: [{{ '(' if param.isClosure }}{{ param.typeName.unwrappedTypeName }}{{ ')' if param.isClosure }}{%if param.typeName.isOptional%}?{%endif%}]{% endfor %} { + get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } } + } {% elif not method.parameters.count == 0 and not hasNonEscapingClosures %} - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? - {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = [] + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedArguments: ({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})? { + get { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedArgumentsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedArguments = newValue } } + } + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock = NSLock() + private {% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] = [] + {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReceivedInvocations: [({% for param in method.parameters %}{{ param.name }}: {{ param.unwrappedTypeName if param.typeAttributes.escaping else param.typeName }}{{ ', ' if not forloop.last }}{% endfor %})] { + get { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations } } + set { {% call swiftifyMethodName method.selectorName %}ReceivedInvocationsLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReceivedInvocations = newValue } } + } {% endif %} {% if not method.returnTypeName.isVoid and not method.isInitializer %} + private {% call staticSpecifier method %}let {% call swiftifyMethodName method.selectorName %}ReturnValueLock = NSLock() {% call accessLevel method.accessLevel %}var {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} {% call accessLevel method.accessLevel %}{% call staticSpecifier method %}var {% call swiftifyMethodName method.selectorName %}ReturnValue: {{ '(' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ method.returnTypeName }}{{ ')' if method.returnTypeName.isClosure and not method.isOptionalReturnType }}{{ '!' if not method.isOptionalReturnType }} { - get { - if Thread.isMainThread { - return {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue - } else { - var returnValue: {{ method.returnTypeName }}? = nil - DispatchQueue.main.sync { - returnValue = {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue - } - - return returnValue! - } - } - set { - if Thread.isMainThread { - {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue - } else { - DispatchQueue.main.sync { - {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue - } - } - } + get { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue } } + set { {% call swiftifyMethodName method.selectorName %}ReturnValueLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingReturnValue = newValue } } } {% endif %} {% call methodClosureDeclaration method %} @@ -134,7 +114,7 @@ import {{ import }} {% if method.throws %} {% call methodThrowableErrorUsage method %} {% endif %} - {% call swiftifyMethodName method.selectorName %}CallsCount += 1 + {% call swiftifyMethodName method.selectorName %}CallsCountLock.withLock { {% call swiftifyMethodName method.selectorName %}UnderlyingCallsCount += 1 } {% call methodReceivedParameters method %} {% if method.returnTypeName.isVoid %} {% if method.throws %}try {% endif %}{% if method.isAsync %}await {% endif %}{% call methodClosureName method %}?({% call methodClosureCallParameters method %})