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

Function makeMemoryStore

packages/infra/src/Store/Memory.ts:514–569  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

512}
513
514export const makeMemoryStore = () => ({
515 make: Effect.fnUntraced(function*<IdKey extends keyof Encoded, Encoded extends FieldValues, R, E>(
516 modelName: string,
517 idKey: IdKey,
518 seed?: Effect.Effect<Iterable<Encoded>, E, R>,
519 config?: StoreConfig<Encoded>
520 ) {
521 const primary = yield* makeMemoryStoreInt<IdKey, Encoded, R, E>(
522 modelName,
523 idKey,
524 "primary",
525 seed,
526 config?.defaultValues
527 )
528 const ctx = yield* Effect.context<R>()
529 const stores = new Map([["primary", primary]])
530 const semaphores = new Map<string, Semaphore.Semaphore>()
531 const getSem = (ns: string) => {
532 let sem = semaphores.get(ns)
533 if (!sem) {
534 sem = Semaphore.makeUnsafe(1)
535 semaphores.set(ns, sem)
536 }
537 return sem
538 }
539 const ensureStore = (namespace: string) =>
540 getSem(namespace).withPermits(1)(Effect.suspend(() => {
541 const store = stores.get(namespace)
542 if (store) return Effect.succeed(store)
543 if (config?.allowNamespace && !config.allowNamespace(namespace)) {
544 throw new Error(`Namespace ${namespace} not allowed!`)
545 }
546 return makeMemoryStoreInt(modelName, idKey, namespace, seed, config?.defaultValues)
547 .pipe(
548 Effect.orDie,
549 Effect.provide(ctx),
550 Effect.tap((store) => Effect.sync(() => stores.set(namespace, store)))
551 )
552 }))
553 const getStore = !config?.allowNamespace
554 ? Effect.succeed(primary)
555 : storeId.pipe(Effect.flatMap((namespace) => ensureStore(namespace)))
556 const s: Store<IdKey, Encoded> = {
557 seedNamespace: (namespace) => ensureStore(namespace).pipe(Effect.asVoid),
558 all: Effect.flatMap(getStore, (_) => _.all),
559 queryRaw: (...args) => Effect.flatMap(getStore, (_) => _.queryRaw(...args)),
560 find: (...args) => Effect.flatMap(getStore, (_) => _.find(...args)),
561 filter: (...args) => Effect.flatMap(getStore, (_) => _.filter(...args)),
562 set: (...args) => Effect.flatMap(getStore, (_) => _.set(...args)),
563 batchSet: (...args) => Effect.flatMap(getStore, (_) => _.batchSet(...args)),
564 bulkSet: (...args) => Effect.flatMap(getStore, (_) => _.bulkSet(...args)),
565 batchRemove: (...args) => Effect.flatMap(getStore, (_) => _.batchRemove(...args))
566 }
567 return s
568 })
569})
570
571export const MemoryStoreLive = StoreMaker.toLayer(Effect.sync(() => makeMemoryStore()))

Callers 1

Memory.tsFile · 0.85

Calls 7

makeMemoryStoreIntFunction · 0.85
filterMethod · 0.80
ensureStoreFunction · 0.70
contextMethod · 0.65
pipeMethod · 0.65
setMethod · 0.65
succeedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…