LiveLoveApp logo

Exercise - ReplaySubject


  1. Open exercise on codesandbox. I've already imported the ReplaySubject class and the necessary operator functions.
  2. Create a new ReplaySubject and specify the generic type number.
  3. Use the fromEvent creation operator to add an event listener to both the add and sub buttons. Within the pipe() for each mouse event use the map() operator to map to either 1 or -1 appropriately. Finally, subscribe to both event listener Observables and set the ReplaySubject instance as the Observer.
  4. Use the EventTarget.addEventListener to add a click event listener to the calc button.
  5. In the callback function, subscribe to the ReplaySubject instance. Within the pipe() use the scan() operator to sum the next notifications values. Within the subsribe() method set the value property for the sum input element to the next notification value emitted.