@toeverything/y-indexeddb
Advanced tools
Comparing version 0.8.0-canary.29 to 0.8.0-canary.30
import { openDB as S } from "idb"; | ||
import { applyUpdate as m, encodeStateVector as M, encodeStateVectorFromUpdate as _, encodeStateAsUpdate as v, Doc as O, diffUpdate as W, UndoManager as G } from "yjs"; | ||
const k = 1, D = "affine-local"; | ||
function A(e) { | ||
e.createObjectStore("workspace", { keyPath: "id" }), e.createObjectStore("milestone", { keyPath: "id" }); | ||
function A(t) { | ||
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" }); | ||
} | ||
function $(e, a) { | ||
if (e.guid === a) | ||
return e; | ||
for (const o of e.subdocs) { | ||
const t = $(o, a); | ||
if (t) | ||
return t; | ||
function $(t, a) { | ||
if (t.guid === a) | ||
return t; | ||
for (const r of t.subdocs) { | ||
const e = $(r, a); | ||
if (e) | ||
return e; | ||
} | ||
} | ||
const J = (e, a, o = {}) => { | ||
let t = !1; | ||
const s = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(); | ||
const J = (t, a, r = {}) => { | ||
let e = !1; | ||
const s = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Set(); | ||
let d; | ||
const { origin: p = "lazy-provider" } = o; | ||
let l = { | ||
const { origin: l = "lazy-provider" } = r; | ||
let f = { | ||
type: "idle" | ||
}, u = 0; | ||
const g = /* @__PURE__ */ new Set(), f = (n) => { | ||
n.type === "idle" && (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 && (l = n), n.type !== "synced" && (l = n), g.forEach((c) => c()); | ||
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()); | ||
}; | ||
async function j(n) { | ||
const c = n.guid; | ||
f({ | ||
if (!e) | ||
return; | ||
p({ | ||
type: "syncing" | ||
@@ -34,4 +36,14 @@ }); | ||
stateVector: M(n) | ||
}).then((y) => { | ||
if (!e) { | ||
p({ | ||
type: "idle" | ||
}); | ||
return; | ||
} | ||
return p({ | ||
type: "synced" | ||
}), y; | ||
}).catch((y) => { | ||
throw f({ | ||
throw p({ | ||
type: "error", | ||
@@ -41,20 +53,18 @@ error: y | ||
}); | ||
f({ | ||
type: "synced" | ||
}), s.set(c, []), w && m(n, w, p); | ||
s.set(c, []), w && m(n, w, l); | ||
const b = w ? _(w) : void 0; | ||
t && await a.sendDocUpdate(c, v(n, b)); | ||
e && await a.sendDocUpdate(c, v(n, b)); | ||
} | ||
function I(n) { | ||
const c = /* @__PURE__ */ new Set(); | ||
r.set(n.guid, c); | ||
o.set(n.guid, c); | ||
const w = async (y, h) => { | ||
p !== h && (f({ | ||
l !== h && (p({ | ||
type: "syncing" | ||
}), a.sendDocUpdate(n.guid, y).then(() => { | ||
f({ | ||
p({ | ||
type: "synced" | ||
}); | ||
}).catch((C) => { | ||
f({ | ||
p({ | ||
type: "error", | ||
@@ -79,7 +89,9 @@ error: C | ||
var y; | ||
f({ | ||
if (!e) | ||
return; | ||
p({ | ||
type: "syncing" | ||
}); | ||
const b = $(e, c); | ||
b ? (m(b, w, p), s.has(c) && ((y = s.get(c)) == null || y.forEach((h) => m(b, h, p)), s.delete(c))) : (console.warn("idb: doc not found", c), s.set(c, (s.get(c) ?? []).concat(w))), f({ | ||
const b = $(t, c); | ||
b ? (m(b, w, l), s.has(c) && ((y = s.get(c)) == null || y.forEach((h) => m(b, h, l)), s.delete(c))) : (console.warn("idb: doc not found", c), s.set(c, (s.get(c) ?? []).concat(w))), p({ | ||
type: "synced" | ||
@@ -96,24 +108,32 @@ }); | ||
i.delete(n.guid); | ||
const c = r.get(n.guid); | ||
c && (c.forEach((w) => w()), r.delete(n.guid)), n.subdocs.forEach(V); | ||
const c = o.get(n.guid); | ||
c && (c.forEach((w) => w()), o.delete(n.guid)), n.subdocs.forEach(V); | ||
} | ||
function F() { | ||
r.forEach((n) => { | ||
o.forEach((n) => { | ||
n.forEach((c) => c()); | ||
}), r.clear(), i.clear(); | ||
}), o.clear(), i.clear(); | ||
} | ||
function H() { | ||
t = !0, f({ | ||
e = !0, p({ | ||
type: "syncing" | ||
}), x(e).catch((n) => { | ||
f({ | ||
}), x(t).then(() => { | ||
if (!e) { | ||
p({ | ||
type: "idle" | ||
}); | ||
return; | ||
} | ||
p({ | ||
type: "synced" | ||
}); | ||
}).catch((n) => { | ||
p({ | ||
type: "error", | ||
error: n | ||
}), console.error(n); | ||
}), f({ | ||
type: "synced" | ||
}), z(); | ||
} | ||
async function N() { | ||
t = !1, f({ | ||
e = !1, p({ | ||
type: "idle" | ||
@@ -124,3 +144,3 @@ }), F(), d == null || d(), d = void 0; | ||
get status() { | ||
return l; | ||
return f; | ||
}, | ||
@@ -133,3 +153,3 @@ subscribeStatusChange(n) { | ||
get connected() { | ||
return t; | ||
return e; | ||
}, | ||
@@ -140,34 +160,34 @@ passive: !0, | ||
}; | ||
}, L = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), K = async (e) => { | ||
}, L = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), K = async (t) => { | ||
window.addEventListener("beforeunload", L, { | ||
capture: !0 | ||
}), await e, window.removeEventListener("beforeunload", L, { | ||
}), await t, window.removeEventListener("beforeunload", L, { | ||
capture: !0 | ||
}); | ||
}; | ||
function B(e, a = "val does not exist") { | ||
if (e == null) | ||
function B(t, a = "val does not exist") { | ||
if (t == null) | ||
throw a instanceof Error ? a : new Error(a); | ||
} | ||
let U; | ||
function E(e) { | ||
function E(t) { | ||
const a = new O(); | ||
return e.forEach((o) => { | ||
m(a, o); | ||
return t.forEach((r) => { | ||
m(a, r); | ||
}), v(a); | ||
} | ||
async function Q(e) { | ||
async function Q(t) { | ||
return new Promise((a) => { | ||
const o = indexedDB.open(e); | ||
let t = !0; | ||
o.onsuccess = function() { | ||
o.result.close(), t || indexedDB.deleteDatabase(e), a(t); | ||
}, o.onupgradeneeded = function() { | ||
t = !1; | ||
const r = indexedDB.open(t); | ||
let e = !0; | ||
r.onsuccess = function() { | ||
r.result.close(), e || indexedDB.deleteDatabase(t), a(e); | ||
}, r.onupgradeneeded = function() { | ||
e = !1; | ||
}; | ||
}); | ||
} | ||
async function Z(e, a, o = D) { | ||
async function Z(t, a, r = D) { | ||
do | ||
if (!U || localStorage.getItem(`${o}-migration`) !== "true") { | ||
if (!U || localStorage.getItem(`${r}-migration`) !== "true") { | ||
try { | ||
@@ -177,9 +197,9 @@ U = await indexedDB.databases(); | ||
if (await Q(a)) { | ||
await S(a, 1).then(async (t) => { | ||
if (!t.objectStoreNames.contains("updates")) | ||
await S(a, 1).then(async (e) => { | ||
if (!e.objectStoreNames.contains("updates")) | ||
return; | ||
const r = await t.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(r) || !r.every((l) => l instanceof Uint8Array)) | ||
const o = await e.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(o) || !o.every((f) => f instanceof Uint8Array)) | ||
return; | ||
const i = E(r), d = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
const i = E(o), d = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await d.get(a) || (console.log("upgrading the database"), await d.put({ | ||
@@ -199,13 +219,13 @@ id: a, | ||
await Promise.all( | ||
U && U.map((t) => { | ||
if (t.name && t.version === 1) { | ||
const s = t.name, r = t.version; | ||
return S(s, r).then( | ||
U && U.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")) | ||
return; | ||
const p = await i.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(p) || !p.every((f) => f instanceof Uint8Array)) | ||
const l = await i.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(l) || !l.every((p) => p instanceof Uint8Array)) | ||
return; | ||
const l = E(p), u = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
const f = E(l), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(s) || (console.log("upgrading the database"), await u.put({ | ||
@@ -216,3 +236,3 @@ id: s, | ||
timestamp: Date.now(), | ||
update: l | ||
update: f | ||
} | ||
@@ -225,3 +245,3 @@ ] | ||
}) | ||
), localStorage.setItem(`${o}-migration`, "true"); | ||
), localStorage.setItem(`${r}-migration`, "true"); | ||
break; | ||
@@ -231,13 +251,13 @@ } | ||
} | ||
async function ee(e, a = D) { | ||
const r = await (await S(a, k, { | ||
async function ee(t, a = D) { | ||
const o = await (await S(a, k, { | ||
upgrade: A | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(e); | ||
return r ? E(r.updates.map(({ update: i }) => i)) : !1; | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(t); | ||
return o ? E(o.updates.map(({ update: i }) => i)) : !1; | ||
} | ||
async function te(e, a, o = D) { | ||
await (await S(o, k, { | ||
async function te(t, a, r = D) { | ||
await (await S(r, k, { | ||
upgrade: A | ||
})).transaction("workspace", "readwrite").objectStore("workspace").put({ | ||
id: e, | ||
id: t, | ||
updates: [ | ||
@@ -252,10 +272,10 @@ { | ||
let q = 500; | ||
function ne(e) { | ||
q = e; | ||
function ne(t) { | ||
q = t; | ||
} | ||
const R = ({ | ||
dbName: e, | ||
dbName: t, | ||
mergeCount: a | ||
}) => { | ||
const o = S(e, k, { | ||
const r = S(t, k, { | ||
upgrade: A | ||
@@ -265,10 +285,10 @@ }); | ||
...{ | ||
queryDocState: async (s, r) => { | ||
queryDocState: async (s, o) => { | ||
var i; | ||
try { | ||
const l = await (await o).transaction("workspace", "readonly").objectStore("workspace").get(s); | ||
if (!l) | ||
const f = await (await r).transaction("workspace", "readonly").objectStore("workspace").get(s); | ||
if (!f) | ||
return !1; | ||
const { updates: u } = l, g = E(u.map(({ update: j }) => j)); | ||
return r != null && r.stateVector ? W(g, r == null ? void 0 : r.stateVector) : g; | ||
const { updates: u } = f, g = E(u.map(({ update: j }) => j)); | ||
return o != null && o.stateVector ? W(g, o == null ? void 0 : o.stateVector) : g; | ||
} catch (d) { | ||
@@ -280,16 +300,16 @@ if (!((i = d.message) != null && i.includes("The database connection is closing."))) | ||
}, | ||
sendDocUpdate: async (s, r) => { | ||
sendDocUpdate: async (s, o) => { | ||
var i; | ||
try { | ||
const p = (await o).transaction("workspace", "readwrite").objectStore("workspace"), { updates: l } = await p.get(s) ?? { updates: [] }; | ||
const l = (await r).transaction("workspace", "readwrite").objectStore("workspace"), { updates: f } = await l.get(s) ?? { updates: [] }; | ||
let u = [ | ||
...l, | ||
{ timestamp: Date.now(), update: r } | ||
...f, | ||
{ timestamp: Date.now(), update: o } | ||
]; | ||
if (a && u.length >= a) { | ||
const g = E(u.map(({ update: f }) => f)); | ||
const g = E(u.map(({ update: p }) => p)); | ||
u = [{ timestamp: Date.now(), update: g }]; | ||
} | ||
await K( | ||
p.put({ | ||
l.put({ | ||
id: s, | ||
@@ -306,53 +326,53 @@ updates: u | ||
disconnect: () => { | ||
o.then((s) => s.close()).catch(console.error); | ||
r.then((s) => s.close()).catch(console.error); | ||
}, | ||
cleanup: async () => { | ||
await (await o).clear("workspace"); | ||
await (await r).clear("workspace"); | ||
} | ||
}; | ||
}, ae = (e, a = D) => { | ||
let o = null, t = null; | ||
}, ae = (t, a = D) => { | ||
let r = null, e = null; | ||
const s = { | ||
get status() { | ||
return B(t), t.status; | ||
return B(e), e.status; | ||
}, | ||
subscribeStatusChange(r) { | ||
return B(t), t.subscribeStatusChange(r); | ||
subscribeStatusChange(o) { | ||
return B(e), e.subscribeStatusChange(o); | ||
}, | ||
connect: () => { | ||
s.connected && s.disconnect(), o = R({ dbName: a, mergeCount: q }), t = J(e, o, { origin: "idb" }), t.connect(); | ||
s.connected && s.disconnect(), r = R({ dbName: a, mergeCount: q }), e = J(t, r, { origin: "idb" }), e.connect(); | ||
}, | ||
disconnect: () => { | ||
o == null || o.disconnect(), t == null || t.disconnect(), o = null, t = null; | ||
r == null || r.disconnect(), e == null || e.disconnect(), r = null, e = null; | ||
}, | ||
cleanup: async () => { | ||
await (o == null ? void 0 : o.cleanup()); | ||
await (r == null ? void 0 : r.cleanup()); | ||
}, | ||
get connected() { | ||
return (t == null ? void 0 : t.connected) || !1; | ||
return (e == null ? void 0 : e.connected) || !1; | ||
} | ||
}; | ||
return s; | ||
}, P = "snapshot-origin", T = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), oe = async (e) => { | ||
}, P = "snapshot-origin", T = (t) => (t.preventDefault(), t.returnValue = "Data is not saved. Are you sure you want to leave?"), re = async (t) => { | ||
window.addEventListener("beforeunload", T, { | ||
capture: !0 | ||
}), await e, window.removeEventListener("beforeunload", T, { | ||
}), await t, window.removeEventListener("beforeunload", T, { | ||
capture: !0 | ||
}); | ||
}; | ||
function re(e, a, o) { | ||
const t = new O(); | ||
m(t, a, P); | ||
const s = M(e), r = M(t), i = v( | ||
e, | ||
r | ||
function oe(t, a, r) { | ||
const e = new O(); | ||
m(e, a, P); | ||
const s = M(t), o = M(e), i = v( | ||
t, | ||
o | ||
), d = new G( | ||
[...t.share.keys()].map((l) => { | ||
const u = o(l); | ||
[...e.share.keys()].map((f) => { | ||
const u = r(f); | ||
if (u === "Text") | ||
return t.getText(l); | ||
return e.getText(f); | ||
if (u === "Map") | ||
return t.getMap(l); | ||
return e.getMap(f); | ||
if (u === "Array") | ||
return t.getArray(l); | ||
return e.getArray(f); | ||
throw new Error("Unknown type"); | ||
@@ -364,8 +384,8 @@ }), | ||
); | ||
m(t, i, P), d.undo(); | ||
const p = v( | ||
t, | ||
m(e, i, P), d.undo(); | ||
const l = v( | ||
e, | ||
s | ||
); | ||
m(e, p, P); | ||
m(t, l, P); | ||
} | ||
@@ -382,17 +402,17 @@ class se extends Error { | ||
} | ||
const ie = async (e, a, o, t = D) => { | ||
const i = (await S(t, k, { | ||
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"), p = v(a); | ||
d ? (d.milestone[o] = p, await i.put(d)) : await i.put({ | ||
id: e, | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await i.get("id"), l = v(a); | ||
d ? (d.milestone[r] = l, await i.put(d)) : await i.put({ | ||
id: t, | ||
milestone: { | ||
[o]: p | ||
[r]: l | ||
} | ||
}); | ||
}, de = async (e, a = D) => { | ||
const r = await (await S(a, k, { | ||
}, de = async (t, a = D) => { | ||
const o = await (await S(a, k, { | ||
upgrade: A | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(e); | ||
return r ? r.milestone : null; | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(t); | ||
return o ? o.milestone : null; | ||
}; | ||
@@ -410,8 +430,8 @@ export { | ||
te as overwriteBinary, | ||
re as revertUpdate, | ||
oe as revertUpdate, | ||
ne as setMergeCount, | ||
Z as tryMigrate, | ||
A as upgradeDB, | ||
oe as writeOperation | ||
re as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.8.0-canary.29", | ||
"version": "0.8.0-canary.30", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -40,4 +40,4 @@ "repository": "toeverything/AFFiNE", | ||
"@affine/y-provider": "workspace:*", | ||
"@blocksuite/blocks": "0.0.0-20230821151132-2bd0c7f6-nightly", | ||
"@blocksuite/store": "0.0.0-20230821151132-2bd0c7f6-nightly", | ||
"@blocksuite/blocks": "0.0.0-20230822230555-98129627-nightly", | ||
"@blocksuite/store": "0.0.0-20230822230555-98129627-nightly", | ||
"vite": "^4.4.9", | ||
@@ -44,0 +44,0 @@ "vite-plugin-dts": "3.5.2", |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.8.0-canary.29", | ||
"version": "0.8.0-canary.30", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -43,5 +43,5 @@ "repository": "toeverything/AFFiNE", | ||
"devDependencies": { | ||
"@affine/y-provider": "0.8.0-canary.29", | ||
"@blocksuite/blocks": "0.0.0-20230821151132-2bd0c7f6-nightly", | ||
"@blocksuite/store": "0.0.0-20230821151132-2bd0c7f6-nightly", | ||
"@affine/y-provider": "0.8.0-canary.30", | ||
"@blocksuite/blocks": "0.0.0-20230822230555-98129627-nightly", | ||
"@blocksuite/store": "0.0.0-20230822230555-98129627-nightly", | ||
"vite": "^4.4.9", | ||
@@ -48,0 +48,0 @@ "vite-plugin-dts": "3.5.2", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101884
526