tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
290
fork
atom
a tool for shared writing and social publishing
290
fork
atom
overview
issues
27
pulls
pipelines
some random small tweaks
awarm.space
2 years ago
73b3e836
248ec3a7
+25
-22
5 changed files
expand all
collapse all
unified
split
app
[doc_id]
Blocks.tsx
replicache
clientMutationContext.ts
index.tsx
mutations.ts
serverMutationContext.ts
+1
-1
app/[doc_id]/Blocks.tsx
···
9
9
});
10
10
return (
11
11
<button
12
12
-
onClick={() => {
12
12
+
onMouseDown={() => {
13
13
rep?.rep?.mutate.addBlock({
14
14
parent: props.entityID,
15
15
position: generateKeyBetween(null, blocks[0]?.data.position || null),
+1
-1
replicache/clientMutationContext.ts
···
41
41
(existingFact[0]?.data as Fact<typeof f.attribute>["data"]).value,
42
42
);
43
43
const newUpdate = base64.toByteArray(f.data.value);
44
44
-
const updateBytes = Y.mergeUpdatesV2([oldUpdate, newUpdate]);
44
44
+
const updateBytes = Y.mergeUpdates([oldUpdate, newUpdate]);
45
45
data.value = base64.fromByteArray(updateBytes);
46
46
}
47
47
}
+22
-18
replicache/index.tsx
···
1
1
"use client";
2
2
-
import { createContext, useContext, useEffect, useState } from "react";
2
2
+
import { createContext, useContext, useEffect, useMemo, useState } from "react";
3
3
+
import { useSubscribe } from "replicache-react";
3
4
import { DeepReadonlyObject, Replicache, WriteTransaction } from "replicache";
4
5
import { Pull } from "./pull";
5
6
import { mutations } from "./mutations";
···
116
117
attribute: A,
117
118
): CardinalityResult<A> {
118
119
let { rep, initialFacts } = useReplicache();
119
119
-
let fallbackData = initialFacts.filter(
120
120
-
(f) => f.entity === entity && f.attribute === attribute,
120
120
+
let fallbackData = useMemo(
121
121
+
() =>
122
122
+
initialFacts.filter(
123
123
+
(f) => f.entity === entity && f.attribute === attribute,
124
124
+
),
125
125
+
[entity, attribute, initialFacts],
121
126
);
122
122
-
let [data, setData] = useState<DeepReadonlyObject<Fact<A>[]>>(
123
123
-
fallbackData as DeepReadonlyObject<Fact<A>>[],
127
127
+
let data = useSubscribe(
128
128
+
rep,
129
129
+
(tx) => {
130
130
+
return tx
131
131
+
.scan<Fact<A>>({ indexName: "eav", prefix: `${entity}-${attribute}` })
132
132
+
.toArray();
133
133
+
},
134
134
+
{
135
135
+
default: null,
136
136
+
dependencies: [entity, attribute],
137
137
+
},
124
138
);
125
125
-
useEffect(() => {
126
126
-
if (!rep) return;
127
127
-
return rep.subscribe(
128
128
-
(tx) => {
129
129
-
return tx
130
130
-
.scan<Fact<A>>({ indexName: "eav", prefix: `${entity}-${attribute}` })
131
131
-
.toArray();
132
132
-
},
133
133
-
{ onData: setData },
134
134
-
);
135
135
-
}, [entity, attribute, rep]);
139
139
+
let d = data || (attribute === "card/block" ? fallbackData : []);
136
140
return Attributes[attribute].cardinality === "many"
137
137
-
? (data as CardinalityResult<A>)
138
138
-
: (data[0] as CardinalityResult<A>);
141
141
+
? (d as CardinalityResult<A>)
142
142
+
: (d[0] as CardinalityResult<A>);
139
143
}
-1
replicache/mutations.ts
···
36
36
};
37
37
38
38
const removeBlock: Mutation<{ blockEntity: string }> = async (args, ctx) => {
39
39
-
console.log(args);
40
39
await ctx.deleteEntity(args.blockEntity);
41
40
};
42
41
+1
-1
replicache/serverMutationContext.ts
···
61
61
(existingFact[0]?.data as Fact<typeof f.attribute>["data"]).value,
62
62
);
63
63
const newUpdate = base64.toByteArray(f.data.value);
64
64
-
const updateBytes = Y.mergeUpdatesV2([oldUpdate, newUpdate]);
64
64
+
const updateBytes = Y.mergeUpdates([oldUpdate, newUpdate]);
65
65
data.value = base64.fromByteArray(updateBytes);
66
66
}
67
67
}