This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
storage.js
110 lines (103 loc) · 3.44 KB
/
storage.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
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {combineReducers} from 'redux';
import {General} from 'mattermost-redux/constants';
import {StorageTypes} from 'utils/constants';
function storage(state = {}, action) {
var key;
switch (action.type) {
case StorageTypes.SET_ITEM: {
if (!state[action.data.prefix + action.data.name] ||
!state[action.data.prefix + action.data.name].timestamp ||
state[action.data.prefix + action.data.name].timestamp < action.data.timestamp
) {
const nextState = {...state};
nextState[action.data.prefix + action.data.name] = {
timestamp: action.data.timestamp,
value: action.data.value,
};
return nextState;
}
return state;
}
case StorageTypes.REMOVE_ITEM: {
const nextState = {...state};
Reflect.deleteProperty(nextState, action.data.prefix + action.data.name);
return nextState;
}
case StorageTypes.SET_GLOBAL_ITEM: {
if (!state[action.data.name] ||
!state[action.data.name].timestamp ||
state[action.data.name].timestamp < action.data.timestamp
) {
const nextState = {...state};
nextState[action.data.name] = {
timestamp: action.data.timestamp,
value: action.data.value,
};
return nextState;
}
return state;
}
case StorageTypes.REMOVE_GLOBAL_ITEM: {
const nextState = {...state};
Reflect.deleteProperty(nextState, action.data.name);
return nextState;
}
case StorageTypes.CLEAR: {
const cleanState = {};
if (action.data && action.data.exclude && action.data.exclude.forEach) {
action.data.exclude.forEach((excluded) => {
if (state[excluded]) {
cleanState[excluded] = state[excluded];
}
});
}
return cleanState;
}
case StorageTypes.ACTION_ON_GLOBAL_ITEMS_WITH_PREFIX: {
const nextState = {...state};
for (key in state) {
if (key.lastIndexOf(action.data.prefix, 0) === 0) {
nextState[key] = {
timestamp: new Date(),
value: action.data.action(key, state[key].value),
};
}
}
return nextState;
}
case StorageTypes.ACTION_ON_ITEMS_WITH_PREFIX: {
const nextState = {...state};
var globalPrefix = action.data.globalPrefix;
var globalPrefixLen = action.data.globalPrefix.length;
for (key in state) {
if (key.lastIndexOf(globalPrefix + action.data.prefix, 0) === 0) {
var userkey = key.substring(globalPrefixLen);
nextState[key] = {
timestamp: new Date(),
value: action.data.action(userkey, state[key].value),
};
}
}
return nextState;
}
case StorageTypes.STORAGE_REHYDRATE: {
return {...state, ...action.data};
}
default:
return state;
}
}
function initialized(state = false, action) {
switch (action.type) {
case General.STORE_REHYDRATION_COMPLETE:
return state || action.complete;
default:
return state;
}
}
export default combineReducers({
storage,
initialized,
});