-
Notifications
You must be signed in to change notification settings - Fork 0
/
topics.js
49 lines (45 loc) · 1.49 KB
/
topics.js
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
// @ts-check
/** @typedef {import("@cuser/proto/types/graphs").GraphTopic} GraphTopic */
/** @typedef {import("@cuser/proto/types/actions").ActionPublishMessage} ActionPublishMessage */
/** @typedef {import("@cuser/proto/types/actions").ActionUpdateMessage} ActionUpdateMessage */
/** @typedef {import("@cuser/proto/types/actions").ActionDeleteMessage} ActionDeleteMessage */
// Core
const assert = require('@cuser/utils/assert');
// Types
const {
TYPE_ACTION_PUBLISH_MESSAGE,
TYPE_ACTION_UPDATE_MESSAGE,
TYPE_ACTION_DELETE_MESSAGE,
TYPE_ACTION_DEFAULT,
} = require('../types/actions');
const {
TYPE_ERROR_MISSING_RESOURCE_ID,
} = require('../types/errors');
// Utils
const createReducer = require('../utils/createReducer');
/**
* Topic reducer
* @param {Record<string,GraphTopic>} [state]
* @param {ActionPublishMessage|ActionUpdateMessage|ActionDeleteMessage} action
*/
const topicsReducer = createReducer({
[TYPE_ACTION_PUBLISH_MESSAGE]: (state, { payload: { topicId } }) => ({
...state,
[topicId]: '@topic'
}),
[TYPE_ACTION_UPDATE_MESSAGE]: (state, { payload: { topicId } }) => {
assert(state && state[topicId], TYPE_ERROR_MISSING_RESOURCE_ID, 'Topic', topicId);
return {
...state,
[topicId]: '@topic'
}
},
[TYPE_ACTION_DELETE_MESSAGE]: (state, { payload: { topicId } }) => {
assert(state && state[topicId], TYPE_ERROR_MISSING_RESOURCE_ID, 'Topic', topicId);
return {
...state,
[topicId]: '@topic'
}
}
});
module.exports = topicsReducer;