Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created June 28, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retorquere/9c7cd0d3cf8b9ca534473c0c28a3440c to your computer and use it in GitHub Desktop.
Save retorquere/9c7cd0d3cf8b9ca534473c0c28a3440c to your computer and use it in GitHub Desktop.
/* eslint-disable @typescript-eslint/member-ordering */
Components.utils.import('resource://gre/modules/osfile.jsm')
import { log } from '../../logger'
// Components.utils.import('resource://gre/modules/Sqlite.jsm')
// declare const Sqlite: any
export class File {
public mode = 'reference'
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
public async exportDatabase(name: string, dbref: any, callback: ((v: null) => void)): Promise<void> {
try {
const parts = [
this.save(name, {...dbref, ...{collections: dbref.collections.map((coll: { name: string }) => coll.name)}}, true),
]
for (const coll of dbref.collections) {
parts.push(this.save(`${name}.${coll.name}`, coll, coll.dirty))
}
await Zotero.Promise.all(parts)
callback(null)
}
catch (err) {
callback(err)
}
}
private async save(name: string, data, dirty: boolean) {
const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const save = dirty || !(await OS.File.exists(path))
if (!save) return null
await OS.File.writeAtomic(path, JSON.stringify(data), { encoding: 'utf-8', tmpPath: `${path}.tmp`})
}
public async loadDatabase(name: string, callback: ((v: null) => void)): Promise<void> {
try {
const db = await this.load(name)
if (!db) return callback(null)
db.collections = await Zotero.Promise.all(db.collections.map(async collname => {
const coll = await this.load(`${name}.${collname}`)
if (coll) {
coll.cloneObjects = true // https://github.com/techfort/LokiJS/issues/47#issuecomment-362425639
coll.adaptiveBinaryIndices = false // https://github.com/techfort/LokiJS/issues/654
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return coll
}
else {
log.debug('DB.Store.loadDatabaseVersionAsync:', `Could not load ${name}.${collname}`)
return null
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
})).filter(coll => coll)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
callback(db)
}
catch (err) {
log.debug('DB.Store.loadDatabase', name, err)
callback(err)
}
}
private async load(name) {
const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
const exists = await OS.File.exists(path)
if (!exists) return null
const data = JSON.parse(await OS.File.read(path, { encoding: 'utf-8' }) as unknown as string)
// this is intentional. If all is well, the database will be retained in memory until it's saved at
// shutdown. If all is not well, this will make sure the caches are rebuilt from scratch on next start
await OS.File.move(path, `${path}.bak`)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return data
}
}
@coderaiser
Copy link

It transformed with no problems:

 /* eslint-disable @typescript-eslint/member-ordering */

Components.utils.import('resource://gre/modules/osfile.jsm')

import { log } from '../../logger'

// Components.utils.import('resource://gre/modules/Sqlite.jsm')
// declare const Sqlite: any

export class File {
  public mode = 'reference'

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  public async exportDatabase(name: string, dbref: any, callback: ((v: null) => void)): Promise<void> {
+     __estrace.enter('exportDatabase', 'file://*:14', arguments);
    try {
      const parts = [
-         this.save(name, {...dbref, ...{collections: dbref.collections.map((coll: { name: string }) => coll.name)}}, true),
+         this.save(name, {...dbref, ...{collections: dbref.collections.map((coll: { name: string }) => {
+           __estrace.enter('<anonymous:17>', 'file://*:17', arguments);
+
+           {
+             const __estrace_result = coll.name;
+             __estrace.exit('<anonymous:17>', 'file://*:17', __estrace_result);
+             return __estrace_result;
+           }
+         })}}, true),
      ]
      for (const coll of dbref.collections) {
        parts.push(this.save(`${name}.${coll.name}`, coll, coll.dirty))
      }

      await Zotero.Promise.all(parts)

      callback(null)
    }
    catch (err) {
      callback(err)
    }
+     __estrace.exit('exportDatabase', 'file://*:14');
  }

  private async save(name: string, data, dirty: boolean) {
+     __estrace.enter('save', 'file://*:32', arguments);
    const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
    const save = dirty || !(await OS.File.exists(path))

-     if (!save) return null
+     if (!save) {
+       const __estrace_result = null;
+       __estrace.exit('save', 'file://*:32', __estrace_result);
+       return __estrace_result;
+     }

    await OS.File.writeAtomic(path, JSON.stringify(data), { encoding: 'utf-8', tmpPath: `${path}.tmp`})
  }

  public async loadDatabase(name: string, callback: ((v: null) => void)): Promise<void> {
+     __estrace.enter('loadDatabase', 'file://*:41', arguments);
    try {
      const db = await this.load(name)
      if (!db) return callback(null)

      db.collections = await Zotero.Promise.all(db.collections.map(async collname => {
+         __estrace.enter('<anonymous:46>', 'file://*:46', arguments);
        const coll = await this.load(`${name}.${collname}`)
        if (coll) {
          coll.cloneObjects = true // https://github.com/techfort/LokiJS/issues/47#issuecomment-362425639
          coll.adaptiveBinaryIndices = false // https://github.com/techfort/LokiJS/issues/654
          // eslint-disable-next-line @typescript-eslint/no-unsafe-return
          return coll
        }
        else {
          log.debug('DB.Store.loadDatabaseVersionAsync:', `Could not load ${name}.${collname}`)
          return null
        }
-       // eslint-disable-next-line @typescript-eslint/no-unsafe-return
-       })).filter(coll => coll)
+         // eslint-disable-next-line @typescript-eslint/no-unsafe-return
+         __estrace.exit('<anonymous:46>', 'file://*:46');
+       })).filter(coll => {
+         __estrace.enter('<anonymous:59>', 'file://*:59', arguments);
+
+         {
+           const __estrace_result = coll;
+           __estrace.exit('<anonymous:59>', 'file://*:59', __estrace_result);
+           return __estrace_result;
+         }
+       })

      // eslint-disable-next-line @typescript-eslint/no-unsafe-return
      callback(db)
    }
    catch (err) {
      log.debug('DB.Store.loadDatabase', name, err)
      callback(err)
    }
+     __estrace.exit('loadDatabase', 'file://*:41');
  }

  private async load(name) {
+     __estrace.enter('load', 'file://*:70', arguments);
    const path = OS.Path.join(Zotero.BetterBibTeX.dir, `${name}.json`)
    const exists = await OS.File.exists(path)

-     if (!exists) return null
+     if (!exists) {
+       const __estrace_result = null;
+       __estrace.exit('load', 'file://*:70', __estrace_result);
+       return __estrace_result;
+     }

    const data = JSON.parse(await OS.File.read(path, { encoding: 'utf-8' }) as unknown as string)

    // this is intentional. If all is well, the database will be retained in memory until it's saved at
    // shutdown. If all is not well, this will make sure the caches are rebuilt from scratch on next start
    await OS.File.move(path, `${path}.bak`)

-     // eslint-disable-next-line @typescript-eslint/no-unsafe-return
-     return data
+     {
+       const __estrace_result = data;
+       __estrace.exit('load', 'file://*:70', __estrace_result);
+       return __estrace_result;
+     }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment