@toeverything/y-indexeddb
Advanced tools
Comparing version 0.5.4-canary.20 to 0.5.4-canary.21
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 U(t) { | ||
function j(t) { | ||
t.createObjectStore("workspace", { keyPath: "id" }), t.createObjectStore("milestone", { keyPath: "id" }); | ||
@@ -10,7 +10,7 @@ } | ||
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; | ||
@@ -20,5 +20,5 @@ }; | ||
} | ||
async function _(t, a, r = D) { | ||
async function _(t, a, s = D) { | ||
do | ||
if (!E || localStorage.getItem(`${r}-migration`) !== "true") { | ||
if (!E || localStorage.getItem(`${s}-migration`) !== "true") { | ||
try { | ||
@@ -57,3 +57,3 @@ E = await indexedDB.databases(); | ||
const c = await o.transaction("updates", "readonly").objectStore("updates").getAll(); | ||
if (!Array.isArray(c) || !c.every((y) => y instanceof Uint8Array)) | ||
if (!Array.isArray(c) || !c.every((g) => g instanceof Uint8Array)) | ||
return; | ||
@@ -74,3 +74,3 @@ const p = x(c), u = t.transaction("workspace", "readwrite").objectStore("workspace"); | ||
}) | ||
), localStorage.setItem(`${r}-migration`, "true"); | ||
), localStorage.setItem(`${s}-migration`, "true"); | ||
break; | ||
@@ -82,5 +82,5 @@ } | ||
const n = await (await S(a, A, { | ||
upgrade: U | ||
upgrade: j | ||
})).transaction("workspace", "readonly").objectStore("workspace").get(t); | ||
return n ? x(n.updates.map(({ update: o }) => o)) : new Uint8Array(0); | ||
return n ? x(n.updates.map(({ update: o }) => o)) : !1; | ||
} | ||
@@ -99,3 +99,3 @@ const h = Symbol("indexeddb-provider-origin"), k = Symbol("snapshot-origin"); | ||
} | ||
function G(t, a, r) { | ||
function G(t, a, s) { | ||
const e = new M(); | ||
@@ -108,3 +108,3 @@ b(e, a, k); | ||
[...e.share.keys()].map((p) => { | ||
const u = r(p); | ||
const u = s(p); | ||
if (u === "Text") | ||
@@ -134,10 +134,10 @@ return e.getText(p); | ||
} | ||
const H = async (t, a, r, e = D) => { | ||
const H = async (t, a, s, e = D) => { | ||
const o = (await S(e, A, { | ||
upgrade: U | ||
upgrade: j | ||
})).transaction("milestone", "readwrite").objectStore("milestone"), d = await o.get("id"), c = m(a); | ||
d ? (d.milestone[r] = c, await o.put(d)) : await o.put({ | ||
d ? (d.milestone[s] = c, await o.put(d)) : await o.put({ | ||
id: t, | ||
milestone: { | ||
[r]: c | ||
[s]: c | ||
} | ||
@@ -147,28 +147,28 @@ }); | ||
const n = await (await S(a, A, { | ||
upgrade: U | ||
upgrade: j | ||
})).transaction("milestone", "readonly").objectStore("milestone").get(t); | ||
return n ? n.milestone : null; | ||
}, K = (t, a, r = D) => { | ||
}, K = (t, a, s = D) => { | ||
let e, i, n = !0, o = !1; | ||
async function d(f, y) { | ||
const g = await c; | ||
if (!o || y === h) | ||
async function d(f, g) { | ||
const y = await c; | ||
if (!o || g === h) | ||
return; | ||
const w = g.transaction("workspace", "readwrite").objectStore("workspace"); | ||
let s = await w.get(t); | ||
if (s || (s = { | ||
const w = y.transaction("workspace", "readwrite").objectStore("workspace"); | ||
let r = await w.get(t); | ||
if (r || (r = { | ||
id: t, | ||
updates: [] | ||
}), s.updates.push({ | ||
}), r.updates.push({ | ||
timestamp: Date.now(), | ||
update: f | ||
}), s.updates.length > T) { | ||
const j = s.updates.map(({ update: P }) => P), l = new M(); | ||
}), r.updates.length > T) { | ||
const P = r.updates.map(({ update: U }) => U), l = new M(); | ||
l.transact(() => { | ||
j.forEach((P) => { | ||
b(l, P, h); | ||
P.forEach((U) => { | ||
b(l, U, h); | ||
}); | ||
}, h); | ||
const V = m(l); | ||
s = { | ||
r = { | ||
id: t, | ||
@@ -181,8 +181,8 @@ updates: [ | ||
] | ||
}, await v(w.put(s)); | ||
}, await v(w.put(r)); | ||
} else | ||
await v(w.put(s)); | ||
await v(w.put(r)); | ||
} | ||
const c = S(r, A, { | ||
upgrade: U | ||
const c = S(s, A, { | ||
upgrade: j | ||
}), p = async () => { | ||
@@ -192,10 +192,10 @@ o = !0, (await c).close(); | ||
connect: async () => { | ||
u.whenSynced = new Promise((w, s) => { | ||
n = !0, e = w, i = s; | ||
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, r); | ||
const y = f.transaction("workspace", "readwrite").objectStore("workspace"), g = await y.get(t); | ||
await _(f, t, s); | ||
const g = f.transaction("workspace", "readwrite").objectStore("workspace"), y = await g.get(t); | ||
if (o) { | ||
if (!g) | ||
if (!y) | ||
await v( | ||
@@ -213,20 +213,20 @@ f.put("workspace", { | ||
else { | ||
const w = g.updates.map(({ update: l }) => l), s = new M(); | ||
s.transact(() => { | ||
const w = y.updates.map(({ update: l }) => l), r = new M(); | ||
r.transact(() => { | ||
w.forEach((l) => { | ||
b(s, l); | ||
b(r, l); | ||
}); | ||
}, h); | ||
const j = I( | ||
const P = I( | ||
m(a), | ||
m(s) | ||
m(r) | ||
); | ||
await v( | ||
y.put({ | ||
...g, | ||
g.put({ | ||
...y, | ||
updates: [ | ||
...g.updates, | ||
...y.updates, | ||
{ | ||
timestamp: Date.now(), | ||
update: j | ||
update: P | ||
} | ||
@@ -264,5 +264,5 @@ ] | ||
_ as tryMigrate, | ||
U as upgradeDB, | ||
j as upgradeDB, | ||
v as writeOperation | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -9,2 +9,2 @@ import type { IDBPDatabase } from 'idb/build/entry'; | ||
export declare function tryMigrate(db: IDBPDatabase<BlockSuiteBinaryDB>, id: string, dbName?: string): Promise<void>; | ||
export declare function downloadBinary(id: string, dbName?: string): Promise<UpdateMessage['update']>; | ||
export declare function downloadBinary(id: string, dbName?: string): Promise<UpdateMessage['update'] | false>; |
{ | ||
"name": "@toeverything/y-indexeddb", | ||
"type": "module", | ||
"version": "0.5.4-canary.20", | ||
"version": "0.5.4-canary.21", | ||
"scripts": { | ||
@@ -35,4 +35,4 @@ "build": "vite build" | ||
"devDependencies": { | ||
"@blocksuite/blocks": "0.0.0-20230501021333-90d9a4af-nightly", | ||
"@blocksuite/store": "0.0.0-20230501021333-90d9a4af-nightly", | ||
"@blocksuite/blocks": "0.0.0-20230502232311-bfe52748-nightly", | ||
"@blocksuite/store": "0.0.0-20230502232311-bfe52748-nightly", | ||
"vite": "^4.3.4", | ||
@@ -39,0 +39,0 @@ "vite-plugin-dts": "^2.3.0", |
# @toeverything/y-indexeddb | ||
> This package haven't been published yet. | ||
## Usage | ||
```ts | ||
import { createIndexedDBProvider } from '@toeverything/y-indexeddb'; | ||
import { createIndexedDBProvider, downloadBinary } from '@toeverything/y-indexeddb'; | ||
import * as Y from 'yjs'; | ||
const yDoc = new Y.Doc(); | ||
// sync yDoc with indexedDB | ||
const provider = createIndexedDBProvider('docName', yDoc); | ||
@@ -18,2 +17,9 @@ provider.connect(); | ||
}); | ||
// dowload binary data from indexedDB for once | ||
downloadBinary('docName').then(blob => { | ||
if (blob !== false) { | ||
Y.applyUpdate(yDoc, blob); | ||
} | ||
}); | ||
``` |
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
56275
25