MCPcopy Create free account
hub / github.com/effect-app/libs / makeMemoryStoreInt

Function makeMemoryStoreInt

packages/infra/src/Store/Memory.ts:330–512  ·  view source on GitHub ↗
(
  modelName: string,
  idKey: IdKey,
  namespace: string,
  seed?: Effect.Effect<Iterable<Encoded>, E, R>,
  _defaultValues?: Partial<Encoded>
)

Source from the content-addressed store, hash-verified

328}
329
330export function makeMemoryStoreInt<IdKey extends keyof Encoded, Encoded extends FieldValues, R = never, E = never>(
331 modelName: string,
332 idKey: IdKey,
333 namespace: string,
334 seed?: Effect.Effect<Iterable<Encoded>, E, R>,
335 _defaultValues?: Partial<Encoded>
336) {
337 type PM = PersistenceModelType<Encoded>
338 return Effect.gen(function*() {
339 const updateETag = makeUpdateETag(modelName)
340 const items_ = yield* seed ?? Effect.sync(() => [])
341 const defaultValues = _defaultValues ?? {}
342
343 const items = new Map([...items_].map((_) => [_[idKey], { _etag: undefined, ...defaultValues, ..._ }] as const))
344 const store = Ref.makeUnsafe<ReadonlyMap<Encoded[IdKey], PM>>(items)
345 const sem = Semaphore.makeUnsafe(1)
346 const withPermit = sem.withPermits(1)
347 const values = Effect.map(Ref.get(store), (s) => s.values())
348
349 const all = Effect.map(values, Array.fromIterable)
350
351 const batchSet = (items: NonEmptyReadonlyArray<PM>) =>
352 Effect
353 .forEach(items, (i) => Effect.flatMap(s.find(i[idKey]), (current) => updateETag(i, idKey, current)))
354 .pipe(
355 Effect
356 .tap((items) =>
357 Ref
358 .get(store)
359 .pipe(
360 Effect
361 .map((m) => {
362 const mut = m as Map<Encoded[IdKey], PM>
363 items.forEach((e) => mut.set(e[idKey], e))
364 return mut
365 }),
366 Effect
367 .flatMap((_) => Ref.set(store, _))
368 )
369 ),
370 Effect
371 .map((_) => _),
372 withPermit
373 )
374
375 const batchRemove = (items: NonEmptyReadonlyArray<Encoded[IdKey]>) =>
376 Ref
377 .get(store)
378 .pipe(
379 Effect
380 .map((m) => {
381 return new Map([...m].filter(([_k]) => !items.includes(_k)))
382 }),
383 Effect
384 .flatMap((_) => Ref.set(store, _))
385 )
386 .pipe(
387 withPermit

Callers 3

makeDiskStoreIntFunction · 0.90
makeMemoryStoreFunction · 0.85
ensureStoreFunction · 0.85

Calls 11

makeUpdateETagFunction · 0.90
annotateDbFunction · 0.90
memFilterFunction · 0.85
syncMethod · 0.80
logQueryFunction · 0.70
mapMethod · 0.65
withPermitsMethod · 0.65
getMethod · 0.65
pipeMethod · 0.65
setMethod · 0.65
pipeFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…