( modelName: string, idKey: IdKey, namespace: string, seed?: Effect.Effect<Iterable<Encoded>, E, R>, _defaultValues?: Partial<Encoded> )
| 328 | } |
| 329 | |
| 330 | export 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 |
no test coverage detected
searching dependent graphs…