web: allow multi tab access when using idb vfs

This commit is contained in:
Abdullah Atta
2024-07-10 09:01:42 +05:00
committed by Abdullah Atta
parent 8302f8ba4c
commit 727ac6d907
5 changed files with 36 additions and 17 deletions

View File

@@ -56,22 +56,29 @@ async function initializeDatabase(persistence: DatabasePersistence) {
);
await storage.migrate();
const multiTab = !!globalThis.SharedWorker && isFeatureSupported("opfs");
database.setup({
sqliteOptions: {
dialect: (name, init) =>
createDialect(persistence === "memory" ? ":memory:" : name, true, init),
createDialect({
name: persistence === "memory" ? ":memory:" : name,
encrypted: true,
async: !isFeatureSupported("opfs"),
init,
multiTab
}),
...(IS_DESKTOP_APP || isFeatureSupported("opfs")
? { journalMode: "WAL", lockingMode: "exclusive" }
: {
journalMode: "MEMORY",
lockingMode: "exclusive"
lockingMode: "normal"
}),
tempStore: "memory",
synchronous: "normal",
pageSize: 8192,
cacheSize: -32000,
password: Buffer.from(databaseKey).toString("hex"),
skipInitialization: !IS_DESKTOP_APP && !!globalThis.SharedWorker
skipInitialization: !IS_DESKTOP_APP && multiTab
},
storage: storage,
eventsource: EventSource,

View File

@@ -1,7 +1,7 @@
/* eslint-disable header/header */
// Copyright 2022 Roy T. Hashimoto. All Rights Reserved.
import * as VFS from "./VFS.js";
import { WebLocksExclusive as WebLocks } from "./WebLocks.js";
import { WebLocksShared as WebLocks } from "./WebLocks.js";
import { IDBContext } from "./IDBContext.js";
const SECTOR_SIZE = 512;

View File

@@ -32,6 +32,7 @@ import Worker from "./sqlite.worker.desktop.ts?worker";
import type { SQLiteWorker } from "./sqlite.worker.desktop";
import { wrap, Remote } from "comlink";
import { Mutex } from "async-mutex";
import { DialectOptions } from ".";
class SqliteDriver implements Driver {
connection?: DatabaseConnection;
@@ -100,11 +101,11 @@ class SqliteWorkerConnection implements DatabaseConnection {
}
}
export const createDialect = (name: string, _encrypted: boolean): Dialect => {
export const createDialect = (options: DialectOptions): Dialect => {
return {
createDriver: () =>
new SqliteDriver({
name
name: options.name
}),
createAdapter: () => new SqliteAdapter(),
createIntrospector: (db) => new SqliteIntrospector(db),

View File

@@ -27,7 +27,6 @@ import {
WaSqliteWorkerMultipleTabDriver,
WaSqliteWorkerSingleTabDriver
} from "./wa-sqlite-kysely-driver";
import { isFeatureSupported } from "../../utils/feature-check";
declare module "kysely" {
interface Driver {
@@ -35,22 +34,26 @@ declare module "kysely" {
}
}
export const createDialect = (
name: string,
encrypted: boolean,
init?: () => Promise<void>
): Dialect => {
export type DialectOptions = {
name: string;
encrypted: boolean;
async: boolean;
multiTab: boolean;
init?: () => Promise<void>;
};
export const createDialect = (options: DialectOptions): Dialect => {
const { async, encrypted, multiTab, name, init } = options;
return {
createDriver: () =>
globalThis.SharedWorker
multiTab
? new WaSqliteWorkerMultipleTabDriver({
async: !isFeatureSupported("opfs"),
async,
dbName: name,
encrypted,
init
})
: new WaSqliteWorkerSingleTabDriver({
async: !isFeatureSupported("opfs"),
async,
dbName: name,
encrypted
}),

View File

@@ -31,9 +31,17 @@ import { isFeatureSupported } from "./feature-check";
let logger: typeof _logger = new NoopLogger();
async function initializeLogger() {
const multiTab = !!globalThis.SharedWorker && isFeatureSupported("opfs");
await initialize(
{
dialect: (name, init) => createDialect(name, false, init),
dialect: (name, init) =>
createDialect({
name,
init,
async: !isFeatureSupported("opfs"),
multiTab,
encrypted: false
}),
...(IS_DESKTOP_APP || isFeatureSupported("opfs")
? { journalMode: "WAL", lockingMode: "exclusive" }
: {
@@ -44,7 +52,7 @@ async function initializeLogger() {
synchronous: "normal",
pageSize: 8192,
cacheSize: -32000,
skipInitialization: !IS_DESKTOP_APP && !!globalThis.SharedWorker
skipInitialization: !IS_DESKTOP_APP && multiTab
},
false
);