<commit_before>/** @babel */

import fs from 'fs-plus'
import path from 'path'

import * as NotesFs from './notes-fs'

export default {
  config: require('./config.coffee').config,

  activate (state) {
    this.dq = null
    this.ready = undefined

    if (state) {
      makeReady(this, atom.deserializers.deserialize(state))
    } else {
      makeReady(this)
    }
  },

  serialize () {
    if (this.dq) {
      return {
        'deserializer': 'SearchIndex',
        'data': JSON.stringify(this.dq.searchIndex)
      }
    }
  },

  deserializeSearchIndex ({data}) {
    try {
      return JSON.parse(data)
    } catch (_) {
      return null
    }
  },

  async deactivate () {
    __guard__(this.subs, x => x.dispose())
    this.subs = null
  },

  /** Returns our package's NotesView controller. */
  getNotesView () {
    if (!_notesView) {
      const {Disposable} = require('atom')
      const NotesView = require('./notes-view')

      _notesView = new NotesView(this.store)
      this.subs.add(new Disposable(() => {
        _notesView.destroy()
        _notesView = null
      }))
    }

    return _notesView
  },

  /** Returns true iff the given file path is a note. */
  isNote (filePath) {
    const NotesFs = require('./notes-fs')
    return NotesFs.isNote(filePath)
  }
}

let _notesView

/** Begin loading our document store in the background when event queue is empty.
  *
  * Note: We will signal that our module is ready when we set this.ready to true.
  */
function makeReady (self, searchIndex = null) {
  setTimeout(() => {
    if (!ensureNotesDirectory()) {
      // If we don't have a suitable notes directory, we can't finish activation.
      __guard__(atom.packages.getActivePackage('atom-notes'), x => x.deactivate())
      self.ready = false
      return
    }

    self.store = new Promise(function (resolve, reject) {
      const DocQuery = require('docquery')
      let options = {
        recursive: true,
        extensions: atom.config.get('atom-notes.extensions')
      }
      if (searchIndex) options.searchIndex = searchIndex
      let dq = new DocQuery(NotesFs.getNotesDirectory(), options)
      dq.on('ready', () => {
        self.ready = true
        dq.loaded = true
      })
      if (!atom.config.get('atom-notes.useLunrPipeline')) {
        dq.searchIndex.pipeline.reset()
      }
      resolve(dq)
    })
    self.store.then(dq => (self.dq = dq))

    handleEvents(self)
  }, 0)
}

function handleEvents (self) {<commit_msg>Move deserialization into async flow.<commit_after>/** @babel */

import fs from 'fs-plus'
import path from 'path'

import * as NotesFs from './notes-fs'

export default {
  config: require('./config.coffee').config,

  activate (state) {
    this.dq = null
    this.ready = undefined

    if (state) {
      makeReady(this, state)
    } else {
      makeReady(this)
    }
  },

  serialize () {
    if (this.dq) {
      return {
        'deserializer': 'SearchIndex',
        'data': JSON.stringify(this.dq.searchIndex)
      }
    }
  },

  deserializeSearchIndex ({data}) {
    try {
      return JSON.parse(data)
    } catch (_) {
      return null
    }
  },

  async deactivate () {
    __guard__(this.subs, x => x.dispose())
    this.subs = null
  },

  /** Returns our package's NotesView controller. */
  getNotesView () {
    if (!_notesView) {
      const {Disposable} = require('atom')
      const NotesView = require('./notes-view')

      _notesView = new NotesView(this.store)
      this.subs.add(new Disposable(() => {
        _notesView.destroy()
        _notesView = null
      }))
    }

    return _notesView
  },

  /** Returns true iff the given file path is a note. */
  isNote (filePath) {
    const NotesFs = require('./notes-fs')
    return NotesFs.isNote(filePath)
  }
}

let _notesView

/** Begin loading our document store in the background when event queue is empty.
  *
  * Note: We will signal that our module is ready when we set this.ready to true.
  */
function makeReady (self, state = null) {
  setTimeout(() => {
    if (!ensureNotesDirectory()) {
      // If we don't have a suitable notes directory, we can't finish activation.
      __guard__(atom.packages.getActivePackage('atom-notes'), x => x.deactivate())
      self.ready = false
      return
    }

    self.store = new Promise(function (resolve, reject) {
      const DocQuery = require('docquery')
      let options = {
        recursive: true,
        extensions: atom.config.get('atom-notes.extensions')
      }
      if (state) options.searchIndex = atom.deserializers.deserialize(state)
      let dq = new DocQuery(NotesFs.getNotesDirectory(), options)
      dq.on('ready', () => {
        self.ready = true
        dq.loaded = true
      })
      if (!atom.config.get('atom-notes.useLunrPipeline')) {
        dq.searchIndex.pipeline.reset()
      }
      resolve(dq)
    })
    self.store.then(dq => (self.dq = dq))

    handleEvents(self)
  }, 0)
}

function handleEvents (self) {
{'fraction_non_alphanumeric': 0.13436744560838035, 'fraction_numerical': 0.00040290088638195, 'mean_word_length': 2.443134535367545, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '303020', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1522, 'n_words': 386}