Skip to content

Commit 1da4291

Browse files
author
edgar rodriguez
committed
garbage collector protocol
1 parent 7af8cf2 commit 1da4291

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+420
-314
lines changed

lib/cooldown/index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Config, CacheableAction, RarAction } from "../core/index";
1+
import { Config, CacheableAction, RarAction } from "../core";
22
import { Duration, Moment } from "moment";
3-
import { UpdatedProtocol } from "../core/protocols/UPDATED_PROTOCOL";
4-
import { AppendedProtocol, UpsertedAction } from "../core/protocols/APPENDED_PROTOCOL";
5-
import { RetryAllProtocol, RetryAllCommand } from "../core/protocols/RETRY_ALL_PROTOCOL";
3+
import { UpdatedProtocol } from "../core/protocols/UpdatedProtocol";
4+
import { AppendedProtocol, UpsertedAction } from "../core/protocols/AppendProtocol";
5+
import { RetryAllProtocol, RetryAllCommand } from "../core/protocols/RetryAllProtocol";
66
export declare const CANCEL_COOLDOWN = "CANCEL_COOLDOWN";
77
export declare const COOL_AND_RETRY_ALL = "COOL_AND_RETRY_ALL";
88
export declare type cancelCooldownAction = RarAction & UpsertedAction & {

lib/cooldown/index.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
const index_1 = require("../core/index");
3+
const core_1 = require("../core");
44
const now_1 = require("../now");
5-
const UPDATED_PROTOCOL_1 = require("../core/protocols/UPDATED_PROTOCOL");
6-
const APPENDED_PROTOCOL_1 = require("../core/protocols/APPENDED_PROTOCOL");
7-
const RETRY_ALL_PROTOCOL_1 = require("../core/protocols/RETRY_ALL_PROTOCOL");
5+
const UpdatedProtocol_1 = require("../core/protocols/UpdatedProtocol");
6+
const AppendProtocol_1 = require("../core/protocols/AppendProtocol");
7+
const RetryAllProtocol_1 = require("../core/protocols/RetryAllProtocol");
88
const upsert_1 = require("../core/upsert");
99
exports.CANCEL_COOLDOWN = `CANCEL_COOLDOWN`;
1010
exports.COOL_AND_RETRY_ALL = `COOL_AND_RETRY_ALL`;
@@ -14,31 +14,31 @@ function coolDownUntil(action, config) {
1414
exports.coolDownUntil = coolDownUntil;
1515
function coolAndRetryAllActionCreator() {
1616
return {
17-
type: index_1.REDUX_ACTION_RETRY,
18-
[index_1.RETRY_ALL]: true,
17+
type: core_1.REDUX_ACTION_RETRY,
18+
[core_1.RETRY_ALL]: true,
1919
[exports.COOL_AND_RETRY_ALL]: true,
2020
};
2121
}
2222
exports.coolAndRetryAllActionCreator = coolAndRetryAllActionCreator;
2323
function cancelCooldownActionCreator(action) {
2424
return {
25-
type: index_1.REDUX_ACTION_RETRY,
25+
type: core_1.REDUX_ACTION_RETRY,
2626
[upsert_1.UPSERTED]: action,
2727
[exports.CANCEL_COOLDOWN]: true,
2828
};
2929
}
3030
exports.cancelCooldownActionCreator = cancelCooldownActionCreator;
3131
function Cooldown(config) {
3232
return {
33-
[RETRY_ALL_PROTOCOL_1.RETRY_ALL_PROTOCOL]: (action, cachedAction) => {
33+
[RetryAllProtocol_1.RETRY_ALL_PROTOCOL]: (action, cachedAction) => {
3434
return (exports.COOL_AND_RETRY_ALL in action)
3535
? true
3636
: now_1.now().isSameOrAfter(cachedAction.coolDownUntil);
3737
},
38-
[UPDATED_PROTOCOL_1.UPDATED_PROTOCOL]: (action, cachedAction) => ({
38+
[UpdatedProtocol_1.UPDATED_PROTOCOL]: (action, cachedAction) => ({
3939
coolDownUntil: exports.CANCEL_COOLDOWN in action ? now_1.now() : coolDownUntil(cachedAction.action, config)
4040
}),
41-
[APPENDED_PROTOCOL_1.APPENDED_PROTOCOL]: (action) => ({
41+
[AppendProtocol_1.APPENDED_PROTOCOL]: (action) => ({
4242
coolDownUntil: coolDownUntil(action[upsert_1.UPSERTED], config)
4343
})
4444
};

lib/core/createRetryMechanism.js

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const remove_1 = require("./remove");
88
const retryAll_1 = require("./retryAll");
99
const ReducerProtocol_1 = require("./protocols/ReducerProtocol");
1010
const MiddlewareProtocol_1 = require("./protocols/MiddlewareProtocol");
11+
const garbageCollector_1 = require("./garbageCollector");
1112
function createRetryMechanishm(initConfig) {
1213
const defaultConfig = {
1314
stateKeyName: _1.REDUX_ACTION_RETRY,
@@ -17,6 +18,7 @@ function createRetryMechanishm(initConfig) {
1718
reset_1.reset,
1819
remove_1.remove,
1920
retryAll_1.retryAll,
21+
garbageCollector_1.garbageCollector
2022
]
2123
};
2224
const config = ramda_1.mergeWithKey((k, l, r) => k === 'extensions' ? [...l, ...r] : r, defaultConfig, initConfig);

lib/core/garbageCollector.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Config } from '.';
2+
import { MiddlewareProtocol } from './protocols/MiddlewareProtocol';
3+
export declare function garbageCollector(config: Config): MiddlewareProtocol;

lib/core/garbageCollector.js

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"use strict";
2+
// import {
3+
// reject,
4+
// over,
5+
// anyPass,
6+
// find,
7+
// } from 'ramda';
8+
// import {
9+
// Config,
10+
// State,
11+
// INITIAL_STATE,
12+
// REDUX_ACTION_RETRY,
13+
// CachedAction,
14+
// CacheableAction,
15+
// RarAction,
16+
// } from '.';
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
// import { AnyAction } from 'redux';
19+
// import { cacheLens } from './utils';
20+
// import { COLLECT_GARBAGE, isCollectGarbaseCommand } from './protocols/garbageCollectorProtocol';
21+
// export function garbageCollector(_config: Config) {
22+
// return {
23+
// mi: () =>
24+
// (state: State = INITIAL_STATE, action: AnyAction): State => {
25+
// if (isCollectGarbaseCommand(action)) {
26+
// return over(
27+
// cacheLens,
28+
// reject<CachedAction>(
29+
// anyPass([
30+
// cachedAction => !!find(
31+
// removedAction => {
32+
// return removedAction.meta[REDUX_ACTION_RETRY].id === cachedAction.action.meta[REDUX_ACTION_RETRY].id
33+
// }
34+
// , action[REMOVED]),
35+
// ])
36+
// ),
37+
// state
38+
// )
39+
// }
40+
// return state
41+
// }
42+
// }
43+
// }
44+
const ramda_1 = require("ramda");
45+
const garbageCollectorProtocol_1 = require("./protocols/garbageCollectorProtocol");
46+
const RemovedProtocol_1 = require("./protocols/RemovedProtocol");
47+
// import anyPass from 'ramda/es/anyPass';
48+
function garbageCollector(config) {
49+
return {
50+
middleware: visitors => {
51+
const protocolVisitors = visitors.reduce((acc, visitor) => {
52+
if (garbageCollectorProtocol_1.isGarbageCollectorProtocol(visitor)) {
53+
acc.push(visitor[garbageCollectorProtocol_1.GARBAGE_COLLECTOR_PROTOCOL]);
54+
}
55+
return acc;
56+
}, []);
57+
const shouldCollect = ramda_1.anyPass(protocolVisitors);
58+
return api => next => action => {
59+
if (garbageCollectorProtocol_1.isCollectGarbageCommand(action)) {
60+
const result = next(action);
61+
const cache = ramda_1.path([config.stateKeyName, 'cache'], api.getState());
62+
const actionsToRemove = ramda_1.into([], ramda_1.compose(ramda_1.filter(shouldCollect), ramda_1.map((x) => x.action)), cache);
63+
if (!ramda_1.isEmpty(actionsToRemove)) {
64+
api.dispatch(RemovedProtocol_1.removeActionsCreator(actionsToRemove));
65+
}
66+
// const aaa = cache!.reduce(
67+
// (acc, cachedAction) => {
68+
// if (shouldRetry(action, cachedAction)) {
69+
// api.dispatch(cachedAction.action)
70+
// }
71+
// },
72+
// {}
73+
// )
74+
return result;
75+
}
76+
else {
77+
return next(action);
78+
}
79+
};
80+
}
81+
};
82+
}
83+
exports.garbageCollector = garbageCollector;

lib/core/index.d.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
export * from "./createRetryMechanism";
2-
export * from "./protocols/APPENDED_PROTOCOL";
2+
export * from "./protocols/AppendProtocol";
33
export * from "./protocols/MiddlewareProtocol";
4-
export * from "./protocols/REMOVED_PROTOCOL";
5-
export * from "./protocols/RETRY_ALL_PROTOCOL";
4+
export * from "./protocols/RemovedProtocol";
5+
export * from "./protocols/RetryAllProtocol";
66
export * from "./protocols/ReducerProtocol";
7-
export * from "./protocols/UPDATED_PROTOCOL";
7+
export * from "./protocols/UpdatedProtocol";
8+
export * from "./protocols/garbageCollectorProtocol";
89
export * from "./remove";
910
export * from "./reset";
1011
export * from "./retryAll";
1112
export * from "./types";
1213
export * from "./upsert";
1314
export * from "./utils";
14-
export * from "../cooldown";
15-
export * from "../removeAndRetryAll";
16-
export * from "../times";
17-
export * from "../timeToLive";

lib/core/index.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ function __export(m) {
44
}
55
Object.defineProperty(exports, "__esModule", { value: true });
66
__export(require("./createRetryMechanism"));
7-
__export(require("./protocols/APPENDED_PROTOCOL"));
7+
__export(require("./protocols/AppendProtocol"));
88
__export(require("./protocols/MiddlewareProtocol"));
9-
__export(require("./protocols/REMOVED_PROTOCOL"));
10-
__export(require("./protocols/RETRY_ALL_PROTOCOL"));
9+
__export(require("./protocols/RemovedProtocol"));
10+
__export(require("./protocols/RetryAllProtocol"));
1111
__export(require("./protocols/ReducerProtocol"));
12-
__export(require("./protocols/UPDATED_PROTOCOL"));
12+
__export(require("./protocols/UpdatedProtocol"));
13+
__export(require("./protocols/garbageCollectorProtocol"));
1314
__export(require("./remove"));
1415
__export(require("./reset"));
1516
__export(require("./retryAll"));
1617
__export(require("./types"));
1718
__export(require("./upsert"));
1819
__export(require("./utils"));
19-
__export(require("../cooldown"));
20-
__export(require("../removeAndRetryAll"));
21-
__export(require("../times"));
22-
__export(require("../timeToLive"));

lib/core/protocols/MiddlewareProtocol.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Middleware } from 'redux';
2-
import { State } from '../index';
2+
import { State } from '../../core';
33
export interface MiddlewareProtocol<U = {}> {
44
middleware: (initializedExtensions: any[]) => Middleware<State<U>>;
55
}

lib/core/protocols/ReducerProtocol.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Reducer, AnyAction } from 'redux';
2-
import { State } from '../index';
2+
import { State } from '../../core';
33
export interface ReducerProtocol<U = {}> {
44
reducer: (initializedExtensions: any[]) => Reducer<State<U>, AnyAction>;
55
}

lib/core/protocols/REMOVED_PROTOCOL.d.ts renamed to lib/core/protocols/RemovedProtocol.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export declare type RemoveProtocol<U> = {
99
};
1010
export declare const REMOVED = "REMOVED";
1111
export declare function removeActionCreator(action: CacheableAction): RemovedEvent;
12+
export declare function removeActionsCreator(actions: CacheableAction[]): RemovedEvent;
1213
export declare type RemovedEvent = RarAction & {
1314
[REMOVED]: readonly CacheableAction[];
1415
};

lib/core/protocols/REMOVED_PROTOCOL.js renamed to lib/core/protocols/RemovedProtocol.js

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ function removeActionCreator(action) {
1010
};
1111
}
1212
exports.removeActionCreator = removeActionCreator;
13+
function removeActionsCreator(actions) {
14+
return {
15+
type: __1.REDUX_ACTION_RETRY,
16+
[exports.REMOVED]: actions
17+
};
18+
}
19+
exports.removeActionsCreator = removeActionsCreator;
1320
function isRemovedEvent(action) {
1421
return exports.REMOVED in action;
1522
}

lib/core/protocols/UPDATED_PROTOCOL.d.ts renamed to lib/core/protocols/UpdatedProtocol.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CachedAction } from '..';
2-
import { UpsertedAction } from './APPENDED_PROTOCOL';
2+
import { UpsertedAction } from './AppendProtocol';
33
export declare const UPDATED_PROTOCOL = "@@UPDATED_PROTOCOL";
44
export interface UpdatedProtocolFn<U = {}> {
55
(action: UpsertedAction, cachedAction: CachedAction<U>): U;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { CachedAction, RarAction } from '..';
2+
import { AnyAction } from 'redux';
3+
export declare const GARBAGE_COLLECTOR_PROTOCOL = "@@GARBAGE_COLLECTOR_PROTOCOL";
4+
export interface GarbageCollectorProtocolFn<U = {}> {
5+
(action: CachedAction<U>): boolean;
6+
}
7+
export declare type GarbageCollectorProtocol<U = {}> = {
8+
[GARBAGE_COLLECTOR_PROTOCOL]: GarbageCollectorProtocolFn<U>;
9+
};
10+
export declare const COLLECT_GARBAGE = "COLLECT_GARBAGE";
11+
export declare function collectGarbageActionCreator(): CollectGarbageCommand;
12+
export declare type CollectGarbageCommand = RarAction & {
13+
[COLLECT_GARBAGE]: true;
14+
};
15+
export declare function isCollectGarbageCommand(action: AnyAction): action is CollectGarbageCommand;
16+
export declare function isGarbageCollectorProtocol<U = {}>(action: any): action is GarbageCollectorProtocol<U>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const __1 = require("..");
4+
exports.GARBAGE_COLLECTOR_PROTOCOL = '@@GARBAGE_COLLECTOR_PROTOCOL';
5+
exports.COLLECT_GARBAGE = 'COLLECT_GARBAGE';
6+
function collectGarbageActionCreator() {
7+
return {
8+
type: __1.REDUX_ACTION_RETRY,
9+
[exports.COLLECT_GARBAGE]: true,
10+
};
11+
}
12+
exports.collectGarbageActionCreator = collectGarbageActionCreator;
13+
function isCollectGarbageCommand(action) {
14+
return exports.COLLECT_GARBAGE in action;
15+
}
16+
exports.isCollectGarbageCommand = isCollectGarbageCommand;
17+
function isGarbageCollectorProtocol(action) {
18+
return exports.GARBAGE_COLLECTOR_PROTOCOL in action;
19+
}
20+
exports.isGarbageCollectorProtocol = isGarbageCollectorProtocol;

lib/core/remove.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const ramda_1 = require("ramda");
44
const _1 = require(".");
55
const utils_1 = require("./utils");
6-
const REMOVED_PROTOCOL_1 = require("./protocols/REMOVED_PROTOCOL");
6+
const RemovedProtocol_1 = require("./protocols/RemovedProtocol");
77
function remove(_config) {
88
return {
99
reducer: () => (state = _1.INITIAL_STATE, action) => {
10-
if (REMOVED_PROTOCOL_1.isRemovedEvent(action)) {
10+
if (RemovedProtocol_1.isRemovedEvent(action)) {
1111
return ramda_1.over(utils_1.cacheLens, ramda_1.reject(ramda_1.anyPass([
1212
cachedAction => !!ramda_1.find(removedAction => {
1313
return removedAction.meta[_1.REDUX_ACTION_RETRY].id === cachedAction.action.meta[_1.REDUX_ACTION_RETRY].id;
14-
}, action[REMOVED_PROTOCOL_1.REMOVED]),
14+
}, action[RemovedProtocol_1.REMOVED]),
1515
])), state);
1616
}
1717
return state;

lib/core/retryAll.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Config } from '.';
2-
import { RetryAllCommand } from "./protocols/RETRY_ALL_PROTOCOL";
2+
import { RetryAllCommand } from "./protocols/RetryAllProtocol";
33
import { MiddlewareProtocol } from './protocols/MiddlewareProtocol';
44
export declare function retryAll(config: Config): MiddlewareProtocol;
55
export declare function retryAllActionCreator(): RetryAllCommand;

lib/core/retryAll.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const ramda_1 = require("ramda");
44
const _1 = require(".");
5-
const RETRY_ALL_PROTOCOL_1 = require("./protocols/RETRY_ALL_PROTOCOL");
5+
const RetryAllProtocol_1 = require("./protocols/RetryAllProtocol");
66
function retryAll(config) {
77
return {
88
middleware: visitors => {
99
const shouldRetryFunctions = visitors.reduce((acc, visitor) => {
10-
if (RETRY_ALL_PROTOCOL_1.isRetryAllProtocol(visitor)) {
11-
acc.push(visitor[RETRY_ALL_PROTOCOL_1.RETRY_ALL_PROTOCOL]);
10+
if (RetryAllProtocol_1.isRetryAllProtocol(visitor)) {
11+
acc.push(visitor[RetryAllProtocol_1.RETRY_ALL_PROTOCOL]);
1212
}
1313
return acc;
1414
}, []);

lib/core/upsert.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AnyAction } from 'redux';
22
import { Config } from '.';
33
import { RarAction, CacheableAction } from '.';
4-
import { UpsertedAction } from "./protocols/APPENDED_PROTOCOL";
4+
import { UpsertedAction } from "./protocols/AppendProtocol";
55
import { ReducerProtocol } from './protocols/ReducerProtocol';
66
import { MiddlewareProtocol } from './protocols/MiddlewareProtocol';
77
export declare const UPSERTED = "UPSERTED";

lib/core/upsert.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const ramda_1 = require("ramda");
44
const _1 = require(".");
55
const _2 = require(".");
6-
const UPDATED_PROTOCOL_1 = require("./protocols/UPDATED_PROTOCOL");
7-
const APPENDED_PROTOCOL_1 = require("./protocols/APPENDED_PROTOCOL");
6+
const UpdatedProtocol_1 = require("./protocols/UpdatedProtocol");
7+
const AppendProtocol_1 = require("./protocols/AppendProtocol");
88
const utils_1 = require("./utils");
99
exports.UPSERTED = 'UPSERTED';
1010
function upsert(config) {
@@ -18,11 +18,11 @@ function upsert(config) {
1818
},
1919
reducer: (initializedExtensions) => {
2020
const [appendedProtocols, updatedProtocols,] = initializedExtensions.reduce((acc, extension) => {
21-
if (APPENDED_PROTOCOL_1.isAppendedProtocol(extension)) {
22-
acc[0].push(extension[APPENDED_PROTOCOL_1.APPENDED_PROTOCOL]);
21+
if (AppendProtocol_1.isAppendedProtocol(extension)) {
22+
acc[0].push(extension[AppendProtocol_1.APPENDED_PROTOCOL]);
2323
}
24-
if (UPDATED_PROTOCOL_1.isUpdatedProtocol(extension)) {
25-
acc[1].push(extension[UPDATED_PROTOCOL_1.UPDATED_PROTOCOL]);
24+
if (UpdatedProtocol_1.isUpdatedProtocol(extension)) {
25+
acc[1].push(extension[UpdatedProtocol_1.UPDATED_PROTOCOL]);
2626
}
2727
return acc;
2828
}, [

lib/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export * from "./core";
2+
export * from "./cooldown";
3+
export * from "./times";
4+
export * from "./timeToLive";
5+
export * from "./core";

lib/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
function __export(m) {
3+
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4+
}
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
__export(require("./core"));
7+
__export(require("./cooldown"));
8+
__export(require("./times"));
9+
__export(require("./timeToLive"));
10+
__export(require("./core"));

0 commit comments

Comments
 (0)