@@ -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