11import { DateTime } from "luxon" ;
22import Loki , { type Collection } from "lokijs" ;
33import { v4 } from "uuid" ;
4- import { DriverError , MaxAttemptsExceededError } from "../error.js" ;
4+ import {
5+ DriverError ,
6+ DriverInitializationError ,
7+ DriverNoMatchingAckError ,
8+ DriverNoMatchingRefError ,
9+ MaxAttemptsExceededError ,
10+ } from "../error.js" ;
511import { QueueDoc } from "../types.js" ;
612import { BaseDriver } from "./base.js" ;
713import { loadModule } from "@brillout/load-module" ;
@@ -97,15 +103,15 @@ export const getClient = (identifier: string) => {
97103 * LokiJS Driver Class. Creates a connection that allows DocMQ to talk to
98104 * an in-memory LokiJS instance
99105 */
100- export class LokiDriver extends BaseDriver {
106+ export class LokiDriver extends BaseDriver < Loki , Collection < LokiDoc > > {
101107 protected _db : Loki | undefined ;
102108 protected _jobs : Collection < LokiDoc > | undefined ;
103109
104110 /** Get the Parent Schema object associated with the job list */
105111 async getSchema ( ) {
106112 await this . ready ( ) ;
107113 if ( ! this . _db ) {
108- throw new Error ( "init" ) ;
114+ throw new DriverInitializationError ( ) ;
109115 }
110116 return Promise . resolve ( this . _db ) ;
111117 }
@@ -114,7 +120,7 @@ export class LokiDriver extends BaseDriver {
114120 async getTable ( ) {
115121 await this . ready ( ) ;
116122 if ( ! this . _jobs ) {
117- throw new Error ( "init" ) ;
123+ throw new DriverInitializationError ( ) ;
118124 }
119125 return Promise . resolve ( this . _jobs ) ;
120126 }
@@ -158,7 +164,7 @@ export class LokiDriver extends BaseDriver {
158164 await this . ready ( ) ;
159165
160166 if ( ! this . _jobs ) {
161- throw new Error ( "init" ) ;
167+ throw new DriverInitializationError ( ) ;
162168 }
163169
164170 this . _jobs . startTransaction ( ) ;
@@ -170,7 +176,7 @@ export class LokiDriver extends BaseDriver {
170176 await this . ready ( ) ;
171177
172178 if ( ! this . _jobs ) {
173- throw new Error ( "init" ) ;
179+ throw new DriverInitializationError ( ) ;
174180 }
175181
176182 const now = DateTime . now ( ) ;
@@ -204,7 +210,7 @@ export class LokiDriver extends BaseDriver {
204210 await this . ready ( ) ;
205211
206212 if ( ! this . _jobs ) {
207- throw new Error ( "init" ) ;
213+ throw new DriverInitializationError ( ) ;
208214 }
209215
210216 if ( ack === null ) {
@@ -230,15 +236,15 @@ export class LokiDriver extends BaseDriver {
230236 . data ( ) ?. [ 0 ] ;
231237
232238 if ( ! next ) {
233- throw new Error ( "NO_MATCHING_JOB" ) ;
239+ throw new DriverNoMatchingAckError ( ack ) ;
234240 }
235241 }
236242
237243 async fail ( ack : string , retryIn : number , attempt : number ) : Promise < void > {
238244 await this . ready ( ) ;
239245
240246 if ( ! this . _jobs ) {
241- throw new Error ( "init" ) ;
247+ throw new DriverInitializationError ( ) ;
242248 }
243249 if ( ack === null ) {
244250 throw new Error ( "ERR_NULL_ACK" ) ;
@@ -263,7 +269,7 @@ export class LokiDriver extends BaseDriver {
263269 . data ( ) ?. [ 0 ] ;
264270
265271 if ( ! next ) {
266- throw new Error ( "NO_MATCHING_JOB" ) ;
272+ throw new DriverNoMatchingAckError ( ack ) ;
267273 }
268274 }
269275
@@ -277,7 +283,7 @@ export class LokiDriver extends BaseDriver {
277283 }
278284
279285 if ( ! this . _jobs ) {
280- throw new Error ( "init" ) ;
286+ throw new DriverInitializationError ( ) ;
281287 }
282288
283289 const err = new MaxAttemptsExceededError (
@@ -307,15 +313,15 @@ export class LokiDriver extends BaseDriver {
307313 . data ( ) ?. [ 0 ] ;
308314
309315 if ( ! next ) {
310- throw new Error ( "NO_MATCHING_JOB" ) ;
316+ throw new DriverNoMatchingAckError ( ackVal ) ;
311317 }
312318 }
313319
314320 async ping ( ack : string , extendBy = 15 ) : Promise < void > {
315321 await this . ready ( ) ;
316322
317323 if ( ! this . _jobs ) {
318- throw new Error ( "init" ) ;
324+ throw new DriverInitializationError ( ) ;
319325 }
320326 if ( ack === null ) {
321327 throw new Error ( "ERR_NULL_ACK" ) ;
@@ -338,15 +344,15 @@ export class LokiDriver extends BaseDriver {
338344 . data ( ) ?. [ 0 ] ;
339345
340346 if ( ! next ) {
341- throw new Error ( "NO_MATCHING_JOB" ) ;
347+ throw new DriverNoMatchingAckError ( ack ) ;
342348 }
343349 }
344350
345351 async promote ( ref : string ) : Promise < void > {
346352 await this . ready ( ) ;
347353
348354 if ( ! this . _jobs ) {
349- throw new Error ( "init" ) ;
355+ throw new DriverInitializationError ( ) ;
350356 }
351357
352358 const now = DateTime . now ( ) ;
@@ -367,15 +373,15 @@ export class LokiDriver extends BaseDriver {
367373 . data ( ) ?. [ 0 ] ;
368374
369375 if ( ! next ) {
370- throw new Error ( "NO_MATCHING_JOB" ) ;
376+ throw new DriverNoMatchingRefError ( ref ) ;
371377 }
372378 }
373379
374380 async delay ( ref : string , delayBy : number ) : Promise < void > {
375381 await this . ready ( ) ;
376382
377383 if ( ! this . _jobs ) {
378- throw new Error ( "init" ) ;
384+ throw new DriverInitializationError ( ) ;
379385 }
380386
381387 const next = this . _jobs
@@ -396,15 +402,15 @@ export class LokiDriver extends BaseDriver {
396402 . data ( ) ?. [ 0 ] ;
397403
398404 if ( ! next ) {
399- throw new Error ( "NO_MATCHING_JOB" ) ;
405+ throw new DriverNoMatchingRefError ( ref ) ;
400406 }
401407 }
402408
403409 async replay ( ref : string ) : Promise < void > {
404410 await this . ready ( ) ;
405411
406412 if ( ! this . _jobs ) {
407- throw new Error ( "init" ) ;
413+ throw new DriverInitializationError ( ) ;
408414 }
409415
410416 const last = this . _jobs
@@ -427,7 +433,7 @@ export class LokiDriver extends BaseDriver {
427433 . data ( ) ?. [ 0 ] ;
428434
429435 if ( ! last ) {
430- throw new Error ( "NO_MATCHING_JOB" ) ;
436+ throw new DriverNoMatchingRefError ( ref ) ;
431437 }
432438
433439 const next : LokiDoc = JSON . parse ( JSON . stringify ( last ) ) ;
@@ -446,7 +452,7 @@ export class LokiDriver extends BaseDriver {
446452 await this . ready ( ) ;
447453
448454 if ( ! this . _jobs ) {
449- throw new Error ( "init" ) ;
455+ throw new DriverInitializationError ( ) ;
450456 }
451457
452458 this . _jobs
@@ -465,7 +471,7 @@ export class LokiDriver extends BaseDriver {
465471 await this . ready ( ) ;
466472
467473 if ( ! this . _jobs ) {
468- throw new Error ( "init" ) ;
474+ throw new DriverInitializationError ( ) ;
469475 }
470476
471477 // remove all future existing
@@ -481,7 +487,7 @@ export class LokiDriver extends BaseDriver {
481487 await this . ready ( ) ;
482488
483489 if ( ! this . _jobs ) {
484- throw new Error ( "init" ) ;
490+ throw new DriverInitializationError ( ) ;
485491 }
486492 if ( ! ref ) {
487493 throw new Error ( "No ref provided" ) ;
@@ -509,7 +515,7 @@ export class LokiDriver extends BaseDriver {
509515 return ;
510516 }
511517 if ( ! this . _jobs ) {
512- throw new Error ( "init" ) ;
518+ throw new DriverInitializationError ( ) ;
513519 }
514520
515521 const next : QueueDoc = {
@@ -554,7 +560,7 @@ export class LokiDriver extends BaseDriver {
554560 await this . ready ( ) ;
555561
556562 if ( ! this . _jobs ) {
557- throw new Error ( "init" ) ;
563+ throw new DriverInitializationError ( ) ;
558564 }
559565
560566 this . _jobs . addListener ( "insert" , ( ) => {
0 commit comments