Concat VS Merge function in RxJS

merge can interleave the outputs, while concat will first wait for earlier streams to finish before processing later streams

Observable.merge(
        Observable.interval(1, TimeUnit.SECONDS).map(id -> "A" + id),
        Observable.interval(1, TimeUnit.SECONDS).map(id -> "B" + id))
.subscribe(System.out::println);
A0 B0 A1 B1 B2 A2 B3 A3 B4 A4

Observable.concat(
        Observable.interval(1, TimeUnit.SECONDS).map(id -> "A" + id),
        Observable.interval(1, TimeUnit.SECONDS).map(id -> "B" + id))
.subscribe(System.out::println);
A0 A1 A2 A3 A4 A5 A6 A7 A8

References
https://stackoverflow.com/questions/38903094/concat-vs-merge-operator
https://rxjs.dev/api/index/function/merge
https://rxjs.dev/api/index/function/concat

Ignore few static files in express static

var mime = require('mime-types')
var serveStatic = require('serve-static')

app.use(serveStatic(__dirname + '/public', {
  maxAge: '1y',
  setHeaders: function (res, path) {
    if (mime.lookup(path) === 'text/html') {
      res.setHeader('Cache-Control', 'public, max-age=0')
    }
  }
}))
app.use(function (req, res, next) {
  console.log(req.url);
  if (req.url !== '/app/index.html') {
    res.header('Cache-Control', 'public, max-age=600s')
  }
  next();
});
app.use('/app', express.static(path.resolve(__dirname, './app')));

References
https://stackoverflow.com/questions/45076710/can-i-ignore-few-static-files-in-express-static
https://github.com/expressjs/serve-static/issues/32

Producer/Consumer in RxJS

this.newAlarm = new Observable<SocketAlarmDto>(observer => {

  socket.on('alarm', (data: SocketAlarmDto) => {
    observer.next(data);
  });

});
this.newAlarmSubscription = this.socket.newAlarm.subscribe((data: SocketAlarmDto) => {
  async.series([
    (callback => this.syncAlarms(callback)),
    (callback => this.showAlarms(callback))
  ]);

});
this.newAlarmSubscription.unsubscribe();

 

Providing a singleton service on Angular

There are two ways to make a service a singleton in Angular:

  • Declare root for the value of the @Injectable() providedIn property
  • Include the service in the AppModule or in a module that is only imported by the AppModule

Using providedIn

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class UserService {
}

NgModule providers array

@NgModule({
  ...
  providers: [UserService],
  ...
})

References
https://angular.io/guide/singleton-services