All files / src/lib/core/message-manifest message-manifest.reducers.ts

96.08% Statements 49/51
100% Branches 12/12
94.59% Functions 35/37
95.92% Lines 47/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x 1x 1x 1x 1x                   41x     1x       1x         1x   2x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x     2x                 1x               2x     4x                 2x     4x                 2x               2x   4x         1x                           1x                           1x                           1x                           1x     2x               1x     2x                   40x                 1x     1x     1x     1x     1x   1x   1x  
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
import { Action, createReducer, on } from '@ngrx/store';
import { MessageManifest } from './message-manifest';
import { MessageManifestEntryMessage } from './message-manifest-entry-message';
import * as MessageManifestActions from './message-manifest.actions';
 
export enum ConnectionStatus {
  CONNECTED = 'CONNECTED',
  CONNECTING = 'CONNECTING',
  DISCONNECTED = 'DISCONNECTED',
  DISCONNECTING = 'DISCONNECTING'
}
 
export interface MessageManifestState extends EntityState<MessageManifest> {
  pendingMessages: Array<MessageManifestEntryMessage>;
  currentMessage: MessageManifestEntryMessage;
}
 
// tslint:disable-next-line:only-arrow-functions
export function selectManifestByName(manifest: MessageManifest): string {
  return manifest.name;
}
 
export const adapter: EntityAdapter<MessageManifest> = createEntityAdapter<MessageManifest>({
  selectId: selectManifestByName
});
 
export const initialState: MessageManifestState = adapter.getInitialState({
  pendingMessages: [],
  currentMessage: undefined
});
 
const messageManifestReducer = createReducer(
  initialState,
  on(MessageManifestActions.addManifest, (state, { manifest }) => adapter.addOne(manifest, state)),
  on(MessageManifestActions.addManifests, (state, { manifests }) => adapter.addMany(manifests, state)),
  on(MessageManifestActions.clearManifests, state => adapter.removeAll({ ...state, selectedMessageManifestId: undefined })),
  on(MessageManifestActions.deleteManifest, (state, { id }) => adapter.removeOne(id, state)),
  on(MessageManifestActions.deleteManifests, (state, { ids }) => adapter.removeMany(ids, state)),
  on(MessageManifestActions.deleteManifestsByPredicate, (state, { predicate }) => adapter.removeMany(predicate, state)),
  on(MessageManifestActions.loadManifests, (state, { manifests }) => adapter.setAll(manifests, state)),
  on(MessageManifestActions.mapManifest, (state, { entityMap }) => adapter.mapOne(entityMap, state)),
  on(MessageManifestActions.mapManifests, (state, { entityMap }) => adapter.map(entityMap, state)),
  on(MessageManifestActions.setManifest, (state, { manifest }) => adapter.setOne(manifest, state)),
  on(MessageManifestActions.updateManifest, (state, { update }) => adapter.updateOne(update, state)),
  on(MessageManifestActions.updateManifests, (state, { updates }) => adapter.updateMany(updates, state)),
  on(MessageManifestActions.upsertManifest, (state, { manifest }) => adapter.upsertOne(manifest, state)),
  on(MessageManifestActions.upsertManifests, (state, { manifests }) => adapter.upsertMany(manifests, state)),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.receiveMessage, (state, { manifest, entry, message }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(e => e.name === entry.name ? { ...e, message } : e)
      }
    }, {
      ...state,
      currentMessage: undefined
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.submitMessage, (state, { message }) => {
    return {
      ...state,
      currentMessage: message
    // tslint:disable-next-line:semicolon
    }
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.submitMessageSuccess, (state, { manifest, message }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(e => e.name === message.entryName ? { ...e, message } : e)
      }
    }, {
      ...state,
      currentMessage: undefined
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.submitMessageFailure, (state, { manifest, message }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(e => e.name === message.entryName ? { ...e, message } : e)
      }
    }, {
      ...state,
      currentMessage: undefined
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.queueMessage, (state, { message }) => {
    return {
      ...state,
      pendingMessages: state.pendingMessages.concat([{ ...message }]),
      currentMessage: undefined
    };
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.dequeueMessage, (state, { message }) => {
    return {
      ...state,
      pendingMessages: state.pendingMessages.filter(m => m.manifestName !== message.manifestName || m.entryName !== message.entryName)
    };
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.connectManifest, (state, { manifest }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        connection: {
          status: ConnectionStatus.CONNECTING,
          frame: undefined
        }
      }
    }, {
      ...state
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.connectManifestConnected, (state, { manifest, frame }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        connection: {
          status: ConnectionStatus.CONNECTED,
          frame
        }
      }
    }, {
      ...state
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.disconnectManifest, (state, { manifest }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        connection: {
          status: ConnectionStatus.DISCONNECTING,
          frame: undefined
        }
      }
    }, {
      ...state
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.disconnectManifestDisconnected, (state, { manifest, frame }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        connection: {
          status: ConnectionStatus.DISCONNECTED,
          frame
        }
      }
    }, {
      ...state
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.subscribeManifestSuccess, (state, { manifest, entry, subscription }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: state.entities[manifest.name].entries.map(e => e.name === entry.name ? { ...e, id: subscription.id } : e)
      }
    }, {
      ...state
    });
  }),
  // tslint:disable-next-line:arrow-return-shorthand
  on(MessageManifestActions.unsubscribeManifestSuccess, (state, { manifest, entry }) => {
    return adapter.updateOne({
      id: manifest.name,
      changes: {
        entries: manifest.entries.filter(e => e.name !== entry.name)
      }
    }, {
      ...state
    });
  })
);
 
// tslint:disable-next-line:typedef
export function reducer(state: MessageManifestState | undefined, action: Action) {
  return messageManifestReducer(state, action);
}
 
// get the selectors.
const {
  selectIds,
  selectEntities,
  selectAll,
  selectTotal
} = adapter.getSelectors();
 
// select the array of message manifest names.
export const selectManifestNames = selectIds;
 
// select the dictionary of message manifest subscriptions.
export const selectManifestEntities = selectEntities;
 
// select the array of message messageManifests.
export const selectAllManifests = selectAll;
 
// select the total message manifest count.
export const selectManifestTotal = selectTotal;
 
export const selectCurrentMessage = (state: MessageManifestState) => state.currentMessage;
 
export const selectPendingMessage = (state: MessageManifestState) => state.pendingMessages;