<commit_before>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  refines: 'foam.core.Listener',
  requires: [
    'foam.swift.Field',
  ],
  properties: [
    {
      class: 'String',
      name: 'swiftListenerName',
      expression: function(swiftName) { return swiftName + '_listener'; },
    },
    {
      class: 'String',
      name: 'swiftListenerMethodName',
      expression: function(swiftName) { return swiftName + '_method'; },
    },
  ],
  methods: [
    function writeToSwiftClass(cls, superAxiom, parentCls) {
      if ( !this.swiftCode ) return;
      var override = !!(superAxiom && superAxiom.swiftCode)
      cls.field(this.Field.create({
        name: this.swiftListenerName,
        initializer: this.swiftInit(),
        type: 'Listener',
        override: override,
        lazy: true,
      }));
      if (!override) {
        cls.method(this.Method.create({
          name: this.swiftName,
          body: this.swiftListenerName + '(Subscription(detach: {}), [])',
          override: !!(superAxiom && superAxiom.swiftCode),
        }));
        cls.fields.push(this.Field.create({
          visibility: 'public',
          static: true,
          final: true,
          name: this.swiftPrivateAxiomName,
          type: 'MethodInfo',
          initializer: this.swiftMethodInfoInit(),
        }));
        cls.methods.push(this.Method.create({
          visibility: 'public',
          class: true,
          name: this.swiftAxiomName,
          returnType: 'MethodInfo',
          body: 'return ' + this.swiftPrivateAxiomName,
          override: this.getSwiftOverride(parentCls),
        }));
      }
      cls.method(this.Method.create({
        name: this.swiftListenerMethodName,
        body: this.swiftCode,
        args: [
          this.SwiftArgument.create({
            localName: 'sub',
            type: 'Subscription',
          }),
          this.SwiftArgument.create({
            localName: 'args',
            type: '[Any?]',
          }),
        ],
        visibility: 'private',
        override: !!(superAxiom && superAxiom.swiftCode),
      }));
    },
  ],
  templates: [
    {
      name: 'swiftInit',
      args: [],
      template: function() {/*
<% if ( this.isMerged || this.isFramed ) { %>

var triggered = false
return { [weak self] sub, args in
  if triggered { return }
  triggered = true
  Timer.scheduledTimer(
      withTimeInterval: <%= (this.isMerged ? this.mergeDelay : 30)/1000 %>,
      repeats: false) { _ in
    triggered = false
    self?.<%=this.swiftListenerMethodName%>(sub, args)
  }
} as (Subscription, [Any?]) -> Void

<% } else { %>

return { [weak self] sub, args in
  self?.<%=this.swiftListenerMethodName%>(sub, args)
} as (Subscription, [Any?]) -> Void
<commit_msg>(Swift) Make listeners use the main thread for their Timer.<commit_after>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

foam.CLASS({
  refines: 'foam.core.Listener',
  requires: [
    'foam.swift.Field',
  ],
  properties: [
    {
      class: 'String',
      name: 'swiftListenerName',
      expression: function(swiftName) { return swiftName + '_listener'; },
    },
    {
      class: 'String',
      name: 'swiftListenerDispatchQueue',
      value: 'DispatchQueue.main',
    },
    {
      class: 'String',
      name: 'swiftListenerMethodName',
      expression: function(swiftName) { return swiftName + '_method'; },
    },
  ],
  methods: [
    function writeToSwiftClass(cls, superAxiom, parentCls) {
      if ( !this.swiftCode ) return;
      var override = !!(superAxiom && superAxiom.swiftCode)
      cls.field(this.Field.create({
        name: this.swiftListenerName,
        initializer: this.swiftInit(),
        type: 'Listener',
        override: override,
        lazy: true,
      }));
      if (!override) {
        cls.method(this.Method.create({
          name: this.swiftName,
          body: this.swiftListenerName + '(Subscription(detach: {}), [])',
          override: !!(superAxiom && superAxiom.swiftCode),
        }));
        cls.fields.push(this.Field.create({
          visibility: 'public',
          static: true,
          final: true,
          name: this.swiftPrivateAxiomName,
          type: 'MethodInfo',
          initializer: this.swiftMethodInfoInit(),
        }));
        cls.methods.push(this.Method.create({
          visibility: 'public',
          class: true,
          name: this.swiftAxiomName,
          returnType: 'MethodInfo',
          body: 'return ' + this.swiftPrivateAxiomName,
          override: this.getSwiftOverride(parentCls),
        }));
      }
      cls.method(this.Method.create({
        name: this.swiftListenerMethodName,
        body: this.swiftCode,
        args: [
          this.SwiftArgument.create({
            localName: 'sub',
            type: 'Subscription',
          }),
          this.SwiftArgument.create({
            localName: 'args',
            type: '[Any?]',
          }),
        ],
        visibility: 'private',
        override: !!(superAxiom && superAxiom.swiftCode),
      }));
    },
  ],
  templates: [
    {
      name: 'swiftInit',
      args: [],
      template: function() {/*
<% if ( this.isMerged || this.isFramed ) { %>

var triggered = false
return { [weak self] sub, args in
  if triggered { return }
  triggered = true
  DispatchQueue.main.async {
    Timer.scheduledTimer(
        withTimeInterval: <%= (this.isMerged ? this.mergeDelay : 30)/1000 %>,
        repeats: false) { _ in
      triggered = false
      <%=this.swiftListenerDispatchQueue%>.async {
        self?.<%=this.swiftListenerMethodName%>(sub, args)
      }
    }
  }
} as (Subscription, [Any?]) -> Void

<% } else { %>

return { [weak self] sub, args in
  self?.<%=this.swiftListenerMethodName%>(sub, args)
} as (Subscription, [Any?]) -> Void

{'fraction_non_alphanumeric': 0.14653865588681153, 'fraction_numerical': 0.00404244567963618, 'mean_word_length': 2.2412663755458517, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7130875', 'n_tokens_mistral': 1863, 'n_tokens_neox': 1794, 'n_words': 355}