interface: update reducers for immer

This commit is contained in:
Liam Fitzgerald 2021-03-31 14:44:52 +10:00
parent f5022409c9
commit b06b4388c9
No known key found for this signature in database
GPG Key ID: D390E12C61D1CFFB
3 changed files with 25 additions and 21 deletions

View File

@ -25,7 +25,7 @@ const reduceConfigured = (json, state: StorageState): StorageState => {
const reduceToken = (json: Cage, state: StorageState): StorageState => { const reduceToken = (json: Cage, state: StorageState): StorageState => {
let data = json['gcp-token']; let data = json['gcp-token'];
if (data) { if (data) {
state = setToken(data, state); setToken(data, state);
} }
return state; return state;
} }

View File

@ -17,7 +17,7 @@ import bigInt, {BigInteger} from 'big-integer';
export const HarkReducer = (json: any) => { export const HarkReducer = (json: any) => {
const data = _.get(json, 'harkUpdate', false); const data = _.get(json, 'harkUpdate', false);
if (data) { if (data) {
reduce(data); reduceState(useHarkState, data, [reduce]);
} }
const graphHookData = _.get(json, 'hark-graph-hook-update', false); const graphHookData = _.get(json, 'hark-graph-hook-update', false);
if (graphHookData) { if (graphHookData) {
@ -39,8 +39,9 @@ export const HarkReducer = (json: any) => {
} }
}; };
function reduce(data) { function reduce(data, state) {
reduceState<HarkState, any>(useHarkState, data, [ const reducers = [
calculateCount,
unread, unread,
read, read,
archive, archive,
@ -56,8 +57,11 @@ function reduce(data) {
seenIndex, seenIndex,
removeGraph, removeGraph,
readAll, readAll,
calculateCount ];
]); const reducer = compose(reducers.map(r => s => {
return r(data, s);
}));
return reducer(state);
} }
function calculateCount(json: any, state: HarkState) { function calculateCount(json: any, state: HarkState) {
@ -148,7 +152,7 @@ function graphWatchSelf(json: any, state: HarkState): HarkState {
function readAll(json: any, state: HarkState): HarkState { function readAll(json: any, state: HarkState): HarkState {
const data = _.get(json, 'read-all'); const data = _.get(json, 'read-all');
if(data) { if(data) {
state = clearState(state); clearState(state);
} }
return state; return state;
} }
@ -164,7 +168,7 @@ function removeGraph(json: any, state: HarkState): HarkState {
function seenIndex(json: any, state: HarkState): HarkState { function seenIndex(json: any, state: HarkState): HarkState {
const data = _.get(json, 'seen-index'); const data = _.get(json, 'seen-index');
if(data) { if(data) {
state = updateNotificationStats(state, data.index, 'last', () => data.time); updateNotificationStats(state, data.index, 'last', () => data.time);
} }
return state; return state;
} }
@ -172,7 +176,7 @@ function seenIndex(json: any, state: HarkState): HarkState {
function readEach(json: any, state: HarkState): HarkState { function readEach(json: any, state: HarkState): HarkState {
const data = _.get(json, 'read-each'); const data = _.get(json, 'read-each');
if (data) { if (data) {
state = updateUnreads(state, data.index, u => u.delete(data.target)); updateUnreads(state, data.index, u => u.delete(data.target));
} }
return state; return state;
} }
@ -180,7 +184,7 @@ function readEach(json: any, state: HarkState): HarkState {
function readSince(json: any, state: HarkState): HarkState { function readSince(json: any, state: HarkState): HarkState {
const data = _.get(json, 'read-count'); const data = _.get(json, 'read-count');
if(data) { if(data) {
state = updateUnreadCount(state, data, () => 0); updateUnreadCount(state, data, () => 0);
} }
return state; return state;
} }
@ -188,7 +192,7 @@ function readSince(json: any, state: HarkState): HarkState {
function unreadSince(json: any, state: HarkState): HarkState { function unreadSince(json: any, state: HarkState): HarkState {
const data = _.get(json, 'unread-count'); const data = _.get(json, 'unread-count');
if(data) { if(data) {
state = updateUnreadCount(state, data.index, u => u + 1); updateUnreadCount(state, data.index, u => u + 1);
} }
return state; return state;
} }
@ -196,7 +200,7 @@ function unreadSince(json: any, state: HarkState): HarkState {
function unreadEach(json: any, state: HarkState): HarkState { function unreadEach(json: any, state: HarkState): HarkState {
const data = _.get(json, 'unread-each'); const data = _.get(json, 'unread-each');
if(data) { if(data) {
state = updateUnreads(state, data.index, us => us.add(data.target)); updateUnreads(state, data.index, us => us.add(data.target));
} }
return state; return state;
} }
@ -204,7 +208,7 @@ function unreadEach(json: any, state: HarkState): HarkState {
function unreads(json: any, state: HarkState): HarkState { function unreads(json: any, state: HarkState): HarkState {
const data = _.get(json, 'unreads'); const data = _.get(json, 'unreads');
if(data) { if(data) {
state = clearState(state); clearState(state);
data.forEach(({ index, stats }) => { data.forEach(({ index, stats }) => {
const { unreads, notifications, last } = stats; const { unreads, notifications, last } = stats;
updateNotificationStats(state, index, 'last', () => last); updateNotificationStats(state, index, 'last', () => last);
@ -212,11 +216,11 @@ function unreads(json: any, state: HarkState): HarkState {
addNotificationToUnread(state, index, makePatDa(time)); addNotificationToUnread(state, index, makePatDa(time));
}); });
if('count' in unreads) { if('count' in unreads) {
state = updateUnreadCount(state, index, (u = 0) => u + unreads.count); updateUnreadCount(state, index, (u = 0) => u + unreads.count);
} else { } else {
state = updateUnreads(state, index, s => new Set()); updateUnreads(state, index, s => new Set());
unreads.each.forEach((u: string) => { unreads.each.forEach((u: string) => {
state = updateUnreads(state, index, s => s.add(u)); updateUnreads(state, index, s => s.add(u));
}); });
} }
}); });
@ -240,10 +244,8 @@ function clearState(state: HarkState): HarkState {
}, },
notificationsCount: 0 notificationsCount: 0
}; };
Object.assign(state, initialState);
Object.keys(initialState).forEach((key) => {
state[key] = initialState[key];
});
return state; return state;
} }
@ -361,7 +363,9 @@ const timebox = (json: any, state: HarkState): HarkState => {
function more(json: any, state: HarkState): HarkState { function more(json: any, state: HarkState): HarkState {
const data = _.get(json, 'more', false); const data = _.get(json, 'more', false);
if (data) { if (data) {
_.forEach(data, d => reduce(d)); _.forEach(data, d => {
reduce(d, state);
});
} }
return state; return state;
} }

View File

@ -25,7 +25,7 @@ export default class MetadataReducer {
const groupInitial = (json: MetadataUpdate, state: MetadataState): MetadataState => { const groupInitial = (json: MetadataUpdate, state: MetadataState): MetadataState => {
const data = _.get(json, 'initial-group', false); const data = _.get(json, 'initial-group', false);
if(data) { if(data) {
state = associations(data, state); associations(data, state);
} }
return state; return state;
} }