@toeverything/y-indexeddb
Advanced tools
Comparing version 0.5.4-canary.21 to 0.5.4-canary.22
@@ -9,2 +9,5 @@ import { Doc } from 'yjs'; | ||
} | ||
export declare class CleanupWhenConnectingError extends Error { | ||
constructor(); | ||
} | ||
export declare const markMilestone: (id: string, doc: Doc, name: string, dbName?: string) => Promise<void>; | ||
@@ -11,0 +14,0 @@ export declare const getMilestones: (id: string, dbName?: string) => Promise<null | WorkspaceMilestone['milestone']>; |
@@ -1,33 +0,33 @@ | ||
import { openDB as S } from "idb"; | ||
import { mergeUpdates as x, Doc as M, applyUpdate as b, encodeStateVector as B, encodeStateAsUpdate as m, UndoManager as C, diffUpdate as I } from "yjs"; | ||
const A = 1, D = "affine-local"; | ||
function j(t) { | ||
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" }); | ||
import { openDB as h } from "idb"; | ||
import { mergeUpdates as x, Doc as M, applyUpdate as b, encodeStateVector as B, encodeStateAsUpdate as m, UndoManager as V, diffUpdate as I } from "yjs"; | ||
const A = 1, k = "affine-local"; | ||
function E(e) { | ||
e.createObjectStore("workspace", { keyPath: "id" }), e.createObjectStore("milestone", { keyPath: "id" }); | ||
} | ||
let E; | ||
async function L(t) { | ||
let U; | ||
async function L(e) { | ||
return new Promise((a) => { | ||
const s = indexedDB.open(t); | ||
let e = !0; | ||
s.onsuccess = function() { | ||
s.result.close(), e || indexedDB.deleteDatabase(t), a(e); | ||
}, s.onupgradeneeded = function() { | ||
e = !1; | ||
const r = indexedDB.open(e); | ||
let t = !0; | ||
r.onsuccess = function() { | ||
r.result.close(), t || indexedDB.deleteDatabase(e), a(t); | ||
}, r.onupgradeneeded = function() { | ||
t = !1; | ||
}; | ||
}); | ||
} | ||
async function _(t, a, s = D) { | ||
async function _(e, a, r = k) { | ||
do | ||
if (!E || localStorage.getItem(`${s}-migration`) !== "true") { | ||
if (!U || localStorage.getItem(`${r}-migration`) !== "true") { | ||
try { | ||
E = await indexedDB.databases(); | ||
U = await indexedDB.databases(); | ||
} catch { | ||
if (await L(a)) { | ||
await S(a, 1).then(async (e) => { | ||
if (!e.objectStoreNames.contains("updates")) | ||
await h(a, 1).then(async (t) => { | ||
if (!t.objectStoreNames.contains("updates")) | ||
return; | ||
const n = await e.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(n) || !n.every((p) => p instanceof Uint8Array)) | ||
const o = await t.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(o) || !o.every((p) => p instanceof Uint8Array)) | ||
return; | ||
const o = x(n), d = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
const n = x(o), d = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await d.get(a) || (console.log("upgrading the database"), await d.put({ | ||
@@ -38,3 +38,3 @@ id: a, | ||
timestamp: Date.now(), | ||
update: o | ||
update: n | ||
} | ||
@@ -48,13 +48,13 @@ ] | ||
await Promise.all( | ||
E.map((e) => { | ||
if (e.name && e.version === 1) { | ||
const i = e.name, n = e.version; | ||
return S(i, n).then( | ||
async (o) => { | ||
if (!o.objectStoreNames.contains("updates")) | ||
U.map((t) => { | ||
if (t.name && t.version === 1) { | ||
const i = t.name, o = t.version; | ||
return h(i, o).then( | ||
async (n) => { | ||
if (!n.objectStoreNames.contains("updates")) | ||
return; | ||
const c = await o.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(c) || !c.every((g) => g instanceof Uint8Array)) | ||
const s = await n.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(s) || !s.every((g) => g instanceof Uint8Array)) | ||
return; | ||
const p = x(c), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
const p = x(s), u = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(i) || (console.log("upgrading the database"), await u.put({ | ||
@@ -73,3 +73,3 @@ id: i, | ||
}) | ||
), localStorage.setItem(`${s}-migration`, "true"); | ||
), localStorage.setItem(`${r}-migration`, "true"); | ||
break; | ||
@@ -79,47 +79,47 @@ } | ||
} | ||
async function N(t, a = D) { | ||
const n = await (await S(a, A, { | ||
upgrade: j | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(t); | ||
return n ? x(n.updates.map(({ update: o }) => o)) : !1; | ||
async function W(e, a = k) { | ||
const o = await (await h(a, A, { | ||
upgrade: E | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(e); | ||
return o ? x(o.updates.map(({ update: n }) => n)) : !1; | ||
} | ||
const h = Symbol("indexeddb-provider-origin"), k = Symbol("snapshot-origin"); | ||
let T = 500; | ||
const O = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), v = async (t) => { | ||
window.addEventListener("beforeunload", O, { | ||
const S = Symbol("indexeddb-provider-origin"), D = Symbol("snapshot-origin"); | ||
let O = 500; | ||
const C = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), v = async (e) => { | ||
window.addEventListener("beforeunload", C, { | ||
capture: !0 | ||
}), await t, window.removeEventListener("beforeunload", O, { | ||
}), await e, window.removeEventListener("beforeunload", C, { | ||
capture: !0 | ||
}); | ||
}; | ||
function z(t) { | ||
T = t; | ||
function z(e) { | ||
O = e; | ||
} | ||
function G(t, a, s) { | ||
const e = new M(); | ||
b(e, a, k); | ||
const i = B(t), n = B(e), o = m( | ||
t, | ||
n | ||
), d = new C( | ||
[...e.share.keys()].map((p) => { | ||
const u = s(p); | ||
function G(e, a, r) { | ||
const t = new M(); | ||
b(t, a, D); | ||
const i = B(e), o = B(t), n = m( | ||
e, | ||
o | ||
), d = new V( | ||
[...t.share.keys()].map((p) => { | ||
const u = r(p); | ||
if (u === "Text") | ||
return e.getText(p); | ||
return t.getText(p); | ||
if (u === "Map") | ||
return e.getMap(p); | ||
return t.getMap(p); | ||
if (u === "Array") | ||
return e.getArray(p); | ||
return t.getArray(p); | ||
throw new Error("Unknown type"); | ||
}), | ||
{ | ||
trackedOrigins: /* @__PURE__ */ new Set([k]) | ||
trackedOrigins: /* @__PURE__ */ new Set([D]) | ||
} | ||
); | ||
b(e, o, k), d.undo(); | ||
const c = m( | ||
e, | ||
b(t, n, D), d.undo(); | ||
const s = m( | ||
t, | ||
i | ||
); | ||
b(t, c, k); | ||
b(e, s, D); | ||
} | ||
@@ -131,68 +131,73 @@ class $ extends Error { | ||
} | ||
const H = async (t, a, s, e = D) => { | ||
const o = (await S(e, A, { | ||
upgrade: j | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await o.get("id"), c = m(a); | ||
d ? (d.milestone[s] = c, await o.put(d)) : await o.put({ | ||
id: t, | ||
class q extends Error { | ||
constructor() { | ||
super("Cleanup when connecting"); | ||
} | ||
} | ||
const H = async (e, a, r, t = k) => { | ||
const n = (await h(t, A, { | ||
upgrade: E | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await n.get("id"), s = m(a); | ||
d ? (d.milestone[r] = s, await n.put(d)) : await n.put({ | ||
id: e, | ||
milestone: { | ||
[s]: c | ||
[r]: s | ||
} | ||
}); | ||
}, J = async (t, a = D) => { | ||
const n = await (await S(a, A, { | ||
upgrade: j | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(t); | ||
return n ? n.milestone : null; | ||
}, K = (t, a, s = D) => { | ||
let e, i, n = !0, o = !1; | ||
}, J = async (e, a = k) => { | ||
const o = await (await h(a, A, { | ||
upgrade: E | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(e); | ||
return o ? o.milestone : null; | ||
}, K = (e, a, r = k) => { | ||
let t, i, o = !0, n = !1; | ||
async function d(f, g) { | ||
const y = await c; | ||
if (!o || g === h) | ||
const y = await s; | ||
if (!n || g === S) | ||
return; | ||
const w = y.transaction("workspace", "readwrite").objectStore("workspace"); | ||
let r = await w.get(t); | ||
if (r || (r = { | ||
id: t, | ||
let c = await w.get(e); | ||
if (c || (c = { | ||
id: e, | ||
updates: [] | ||
}), r.updates.push({ | ||
}), c.updates.push({ | ||
timestamp: Date.now(), | ||
update: f | ||
}), r.updates.length > T) { | ||
const P = r.updates.map(({ update: U }) => U), l = new M(); | ||
}), c.updates.length > O) { | ||
const j = c.updates.map(({ update: P }) => P), l = new M(); | ||
l.transact(() => { | ||
P.forEach((U) => { | ||
b(l, U, h); | ||
j.forEach((P) => { | ||
b(l, P, S); | ||
}); | ||
}, h); | ||
const V = m(l); | ||
r = { | ||
id: t, | ||
}, S); | ||
const T = m(l); | ||
c = { | ||
id: e, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: V | ||
update: T | ||
} | ||
] | ||
}, await v(w.put(r)); | ||
}, await v(w.put(c)); | ||
} else | ||
await v(w.put(r)); | ||
await v(w.put(c)); | ||
} | ||
const c = S(s, A, { | ||
upgrade: j | ||
const s = h(r, A, { | ||
upgrade: E | ||
}), p = async () => { | ||
o = !0, (await c).close(); | ||
n = !0, (await s).close(); | ||
}, u = { | ||
connect: async () => { | ||
u.whenSynced = new Promise((w, r) => { | ||
n = !0, e = w, i = r; | ||
}), o = !0, a.on("update", d), a.on("destroy", p); | ||
const f = await c; | ||
await _(f, t, s); | ||
const g = f.transaction("workspace", "readwrite").objectStore("workspace"), y = await g.get(t); | ||
if (o) { | ||
u.whenSynced = new Promise((w, c) => { | ||
o = !0, t = w, i = c; | ||
}), n = !0, a.on("update", d), a.on("destroy", p); | ||
const f = await s; | ||
await _(f, e, r); | ||
const g = f.transaction("workspace", "readwrite").objectStore("workspace"), y = await g.get(e); | ||
if (n) { | ||
if (!y) | ||
await v( | ||
f.put("workspace", { | ||
id: t, | ||
id: e, | ||
updates: [ | ||
@@ -207,11 +212,11 @@ { | ||
else { | ||
const w = y.updates.map(({ update: l }) => l), r = new M(); | ||
r.transact(() => { | ||
const w = y.updates.map(({ update: l }) => l), c = new M(); | ||
c.transact(() => { | ||
w.forEach((l) => { | ||
b(r, l); | ||
b(c, l); | ||
}); | ||
}, h); | ||
const P = I( | ||
}, S); | ||
const j = I( | ||
m(a), | ||
m(r) | ||
m(c) | ||
); | ||
@@ -225,3 +230,3 @@ await v( | ||
timestamp: Date.now(), | ||
update: P | ||
update: j | ||
} | ||
@@ -234,11 +239,14 @@ ] | ||
}); | ||
}, h); | ||
}, S); | ||
} | ||
n = !1, e(); | ||
o = !1, t(); | ||
} | ||
}, | ||
disconnect() { | ||
o = !1, n && i(new $()), a.off("update", d), a.off("destroy", p); | ||
n = !1, o && i(new $()), a.off("update", d), a.off("destroy", p); | ||
}, | ||
cleanup() { | ||
async cleanup() { | ||
if (n) | ||
throw new q(); | ||
(await s).delete("workspace", e); | ||
}, | ||
@@ -250,7 +258,8 @@ whenSynced: Promise.resolve() | ||
export { | ||
D as DEFAULT_DB_NAME, | ||
q as CleanupWhenConnectingError, | ||
k as DEFAULT_DB_NAME, | ||
$ as EarlyDisconnectError, | ||
K as createIndexedDBProvider, | ||
A as dbVersion, | ||
N as downloadBinary, | ||
W as downloadBinary, | ||
J as getMilestones, | ||
@@ -261,5 +270,5 @@ H as markMilestone, | ||
_ as tryMigrate, | ||
j as upgradeDB, | ||
E as upgradeDB, | ||
v as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -8,3 +8,3 @@ import type { DBSchema, IDBPDatabase } from 'idb/build/entry'; | ||
disconnect: () => void; | ||
cleanup: () => void; | ||
cleanup: () => Promise<void>; | ||
whenSynced: Promise<void>; | ||
@@ -11,0 +11,0 @@ } |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.5.4-canary.21", | ||
"version": "0.5.4-canary.22", | ||
"scripts": { | ||
@@ -35,7 +35,7 @@ "build": "vite build" | ||
"devDependencies": { | ||
"@blocksuite/blocks": "0.0.0-20230502232311-bfe52748-nightly", | ||
"@blocksuite/store": "0.0.0-20230502232311-bfe52748-nightly", | ||
"@blocksuite/blocks": "0.0.0-20230503040956-5c49643f-nightly", | ||
"@blocksuite/store": "0.0.0-20230503040956-5c49643f-nightly", | ||
"vite": "^4.3.4", | ||
"vite-plugin-dts": "^2.3.0", | ||
"y-indexeddb": "^9.0.10" | ||
"y-indexeddb": "^9.0.11" | ||
}, | ||
@@ -42,0 +42,0 @@ "peerDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
57605
348