@@ -13,24 +13,31 @@ export interface Entity {
1313 id : string ;
1414}
1515
16- export interface OperationError < H , E > {
17- entity : H ;
18- error : E ;
16+ export interface OperationError < Input , ErrorOutput > {
17+ entity : Input ;
18+ error : ErrorOutput ;
1919}
2020
21- export type OperationResult < H , E > = Result < H , OperationError < H , E > > ;
21+ export type OperationResult < Input , Output , ErrorOutput > = Result <
22+ Output ,
23+ OperationError < Input , ErrorOutput >
24+ > ;
2225
23- export type Operation < H , E > = ( entity : H ) => Promise < Result < H , E > > ;
24- export type BulkOperation < H , E > = ( entities : H [ ] ) => Promise < Array < OperationResult < H , E > > > ;
26+ export type Operation < Input , Output , ErrorOutput > = (
27+ entity : Input
28+ ) => Promise < Result < Output , ErrorOutput > > ;
29+ export type BulkOperation < Input , Output , ErrorOutput > = (
30+ entities : Input [ ]
31+ ) => Promise < Array < OperationResult < Input , Output , ErrorOutput > > > ;
2532
26- export function createBuffer < H extends Entity , E > (
27- bulkOperation : BulkOperation < H , E >
28- ) : Operation < H , E > {
33+ export function createBuffer < Input extends Entity , Output extends Entity , ErrorOutput > (
34+ bulkOperation : BulkOperation < Input , Output , ErrorOutput >
35+ ) : Operation < Input , Output , ErrorOutput > {
2936 const flushBuffer = new Subject < void > ( ) ;
3037 const storeUpdateBuffer = new Subject < {
31- entity : H ;
32- onSuccess : ( entity : Ok < H > ) => void ;
33- onFailure : ( error : Err < E > ) => void ;
38+ entity : Input ;
39+ onSuccess : ( entity : Ok < Output > ) => void ;
40+ onFailure : ( error : Err < ErrorOutput > ) => void ;
3441 } > ( ) ;
3542
3643 storeUpdateBuffer
@@ -48,7 +55,7 @@ export function createBuffer<H extends Entity, E>(
4855 ( entity ) => {
4956 entityById [ entity . id ] . onSuccess ( asOk ( entity ) ) ;
5057 } ,
51- ( { entity, error } : OperationError < H , E > ) => {
58+ ( { entity, error } : OperationError < Input , ErrorOutput > ) => {
5259 entityById [ entity . id ] . onFailure ( asErr ( error ) ) ;
5360 }
5461 )
@@ -59,7 +66,7 @@ export function createBuffer<H extends Entity, E>(
5966 } ) ;
6067 } ) ;
6168
62- return async function ( entity : H ) {
69+ return async function ( entity : Input ) {
6370 return new Promise ( ( resolve , reject ) => {
6471 // ensure we flush by the end of the "current" event loop tick
6572 setImmediate ( ( ) => flushBuffer . next ( ) ) ;
0 commit comments