@toeverything/y-indexeddb
Advanced tools
Comparing version 0.7.0-canary.14 to 0.7.0-canary.15
@@ -1,35 +0,35 @@ | ||
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" }); | ||
import { openDB as S } from "idb"; | ||
import { mergeUpdates as T, Doc as V, applyUpdate as h, encodeStateVector as I, encodeStateAsUpdate as y, UndoManager as z, diffUpdate as J } from "yjs"; | ||
const j = 1, E = "affine-local"; | ||
function P(t) { | ||
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" }); | ||
} | ||
let U; | ||
async function L(e) { | ||
return new Promise((a) => { | ||
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; | ||
let H; | ||
async function K(t) { | ||
return new Promise((r) => { | ||
const i = indexedDB.open(t); | ||
let a = !0; | ||
i.onsuccess = function() { | ||
i.result.close(), a || indexedDB.deleteDatabase(t), r(a); | ||
}, i.onupgradeneeded = function() { | ||
a = !1; | ||
}; | ||
}); | ||
} | ||
async function _(e, a, r = k) { | ||
async function Q(t, r, i = E) { | ||
do | ||
if (!U || localStorage.getItem(`${r}-migration`) !== "true") { | ||
if (!H || localStorage.getItem(`${i}-migration`) !== "true") { | ||
try { | ||
U = await indexedDB.databases(); | ||
H = await indexedDB.databases(); | ||
} catch { | ||
if (await L(a)) { | ||
await h(a, 1).then(async (t) => { | ||
if (!t.objectStoreNames.contains("updates")) | ||
if (await K(r)) { | ||
await S(r, 1).then(async (a) => { | ||
if (!a.objectStoreNames.contains("updates")) | ||
return; | ||
const o = await t.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(o) || !o.every((u) => u instanceof Uint8Array)) | ||
const s = await a.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(s) || !s.every((f) => f instanceof Uint8Array)) | ||
return; | ||
const n = x(o), d = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await d.get(a) || (console.log("upgrading the database"), await d.put({ | ||
id: a, | ||
const n = T(s), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(r) || (console.log("upgrading the database"), await u.put({ | ||
id: r, | ||
updates: [ | ||
@@ -47,19 +47,19 @@ { | ||
await Promise.all( | ||
U.map((t) => { | ||
if (t.name && t.version === 1) { | ||
const i = t.name, o = t.version; | ||
return h(i, o).then( | ||
H.map((a) => { | ||
if (a.name && a.version === 1) { | ||
const w = a.name, s = a.version; | ||
return S(w, s).then( | ||
async (n) => { | ||
if (!n.objectStoreNames.contains("updates")) | ||
return; | ||
const s = await n.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(s) || !s.every((g) => g instanceof Uint8Array)) | ||
const d = await n.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(d) || !d.every((A) => A instanceof Uint8Array)) | ||
return; | ||
const u = x(s), p = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await p.get(i) || (console.log("upgrading the database"), await p.put({ | ||
id: i, | ||
const f = T(d), g = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await g.get(w) || (console.log("upgrading the database"), await g.put({ | ||
id: w, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: u | ||
update: f | ||
} | ||
@@ -72,3 +72,3 @@ ] | ||
}) | ||
), localStorage.setItem(`${r}-migration`, "true"); | ||
), localStorage.setItem(`${i}-migration`, "true"); | ||
break; | ||
@@ -78,49 +78,49 @@ } | ||
} | ||
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; | ||
async function ee(t, r = E) { | ||
const s = await (await S(r, j, { | ||
upgrade: P | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(t); | ||
return s ? T(s.updates.map(({ update: n }) => n)) : !1; | ||
} | ||
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, { | ||
const v = Symbol("indexeddb-provider-origin"), M = Symbol("snapshot-origin"); | ||
let $ = 500; | ||
const L = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), U = async (t) => { | ||
window.addEventListener("beforeunload", L, { | ||
capture: !0 | ||
}), await e, window.removeEventListener("beforeunload", C, { | ||
}), await t, window.removeEventListener("beforeunload", L, { | ||
capture: !0 | ||
}); | ||
}; | ||
function z(e) { | ||
O = e; | ||
function te(t) { | ||
$ = t; | ||
} | ||
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((u) => { | ||
const p = r(u); | ||
if (p === "Text") | ||
return t.getText(u); | ||
if (p === "Map") | ||
return t.getMap(u); | ||
if (p === "Array") | ||
return t.getArray(u); | ||
function ae(t, r, i) { | ||
const a = new V(); | ||
h(a, r, M); | ||
const w = I(t), s = I(a), n = y( | ||
t, | ||
s | ||
), u = new z( | ||
[...a.share.keys()].map((f) => { | ||
const g = i(f); | ||
if (g === "Text") | ||
return a.getText(f); | ||
if (g === "Map") | ||
return a.getMap(f); | ||
if (g === "Array") | ||
return a.getArray(f); | ||
throw new Error("Unknown type"); | ||
}), | ||
{ | ||
trackedOrigins: /* @__PURE__ */ new Set([D]) | ||
trackedOrigins: /* @__PURE__ */ new Set([M]) | ||
} | ||
); | ||
b(t, n, D), d.undo(); | ||
const s = m( | ||
t, | ||
i | ||
h(a, n, M), u.undo(); | ||
const d = y( | ||
a, | ||
w | ||
); | ||
b(e, s, D); | ||
h(t, d, M); | ||
} | ||
class $ extends Error { | ||
class R extends Error { | ||
constructor() { | ||
@@ -130,3 +130,3 @@ super("Early disconnect"); | ||
} | ||
class q extends Error { | ||
class X extends Error { | ||
constructor() { | ||
@@ -136,116 +136,154 @@ 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, | ||
const ne = async (t, r, i, a = E) => { | ||
const n = (await S(a, j, { | ||
upgrade: P | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), u = await n.get("id"), d = y(r); | ||
u ? (u.milestone[i] = d, await n.put(u)) : await n.put({ | ||
id: t, | ||
milestone: { | ||
[r]: s | ||
[i]: d | ||
} | ||
}); | ||
}, 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 s; | ||
if (!n || g === S) | ||
return; | ||
const w = y.transaction("workspace", "readwrite").objectStore("workspace"); | ||
let c = await w.get(e); | ||
if (c || (c = { | ||
id: e, | ||
updates: [] | ||
}), c.updates.push({ | ||
timestamp: Date.now(), | ||
update: f | ||
}), c.updates.length > O) { | ||
const j = c.updates.map(({ update: P }) => P), l = new M(); | ||
l.transact(() => { | ||
j.forEach((P) => { | ||
b(l, P, S); | ||
}); | ||
}, S); | ||
const T = m(l); | ||
c = { | ||
id: e, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: T | ||
} | ||
] | ||
}, await v(w.put(c)); | ||
} else | ||
await v(w.put(c)); | ||
}, re = async (t, r = E) => { | ||
const s = await (await S(r, j, { | ||
upgrade: P | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(t); | ||
return s ? s.milestone : null; | ||
}, se = (t, r, i = E) => { | ||
let a, w, s = !0, n = !1; | ||
const u = S(i, j, { | ||
upgrade: P | ||
}), d = /* @__PURE__ */ new WeakMap(), f = /* @__PURE__ */ new WeakMap(), g = /* @__PURE__ */ new WeakMap(), x = (o, e) => { | ||
if (d.has(e)) | ||
return d.get(e); | ||
const c = async function(l, b) { | ||
const D = await u; | ||
if (!n || b === v) | ||
return; | ||
const k = D.transaction("workspace", "readwrite").objectStore("workspace"); | ||
let p = await k.get(o); | ||
if (p || (p = { | ||
id: o, | ||
updates: [] | ||
}), p.updates.push({ | ||
timestamp: Date.now(), | ||
update: l | ||
}), p.updates.length > $) { | ||
const F = p.updates.map(({ update: C }) => C), B = new V(); | ||
B.transact(() => { | ||
F.forEach((C) => { | ||
h(B, C, v); | ||
}); | ||
}, v); | ||
const N = y(B); | ||
p = { | ||
id: o, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: N | ||
} | ||
] | ||
}, await U(k.put(p)); | ||
} else | ||
await U(k.put(p)); | ||
}; | ||
return d.set(e, c), c; | ||
}, A = (o, e) => { | ||
if (f.has(e)) | ||
return f.get(e); | ||
const c = async function() { | ||
(await u).close(); | ||
}; | ||
return f.set(e, c), c; | ||
}, W = (o, e) => { | ||
if (g.has(e)) | ||
return g.get(e); | ||
const c = async function(l) { | ||
l.removed.forEach((b) => { | ||
O(b.guid, b); | ||
}), l.loaded.forEach((b) => { | ||
_(b.guid, b); | ||
}); | ||
}; | ||
return g.set(e, c), c; | ||
}; | ||
function _(o, e) { | ||
e.on("update", x(o, e)), e.on("destroy", A(o, e)), e.on("subdocs", W(o, e)); | ||
} | ||
const s = h(r, A, { | ||
upgrade: E | ||
}), u = async () => { | ||
n = !0, (await s).close(); | ||
}, p = { | ||
function O(o, e) { | ||
e.subdocs.forEach((c) => { | ||
O(c.guid, c); | ||
}), e.off("update", x(o, e)), e.off("destroy", A(o, e)), e.off("subdocs", W(o, e)); | ||
} | ||
async function q(o, e) { | ||
const c = await u, m = c.transaction("workspace", "readwrite").objectStore("workspace"), l = await m.get(o); | ||
if (n) | ||
if (!l) | ||
await U( | ||
c.put("workspace", { | ||
id: o, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: y(e) | ||
} | ||
] | ||
}) | ||
); | ||
else { | ||
const b = l.updates.map(({ update: p }) => p), D = new V(); | ||
D.transact(() => { | ||
b.forEach((p) => { | ||
h(D, p); | ||
}); | ||
}, v); | ||
const k = J( | ||
y(e), | ||
y(D) | ||
); | ||
await U( | ||
m.put({ | ||
...l, | ||
updates: [ | ||
...l.updates, | ||
{ | ||
timestamp: Date.now(), | ||
update: k | ||
} | ||
] | ||
}) | ||
), e.transact(() => { | ||
b.forEach((p) => { | ||
h(e, p); | ||
}); | ||
}, v); | ||
} | ||
} | ||
const G = { | ||
connect: async () => { | ||
if (n) | ||
return; | ||
p.whenSynced = new Promise((w, c) => { | ||
o = !0, t = w, i = c; | ||
}), n = !0, a.on("update", d), a.on("destroy", u); | ||
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: e, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: m(a) | ||
} | ||
] | ||
}) | ||
); | ||
else { | ||
const w = y.updates.map(({ update: l }) => l), c = new M(); | ||
c.transact(() => { | ||
w.forEach((l) => { | ||
b(c, l); | ||
}); | ||
}, S); | ||
const j = I( | ||
m(a), | ||
m(c) | ||
); | ||
await v( | ||
g.put({ | ||
...y, | ||
updates: [ | ||
...y.updates, | ||
{ | ||
timestamp: Date.now(), | ||
update: j | ||
} | ||
] | ||
}) | ||
), a.transact(() => { | ||
w.forEach((l) => { | ||
b(a, l); | ||
}); | ||
}, S); | ||
} | ||
o = !1, t(); | ||
G.whenSynced = new Promise((c, m) => { | ||
s = !0, a = c, w = m; | ||
}), n = !0, _(t, r); | ||
const o = await u; | ||
if (await Q(o, t, i), !n) | ||
return; | ||
const e = []; | ||
for (e.push([t, r]); e.length > 0; ) { | ||
const [c, m] = e.pop(); | ||
await q(c, m), m.subdocs.forEach((l) => { | ||
e.push([l.guid, l]); | ||
}); | ||
} | ||
s = !1, a(); | ||
}, | ||
disconnect() { | ||
n = !1, o && i(new $()), a.off("update", d), a.off("destroy", u); | ||
n = !1, s && w(new R()), O(t, r); | ||
}, | ||
async cleanup() { | ||
if (n) | ||
throw new q(); | ||
await (await s).delete("workspace", e); | ||
throw new X(); | ||
await (await u).delete("workspace", t); | ||
}, | ||
@@ -257,19 +295,19 @@ whenSynced: Promise.resolve(), | ||
}; | ||
return p; | ||
return G; | ||
}; | ||
export { | ||
q as CleanupWhenConnectingError, | ||
k as DEFAULT_DB_NAME, | ||
$ as EarlyDisconnectError, | ||
K as createIndexedDBProvider, | ||
A as dbVersion, | ||
W as downloadBinary, | ||
J as getMilestones, | ||
H as markMilestone, | ||
G as revertUpdate, | ||
z as setMergeCount, | ||
_ as tryMigrate, | ||
E as upgradeDB, | ||
v as writeOperation | ||
X as CleanupWhenConnectingError, | ||
E as DEFAULT_DB_NAME, | ||
R as EarlyDisconnectError, | ||
se as createIndexedDBProvider, | ||
j as dbVersion, | ||
ee as downloadBinary, | ||
re as getMilestones, | ||
ne as markMilestone, | ||
ae as revertUpdate, | ||
te as setMergeCount, | ||
Q as tryMigrate, | ||
P as upgradeDB, | ||
U as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.7.0-canary.14", | ||
"version": "0.7.0-canary.15", | ||
"scripts": { | ||
@@ -35,4 +35,4 @@ "build": "vite build" | ||
"devDependencies": { | ||
"@blocksuite/blocks": "0.0.0-20230607055421-9b20fcaf-nightly", | ||
"@blocksuite/store": "0.0.0-20230607055421-9b20fcaf-nightly", | ||
"@blocksuite/blocks": "0.0.0-20230613142146-d72d4600-nightly", | ||
"@blocksuite/store": "0.0.0-20230613142146-d72d4600-nightly", | ||
"vite": "^4.3.9", | ||
@@ -39,0 +39,0 @@ "vite-plugin-dts": "^2.3.0", |
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
Sorry, the diff of this file is not supported yet
70243
395