deno.land / x / prayers@v1.6.0 / src / ReactiveCalculator.ts

ReactiveCalculator.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import { Observable, defer, merge, timer } from 'rxjs'import { delay, repeat } from 'rxjs/operators'import { Coordinates, Prayer, PrayerTimes, Qibla, SunnahTimes } from 'adhan'import { Logger } from 'tslog'import type { Subscriber, Subscription } from 'rxjs'
import { BaseCalculator } from './Base'import { EventType, PrayerNames, TimesNames } from './types/TimeObject'import type { CalculationsConfig, FinalCalculationsConfig, ReactiveCalculationsConfig,} from './types/CalculationsConfig'import type { PrayerNamesType, TimeEventObject, TimeObject } from './types/TimeObject'import type { CoordinatesObject } from './types/Coordinates'import type { Iqama } from './types/Iqama'import { subscriptionsSymbols } from './types/Subscriptions'import { dateByAddingMinutes } from './utils/DatesUtils'
export class ReactiveCalculator extends BaseCalculator { private _subscriptions = new Map<symbol, Subscription>() private _prayerTimes!: TimeObject[] private _currentPrayer!: TimeObject private _nextPrayer!: TimeObject private _middleOfTheNight!: TimeObject private _thirdOfTheNight!: TimeObject private _initialized = false
constructor(rConfig: ReactiveCalculationsConfig) { const config: CalculationsConfig = { date: new Date(), ...rConfig, } super(config)
this._logger = new Logger({ name: `ReactiveCalculator-${Math.random().toString(36).slice(-6)}`, minLevel: config.debug ? 2 : 4, prefix: ['ReactiveCalculator:'], })
this._logger.debug('logger initialized successfully.')
this._setup() }
private _setup() { this._logger.debug('_setup function invoked...') // initially we populate the properties. this._logger.debug('calculating prayer and sunnah times...') this._calculatePrayerTimes() this._calculateCurrentPrayer() this._calculateNextPrayer() this._calculateMiddleOfTheNight() this._calculateThirdOfTheNight()
// we subscribe to time changes and update what needs to be updated
this._logger.debug('setting internal new day subscription...') // the first two subscription are created in isolation so that we keep them throughout the life of the object this._subscriptions.set( subscriptionsSymbols.NEW_SOLAR_DAY_SUBSCRIPTION, this.newSolarDayObserver().subscribe((e) => { this._logger.debug('_setup new day event triggered:', e) this._refreshPrayerCalculator() this._calculatePrayerTimes() this._calculateCurrentPrayer() this._calculateNextPrayer() }) ) this._logger.debug(`map size: ${this._subscriptions.size}`)
this._logger.debug('setting internal new qiyam subscription...') this._subscriptions.set( subscriptionsSymbols.NEW_QIYAM_SUBSCRIPTION, this.newQiyamObserver().subscribe((e) => { this._logger.debug('_setup new qiyam event triggered:', e) this._refreshQiyamCalculator() this._calculateMiddleOfTheNight() this._calculateThirdOfTheNight() }) ) this._logger.debug(`map size: ${this._subscriptions.size}`)
this._logger.debug('setting internal adhan subscription...') this._subscriptions.set( subscriptionsSymbols.ADHAN_SUBSCRIPTION, this.adhanObserver().subscribe((e) => { this._logger.debug('_setup adhan event triggered:', e) this._calculateCurrentPrayer() this._calculateNextPrayer() }) ) this._logger.debug(`map size: ${this._subscriptions.size}`)
this._initialized = true // eslint-disable-next-line @typescript-eslint/no-this-alias const that = this // create proxies that handel future changes to the configs // every time a value in the config is set it will trigger the handler // notice that this includes the `_refreshPrayerCalculator`. in a sense it's recursive but future recursion that._logger.debug('setting up _prayerConfig set trap...') this._prayerConfig = new Proxy(this._prayerConfig, { set<K extends keyof CalculationsConfig>(config: CalculationsConfig, property: K, newVal: CalculationsConfig[K]) { const oldVal = config[property] config[property] = newVal if (that._initialized && oldVal !== newVal) { that._logger.debug('_prayerConfig set trap triggered') const { date, latitude, longitude, method } = config
// create a coordinate object const coordinates = new Coordinates(latitude, longitude)
// create calculation params based on the method name const calculationParams = that._useMethod(method)
that._logger.debug('creating a new _prayerTimesCalculator object') // creating the calculation object that._prayerTimesCalculator = new PrayerTimes(coordinates, date, calculationParams) // clean old subscriptions that._logger.debug( `unsubscribing and cleaning new day and adhan subscriptions. current map size: ${that._subscriptions.size}` ) that._subscriptions.get(subscriptionsSymbols.NEW_SOLAR_DAY_SUBSCRIPTION)?.unsubscribe() that._subscriptions.delete(subscriptionsSymbols.NEW_SOLAR_DAY_SUBSCRIPTION) that._subscriptions.get(subscriptionsSymbols.ADHAN_SUBSCRIPTION)?.unsubscribe() that._subscriptions.delete(subscriptionsSymbols.ADHAN_SUBSCRIPTION) that._logger.debug(`cleaning done. current map size: ${that._subscriptions.size}`) // create new ones that._logger.debug('creating a new subscription for new solar day...') that._subscriptions.set( subscriptionsSymbols.NEW_SOLAR_DAY_SUBSCRIPTION, that.newSolarDayObserver().subscribe((e) => { that._logger.debug('proxy<_prayerConfig> new day event triggered:', e) that._refreshPrayerCalculator() that._calculatePrayerTimes() that._calculateCurrentPrayer() that._calculateNextPrayer() }) ) that._logger.debug(`map size: ${that._subscriptions.size}`) that._logger.debug('creating a new subscription for adhan...') that._subscriptions.set( subscriptionsSymbols.ADHAN_SUBSCRIPTION, that.adhanObserver().subscribe((e) => { that._logger.debug('proxy<_prayerConfig> adhan event triggered:', e) that._calculateCurrentPrayer() that._calculateNextPrayer() }) ) that._logger.debug(`map size: ${that._subscriptions.size}`) // recalculate values that._logger.debug('recalculating prayer times...') that._calculatePrayerTimes() that._calculateCurrentPrayer() that._calculateNextPrayer() } return true }, })
that._logger.debug('setting up _qiyamConfig set trap...') this._qiyamConfig = new Proxy(this._qiyamConfig, { set<K extends keyof CalculationsConfig>(config: CalculationsConfig, property: K, newVal: CalculationsConfig[K]) { const oldVal = config[property] ;(config[property] as typeof oldVal) = newVal
if (that._initialized && oldVal !== newVal) { that._logger.debug('_qiyamConfig set trap triggered') const { date, latitude, longitude, method } = config
// create a coordinate object const coordinates = new Coordinates(latitude, longitude)
// create calculation params based on the method name const calculationParams = that._useMethod(method)
// creating the calculation object const prayerTimesCalculator = new PrayerTimes(coordinates, date, calculationParams) that._logger.debug('creating a new _qiyamTimesCalculator object') that._qiyamTimesCalculator = new SunnahTimes(prayerTimesCalculator) // clean old subscriptions that._logger.debug( `unsubscribing and cleaning new qiyam subscription. current map size: ${that._subscriptions.size}` ) that._subscriptions.get(subscriptionsSymbols.NEW_QIYAM_SUBSCRIPTION)?.unsubscribe() that._subscriptions.delete(subscriptionsSymbols.NEW_QIYAM_SUBSCRIPTION) that._logger.debug(`cleaning done. current map size: ${that._subscriptions.size}`) // create new ones that._logger.debug('creating a new subscription for new qiyam...') that._subscriptions.set( subscriptionsSymbols.NEW_QIYAM_SUBSCRIPTION, that.newQiyamObserver().subscribe((e) => { that._logger.debug('proxy<_qiyamConfig> new qiyam event triggered:', e) that._refreshQiyamCalculator() that._calculateMiddleOfTheNight() that._calculateThirdOfTheNight() }) ) that._logger.debug(`map size: ${that._subscriptions.size}`) // recalculate values that._logger.debug('recalculating sunnah times') that._calculateMiddleOfTheNight() that._calculateThirdOfTheNight() } return true }, }) }
private _calculatePrayerTimes() { this._logger.debug('_calculatePrayerTimes function invoked.') this._prayerTimes = [ { name: Prayer.Fajr, time: this._prayerTimesCalculator.fajr, }, { name: Prayer.Sunrise, time: this._prayerTimesCalculator.sunrise, }, { name: Prayer.Dhuhr, time: this._prayerTimesCalculator.dhuhr, }, { name: Prayer.Asr, time: this._prayerTimesCalculator.asr, }, { name: Prayer.Maghrib, time: this._prayerTimesCalculator.maghrib, }, { name: Prayer.Isha, time: this._adjustForRamadan() ? dateByAddingMinutes(this._prayerTimesCalculator.isha, 30) : this._prayerTimesCalculator.isha, }, ] }
private _calculateCurrentPrayer() { this._logger.debug('_calculateCurrentPrayer function invoked.') const prayer = this._prayerTimesCalculator.currentPrayer() // check if prayer is isha and needs to adjust if (prayer === PrayerNames.ISHA && this._adjustForRamadan()) { this._currentPrayer = { name: this._prayerTimesCalculator.currentPrayer(), time: dateByAddingMinutes(this._prayerTimesCalculator.timeForPrayer(prayer)!, 30), } } else { this._currentPrayer = { name: this._prayerTimesCalculator.currentPrayer(), time: this._prayerTimesCalculator.timeForPrayer(prayer)!, } } }
private _calculateNextPrayer() { this._logger.debug('_calculateNextPrayer function invoked.') const prayer = this._prayerTimesCalculator.nextPrayer() // check if prayer is isha and needs to adjust if (prayer === PrayerNames.ISHA && this._adjustForRamadan()) { this._nextPrayer = { name: this._prayerTimesCalculator.nextPrayer(), time: dateByAddingMinutes(this._prayerTimesCalculator.timeForPrayer(prayer)!, 30), } } else { this._nextPrayer = { name: this._prayerTimesCalculator.nextPrayer(), time: this._prayerTimesCalculator.timeForPrayer(prayer)!, } } }
private _calculateMiddleOfTheNight() { this._logger.debug('_calculateMiddleOfTheNight function invoked.') this._middleOfTheNight = { name: 'middleOfTheNight', time: this._qiyamTimesCalculator.middleOfTheNight } }
private _calculateThirdOfTheNight() { this._logger.debug('_calculateThirdOfTheNight function invoked.') this._thirdOfTheNight = { name: 'lastThirdOfTheNight', time: this._qiyamTimesCalculator.lastThirdOfTheNight } }
public cleanup() { this._logger.debug('cleanup function invoked.') this._subscriptions.forEach((s) => s.unsubscribe()) this._subscriptions.clear() this._logger.debug(`Cleanup done. map size: ${this._subscriptions.size}`) }
public getCurrentPrayerTime(): TimeObject { return this._currentPrayer }
public getNextPrayerTime(): TimeObject { return this._nextPrayer }
public getAllPrayerTimes(): TimeObject[] { return this._prayerTimes }
public getPrayerTime(prayer: PrayerNamesType): Date { // check if prayer is isha and needs to adjust return prayer === PrayerNames.ISHA && this._adjustForRamadan() ? // then add 30 minutes dateByAddingMinutes(this._prayerTimesCalculator.timeForPrayer(prayer)!, 30) : // else just return the prayer time this._prayerTimesCalculator.timeForPrayer(prayer)! }
public getMiddleOfTheNightTime(): TimeObject { return this._middleOfTheNight }
public getLastThirdOfTheNightTime(): TimeObject { return this._thirdOfTheNight }
/** * Get the direction, in degrees from North, of the Qibla from a given set of coordinates. * @param Coordinates - optionally pass latitude and longitude values as an object * @returns value representing the direction in degrees from North. */ public getQiblaDirection( { latitude, longitude }: CoordinatesObject = { latitude: this._prayerConfig.latitude, longitude: this._prayerConfig.longitude, } ): number { const coordinates = new Coordinates(latitude, longitude) return Qibla(coordinates) }
public getCalculationOptions(): CalculationsConfig { return this._prayerConfig }
public setCalculationOptions(newConfig: Partial<ReactiveCalculationsConfig>) { this._logger.debug('setCalculationOptions function invoked with the following arg', newConfig) // changing the config should trigger the set traps via the proxy this._prayerConfig = Object.assign(this._prayerConfig, newConfig) this._qiyamConfig = Object.assign(this._qiyamConfig, newConfig) }
/** * this function observes the solar time and triggers and event each time we have a new day * as moon days end after maghrib, solar days end at midnight * this function is use to refresh the calculator * @returns */ public newSolarDayObserver(): Observable<number> { return defer(() => { this._logger.debug(`subscription made for new solar day. subscription map size: ${this._subscriptions.size}`) const timeAtSubscription = new Date() const nextDay = new Date() // get the nearest midnight in future nextDay.setHours(24, 0, 0, 0) const initialDelay = nextDay.getTime() - timeAtSubscription.getTime() // repeat every 24 hours and 1 minute const repeat = 1000 * 60 * 60 * 24 + 1000 * 60 * 1 // will emit first value after nearest midnight (initialDelay) and subsequent values every 24 hours and 1 minutes after this._logger.debug(`initial delay is set to: ${initialDelay} ms`) return timer(initialDelay, repeat) }) }
public newQiyamObserver(): Observable<TimeEventObject> { return defer(() => { this._logger.debug(`subscription made for new qiyam. subscription map size: ${this._subscriptions.size}`) const timeAtSubscription = new Date() const nextDay = new Date() // get the nearest midnight in future nextDay.setHours(24, 0, 0, 0) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const lastThirdOfTheNight = new Date(this.getLastThirdOfTheNightTime().time!) const delayValue = nextDay.getTime() - timeAtSubscription.getTime() + // interval between midnight and last third of the night (lastThirdOfTheNight.getTime() - nextDay.getTime())
this._logger.debug(`delay is set to: ${delayValue} ms`) const internalObserver = new Observable((subscriber: Subscriber<TimeEventObject>) => { subscriber.next({ name: TimesNames.LAST_THIRD_OF_THE_NIGHT, time: this._qiyamTimesCalculator.lastThirdOfTheNight, type: EventType.TRANSIENT, }) subscriber.complete() })
return internalObserver.pipe(delay(delayValue)).pipe(repeat()) }) }
// A function that would emit an event every time a prayer time is due public adhanObserver(): Observable<TimeEventObject> { // we use defer to trigger the observable creation (factory) at subscription time return defer(() => { this._logger.debug(`subscription made for adhan. subscription map size: ${this._subscriptions.size}`) const prayerTimes = this.getAllPrayerTimes() // we capture the time when a subscription happens const timeAtSubscription = new Date() let noPrayersLeftForToday = true
// create the inner observable const innerObservable = new Observable((subscriber: Subscriber<TimeEventObject>) => { // we create value to emit based on the subscription time prayerTimes.forEach((prayer, i) => { // calculate the delay needed to issue a prayer event starting from now // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const delay = prayer.time!.getTime() - timeAtSubscription.getTime() // if the delay is positive (prayer time is in the future) we create a value to emit if (delay >= 0) { noPrayersLeftForToday = false // we create an event of the the prayer based on the delay setTimeout(() => { this._logger.debug(`emitting next adhan event: ${prayer.name}`) subscriber.next({ name: prayer.name, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion time: prayer.time!, type: EventType.ADHAN, }) // if it's the last prayer we complete if (prayer.name === 'isha') { this._logger.debug('adhan subscription complete.') subscriber.complete() } }, delay) }
if (noPrayersLeftForToday && i === prayerTimes.length - 1) { subscriber.complete() } }) })
// apply the repeat operator to the inner observable return innerObservable.pipe(repeat({ delay: () => this.newSolarDayObserver() })) }) }
public iqamaObserver(): Observable<TimeEventObject> { // we use defer to trigger the observable creation (factory) at subscription time return defer(() => { this._logger.debug(`subscription made for iqama. subscription map size: ${this._subscriptions.size}`) const prayerTimes = this.getAllPrayerTimes() let noPrayersLeftForToday = true // we capture the time when a subscription happens const timeAtSubscription = new Date()
const innerObservable = new Observable((subscriber: Subscriber<TimeEventObject>) => { // we create value to emit based on the subscription time prayerTimes.forEach((prayer, i) => { // calculate the delay needed to issue an iqama event starting from subscription time const delay = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion prayer.time!.getTime() + (this._prayerConfig as FinalCalculationsConfig).iqama[prayer.name as keyof Iqama] * 60000 - timeAtSubscription.getTime() // if the delay is positive (iqama is in the future) we create a value to emit if (delay >= 0) { noPrayersLeftForToday = false // we create an event of the the prayer based on the delay setTimeout(() => { this._logger.debug(`emitting next iqama event: ${prayer.name}`) subscriber.next({ name: prayer.name, time: new Date(), type: EventType.IQAMA, }) // if it's the last prayer we complete if (prayer.name === 'isha') { this._logger.debug('iqama subscription complete.') subscriber.complete() } }, delay) }
if (noPrayersLeftForToday && i === prayerTimes.length - 1) { this._logger.debug('iqama subscription complete again.') subscriber.complete() } }) }) return innerObservable.pipe(repeat({ delay: () => this.newSolarDayObserver() })) }) }
public qiyamTimesObserver(): Observable<TimeEventObject> { // we use defer to trigger the observable creation (factory) at subscription time return defer(() => { this._logger.debug(`subscription made for qiyam times. subscription map size: ${this._subscriptions.size}`) const middleOfTheNightTime = this._qiyamTimesCalculator.middleOfTheNight const lastThirdOfTheNightTime = this._qiyamTimesCalculator.lastThirdOfTheNight // we capture the time when a subscription happens const timeAtSubscription = new Date()
const innerObserver = new Observable((subscriber: Subscriber<TimeEventObject>) => { // calculate the delay needed to issue a middleOfTheNight event starting from now const middleDelay = middleOfTheNightTime.getTime() - timeAtSubscription.getTime() // calculate the delay needed to issue a lastThirdOfTheNight event starting from now const lastDelay = lastThirdOfTheNightTime.getTime() - timeAtSubscription.getTime() // if middle of the night time is in the future if (middleDelay >= 0) { // we create an event based on the delay to announce the middle of the night setTimeout(() => { this._logger.debug('emitting middle of the night event.') subscriber.next({ name: TimesNames.MIDDLE_OF_THE_NIGHT, time: middleOfTheNightTime, type: EventType.TRANSIENT, }) subscriber.complete() }, middleDelay) } if (lastDelay >= 0) { // we create an event based on the delay to announce the last third of the night setTimeout(() => { this._logger.debug('emitting last third event.') subscriber.next({ name: TimesNames.LAST_THIRD_OF_THE_NIGHT, time: lastThirdOfTheNightTime, type: EventType.TRANSIENT, }) subscriber.complete() }, lastDelay) } // we end the subscription this._logger.debug('qiyam times subscription complete.') subscriber.complete() })
return innerObserver.pipe(repeat({ delay: () => this.newQiyamObserver() })) }) }
public prayerEventsObserver() { return merge(this.adhanObserver(), this.iqamaObserver()) }
private _refreshPrayerCalculator() { this._logger.debug('_refreshPrayerCalculator function invoked.') // changing the config should trigger the set trap via the proxy this._prayerConfig = Object.assign(this._prayerConfig, { date: new Date(), // refresh the date }) }
private _refreshQiyamCalculator() { this._logger.debug('_refreshQiyamCalculator function invoked.') // changing the config should trigger the set trap via the proxy this._qiyamConfig = Object.assign(this._qiyamConfig, { date: new Date(), // refresh the date }) }}
prayers

Version Info

Tagged at
7 months ago