Hacker News new | ask | show | jobs
by Klaster_1 29 days ago
I do this all the time, getOrInsert would come really handy: you need something from a Map-backed storage, but the value may be unset, so you first check if it's undefined, set the default value, and then use that.

Example:

    update(store, (draft) => {
      if (!draft.alertConfigurations.has(req.params.clusterId))
        draft.alertConfigurations.set(req.params.clusterId, new Map());
      const clusterAlerts = draft.alertConfigurations.get(req.params.clusterId);
      req.body.forEach((alert) => clusterAlerts.set(alert.id, { ...alert, predefined: false }));
    });