internalZcodeDBInit function

void internalZcodeDBInit()

init zcode word database for web.

This library is using asset files sqlite3.wasm and zcode_ime.db. On the Web, these asset files may freeze your web app server because these files are too large and load from the web app server. In that case, I recommend you upload all asset files to your CDN server and customize initializing the engine using initializeEnginemethod with assetBase parameter, see this for more details.

Implementation

void internalZcodeDBInit() async {
  final fs = await IndexedDbFileSystem.open(dbName: 'indexedDB');
  // do not ignore slash, use absolute path
  final wordDbName = "/${basename("/zcode_ime.db")}";
  final xOpenResult =
        fs.xOpen(Sqlite3Filename(wordDbName), SqlFlag.SQLITE_OPEN_READWRITE|SqlFlag.SQLITE_OPEN_CREATE);
  final int size = xOpenResult.file.xFileSize();
  xOpenResult.file.xClose();
  if (size != dbLength) {
    final Uint8List data;
    final key = "packages/zcode_embed_ime_db/db$wordDbName";
    final byteData = await rootBundle.load(key);
    final offset = byteData.offsetInBytes;
    final length = byteData.lengthInBytes;
    data = byteData.buffer.asUint8List(offset, length);
    final xOpenResult =
        fs.xOpen(Sqlite3Filename(wordDbName), SqlFlag.SQLITE_OPEN_READWRITE|SqlFlag.SQLITE_OPEN_CREATE);
    xOpenResult.file.xWrite(data, 0);
    await fs.flush();
  }
  const key = "packages/zcode_embed_ime_db/sqlite3.wasm";
  final byteData = await rootBundle.load(key);
  final offset = byteData.offsetInBytes;
  final length = byteData.lengthInBytes;
  final data = byteData.buffer.asUint8List(offset, length);
  final sqlite = await WasmSqlite3.load(data);
  sqlite.registerVirtualFileSystem(fs, makeDefault: true);
  zcodeDB = sqlite.open(wordDbName);
}