@toeverything/y-indexeddb
Advanced tools
Comparing version 0.8.0-canary.16 to 0.8.0-canary.17
import { openDB as b } from "idb"; | ||
import { applyUpdate as m, encodeStateVector as j, encodeStateVectorFromUpdate as $, encodeStateAsUpdate as P, mergeUpdates as v, diffUpdate as q, Doc as I, UndoManager as z } from "yjs"; | ||
const S = 1, y = "affine-local"; | ||
import { applyUpdate as m, encodeStateVector as x, encodeStateVectorFromUpdate as N, encodeStateAsUpdate as P, mergeUpdates as v, diffUpdate as _, Doc as W, UndoManager as G } from "yjs"; | ||
const E = 1, S = "affine-local"; | ||
function k(e) { | ||
e.createObjectStore("workspace", { keyPath: "id" }), e.createObjectStore("milestone", { keyPath: "id" }); | ||
} | ||
function L(e, o) { | ||
if (e.guid === o) | ||
function T(e, a) { | ||
if (e.guid === a) | ||
return e; | ||
for (const a of e.subdocs) { | ||
const t = L(a, o); | ||
if (t) | ||
return t; | ||
for (const o of e.subdocs) { | ||
const n = T(o, a); | ||
if (n) | ||
return n; | ||
} | ||
} | ||
const F = (e, o, a = {}) => { | ||
let t = !1; | ||
const r = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set(); | ||
let u; | ||
const { origin: i = "lazy-provider" } = a; | ||
async function p(n) { | ||
const d = n.guid, w = await o.queryDocState(d, { | ||
stateVector: j(n) | ||
const J = (e, a, o = {}) => { | ||
let n = !1; | ||
const s = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set(); | ||
let l; | ||
const { origin: d = "lazy-provider" } = o; | ||
let u = { | ||
type: "idle" | ||
}, p = 0; | ||
const y = /* @__PURE__ */ new Set(), f = (t) => { | ||
t.type === "idle" && (p !== 0 && console.error("syncingStatus !== 0, this should not happen"), p = 0), t.type === "syncing" && p++, (t.type === "synced" || t.type === "error") && p--, p < 0 && console.error("syncingStatus < 0, this should not happen"), p === 0 && (u = t), t.type !== "synced" && (u = t), y.forEach((i) => i()); | ||
}; | ||
async function $(t) { | ||
const i = t.guid; | ||
f({ | ||
type: "syncing" | ||
}); | ||
r.set(d, []), w && m(n, w, i); | ||
const g = w ? $(w) : void 0; | ||
await o.sendDocUpdate(d, P(n, g)); | ||
const w = await a.queryDocState(i, { | ||
stateVector: x(t) | ||
}).catch((g) => { | ||
throw f({ | ||
type: "error", | ||
error: g | ||
}), g; | ||
}); | ||
f({ | ||
type: "synced" | ||
}), s.set(i, []), w && m(t, w, d); | ||
const h = w ? N(w) : void 0; | ||
n && await a.sendDocUpdate(i, P(t, h)); | ||
} | ||
function l(n) { | ||
const d = /* @__PURE__ */ new Set(); | ||
s.set(n.guid, d); | ||
const w = async (E, D) => { | ||
i !== D && o.sendDocUpdate(n.guid, E).catch(console.error); | ||
}, g = (E) => { | ||
E.loaded.forEach((D) => { | ||
f(D).catch(console.error); | ||
}), E.removed.forEach((D) => { | ||
function q(t) { | ||
const i = /* @__PURE__ */ new Set(); | ||
r.set(t.guid, i); | ||
const w = async (g, D) => { | ||
d !== D && (f({ | ||
type: "syncing" | ||
}), a.sendDocUpdate(t.guid, g).then(() => { | ||
f({ | ||
type: "synced" | ||
}); | ||
}).catch((V) => { | ||
f({ | ||
type: "error", | ||
error: V | ||
}), console.error(V); | ||
})); | ||
}, h = (g) => { | ||
g.loaded.forEach((D) => { | ||
j(D).catch(console.error); | ||
}), g.removed.forEach((D) => { | ||
M(D); | ||
}); | ||
}; | ||
n.on("update", w), n.on("subdocs", g), d.add(() => { | ||
n.off("update", w), n.off("subdocs", g); | ||
t.on("update", w), t.on("subdocs", h), i.add(() => { | ||
t.off("update", w), t.off("subdocs", h); | ||
}); | ||
} | ||
function h() { | ||
u = o.onDocUpdate?.((n, d) => { | ||
const w = L(e, n); | ||
w ? (m(w, d, i), r.has(n) && (r.get(n)?.forEach((g) => m(w, g, i)), r.delete(n))) : (console.warn("idb: doc not found", n), r.set(n, (r.get(n) ?? []).concat(d))); | ||
function I() { | ||
l = a.onDocUpdate?.((t, i) => { | ||
f({ | ||
type: "syncing" | ||
}); | ||
const w = T(e, t); | ||
w ? (m(w, i, d), s.has(t) && (s.get(t)?.forEach((h) => m(w, h, d)), s.delete(t))) : (console.warn("idb: doc not found", t), s.set(t, (s.get(t) ?? []).concat(i))), f({ | ||
type: "synced" | ||
}); | ||
}); | ||
} | ||
async function f(n) { | ||
c.has(n.guid) || (c.add(n.guid), l(n), await p(n), await Promise.all( | ||
[...n.subdocs].filter((d) => d.shouldLoad).map((d) => f(d)) | ||
async function j(t) { | ||
c.has(t.guid) || (c.add(t.guid), q(t), await $(t), await Promise.all( | ||
[...t.subdocs].filter((i) => i.shouldLoad).map((i) => j(i)) | ||
)); | ||
} | ||
function M(n) { | ||
c.delete(n.guid); | ||
const d = s.get(n.guid); | ||
d && (d.forEach((w) => w()), s.delete(n.guid)), n.subdocs.forEach(M); | ||
function M(t) { | ||
c.delete(t.guid); | ||
const i = r.get(t.guid); | ||
i && (i.forEach((w) => w()), r.delete(t.guid)), t.subdocs.forEach(M); | ||
} | ||
function T() { | ||
s.forEach((n) => { | ||
n.forEach((d) => d()); | ||
}), s.clear(), c.clear(); | ||
function z() { | ||
r.forEach((t) => { | ||
t.forEach((i) => i()); | ||
}), r.clear(), c.clear(); | ||
} | ||
function C() { | ||
t = !0, f(e).catch(console.error), h(); | ||
function F() { | ||
n = !0, f({ | ||
type: "syncing" | ||
}), j(e).catch((t) => { | ||
f({ | ||
type: "error", | ||
error: t | ||
}), console.error(t); | ||
}), f({ | ||
type: "synced" | ||
}), I(); | ||
} | ||
async function O() { | ||
t = !1, T(), u?.(), u = void 0; | ||
async function H() { | ||
n = !1, f({ | ||
type: "idle" | ||
}), z(), l?.(), l = void 0; | ||
} | ||
return { | ||
get status() { | ||
return u; | ||
}, | ||
subscribeStatusChange(t) { | ||
return y.add(t), () => { | ||
y.delete(t); | ||
}; | ||
}, | ||
get connected() { | ||
return t; | ||
return n; | ||
}, | ||
passive: !0, | ||
connect: C, | ||
disconnect: O | ||
connect: F, | ||
disconnect: H | ||
}; | ||
}, x = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), H = async (e) => { | ||
window.addEventListener("beforeunload", x, { | ||
}, C = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), K = async (e) => { | ||
window.addEventListener("beforeunload", C, { | ||
capture: !0 | ||
}), await e, window.removeEventListener("beforeunload", x, { | ||
}), await e, window.removeEventListener("beforeunload", C, { | ||
capture: !0 | ||
}); | ||
}; | ||
let B = 500; | ||
function J(e) { | ||
B = e; | ||
function L(e, a = "val does not exist") { | ||
if (e == null) | ||
throw a instanceof Error ? a : new Error(a); | ||
} | ||
const N = ({ | ||
let O = 500; | ||
function Z(e) { | ||
O = e; | ||
} | ||
const Q = ({ | ||
dbName: e, | ||
mergeCount: o | ||
mergeCount: a | ||
}) => { | ||
const a = b(e, S, { | ||
const o = b(e, E, { | ||
upgrade: k | ||
@@ -100,9 +156,9 @@ }); | ||
...{ | ||
queryDocState: async (r, s) => { | ||
queryDocState: async (s, r) => { | ||
try { | ||
const i = await (await a).transaction("workspace", "readonly").objectStore("workspace").get(r); | ||
if (!i) | ||
const d = await (await o).transaction("workspace", "readonly").objectStore("workspace").get(s); | ||
if (!d) | ||
return !1; | ||
const { updates: p } = i, l = v(p.map(({ update: f }) => f)); | ||
return s?.stateVector ? q(l, s?.stateVector) : l; | ||
const { updates: u } = d, p = v(u.map(({ update: f }) => f)); | ||
return r?.stateVector ? _(p, r?.stateVector) : p; | ||
} catch (c) { | ||
@@ -114,17 +170,17 @@ if (!c.message?.includes("The database connection is closing.")) | ||
}, | ||
sendDocUpdate: async (r, s) => { | ||
sendDocUpdate: async (s, r) => { | ||
try { | ||
const u = (await a).transaction("workspace", "readwrite").objectStore("workspace"), { updates: i } = await u.get(r) ?? { updates: [] }; | ||
let p = [ | ||
...i, | ||
{ timestamp: Date.now(), update: s } | ||
const l = (await o).transaction("workspace", "readwrite").objectStore("workspace"), { updates: d } = await l.get(s) ?? { updates: [] }; | ||
let u = [ | ||
...d, | ||
{ timestamp: Date.now(), update: r } | ||
]; | ||
if (o && p.length >= o) { | ||
const l = v(p.map(({ update: h }) => h)); | ||
p = [{ timestamp: Date.now(), update: l }]; | ||
if (a && u.length >= a) { | ||
const p = v(u.map(({ update: y }) => y)); | ||
u = [{ timestamp: Date.now(), update: p }]; | ||
} | ||
await H( | ||
u.put({ | ||
id: r, | ||
updates: p | ||
await K( | ||
l.put({ | ||
id: s, | ||
updates: u | ||
}) | ||
@@ -139,54 +195,60 @@ ); | ||
disconnect: () => { | ||
a.then((r) => r.close()).catch(console.error); | ||
o.then((s) => s.close()).catch(console.error); | ||
}, | ||
cleanup: async () => { | ||
await (await a).clear("workspace"); | ||
await (await o).clear("workspace"); | ||
} | ||
}; | ||
}, K = (e, o = y) => { | ||
let a = null, t = null; | ||
const r = { | ||
}, ee = (e, a = S) => { | ||
let o = null, n = null; | ||
const s = { | ||
get status() { | ||
return L(n), n.status; | ||
}, | ||
subscribeStatusChange(r) { | ||
return L(n), n.subscribeStatusChange(r); | ||
}, | ||
connect: () => { | ||
r.connected && r.disconnect(), a = N({ dbName: o, mergeCount: B }), t = F(e, a, { origin: "idb" }), t.connect(); | ||
s.connected && s.disconnect(), o = Q({ dbName: a, mergeCount: O }), n = J(e, o, { origin: "idb" }), n.connect(); | ||
}, | ||
disconnect: () => { | ||
a?.disconnect(), t?.disconnect(), a = null, t = null; | ||
o?.disconnect(), n?.disconnect(), o = null, n = null; | ||
}, | ||
cleanup: async () => { | ||
await a?.cleanup(); | ||
await o?.cleanup(); | ||
}, | ||
get connected() { | ||
return t?.connected || !1; | ||
return n?.connected || !1; | ||
} | ||
}; | ||
return r; | ||
return s; | ||
}; | ||
let U; | ||
async function _(e) { | ||
return new Promise((o) => { | ||
const a = indexedDB.open(e); | ||
let t = !0; | ||
a.onsuccess = function() { | ||
a.result.close(), t || indexedDB.deleteDatabase(e), o(t); | ||
}, a.onupgradeneeded = function() { | ||
t = !1; | ||
async function R(e) { | ||
return new Promise((a) => { | ||
const o = indexedDB.open(e); | ||
let n = !0; | ||
o.onsuccess = function() { | ||
o.result.close(), n || indexedDB.deleteDatabase(e), a(n); | ||
}, o.onupgradeneeded = function() { | ||
n = !1; | ||
}; | ||
}); | ||
} | ||
async function Q(e, o, a = y) { | ||
async function te(e, a, o = S) { | ||
do | ||
if (!U || localStorage.getItem(`${a}-migration`) !== "true") { | ||
if (!U || localStorage.getItem(`${o}-migration`) !== "true") { | ||
try { | ||
U = await indexedDB.databases(); | ||
} catch { | ||
if (await _(o)) { | ||
await b(o, 1).then(async (t) => { | ||
if (!t.objectStoreNames.contains("updates")) | ||
if (await R(a)) { | ||
await b(a, 1).then(async (n) => { | ||
if (!n.objectStoreNames.contains("updates")) | ||
return; | ||
const s = await t.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(s) || !s.every((p) => p instanceof Uint8Array)) | ||
const r = await n.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(r) || !r.every((u) => u instanceof Uint8Array)) | ||
return; | ||
const c = v(s), u = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await u.get(o) || (console.log("upgrading the database"), await u.put({ | ||
id: o, | ||
const c = v(r), l = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await l.get(a) || (console.log("upgrading the database"), await l.put({ | ||
id: a, | ||
updates: [ | ||
@@ -204,19 +266,19 @@ { | ||
await Promise.all( | ||
U && U.map((t) => { | ||
if (t.name && t.version === 1) { | ||
const r = t.name, s = t.version; | ||
return b(r, s).then( | ||
U && U.map((n) => { | ||
if (n.name && n.version === 1) { | ||
const s = n.name, r = n.version; | ||
return b(s, r).then( | ||
async (c) => { | ||
if (!c.objectStoreNames.contains("updates")) | ||
return; | ||
const i = await c.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(i) || !i.every((f) => f instanceof Uint8Array)) | ||
const d = await c.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(d) || !d.every((f) => f instanceof Uint8Array)) | ||
return; | ||
const p = v(i), l = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await l.get(r) || (console.log("upgrading the database"), await l.put({ | ||
id: r, | ||
const u = v(d), p = e.transaction("workspace", "readwrite").objectStore("workspace"); | ||
await p.get(s) || (console.log("upgrading the database"), await p.put({ | ||
id: s, | ||
updates: [ | ||
{ | ||
timestamp: Date.now(), | ||
update: p | ||
update: u | ||
} | ||
@@ -229,3 +291,3 @@ ] | ||
}) | ||
), localStorage.setItem(`${a}-migration`, "true"); | ||
), localStorage.setItem(`${o}-migration`, "true"); | ||
break; | ||
@@ -235,10 +297,10 @@ } | ||
} | ||
async function R(e, o = y) { | ||
const s = await (await b(o, S, { | ||
async function ne(e, a = S) { | ||
const r = await (await b(a, E, { | ||
upgrade: k | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(e); | ||
return s ? v(s.updates.map(({ update: c }) => c)) : !1; | ||
return r ? v(r.updates.map(({ update: c }) => c)) : !1; | ||
} | ||
async function X(e, o, a = y) { | ||
await (await b(a, S, { | ||
async function ae(e, a, o = S) { | ||
await (await b(o, E, { | ||
upgrade: k | ||
@@ -250,3 +312,3 @@ })).transaction("workspace", "readwrite").objectStore("workspace").put({ | ||
timestamp: Date.now(), | ||
update: o | ||
update: a | ||
} | ||
@@ -256,24 +318,24 @@ ] | ||
} | ||
const A = "snapshot-origin", V = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), Y = async (e) => { | ||
window.addEventListener("beforeunload", V, { | ||
const A = "snapshot-origin", B = (e) => (e.preventDefault(), e.returnValue = "Data is not saved. Are you sure you want to leave?"), oe = async (e) => { | ||
window.addEventListener("beforeunload", B, { | ||
capture: !0 | ||
}), await e, window.removeEventListener("beforeunload", V, { | ||
}), await e, window.removeEventListener("beforeunload", B, { | ||
capture: !0 | ||
}); | ||
}; | ||
function Z(e, o, a) { | ||
const t = new I(); | ||
m(t, o, A); | ||
const r = j(e), s = j(t), c = P( | ||
function se(e, a, o) { | ||
const n = new W(); | ||
m(n, a, A); | ||
const s = x(e), r = x(n), c = P( | ||
e, | ||
s | ||
), u = new z( | ||
[...t.share.keys()].map((p) => { | ||
const l = a(p); | ||
if (l === "Text") | ||
return t.getText(p); | ||
if (l === "Map") | ||
return t.getMap(p); | ||
if (l === "Array") | ||
return t.getArray(p); | ||
r | ||
), l = new G( | ||
[...n.share.keys()].map((u) => { | ||
const p = o(u); | ||
if (p === "Text") | ||
return n.getText(u); | ||
if (p === "Map") | ||
return n.getMap(u); | ||
if (p === "Array") | ||
return n.getArray(u); | ||
throw new Error("Unknown type"); | ||
@@ -285,10 +347,10 @@ }), | ||
); | ||
m(t, c, A), u.undo(); | ||
const i = P( | ||
t, | ||
r | ||
m(n, c, A), l.undo(); | ||
const d = P( | ||
n, | ||
s | ||
); | ||
m(e, i, A); | ||
m(e, d, A); | ||
} | ||
class ee extends Error { | ||
class re extends Error { | ||
constructor() { | ||
@@ -298,3 +360,3 @@ super("Early disconnect"); | ||
} | ||
class te extends Error { | ||
class ce extends Error { | ||
constructor() { | ||
@@ -304,34 +366,34 @@ super("Cleanup when connecting"); | ||
} | ||
const ne = async (e, o, a, t = y) => { | ||
const c = (await b(t, S, { | ||
const ie = async (e, a, o, n = S) => { | ||
const c = (await b(n, E, { | ||
upgrade: k | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), u = await c.get("id"), i = P(o); | ||
u ? (u.milestone[a] = i, await c.put(u)) : await c.put({ | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), l = await c.get("id"), d = P(a); | ||
l ? (l.milestone[o] = d, await c.put(l)) : await c.put({ | ||
id: e, | ||
milestone: { | ||
[a]: i | ||
[o]: d | ||
} | ||
}); | ||
}, ae = async (e, o = y) => { | ||
const s = await (await b(o, S, { | ||
}, de = async (e, a = S) => { | ||
const r = await (await b(a, E, { | ||
upgrade: k | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(e); | ||
return s ? s.milestone : null; | ||
return r ? r.milestone : null; | ||
}; | ||
export { | ||
te as CleanupWhenConnectingError, | ||
y as DEFAULT_DB_NAME, | ||
ee as EarlyDisconnectError, | ||
K as createIndexedDBProvider, | ||
S as dbVersion, | ||
R as downloadBinary, | ||
ae as getMilestones, | ||
ne as markMilestone, | ||
X as overwriteBinary, | ||
Z as revertUpdate, | ||
J as setMergeCount, | ||
Q as tryMigrate, | ||
ce as CleanupWhenConnectingError, | ||
S as DEFAULT_DB_NAME, | ||
re as EarlyDisconnectError, | ||
ee as createIndexedDBProvider, | ||
E as dbVersion, | ||
ne as downloadBinary, | ||
de as getMilestones, | ||
ie as markMilestone, | ||
ae as overwriteBinary, | ||
se as revertUpdate, | ||
Z as setMergeCount, | ||
te as tryMigrate, | ||
k as upgradeDB, | ||
Y as writeOperation | ||
oe as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.8.0-canary.16", | ||
"version": "0.8.0-canary.17", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -40,4 +40,4 @@ "repository": "toeverything/AFFiNE", | ||
"@affine/y-provider": "workspace:*", | ||
"@blocksuite/blocks": "0.0.0-20230810005427-25adb757-nightly", | ||
"@blocksuite/store": "0.0.0-20230810005427-25adb757-nightly", | ||
"@blocksuite/blocks": "0.0.0-20230810154852-8a8eccea-nightly", | ||
"@blocksuite/store": "0.0.0-20230810154852-8a8eccea-nightly", | ||
"vite": "^4.4.9", | ||
@@ -44,0 +44,0 @@ "vite-plugin-dts": "3.5.1", |
@@ -0,1 +1,2 @@ | ||
import type { StatusAdapter } from '@affine/y-provider'; | ||
import type { DBSchema, IDBPDatabase } from 'idb'; | ||
@@ -5,3 +6,3 @@ export declare const dbVersion = 1; | ||
export declare function upgradeDB(db: IDBPDatabase<BlockSuiteBinaryDB>): void; | ||
export interface IndexedDBProvider { | ||
export interface IndexedDBProvider extends StatusAdapter { | ||
connect: () => void; | ||
@@ -8,0 +9,0 @@ disconnect: () => void; |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.8.0-canary.16", | ||
"version": "0.8.0-canary.17", | ||
"description": "IndexedDB database adapter for Yjs", | ||
@@ -43,5 +43,5 @@ "repository": "toeverything/AFFiNE", | ||
"devDependencies": { | ||
"@affine/y-provider": "0.8.0-canary.16", | ||
"@blocksuite/blocks": "0.0.0-20230810005427-25adb757-nightly", | ||
"@blocksuite/store": "0.0.0-20230810005427-25adb757-nightly", | ||
"@affine/y-provider": "0.8.0-canary.17", | ||
"@blocksuite/blocks": "0.0.0-20230810154852-8a8eccea-nightly", | ||
"@blocksuite/store": "0.0.0-20230810154852-8a8eccea-nightly", | ||
"vite": "^4.4.9", | ||
@@ -48,0 +48,0 @@ "vite-plugin-dts": "3.5.1", |
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
99034
494