Socket
Book a DemoInstallSign in
Socket

@automerge/automerge-repo-react-hooks

Package Overview
Dependencies
Maintainers
4
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@automerge/automerge-repo-react-hooks - npm Package Compare versions

Comparing version

to
2.0.1

test/testSetup.tsx

293

dist/index.js

@@ -1,5 +0,5 @@

import q, { createContext as I, useContext as j, useRef as $, useState as x, useEffect as _, useCallback as S } from "react";
function O(s) {
let l = "pending", r, f;
const h = s.then(
import q, { createContext as I, useContext as j, useRef as $, useState as b, useEffect as y, useCallback as S } from "react";
function O(o) {
let l = "pending", r, i;
const h = o.then(
(p) => {

@@ -9,7 +9,7 @@ l = "success", r = p;

(p) => {
l = "error", f = p;
l = "error", i = p;
}
);
return {
promise: s,
promise: o,
read() {

@@ -20,3 +20,3 @@ switch (l) {

case "error":
throw f;
throw i;
case "success":

@@ -30,21 +30,21 @@ return r;

function k() {
const s = j(F);
if (!s) throw new Error("Repo was not found on RepoContext.");
return s;
const o = j(F);
if (!o) throw new Error("Repo was not found on RepoContext.");
return o;
}
const E = /* @__PURE__ */ new Map();
function N(s, { suspense: l } = { suspense: !1 }) {
const r = k(), f = $(), [h, p] = x();
const x = /* @__PURE__ */ new Map();
function N(o, { suspense: l } = { suspense: !1 }) {
const r = k(), i = $(), [h, p] = b();
let u = h;
if (s && !u) {
const t = r.findWithProgress(s);
if (o && !u) {
const t = r.findWithProgress(o);
t.state === "ready" && (u = t.handle);
}
let n = s ? E.get(s) : void 0;
if (!n && s) {
f.current?.abort(), f.current = new AbortController();
const t = r.find(s, { signal: f.current.signal });
n = O(t), E.set(s, n);
let n = o ? x.get(o) : void 0;
if (!n && o) {
i.current?.abort(), i.current = new AbortController();
const t = r.find(o, { signal: i.current.signal });
n = O(t), x.set(o, n);
}
return _(() => {
return y(() => {
l || !n || n.promise.then((t) => {

@@ -57,11 +57,11 @@ p(t);

}
function Q(s, l = { suspense: !1 }) {
const r = N(s, l), [f, h] = x(() => r?.doc()), [p, u] = x();
_(() => {
function Q(o, l = { suspense: !1 }) {
const r = N(o, l), [i, h] = b(() => r?.doc()), [p, u] = b();
y(() => {
h(r?.doc());
}, [r]), _(() => {
}, [r]), y(() => {
if (!r)
return;
const t = () => h(r.doc()), e = () => {
u(new Error(`Document ${s} was deleted`));
u(new Error(`Document ${o} was deleted`));
};

@@ -71,3 +71,3 @@ return r.on("change", t), r.on("delete", e), () => {

};
}, [r, s]);
}, [r, o]);
const n = S(

@@ -81,24 +81,35 @@ (t, e) => {

throw p;
return f ? [f, n] : [void 0, () => {
return i ? [i, n] : [void 0, () => {
}];
}
function T(s, { suspense: l = !1 } = {}) {
const r = k(), [f, h] = x(() => /* @__PURE__ */ new Map()), p = [], u = /* @__PURE__ */ new Map();
for (const n of s) {
let t = E.get(n);
if (!t)
function W(o, { suspense: l = !1 } = {}) {
const r = k(), [i, h] = b(() => {
const n = /* @__PURE__ */ new Map();
for (const t of o) {
let e;
try {
const e = r.find(n);
t = O(e), E.set(n, t);
e = r.findWithProgress(t);
} catch {
continue;
}
e.state === "ready" && n.set(t, e.handle);
}
return n;
}), p = [], u = /* @__PURE__ */ new Map();
for (const n of o) {
let t = i.get(n), e = x.get(n);
if (!e)
try {
const s = r.find(n);
e = O(s), x.set(n, e);
} catch {
continue;
}
try {
const e = t.read();
u.set(n, e);
} catch (e) {
e instanceof Promise ? p.push(t) : u.set(n, void 0);
t ??= e.read(), u.set(n, t);
} catch (s) {
s instanceof Promise ? p.push(e) : u.set(n, void 0);
}
}
if (_(() => {
if (y(() => {
p.length > 0 ? Promise.allSettled(p.map((n) => n.promise)).then(

@@ -114,20 +125,26 @@ (n) => {

) : h(u);
}, [l, s]), l && p.length > 0)
}, [l, o]), l && p.length > 0)
throw Promise.all(p.map((n) => n.promise));
return f;
return i;
}
function V(s, { suspense: l = !0 } = {}) {
const r = T(s, { suspense: l }), [f, h] = x(() => /* @__PURE__ */ new Map());
_(() => {
function V(o, { suspense: l = !0 } = {}) {
const r = W(o, { suspense: l }), [i, h] = b(() => {
const u = /* @__PURE__ */ new Map();
return r.forEach((n) => {
const t = n?.url;
t && u.set(t, n?.doc());
}), u;
});
y(() => {
const u = /* @__PURE__ */ new Map();
return r.forEach((n, t) => {
if (n) {
const e = () => {
h((o) => {
const c = new Map(o);
h((s) => {
const c = new Map(s);
return c.set(t, n.doc()), c;
});
};
h((o) => {
const c = new Map(o);
h((s) => {
const c = new Map(s);
return c.set(t, n.doc()), c;

@@ -155,15 +172,15 @@ }), n.on("change", e), u.set(t, e);

);
return [f, p];
return [i, p];
}
function H(s) {
return s && s.__esModule && Object.prototype.hasOwnProperty.call(s, "default") ? s.default : s;
function H(o) {
return o && o.__esModule && Object.prototype.hasOwnProperty.call(o, "default") ? o.default : o;
}
var C, R;
function W() {
if (R) return C;
R = 1;
var s = q, l = function(f) {
return typeof f == "function";
}, r = function(f) {
var h = s.useState(f), p = h[0], u = h[1], n = s.useRef(p), t = s.useCallback(function(e) {
var C, P;
function T() {
if (P) return C;
P = 1;
var o = q, l = function(i) {
return typeof i == "function";
}, r = function(i) {
var h = o.useState(i), p = h[0], u = h[1], n = o.useRef(p), t = o.useCallback(function(e) {
n.current = l(e) ? e(n.current) : e, u(n.current);

@@ -175,46 +192,46 @@ }, []);

}
var z = W();
var z = T();
const L = /* @__PURE__ */ H(z);
var D = { exports: {} }, P;
var D = { exports: {} }, R;
function B() {
return P || (P = 1, function(s) {
return R || (R = 1, function(o) {
var l = Object.prototype.hasOwnProperty, r = "~";
function f() {
function i() {
}
Object.create && (f.prototype = /* @__PURE__ */ Object.create(null), new f().__proto__ || (r = !1));
function h(t, e, o) {
this.fn = t, this.context = e, this.once = o || !1;
Object.create && (i.prototype = /* @__PURE__ */ Object.create(null), new i().__proto__ || (r = !1));
function h(t, e, s) {
this.fn = t, this.context = e, this.once = s || !1;
}
function p(t, e, o, c, w) {
if (typeof o != "function")
function p(t, e, s, c, w) {
if (typeof s != "function")
throw new TypeError("The listener must be a function");
var v = new h(o, c || t, w), i = r ? r + e : e;
return t._events[i] ? t._events[i].fn ? t._events[i] = [t._events[i], v] : t._events[i].push(v) : (t._events[i] = v, t._eventsCount++), t;
var v = new h(s, c || t, w), f = r ? r + e : e;
return t._events[f] ? t._events[f].fn ? t._events[f] = [t._events[f], v] : t._events[f].push(v) : (t._events[f] = v, t._eventsCount++), t;
}
function u(t, e) {
--t._eventsCount === 0 ? t._events = new f() : delete t._events[e];
--t._eventsCount === 0 ? t._events = new i() : delete t._events[e];
}
function n() {
this._events = new f(), this._eventsCount = 0;
this._events = new i(), this._eventsCount = 0;
}
n.prototype.eventNames = function() {
var e = [], o, c;
var e = [], s, c;
if (this._eventsCount === 0) return e;
for (c in o = this._events)
l.call(o, c) && e.push(r ? c.slice(1) : c);
return Object.getOwnPropertySymbols ? e.concat(Object.getOwnPropertySymbols(o)) : e;
for (c in s = this._events)
l.call(s, c) && e.push(r ? c.slice(1) : c);
return Object.getOwnPropertySymbols ? e.concat(Object.getOwnPropertySymbols(s)) : e;
}, n.prototype.listeners = function(e) {
var o = r ? r + e : e, c = this._events[o];
var s = r ? r + e : e, c = this._events[s];
if (!c) return [];
if (c.fn) return [c.fn];
for (var w = 0, v = c.length, i = new Array(v); w < v; w++)
i[w] = c[w].fn;
return i;
for (var w = 0, v = c.length, f = new Array(v); w < v; w++)
f[w] = c[w].fn;
return f;
}, n.prototype.listenerCount = function(e) {
var o = r ? r + e : e, c = this._events[o];
var s = r ? r + e : e, c = this._events[s];
return c ? c.fn ? 1 : c.length : 0;
}, n.prototype.emit = function(e, o, c, w, v, i) {
}, n.prototype.emit = function(e, s, c, w, v, f) {
var m = r ? r + e : e;
if (!this._events[m]) return !1;
var a = this._events[m], g = arguments.length, y, d;
var a = this._events[m], g = arguments.length, _, d;
if (a.fn) {

@@ -225,17 +242,17 @@ switch (a.once && this.removeListener(e, a.fn, void 0, !0), g) {

case 2:
return a.fn.call(a.context, o), !0;
return a.fn.call(a.context, s), !0;
case 3:
return a.fn.call(a.context, o, c), !0;
return a.fn.call(a.context, s, c), !0;
case 4:
return a.fn.call(a.context, o, c, w), !0;
return a.fn.call(a.context, s, c, w), !0;
case 5:
return a.fn.call(a.context, o, c, w, v), !0;
return a.fn.call(a.context, s, c, w, v), !0;
case 6:
return a.fn.call(a.context, o, c, w, v, i), !0;
return a.fn.call(a.context, s, c, w, v, f), !0;
}
for (d = 1, y = new Array(g - 1); d < g; d++)
y[d - 1] = arguments[d];
a.fn.apply(a.context, y);
for (d = 1, _ = new Array(g - 1); d < g; d++)
_[d - 1] = arguments[d];
a.fn.apply(a.context, _);
} else {
var A = a.length, b;
var A = a.length, E;
for (d = 0; d < A; d++)

@@ -247,32 +264,32 @@ switch (a[d].once && this.removeListener(e, a[d].fn, void 0, !0), g) {

case 2:
a[d].fn.call(a[d].context, o);
a[d].fn.call(a[d].context, s);
break;
case 3:
a[d].fn.call(a[d].context, o, c);
a[d].fn.call(a[d].context, s, c);
break;
case 4:
a[d].fn.call(a[d].context, o, c, w);
a[d].fn.call(a[d].context, s, c, w);
break;
default:
if (!y) for (b = 1, y = new Array(g - 1); b < g; b++)
y[b - 1] = arguments[b];
a[d].fn.apply(a[d].context, y);
if (!_) for (E = 1, _ = new Array(g - 1); E < g; E++)
_[E - 1] = arguments[E];
a[d].fn.apply(a[d].context, _);
}
}
return !0;
}, n.prototype.on = function(e, o, c) {
return p(this, e, o, c, !1);
}, n.prototype.once = function(e, o, c) {
return p(this, e, o, c, !0);
}, n.prototype.removeListener = function(e, o, c, w) {
}, n.prototype.on = function(e, s, c) {
return p(this, e, s, c, !1);
}, n.prototype.once = function(e, s, c) {
return p(this, e, s, c, !0);
}, n.prototype.removeListener = function(e, s, c, w) {
var v = r ? r + e : e;
if (!this._events[v]) return this;
if (!o)
if (!s)
return u(this, v), this;
var i = this._events[v];
if (i.fn)
i.fn === o && (!w || i.once) && (!c || i.context === c) && u(this, v);
var f = this._events[v];
if (f.fn)
f.fn === s && (!w || f.once) && (!c || f.context === c) && u(this, v);
else {
for (var m = 0, a = [], g = i.length; m < g; m++)
(i[m].fn !== o || w && !i[m].once || c && i[m].context !== c) && a.push(i[m]);
for (var m = 0, a = [], g = f.length; m < g; m++)
(f[m].fn !== s || w && !f[m].once || c && f[m].context !== c) && a.push(f[m]);
a.length ? this._events[v] = a.length === 1 ? a[0] : a : u(this, v);

@@ -282,5 +299,5 @@ }

}, n.prototype.removeAllListeners = function(e) {
var o;
return e ? (o = r ? r + e : e, this._events[o] && u(this, o)) : (this._events = new f(), this._eventsCount = 0), this;
}, n.prototype.off = n.prototype.removeListener, n.prototype.addListener = n.prototype.on, n.prefixed = r, n.EventEmitter = n, s.exports = n;
var s;
return e ? (s = r ? r + e : e, this._events[s] && u(this, s)) : (this._events = new i(), this._eventsCount = 0), this;
}, n.prototype.off = n.prototype.removeListener, n.prototype.addListener = n.prototype.on, n.prefixed = r, n.EventEmitter = n, o.exports = n;
}(D)), D.exports;

@@ -290,25 +307,25 @@ }

const J = /* @__PURE__ */ H(G), M = new J(), X = ({
handle: s,
handle: o,
localUserId: l,
offlineTimeout: r = 3e4,
getTime: f = () => (/* @__PURE__ */ new Date()).getTime()
getTime: i = () => (/* @__PURE__ */ new Date()).getTime()
}) => {
const [h, p, u] = L({}), [n, t, e] = L({});
return _(() => {
const o = (v) => {
const [i, m] = v.message;
i !== l && (e.current[i] || M.emit("new_peer", v), p({
return y(() => {
const s = (v) => {
const [f, m] = v.message;
f !== l && (e.current[f] || M.emit("new_peer", v), p({
...u.current,
[i]: m
[f]: m
}), t({
...e.current,
[i]: f()
[f]: i()
}));
}, c = () => {
const v = u.current, i = e.current, m = f();
for (const a in i)
m - i[a] > r && (delete v[a], delete i[a]);
p(v), t(i);
const v = u.current, f = e.current, m = i();
for (const a in f)
m - f[a] > r && (delete v[a], delete f[a]);
p(v), t(f);
};
s.on("ephemeral-message", o);
o.on("ephemeral-message", s);
const w = setInterval(

@@ -319,27 +336,27 @@ c,

return () => {
s.removeListener("ephemeral-message", o), clearInterval(w);
o.removeListener("ephemeral-message", s), clearInterval(w);
};
}, [s, l, r, f]), [h, n];
}, [o, l, r, i]), [h, n];
}, Y = ({
handle: s,
handle: o,
userId: l,
initialState: r,
heartbeatTime: f = 15e3
heartbeatTime: i = 15e3
}) => {
const [h, p, u] = L(r), n = (t) => {
const e = typeof t == "function" ? t(u.current) : t;
p(e), s.broadcast([l, e]);
p(e), o.broadcast([l, e]);
};
return _(() => {
return y(() => {
if (!l)
return;
const t = () => void s.broadcast([l, u.current]);
const t = () => void o.broadcast([l, u.current]);
t();
const e = setInterval(t, f);
const e = setInterval(t, i);
return () => void clearInterval(e);
}, [s, l, f]), _(() => {
}, [o, l, i]), y(() => {
let t;
const e = M.on("new_peer", () => {
t = setTimeout(
() => s.broadcast([l, u.current]),
() => o.broadcast([l, u.current]),
500

@@ -352,3 +369,3 @@ // Wait for the peer to be ready

};
}, [s, l, M]), [h, n];
}, [o, l, M]), [h, n];
};

@@ -358,3 +375,3 @@ export {

N as useDocHandle,
T as useDocHandles,
W as useDocHandles,
Q as useDocument,

@@ -361,0 +378,0 @@ V as useDocuments,

{
"name": "@automerge/automerge-repo-react-hooks",
"version": "2.0.0",
"version": "2.0.1",
"description": "Hooks to access an Automerge Repo from your react app.",

@@ -18,3 +18,3 @@ "repository": "https://github.com/automerge/automerge-repo/tree/master/packages/automerge-repo-react-hooks",

"@automerge/automerge": "^2.2.8",
"@automerge/automerge-repo": "2.0.0",
"@automerge/automerge-repo": "2.0.1",
"eventemitter3": "^5.0.1",

@@ -50,3 +50,3 @@ "react-usestateref": "^1.0.8"

},
"gitHead": "670d9e0d9d52451453e60a6b64e74c146f1f959c"
"gitHead": "6e8d13f3aebcbc31ff3dd478be85191eee1ad120"
}

@@ -18,4 +18,22 @@ import { AutomergeUrl, DocHandle } from "@automerge/automerge-repo/slim"

const repo = useRepo()
const [handleMap, setHandleMap] = useState<DocHandleMap<T>>(() => new Map())
const [handleMap, setHandleMap] = useState<DocHandleMap<T>>(() => {
const map = new Map()
// Initialize the map with any handles that are ready
for (const id of ids) {
let progress
try {
progress = repo.findWithProgress<T>(id)
} catch (e) {
continue
}
if (progress.state === "ready") {
map.set(id, progress.handle)
}
}
return map
})
const pendingPromises: PromiseWrapper<DocHandle<T>>[] = []

@@ -26,2 +44,3 @@ const nextHandleMap = new Map<AutomergeUrl, DocHandle<T> | undefined>()

for (const id of ids) {
let handle = handleMap.get(id)
let wrapper = wrapperCache.get(id)

@@ -42,3 +61,3 @@ if (!wrapper) {

try {
const handle = wrapper.read() as DocHandle<T>
handle ??= wrapper.read() as DocHandle<T>
nextHandleMap.set(id, handle)

@@ -45,0 +64,0 @@ } catch (e) {

@@ -22,3 +22,12 @@ import { AutomergeUrl } from "@automerge/automerge-repo/slim"

const handleMap = useDocHandles<T>(ids, { suspense })
const [docMap, setDocMap] = useState<DocMap<T>>(() => new Map())
const [docMap, setDocMap] = useState<DocMap<T>>(() => {
const docs: DocMap<T> = new Map()
handleMap.forEach(handle => {
const url = handle?.url
if (url) {
docs.set(url, handle?.doc())
}
})
return docs
})

@@ -25,0 +34,0 @@ useEffect(() => {

@@ -7,3 +7,3 @@ import { defineConfig } from "vitest/config"

globals: true,
setupFiles: [path.join(__dirname, "./test/testSetup.ts")],
setupFiles: [path.join(__dirname, "./test/testSetup.tsx")],
environment: "jsdom",

@@ -10,0 +10,0 @@ coverage: {

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

Sorry, the diff of this file is not supported yet