How to create a boolean observable that emits hot values with switchMap?

Looking to develop a method named

isEmpty:Observable<boolean>
that generates a hot Observable<boolean> by employing a switchMap. Here's what I have so far:

  /**
   * Notifies observers when the store is empty.
   */
  protected notifyOnEmpty = new ReplaySubject<E[]>(1);

  /**
   * Check if the store is empty.
   * 
   * @return A hot {@link Observable<boolean>} indicating the status of the store (empty or not).
   * 
   * @example
     <pre>
    source.isEmpty();
    </pre>
  */
  isEmpty<E>():Observable<boolean> {
    const isCurrentlyEmpty = values(this.entries).length == 0;
    return this.notifyOnEmpty.pipe(startWith(isCurrentlyEmpty), 
                                   switchMap((entries:E[])=>entries.length == 0));
  }

The idea is that the store can use

notifyOnEmpty.next(Object.values(this.entries))
to inform subscribers about whether the store is empty.

However, I encounter an error with the switchMap statement:

[ts] Argument of type '(entries: E[]) => boolean' is not assignable to parameter of type '(value: E[], index: number) => ObservableInput'. Type 'boolean' is not assignable to type 'ObservableInput'. (parameter) entries: E[]

Any suggestions?

Answer №1

The switchMap operator is utilized to choose a new observable for each value. A standard map function suffices to map each Array into a boolean:

import { map, startWith } from 'rxjs/operators';

// ...

isEmpty<E>():Observable<boolean> {
  return this.notifyOnEmpty.pipe(
    startWith(values(this.entries)), 
    map((entries:E[]) => entries.length == 0)
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using the for-each loop in Express.js with Node

I'm currently working on developing a REST API using express with Node. I have a requirement to iterate through a for loop in order to generate the desired JSON output. Here is a snippet of my route file: var Redis = require('ioredis') var ...

The issue of AJAX not functioning on multiple forms on a single page

i have implemented two separate ajax scripts. The first script is for the login functionality. $(document).ready(function() { $("#ajaxform").submit(function(e) { $("#simple-msg").html("<img src='loading.gif'/>"); var postData =""; ...

FitText.js malfunctioning

I'm currently experimenting with using FitText.js to dynamically adjust the size of headlines to fit within the limits of the browser width. Interestingly, while this script successfully resizes the text in multiple sections of my website, it seems t ...

What are some methods for creating a Venn Diagram that includes data within each section using SVG, CSS, or Canvas?

I am attempting to replicate this visual representation using pure SVG, CSS, or Canvas drawing. So far, I have successfully created three circles that overlap and placed a label in the center of each one. However, I'm facing challenges when it comes t ...

Issue with exporting Typescript React component

Despite searching for answers, none of the related questions have been helpful... My goal is to export React components from a TypeScript library that I plan to publish on npm. For testing purposes before publishing, I am utilizing npm link. The structu ...

create a recurring design wallpaper within the TinyMCE editor

One of my functions alters the background of the tinymce editor. However, I am interested in having a wallpaper repeat in a similar fashion to background-repeat: repeat; in CSS. How can I achieve this? Here is the function: function SettinymceImage(bg_i ...

Trigger Google Analytics event when file is downloaded from server

Here is a helpful resource on sending events to Google Analytics via API server sided: Send event to Google Analytics using API server sided An email containing a download link has been sent to a customer: Hello, Please access your product through thi ...

Utilizing JavaScript to implement conditional if-else statements on CSS properties

I am trying to implement conditions on CSS properties in JavaScript. What is the most efficient approach to achieve this? <html> <head> <title>TOOLTIP USING JAVASCRIPT</title> <style> span{ curso ...

Create a new object containing a series of function expressions, but exclude the first function parameter

In my current setup, I have a variable called storePattern const storePattern = { state: { }, mutations: { }, actions: {}, modules: { modal: { actions: { openModal(store, name: string): boolean { console.log('Op ...

Experiencing difficulties connecting JavaScript with HTML

I'm encountering difficulty with implementing my JavaScript on my website. While it functions properly in this JS Fiddle, I am experiencing issues when trying to use it on my actual site. Below is how I have linked the JS on my site: <head> < ...

Stop the execution of javascript code based on the screen width

On my website, I have two menus located in different sections of the page. One menu is shown when the screen width is greater than 555px, while the other menu appears when the screen width is less than or equal to 555px. Although the menus are essentially ...

Karma jasmine and an angular controller that utilizes the 'Controller as' syntax (where 'this' is used instead of $scope)

I'm having trouble setting up karma for my unit tests, specifically on a basic example: Here is the controller file: angular.module('balrogApp.requests', [ /* Dependencies */ ]) // Routes configuration .config(['$routeProvider&a ...

Encountering a problem in React.js and Typescript involving the spread operator that is causing an error

Could someone assist me with my current predicament? I attempted to work with TypeScript and utilize the useReducer hook. const initialState = { a: "a" }; const [state, dispatch] = useReducer(reducer, {...initialState}); I keep encountering an error ...

MongoDB Integration of Collections - No Data Population

Having trouble merging a client and an account collection. When I use res.send(client), only the account id's are returned. Unsure how to include account information in clients. Have seen one to many solutions, but struggling with this two-way relati ...

When requesting URLs on the server via Http, they must be in absolute form

Recently, I developed an Angular Universal application using Angular2 where I made a request to the /category service. this.hsService.getCategories(AppConstants.BASE_URL_GET_CATGORIES).subscribe( resp => { if (resp !== null) { console.log(& ...

Tips for positioning two elements side by side on a small screen using the Bootstrap framework

Greetings! As a beginner, I must apologize for the lack of finesse in my code. Currently, I am facing an issue with the positioning of my name (Tristen Roth) and the navbar-toggler-icon on xs viewports. They are appearing on separate lines vertically, lea ...

After converting my HTML elements to JSX in Next.js, I am experiencing issues with my CSS files not being applied

Hey there, I'm currently working on a website using Next.js. I've converted the HTML elements of a page to JSX elements but for some reason, the CSS of the template isn't showing up. I've double-checked all the paths and even updated th ...

Techniques for Utilizing MongoDB Aggregation to Extract Specific Fields from Results

Welcome, everyone! I'm diving into the world of MongoDB aggregation, and after running some queries, I've finally obtained the following result: "result" : [ { "_id" : "531d84734031c76f06b853f0" }, { "_id" : "5316739 ...

VueX getter not functioning with Async/Await, while log function does work

I am working on a situation where I have a collection of conversations associated with userIDs that I need to iterate through. Within this loop, I must make a call to Firebase to retrieve the corresponding userNames and then generate an object containing t ...

The selector is not able to be located by the jQuery click function

let obj = new object(); obj.loadInterface(); $("button").click(function() { obj.doSomething(); }); The issue arises because obj.loadInterface() loads the button into the DOM at the end of a $.post() function since server data is required to set some ...