interface PriorityCollectionEntry < T > { priority : number ; value : T ; } export class PriorityCollection < T > { private readonly array : Array < PriorityCollectionEntry < T > > = [ ] ; public add ( priority , value ) { const foundIndex = this . array . findIndex ( ( current ) => { if ( priority === current . priority ) { throw new Error ( 'Already have entry with this priority' ) ; } return priority < current . priority ; } ) ; const spliceIndex = foundIndex === - 1 ? this . array . length : foundIndex ; this . array . splice ( spliceIndex , 0 , { priority , value } ) ; } public has ( predicate ) { return this . array . some ( ( entry ) => predicate ( entry . value ) ) ; } public toPrioritizedArray ( ) { return this . array . map ( ( entry ) => entry . value ) ; } }