Skip to content

Commit 08075cc

Browse files
committed
[core] SyncletImplementations
1 parent 564f4d0 commit 08075cc

2 files changed

Lines changed: 73 additions & 12 deletions

File tree

src/@types/index.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,26 @@ export type SyncletImplementations<Depth extends number> = {
102102
onStop?: () => Promise<void>;
103103
onSync?: (address: AnyAddress<Depth>) => Promise<void>;
104104
onSetAtom?: (address: AtomAddress<Depth>) => Promise<void>;
105-
canReceiveMessage?: (context: Context) => Promise<boolean>;
106105
getSendContext?: (receivedContext?: Context) => Promise<Context>;
106+
canReceiveMessage?: (context: Context) => Promise<boolean>;
107+
canReadAtom?: (
108+
address: AtomAddress<Depth>,
109+
context: Context,
110+
) => Promise<boolean>;
111+
canWriteAtom?: (
112+
address: AtomAddress<Depth>,
113+
atom: Atom,
114+
context: Context,
115+
) => Promise<boolean>;
116+
canRemoveAtom?: (
117+
address: AtomAddress<Depth>,
118+
context: Context,
119+
) => Promise<boolean>;
120+
filterChildIds?: (
121+
address: AnyParentAddress<Depth>,
122+
childIds: string[],
123+
context: Context,
124+
) => Promise<string[]>;
107125
};
108126

109127
/// SyncletOptions

src/core/synclet.ts

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ export const createSynclet = async <
9494
onStop,
9595
onSync,
9696
onSetAtom,
97-
canReceiveMessage,
9897
getSendContext,
98+
canReceiveMessage,
99+
canReadAtom,
100+
canWriteAtom,
101+
canRemoveAtom,
102+
filterChildIds,
99103
}: SyncletImplementations<Depth> = {},
100104
options: SyncletOptions = {},
101105
): Promise<ProtectedSynclet<Depth>> => {
@@ -153,6 +157,45 @@ export const createSynclet = async <
153157
): address is AtomsAddress<Depth> & TimestampsAddress<Depth> =>
154158
size(address) == dataDepth - 1;
155159

160+
const readAtom = isUndefined(canReadAtom)
161+
? dataReadAtom
162+
: async (address: AtomAddress<Depth>, context: Context = {}) =>
163+
(await canReadAtom(address, context))
164+
? await dataReadAtom(address, context)
165+
: undefined;
166+
167+
const writeAtom = isUndefined(canWriteAtom)
168+
? dataWriteAtom
169+
: async (address: AtomAddress<Depth>, atom: Atom, context: Context = {}) =>
170+
(await canWriteAtom(address, atom, context))
171+
? await dataWriteAtom(address, atom, context)
172+
: undefined;
173+
174+
const removeAtom = isUndefined(canRemoveAtom)
175+
? dataRemoveAtom
176+
: async (address: AtomAddress<Depth>, context: Context = {}) =>
177+
(await canRemoveAtom(address, context))
178+
? await dataRemoveAtom(address, context)
179+
: undefined;
180+
181+
const readDataChildIds = isUndefined(filterChildIds)
182+
? dataReadChildIds
183+
: async (address: AnyParentAddress<Depth>, context: Context = {}) =>
184+
await filterChildIds(
185+
address,
186+
(await dataReadChildIds(address, context)) ?? [],
187+
context,
188+
);
189+
190+
const readMetaChildIds = isUndefined(filterChildIds)
191+
? metaReadChildIds
192+
: async (address: AnyParentAddress<Depth>, context: Context = {}) =>
193+
await filterChildIds(
194+
address,
195+
(await metaReadChildIds(address, context)) ?? [],
196+
context,
197+
);
198+
156199
const setOrDelAtom = async (
157200
address: AtomAddress<Depth>,
158201
atomOrUndefined: Atom | undefined,
@@ -173,8 +216,8 @@ export const createSynclet = async <
173216
const tasks = [
174217
async () => {
175218
await (isUndefined(atomOrUndefined)
176-
? dataRemoveAtom(address, context)
177-
: dataWriteAtom(address, atomOrUndefined, context));
219+
? removeAtom(address, context)
220+
: writeAtom(address, atomOrUndefined, context));
178221
await metaWriteTimestamp(address, newTimestamp, context);
179222
await onSetAtom?.(address);
180223
},
@@ -193,7 +236,7 @@ export const createSynclet = async <
193236
? ((await dataReadAtoms(address, {})) ?? {})
194237
: objFromEntries(
195238
await promiseAll(
196-
arrayMap(await dataReadChildIds(address, {}), async (childId) => [
239+
arrayMap(await readDataChildIds(address, {}), async (childId) => [
197240
childId,
198241
await getDataForAddress([
199242
...(address as Address),
@@ -211,7 +254,7 @@ export const createSynclet = async <
211254
? ((await metaReadTimestamps(address, {})) ?? {})
212255
: objFromEntries(
213256
await promiseAll(
214-
arrayMap(await metaReadChildIds(address, {}), async (childId) => [
257+
arrayMap(await readMetaChildIds(address, {}), async (childId) => [
215258
childId,
216259
await getMetaForAddress([
217260
...(address as Address),
@@ -228,7 +271,7 @@ export const createSynclet = async <
228271
arrayReduce(
229272
await promiseAll(
230273
arrayMap(
231-
(await metaReadChildIds(address, context)) ?? [],
274+
(await readMetaChildIds(address, context)) ?? [],
232275
async (childId) =>
233276
isAtomsOrTimestampsParentAddress(address)
234277
? getHash(
@@ -267,7 +310,7 @@ export const createSynclet = async <
267310
}
268311
const timestamp = await metaReadTimestamp(address, context);
269312
if (!isUndefined(timestamp)) {
270-
return [timestamp, await dataReadAtom(address, context)];
313+
return [timestamp, await readAtom(address, context)];
271314
}
272315
};
273316

@@ -280,7 +323,7 @@ export const createSynclet = async <
280323
await promiseAll(
281324
arrayMap(
282325
arrayDifference(
283-
(await metaReadChildIds(address, context)) ?? [],
326+
(await readMetaChildIds(address, context)) ?? [],
284327
except,
285328
),
286329
async (id) =>
@@ -413,7 +456,7 @@ export const createSynclet = async <
413456
if (otherTimestamp > myTimestamp) {
414457
return myTimestamp;
415458
} else if (otherTimestamp < myTimestamp) {
416-
return [myTimestamp, await dataReadAtom(address, context)];
459+
return [myTimestamp, await readAtom(address, context)];
417460
}
418461
} else {
419462
log(INVALID_NODE + ' Timestamp vs SubNodes: ' + address, WARN);
@@ -440,7 +483,7 @@ export const createSynclet = async <
440483
myTimestamp,
441484
);
442485
} else if (otherTimestamp < myTimestamp) {
443-
return [myTimestamp, await dataReadAtom(address, context)];
486+
return [myTimestamp, await readAtom(address, context)];
444487
}
445488
} else {
446489
log(INVALID_NODE + ' TimestampAtom vs SubNodes: ' + address, WARN);
@@ -458,7 +501,7 @@ export const createSynclet = async <
458501
const subNodeObj: {[id: string]: MessageNode} = {};
459502
await promiseAll(
460503
arrayMap(
461-
(await metaReadChildIds(address, context)) ?? [],
504+
(await readMetaChildIds(address, context)) ?? [],
462505
async (id) => {
463506
subNodeObj[id] = await readHashOrTimestamp(
464507
[...(address as Address), id] as AnyAddress<Depth>,

0 commit comments

Comments
 (0)