Skip to content

Commit 7af8cf2

Browse files
author
edgar rodriguez
committed
add protocols
declare append and update protocol protocols
1 parent a2a4f54 commit 7af8cf2

Some content is hidden

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

64 files changed

+1271
-649
lines changed

.vscode/launch.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/lib/core/index.js",
12+
"outFiles": [
13+
"${workspaceFolder}/**/*.js"
14+
]
15+
}
16+
]
17+
}

lib/cooldown/index.d.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import { Config, VisitorNode, CachedAction, CacheableAction, RarAction } from "../core/index";
1+
import { Config, CacheableAction, RarAction } from "../core/index";
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";
36
export declare const CANCEL_COOLDOWN = "CANCEL_COOLDOWN";
47
export declare const COOL_AND_RETRY_ALL = "COOL_AND_RETRY_ALL";
5-
export declare type cancelCooldownAction = RarAction & {
6-
[CANCEL_COOLDOWN]: CacheableAction;
8+
export declare type cancelCooldownAction = RarAction & UpsertedAction & {
9+
[CANCEL_COOLDOWN]: true;
710
};
8-
export declare type coolAndRetryAllAction = RarAction & {
11+
export declare type coolAndRetryAllAction = RarAction & RetryAllCommand & {
912
[COOL_AND_RETRY_ALL]: true;
1013
};
1114
export declare type cooldownConfg = {
1215
cooldownTime: Duration;
1316
};
14-
export declare const coolDownUntilKey = "coolDownUntil";
1517
export declare type cooldownWrapAction = {
16-
[coolDownUntilKey]: Moment;
18+
coolDownUntil: Moment;
1719
};
18-
export declare function coolDownUntil(wrap: CachedAction<unknown>, config: Config<cooldownConfg, cooldownWrapAction>): Moment;
20+
export declare function coolDownUntil(action: CacheableAction, config: Config<cooldownConfg, cooldownWrapAction>): Moment;
1921
export declare function coolAndRetryAllActionCreator(): coolAndRetryAllAction;
2022
export declare function cancelCooldownActionCreator(action: CacheableAction): cancelCooldownAction;
21-
export declare function Cooldown(config: Config<cooldownConfg, cooldownWrapAction>): VisitorNode<cooldownWrapAction>;
23+
export declare function Cooldown(config: Config<cooldownConfg, cooldownWrapAction>): RetryAllProtocol<cooldownWrapAction> & AppendedProtocol<cooldownWrapAction> & UpdatedProtocol<cooldownWrapAction>;

lib/cooldown/index.js

+17-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
const ramda_1 = require("ramda");
43
const index_1 = require("../core/index");
54
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");
8+
const upsert_1 = require("../core/upsert");
69
exports.CANCEL_COOLDOWN = `CANCEL_COOLDOWN`;
710
exports.COOL_AND_RETRY_ALL = `COOL_AND_RETRY_ALL`;
8-
exports.coolDownUntilKey = 'coolDownUntil';
9-
function coolDownUntil(wrap, config) {
10-
return now_1.now().add(config.cache[wrap.action.type].cooldownTime);
11+
function coolDownUntil(action, config) {
12+
return now_1.now().add(config.cache[action.type].cooldownTime);
1113
}
1214
exports.coolDownUntil = coolDownUntil;
1315
function coolAndRetryAllActionCreator() {
@@ -21,32 +23,24 @@ exports.coolAndRetryAllActionCreator = coolAndRetryAllActionCreator;
2123
function cancelCooldownActionCreator(action) {
2224
return {
2325
type: index_1.REDUX_ACTION_RETRY,
24-
[exports.CANCEL_COOLDOWN]: action
26+
[upsert_1.UPSERTED]: action,
27+
[exports.CANCEL_COOLDOWN]: true,
2528
};
2629
}
2730
exports.cancelCooldownActionCreator = cancelCooldownActionCreator;
2831
function Cooldown(config) {
2932
return {
30-
shouldRetryAction(action, cachedAction) {
31-
return (action[exports.COOL_AND_RETRY_ALL])
33+
[RETRY_ALL_PROTOCOL_1.RETRY_ALL_PROTOCOL]: (action, cachedAction) => {
34+
return (exports.COOL_AND_RETRY_ALL in action)
3235
? true
33-
: now_1.now().isSameOrAfter(cachedAction[exports.coolDownUntilKey]);
36+
: now_1.now().isSameOrAfter(cachedAction.coolDownUntil);
3437
},
35-
actionWrapper(action) {
36-
return {
37-
[exports.coolDownUntilKey]: coolDownUntil(action, config)
38-
};
39-
},
40-
reducer: (state, action) => {
41-
if (action.type === index_1.REDUX_ACTION_RETRY && action[exports.CANCEL_COOLDOWN]) {
42-
const cancelCooldownAction = action;
43-
const coincidenceIndex = ramda_1.findIndex((cachedAction => cachedAction.action.meta[index_1.REDUX_ACTION_RETRY].id === cancelCooldownAction[exports.CANCEL_COOLDOWN].meta[index_1.REDUX_ACTION_RETRY].id), state.cache);
44-
return (coincidenceIndex < 0)
45-
? state
46-
: ramda_1.set(ramda_1.lensPath(['cache', coincidenceIndex, exports.coolDownUntilKey]), now_1.now(), state);
47-
}
48-
return state;
49-
}
38+
[UPDATED_PROTOCOL_1.UPDATED_PROTOCOL]: (action, cachedAction) => ({
39+
coolDownUntil: exports.CANCEL_COOLDOWN in action ? now_1.now() : coolDownUntil(cachedAction.action, config)
40+
}),
41+
[APPENDED_PROTOCOL_1.APPENDED_PROTOCOL]: (action) => ({
42+
coolDownUntil: coolDownUntil(action[upsert_1.UPSERTED], config)
43+
})
5044
};
5145
}
5246
exports.Cooldown = Cooldown;

lib/core/createRetryMechanism.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Config, State } from '.';
2+
import { Reducer, Middleware } from 'redux';
3+
declare type RetryMechanishm<T, U> = {
4+
stateKeyName: string;
5+
reducer: Reducer<State<U>>;
6+
reduxActionRetryMiddleware: Middleware<State<U>>[];
7+
};
8+
export declare function createRetryMechanishm<T, U>(initConfig: Partial<Config<T, U>>): RetryMechanishm<T, U>;
9+
export {};

lib/core/createRetryMechanism.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const ramda_1 = require("ramda");
4+
const _1 = require(".");
5+
const upsert_1 = require("./upsert");
6+
const reset_1 = require("./reset");
7+
const remove_1 = require("./remove");
8+
const retryAll_1 = require("./retryAll");
9+
const ReducerProtocol_1 = require("./protocols/ReducerProtocol");
10+
const MiddlewareProtocol_1 = require("./protocols/MiddlewareProtocol");
11+
function createRetryMechanishm(initConfig) {
12+
const defaultConfig = {
13+
stateKeyName: _1.REDUX_ACTION_RETRY,
14+
cache: {},
15+
extensions: [
16+
upsert_1.upsert,
17+
reset_1.reset,
18+
remove_1.remove,
19+
retryAll_1.retryAll,
20+
]
21+
};
22+
const config = ramda_1.mergeWithKey((k, l, r) => k === 'extensions' ? [...l, ...r] : r, defaultConfig, initConfig);
23+
const hotExtensions = config.extensions.map(e => e(config));
24+
const [protocols, visitors, middlewares] = hotExtensions.reduce((acc, ext) => {
25+
if (ReducerProtocol_1.isReducerProtocol(ext)) {
26+
acc[0].push(ext);
27+
}
28+
acc[1].push(ext);
29+
if (MiddlewareProtocol_1.isMiddleware(ext)) {
30+
acc[2].push(ext);
31+
}
32+
return acc;
33+
}, [[], [], []]);
34+
const reducers = protocols.reduce((acc, protocol) => {
35+
acc.push(protocol.reducer(visitors));
36+
return acc;
37+
}, []);
38+
return {
39+
stateKeyName: config.stateKeyName,
40+
reducer: function (state = _1.INITIAL_STATE, action) {
41+
return reducers.reduce((s, fn) => fn(s, action), state);
42+
},
43+
reduxActionRetryMiddleware: [
44+
...middlewares.map(m => m.middleware(visitors)),
45+
]
46+
};
47+
}
48+
exports.createRetryMechanishm = createRetryMechanishm;

lib/core/index.d.ts

+17-69
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,17 @@
1-
/// <reference types="ramda" />
2-
import { Reducer, AnyAction } from 'redux';
3-
export interface State<U> {
4-
cache: CachedAction<U>[];
5-
}
6-
export declare const INITIAL_STATE: {
7-
cache: any[];
8-
};
9-
export interface ShouldDoBasedOnActionAndCachedAction<U> {
10-
(action: AnyAction, cachedAction: CachedAction<U>): boolean;
11-
}
12-
export interface ActionWrapper<U> {
13-
(x: CachedAction<U>): U;
14-
}
15-
export interface VisitorNode<U> {
16-
shouldRetryAction?: ShouldDoBasedOnActionAndCachedAction<U>;
17-
actionWrapper?: ActionWrapper<U>;
18-
reducer?: Reducer<State<U>, AnyAction>;
19-
}
20-
export declare type configExtension<T = {}, U = {}> = (config: Config<T, U>) => VisitorNode<U>;
21-
export declare type cacheConfig<T> = {
22-
[s: string]: {
23-
[P in keyof T]: T[P];
24-
} & {
25-
type: string;
26-
};
27-
};
28-
export interface Config<T, U> {
29-
stateKeyName: string;
30-
cache: cacheConfig<T>;
31-
extensions: configExtension<T, U>[];
32-
}
33-
export declare const REDUX_ACTION_RETRY = "REDUX_ACTION_RETRY";
34-
export declare const RESET = "RESET";
35-
export declare const RETRY_ALL = "RETRY_ALL";
36-
export declare const REMOVE = "REMOVE";
37-
export interface CacheableAction extends AnyAction {
38-
meta: {
39-
[REDUX_ACTION_RETRY]: {
40-
id: string;
41-
};
42-
};
43-
}
44-
export declare type CachedAction<U> = {
45-
[P in keyof U]: U[P];
46-
} & {
47-
action: CacheableAction;
48-
};
49-
export declare type RarAction = {
50-
type: typeof REDUX_ACTION_RETRY;
51-
[RESET]?: true;
52-
[REMOVE]?: CacheableAction;
53-
[RETRY_ALL]?: true;
54-
};
55-
export declare function resetActionCreator(): RarAction;
56-
export declare function removeActionCreator(action: CacheableAction): RarAction;
57-
export declare function retryAllActionCreator(): RarAction;
58-
export declare const cacheLens: {
59-
<T, U>(obj: T): U;
60-
set<T, U, V>(val: T, obj: U): V;
61-
};
62-
export declare function createRetryMechanishm<T, U>(initConfig: Partial<Config<T, U>>): {
63-
stateKeyName: string;
64-
reducer: (state: State<U>, action: AnyAction) => State<U>;
65-
reduxActionRetryMiddleware: ({ getState, dispatch }: {
66-
getState: any;
67-
dispatch: any;
68-
}) => (next: any) => (action: AnyAction) => any;
69-
};
1+
export * from "./createRetryMechanism";
2+
export * from "./protocols/APPENDED_PROTOCOL";
3+
export * from "./protocols/MiddlewareProtocol";
4+
export * from "./protocols/REMOVED_PROTOCOL";
5+
export * from "./protocols/RETRY_ALL_PROTOCOL";
6+
export * from "./protocols/ReducerProtocol";
7+
export * from "./protocols/UPDATED_PROTOCOL";
8+
export * from "./remove";
9+
export * from "./reset";
10+
export * from "./retryAll";
11+
export * from "./types";
12+
export * from "./upsert";
13+
export * from "./utils";
14+
export * from "../cooldown";
15+
export * from "../removeAndRetryAll";
16+
export * from "../times";
17+
export * from "../timeToLive";

0 commit comments

Comments
 (0)