()
| 512 | } |
| 513 | |
| 514 | export 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 | |
| 571 | export const MemoryStoreLive = StoreMaker.toLayer(Effect.sync(() => makeMemoryStore())) |
no test coverage detected
searching dependent graphs…