@toeverything/y-indexeddb
Advanced tools
Comparing version 0.9.0-canary.5 to 0.9.0-canary.6
@@ -1,5 +0,5 @@ | ||
import { openDB as S } from "idb"; | ||
import { applyUpdate as m, encodeStateVector as M, encodeStateAsUpdate as E, Doc as O, diffUpdate as _, encodeStateVectorFromUpdate as W, UndoManager as G } from "yjs"; | ||
const k = 1, D = "affine-local"; | ||
function A(t) { | ||
import { openDB as b } from "idb"; | ||
import { applyUpdate as S, encodeStateVector as M, encodeStateAsUpdate as v, Doc as O, diffUpdate as _, encodeStateVectorFromUpdate as W, UndoManager as G } from "yjs"; | ||
const E = 1, D = "affine-local"; | ||
function k(t) { | ||
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" }); | ||
@@ -10,4 +10,4 @@ } | ||
return t; | ||
for (const r of t.subdocs) { | ||
const e = $(r, a); | ||
for (const s of t.subdocs) { | ||
const e = $(s, a); | ||
if (e) | ||
@@ -17,7 +17,7 @@ return e; | ||
} | ||
const J = (t, a, r = {}) => { | ||
const J = (t, a, s = {}) => { | ||
let e = !1; | ||
const s = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(); | ||
const r = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set(); | ||
let d; | ||
const { origin: l = "lazy-provider" } = r; | ||
const { origin: l = "lazy-provider" } = s; | ||
let f = { | ||
@@ -27,6 +27,6 @@ type: "idle" | ||
const g = /* @__PURE__ */ new Set(), p = (n) => { | ||
n.type === "idle" && (e && u !== 0 && console.error("syncingStatus !== 0, this should not happen"), u = 0), n.type === "syncing" && u++, (n.type === "synced" || n.type === "error") && u--, u < 0 && console.error("syncingStatus < 0, this should not happen"), u === 0 && (f = n), n.type !== "synced" && (f = n), g.forEach((c) => c()); | ||
n.type === "idle" && (e && u !== 0 && console.error("syncingStatus !== 0, this should not happen"), u = 0), n.type === "syncing" && u++, (n.type === "synced" || n.type === "error") && u--, u < 0 && console.error("syncingStatus < 0, this should not happen"), u === 0 && (f = n), n.type !== "synced" && (f = n), g.forEach((i) => i()); | ||
}; | ||
async function j(n) { | ||
const c = n.guid; | ||
const i = n.guid; | ||
if (!e) | ||
@@ -37,3 +37,3 @@ return; | ||
}); | ||
const w = await a.queryDocState(c, { | ||
const w = await a.queryDocState(i, { | ||
stateVector: M(n) | ||
@@ -56,14 +56,14 @@ }).then((y) => { | ||
}); | ||
s.set(c, []), w && m(n, w.missing, l), e && (await a.sendDocUpdate( | ||
c, | ||
E(n, w ? w.state : void 0) | ||
r.set(i, []), w && S(n, w.missing, l), e && (await a.sendDocUpdate( | ||
i, | ||
v(n, w ? w.state : void 0) | ||
), n.emit("sync", [])); | ||
} | ||
function I(n) { | ||
const c = /* @__PURE__ */ new Set(); | ||
o.set(n.guid, c); | ||
const w = async (b, h) => { | ||
const i = /* @__PURE__ */ new Set(); | ||
o.set(n.guid, i); | ||
const w = async (m, h) => { | ||
l !== h && (p({ | ||
type: "syncing" | ||
}), a.sendDocUpdate(n.guid, b).then(() => { | ||
}), a.sendDocUpdate(n.guid, m).then(() => { | ||
p({ | ||
@@ -78,10 +78,10 @@ type: "synced" | ||
})); | ||
}, y = (b) => { | ||
b.loaded.forEach((h) => { | ||
}, y = (m) => { | ||
m.loaded.forEach((h) => { | ||
x(h).catch(console.error); | ||
}), b.removed.forEach((h) => { | ||
}), m.removed.forEach((h) => { | ||
V(h); | ||
}); | ||
}; | ||
n.on("update", w), n.on("subdocs", y), c.add(() => { | ||
n.on("update", w), n.on("subdocs", y), i.add(() => { | ||
n.off("update", w), n.off("subdocs", y); | ||
@@ -92,4 +92,4 @@ }); | ||
var n; | ||
d = (n = a.onDocUpdate) == null ? void 0 : n.call(a, (c, w) => { | ||
var b; | ||
d = (n = a.onDocUpdate) == null ? void 0 : n.call(a, (i, w) => { | ||
var m; | ||
if (!e) | ||
@@ -100,4 +100,4 @@ return; | ||
}); | ||
const y = $(t, c); | ||
y ? (m(y, w, l), s.has(c) && ((b = s.get(c)) == null || b.forEach((h) => m(y, h, l)), s.delete(c))) : (console.warn("idb: doc not found", c), s.set(c, (s.get(c) ?? []).concat(w))), p({ | ||
const y = $(t, i); | ||
y ? (S(y, w, l), r.has(i) && ((m = r.get(i)) == null || m.forEach((h) => S(y, h, l)), r.delete(i))) : (console.warn("idb: doc not found", i), r.set(i, (r.get(i) ?? []).concat(w))), p({ | ||
type: "synced" | ||
@@ -108,15 +108,15 @@ }); | ||
async function x(n) { | ||
i.has(n.guid) || (i.add(n.guid), I(n), await j(n), await Promise.all( | ||
[...n.subdocs].filter((c) => c.shouldLoad).map((c) => x(c)) | ||
c.has(n.guid) || (c.add(n.guid), I(n), await j(n), await Promise.all( | ||
[...n.subdocs].filter((i) => i.shouldLoad).map((i) => x(i)) | ||
)); | ||
} | ||
function V(n) { | ||
i.delete(n.guid); | ||
const c = o.get(n.guid); | ||
c && (c.forEach((w) => w()), o.delete(n.guid)), n.subdocs.forEach(V); | ||
c.delete(n.guid); | ||
const i = o.get(n.guid); | ||
i && (i.forEach((w) => w()), o.delete(n.guid)), n.subdocs.forEach(V); | ||
} | ||
function F() { | ||
o.forEach((n) => { | ||
n.forEach((c) => c()); | ||
}), o.clear(), i.clear(); | ||
n.forEach((i) => i()); | ||
}), o.clear(), c.clear(); | ||
} | ||
@@ -175,16 +175,16 @@ function H() { | ||
} | ||
let U; | ||
function v(t) { | ||
let P; | ||
function A(t) { | ||
const a = new O(); | ||
return t.forEach((r) => { | ||
m(a, r); | ||
}), E(a); | ||
return t.forEach((s) => { | ||
S(a, s); | ||
}), v(a); | ||
} | ||
async function Q(t) { | ||
return new Promise((a) => { | ||
const r = indexedDB.open(t); | ||
const s = indexedDB.open(t); | ||
let e = !0; | ||
r.onsuccess = function() { | ||
r.result.close(), e || indexedDB.deleteDatabase(t), a(e); | ||
}, r.onupgradeneeded = function() { | ||
s.onsuccess = function() { | ||
s.result.close(), e || indexedDB.deleteDatabase(t), a(e); | ||
}, s.onupgradeneeded = function() { | ||
e = !1; | ||
@@ -194,10 +194,10 @@ }; | ||
} | ||
async function Z(t, a, r = D) { | ||
async function Z(t, a, s = D) { | ||
do | ||
if (!U || localStorage.getItem(`${r}-migration`) !== "true") { | ||
if (!P || localStorage.getItem(`${s}-migration`) !== "true") { | ||
try { | ||
U = await indexedDB.databases(); | ||
P = await indexedDB.databases(); | ||
} catch { | ||
if (await Q(a)) { | ||
await S(a, 1).then(async (e) => { | ||
await b(a, 1).then(async (e) => { | ||
if (!e.objectStoreNames.contains("updates")) | ||
@@ -208,3 +208,3 @@ return; | ||
return; | ||
const i = v(o), d = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
const c = A(o), d = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await d.get(a) || (console.log("upgrading the database"), await d.put({ | ||
@@ -215,3 +215,3 @@ id: a, | ||
timestamp: Date.now(), | ||
update: i | ||
update: c | ||
} | ||
@@ -225,15 +225,15 @@ ] | ||
await Promise.all( | ||
U && U.map((e) => { | ||
P && P.map((e) => { | ||
if (e.name && e.version === 1) { | ||
const s = e.name, o = e.version; | ||
return S(s, o).then( | ||
async (i) => { | ||
if (!i.objectStoreNames.contains("updates")) | ||
const r = e.name, o = e.version; | ||
return b(r, o).then( | ||
async (c) => { | ||
if (!c.objectStoreNames.contains("updates")) | ||
return; | ||
const l = await i.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
const l = await c.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(l) || !l.every((p) => p instanceof Uint8Array)) | ||
return; | ||
const f = v(l), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(s) || (console.log("upgrading the database"), await u.put({ | ||
id: s, | ||
const f = A(l), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(r) || (console.log("upgrading the database"), await u.put({ | ||
id: r, | ||
updates: [ | ||
@@ -250,3 +250,3 @@ { | ||
}) | ||
), localStorage.setItem(`${r}-migration`, "true"); | ||
), localStorage.setItem(`${s}-migration`, "true"); | ||
break; | ||
@@ -257,10 +257,10 @@ } | ||
async function ee(t, a = D) { | ||
const o = await (await S(a, k, { | ||
upgrade: A | ||
const o = await (await b(a, E, { | ||
upgrade: k | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(t); | ||
return o ? v(o.updates.map(({ update: i }) => i)) : !1; | ||
return o ? A(o.updates.map(({ update: c }) => c)) : !1; | ||
} | ||
async function te(t, a, r = D) { | ||
await (await S(r, k, { | ||
upgrade: A | ||
async function te(t, a, s = D) { | ||
await (await b(s, E, { | ||
upgrade: k | ||
})).transaction("workspace", "readwrite").objectStore("workspace").put({ | ||
@@ -276,4 +276,21 @@ id: t, | ||
} | ||
async function ne(t, a, s = D) { | ||
const o = (await b(s, E, { | ||
upgrade: k | ||
})).transaction("workspace", "readwrite").objectStore("workspace"), c = await o.get(t); | ||
c ? (c.updates.push({ | ||
timestamp: Date.now(), | ||
update: a | ||
}), await o.put(c)) : await o.put({ | ||
id: t, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: a | ||
} | ||
] | ||
}); | ||
} | ||
let q = 500; | ||
function ne(t) { | ||
function ae(t) { | ||
q = t; | ||
@@ -285,17 +302,17 @@ } | ||
}) => { | ||
const r = S(t, k, { | ||
upgrade: A | ||
const s = b(t, E, { | ||
upgrade: k | ||
}); | ||
return { | ||
...{ | ||
queryDocState: async (s, o) => { | ||
var i; | ||
queryDocState: async (r, o) => { | ||
var c; | ||
try { | ||
const f = await (await r).transaction("workspace", "readonly").objectStore("workspace").get(s); | ||
const f = await (await s).transaction("workspace", "readonly").objectStore("workspace").get(r); | ||
if (!f) | ||
return !1; | ||
const { updates: u } = f, g = v(u.map(({ update: j }) => j)); | ||
const { updates: u } = f, g = A(u.map(({ update: j }) => j)); | ||
return { missing: o != null && o.stateVector ? _(g, o == null ? void 0 : o.stateVector) : g, state: W(g) }; | ||
} catch (d) { | ||
if (!((i = d.message) != null && i.includes("The database connection is closing."))) | ||
if (!((c = d.message) != null && c.includes("The database connection is closing."))) | ||
throw d; | ||
@@ -305,6 +322,6 @@ return !1; | ||
}, | ||
sendDocUpdate: async (s, o) => { | ||
var i; | ||
sendDocUpdate: async (r, o) => { | ||
var c; | ||
try { | ||
const l = (await r).transaction("workspace", "readwrite").objectStore("workspace"), { updates: f } = await l.get(s) ?? { updates: [] }; | ||
const l = (await s).transaction("workspace", "readwrite").objectStore("workspace"), { updates: f } = await l.get(r) ?? { updates: [] }; | ||
let u = [ | ||
@@ -315,3 +332,3 @@ ...f, | ||
if (a && u.length >= a) { | ||
const g = v(u.map(({ update: p }) => p)); | ||
const g = A(u.map(({ update: p }) => p)); | ||
u = [{ timestamp: Date.now(), update: g }]; | ||
@@ -321,3 +338,3 @@ } | ||
l.put({ | ||
id: s, | ||
id: r, | ||
updates: u | ||
@@ -327,3 +344,3 @@ }) | ||
} catch (d) { | ||
if (!((i = d.message) != null && i.includes("The database connection is closing."))) | ||
if (!((c = d.message) != null && c.includes("The database connection is closing."))) | ||
throw d; | ||
@@ -334,11 +351,11 @@ } | ||
disconnect: () => { | ||
r.then((s) => s.close()).catch(console.error); | ||
s.then((r) => r.close()).catch(console.error); | ||
}, | ||
cleanup: async () => { | ||
await (await r).clear("workspace"); | ||
await (await s).clear("workspace"); | ||
} | ||
}; | ||
}, ae = (t, a = D) => { | ||
let r = null, e = null; | ||
const s = { | ||
}, se = (t, a = D) => { | ||
let s = null, e = null; | ||
const r = { | ||
get status() { | ||
@@ -351,9 +368,9 @@ return B(e), e.status; | ||
connect: () => { | ||
s.connected && s.disconnect(), r = R({ dbName: a, mergeCount: q }), e = J(t, r, { origin: "idb" }), e.connect(); | ||
r.connected && r.disconnect(), s = R({ dbName: a, mergeCount: q }), e = J(t, s, { origin: "idb" }), e.connect(); | ||
}, | ||
disconnect: () => { | ||
r == null || r.disconnect(), e == null || e.disconnect(), r = null, e = null; | ||
s == null || s.disconnect(), e == null || e.disconnect(), s = null, e = null; | ||
}, | ||
cleanup: async () => { | ||
await (r == null ? void 0 : r.cleanup()); | ||
await (s == null ? void 0 : s.cleanup()); | ||
}, | ||
@@ -364,4 +381,4 @@ get connected() { | ||
}; | ||
return s; | ||
}, P = "snapshot-origin", T = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), re = async (t) => { | ||
return r; | ||
}, U = "snapshot-origin", T = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), oe = async (t) => { | ||
window.addEventListener("beforeunload", T, { | ||
@@ -373,6 +390,6 @@ capture: !0 | ||
}; | ||
function oe(t, a, r) { | ||
function re(t, a, s) { | ||
const e = new O(); | ||
m(e, a, P); | ||
const s = M(t), o = M(e), i = E( | ||
S(e, a, U); | ||
const r = M(t), o = M(e), c = v( | ||
t, | ||
@@ -382,3 +399,3 @@ o | ||
[...e.share.keys()].map((f) => { | ||
const u = r(f); | ||
const u = s(f); | ||
if (u === "Text") | ||
@@ -393,13 +410,13 @@ return e.getText(f); | ||
{ | ||
trackedOrigins: /* @__PURE__ */ new Set([P]) | ||
trackedOrigins: /* @__PURE__ */ new Set([U]) | ||
} | ||
); | ||
m(e, i, P), d.undo(); | ||
const l = E( | ||
S(e, c, U), d.undo(); | ||
const l = v( | ||
e, | ||
s | ||
r | ||
); | ||
m(t, l, P); | ||
S(t, l, U); | ||
} | ||
class se extends Error { | ||
class ce extends Error { | ||
constructor() { | ||
@@ -409,3 +426,3 @@ super("Early disconnect"); | ||
} | ||
class ce extends Error { | ||
class ie extends Error { | ||
constructor() { | ||
@@ -415,15 +432,15 @@ super("Cleanup when connecting"); | ||
} | ||
const ie = async (t, a, r, e = D) => { | ||
const i = (await S(e, k, { | ||
upgrade: A | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await i.get("id"), l = E(a); | ||
d ? (d.milestone[r] = l, await i.put(d)) : await i.put({ | ||
const de = async (t, a, s, e = D) => { | ||
const c = (await b(e, E, { | ||
upgrade: k | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await c.get("id"), l = v(a); | ||
d ? (d.milestone[s] = l, await c.put(d)) : await c.put({ | ||
id: t, | ||
milestone: { | ||
[r]: l | ||
[s]: l | ||
} | ||
}); | ||
}, de = async (t, a = D) => { | ||
const o = await (await S(a, k, { | ||
upgrade: A | ||
}, ue = async (t, a = D) => { | ||
const o = await (await b(a, E, { | ||
upgrade: k | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(t); | ||
@@ -433,18 +450,19 @@ return o ? o.milestone : null; | ||
export { | ||
ce as CleanupWhenConnectingError, | ||
ie as CleanupWhenConnectingError, | ||
D as DEFAULT_DB_NAME, | ||
se as EarlyDisconnectError, | ||
ae as createIndexedDBProvider, | ||
k as dbVersion, | ||
ce as EarlyDisconnectError, | ||
se as createIndexedDBProvider, | ||
E as dbVersion, | ||
ee as downloadBinary, | ||
de as getMilestones, | ||
ie as markMilestone, | ||
v as mergeUpdates, | ||
ue as getMilestones, | ||
de as markMilestone, | ||
A as mergeUpdates, | ||
te as overwriteBinary, | ||
oe as revertUpdate, | ||
ne as setMergeCount, | ||
ne as pushBinary, | ||
re as revertUpdate, | ||
ae as setMergeCount, | ||
Z as tryMigrate, | ||
A as upgradeDB, | ||
re as writeOperation | ||
k as upgradeDB, | ||
oe as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.9.0-canary.5", | ||
"version": "0.9.0-canary.6", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -6,0 +6,0 @@ "repository": "toeverything/AFFiNE", |
@@ -12,2 +12,3 @@ import type { IDBPDatabase } from 'idb'; | ||
export declare function overwriteBinary(guid: string, update: UpdateMessage['update'], dbName?: string): Promise<void>; | ||
export declare function pushBinary(guid: string, update: UpdateMessage['update'], dbName?: string): Promise<void>; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.9.0-canary.5", | ||
"version": "0.9.0-canary.6", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -43,3 +43,3 @@ "repository": "toeverything/AFFiNE", | ||
"devDependencies": { | ||
"@affine/y-provider": "0.9.0-canary.5", | ||
"@affine/y-provider": "0.9.0-canary.6", | ||
"@blocksuite/blocks": "0.0.0-20230829150056-df43987c-nightly", | ||
@@ -46,0 +46,0 @@ "@blocksuite/store": "0.0.0-20230829150056-df43987c-nightly", |
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
105593
547