<commit_before>import {Pos} from "../model"

import {contains, browser} from "../dom"

export class Selection {
  constructor(pm) {
    this.pm = pm

    let start = Pos.start(pm.doc)
    this.range = new TextSelection(start)
    this.lastNonNodePos = null

    this.pollState = null
    this.pollTimeout = null
    this.lastAnchorNode = this.lastHeadNode = this.lastAnchorOffset = this.lastHeadOffset = null
    this.lastNode = null

    pm.content.addEventListener("focus", () => this.receivedFocus())
  }

  setAndSignal(range, clearLast) {
    this.set(range, clearLast)
    this.pm.signal("selectionChange")
  }

  set(range, clearLast) {
    this.range = range
    if (!range.nodePos) this.lastNonNodePos = null
    if (clearLast !== false) this.lastAnchorNode = null
  }

  setNodeAndSignal(pos) {
    this.setNode(pos)
    this.pm.signal("selectionChange")
  }

  pollForUpdate() {
    if (this.pm.input.composing) return
    clearTimeout(this.pollTimeout)
    this.pollState = "update"
    let n = 0, check = () => {
      if (this.pm.input.composing) {
        // Abort
      } else if (this.pm.operation) {
        this.pollTimeout = setTimeout(check, 20)
      } else if (this.readUpdate()) {
        this.pollState = null
        this.pollToSync()
      } else if (++n == 1) {
        this.pollTimeout = setTimeout(check, 50)
      }
    }
    this.pollTimeout = setTimeout(check, 20)
  }

  domChanged() {<commit_msg>Fix restoring of selection sync polling after an update poll<commit_after>import {Pos} from "../model"

import {contains, browser} from "../dom"

export class Selection {
  constructor(pm) {
    this.pm = pm

    let start = Pos.start(pm.doc)
    this.range = new TextSelection(start)
    this.lastNonNodePos = null

    this.pollState = null
    this.pollTimeout = null
    this.lastAnchorNode = this.lastHeadNode = this.lastAnchorOffset = this.lastHeadOffset = null
    this.lastNode = null

    pm.content.addEventListener("focus", () => this.receivedFocus())
  }

  setAndSignal(range, clearLast) {
    this.set(range, clearLast)
    this.pm.signal("selectionChange")
  }

  set(range, clearLast) {
    this.range = range
    if (!range.nodePos) this.lastNonNodePos = null
    if (clearLast !== false) this.lastAnchorNode = null
  }

  setNodeAndSignal(pos) {
    this.setNode(pos)
    this.pm.signal("selectionChange")
  }

  pollForUpdate() {
    if (this.pm.input.composing) return
    clearTimeout(this.pollTimeout)
    this.pollState = "update"
    let n = 0, check = () => {
      if (this.pm.input.composing) {
        // Abort
      } else if (this.pm.operation) {
        this.pollTimeout = setTimeout(check, 20)
      } else if (!this.readUpdate() && ++n == 1) {
        this.pollTimeout = setTimeout(check, 50)
      } else {
        this.pollState = null
        this.pollToSync()
      }
    }
    this.pollTimeout = setTimeout(check, 20)
  }

  domChanged() {
{'fraction_non_alphanumeric': 0.1310178817056396, 'fraction_numerical': 0.005502063273727648, 'mean_word_length': 2.7583979328165373, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6680103', 'n_tokens_mistral': 957, 'n_tokens_neox': 926, 'n_words': 196}