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(); await storage.migrate();
const multiTab = !!globalThis.SharedWorker && isFeatureSupported("opfs");
database.setup({ database.setup({
sqliteOptions: { sqliteOptions: {
dialect: (name, init) => 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") ...(IS_DESKTOP_APP || isFeatureSupported("opfs")
? { journalMode: "WAL", lockingMode: "exclusive" } ? { journalMode: "WAL", lockingMode: "exclusive" }
: { : {
journalMode: "MEMORY", journalMode: "MEMORY",
lockingMode: "exclusive" lockingMode: "normal"
}), }),
tempStore: "memory", tempStore: "memory",
synchronous: "normal", synchronous: "normal",
pageSize: 8192, pageSize: 8192,
cacheSize: -32000, cacheSize: -32000,
password: Buffer.from(databaseKey).toString("hex"), password: Buffer.from(databaseKey).toString("hex"),
skipInitialization: !IS_DESKTOP_APP && !!globalThis.SharedWorker skipInitialization: !IS_DESKTOP_APP && multiTab
}, },
storage: storage, storage: storage,
eventsource: EventSource, eventsource: EventSource,

View File

@@ -1,7 +1,7 @@
/* eslint-disable header/header */ /* eslint-disable header/header */
// Copyright 2022 Roy T. Hashimoto. All Rights Reserved. // Copyright 2022 Roy T. Hashimoto. All Rights Reserved.
import * as VFS from "./VFS.js"; 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"; import { IDBContext } from "./IDBContext.js";
const SECTOR_SIZE = 512; 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 type { SQLiteWorker } from "./sqlite.worker.desktop";
import { wrap, Remote } from "comlink"; import { wrap, Remote } from "comlink";
import { Mutex } from "async-mutex"; import { Mutex } from "async-mutex";
import { DialectOptions } from ".";
class SqliteDriver implements Driver { class SqliteDriver implements Driver {
connection?: DatabaseConnection; 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 { return {
createDriver: () => createDriver: () =>
new SqliteDriver({ new SqliteDriver({
name name: options.name
}), }),
createAdapter: () => new SqliteAdapter(), createAdapter: () => new SqliteAdapter(),
createIntrospector: (db) => new SqliteIntrospector(db), createIntrospector: (db) => new SqliteIntrospector(db),

View File

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

View File

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