dry-ux 1.35.0 → 1.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,29 @@
1
1
  declare type Args<T = void> = {
2
2
  args: T;
3
+ /**
4
+ * If true, it will show a loader when a Dajaxice function is called. Defaults to false.
5
+ */
3
6
  loader?: boolean;
7
+ /**
8
+ * If true, it will cache the result of the Dajaxice function. Defaults to false.
9
+ */
10
+ cache?: boolean;
11
+ /**
12
+ * The duration in minutes to cache the result of the Dajaxice function. Defaults to 15 minutes.
13
+ */
14
+ cacheDuration?: number;
4
15
  skip?: {
16
+ /**
17
+ * If true, it will not check for authentication. Defaults to false.
18
+ */
5
19
  authCheck?: boolean;
20
+ /**
21
+ * If true, it will not use global error handler when a Dajaxice function is called. Defaults to false.
22
+ */
6
23
  errorHandler?: boolean;
24
+ /**
25
+ * If true, it will not show a loader when a Dajaxice function is called. Defaults to false.
26
+ */
7
27
  loader?: boolean;
8
28
  };
9
29
  };
@@ -15,10 +15,11 @@ const DajaxiceProxy = ({ modules, authCheck, onError, loader: showLoaderGlobal,
15
15
  get(target, module) {
16
16
  return new Proxy({}, {
17
17
  get(target, method) {
18
- return function ({ args, skip, loader: showLoaderLocal, } = {}) {
18
+ return function ({ args, skip, loader: showLoader, cache = false, cacheDuration = 15, } = {}) {
19
19
  return new Promise((resolve, reject) => {
20
20
  const methodName = window["Dajaxice"][module][method];
21
- const loader = (showLoaderGlobal || showLoaderLocal) && !(skip === null || skip === void 0 ? void 0 : skip.loader)
21
+ const api = `${module}.${method}`;
22
+ const loader = (showLoaderGlobal || showLoader) && !(skip === null || skip === void 0 ? void 0 : skip.loader)
22
23
  ? Loader_1.Loader.getInstance()
23
24
  : undefined;
24
25
  const hideLoader = () => {
@@ -37,12 +38,27 @@ const DajaxiceProxy = ({ modules, authCheck, onError, loader: showLoaderGlobal,
37
38
  reject(e);
38
39
  };
39
40
  const handleSuccess = (result) => {
41
+ handleResult(result);
42
+ if (cache) {
43
+ storeCache(api, args, result);
44
+ }
45
+ };
46
+ const handleResult = (result) => {
40
47
  hideLoader();
41
48
  resolve(result);
42
49
  };
43
- const fn = () => methodName(handleSuccess, args, {
44
- error_callback: handleError,
45
- });
50
+ const fn = () => {
51
+ if (cache) {
52
+ const cached = checkCache(api, args, cacheDuration);
53
+ if (cached.exists) {
54
+ handleResult(cached.data);
55
+ return;
56
+ }
57
+ }
58
+ return methodName(handleSuccess, args, {
59
+ error_callback: handleError,
60
+ });
61
+ };
46
62
  try {
47
63
  loader === null || loader === void 0 ? void 0 : loader.show();
48
64
  }
@@ -64,3 +80,29 @@ const DajaxiceProxy = ({ modules, authCheck, onError, loader: showLoaderGlobal,
64
80
  },
65
81
  });
66
82
  exports.DajaxiceProxy = DajaxiceProxy;
83
+ const getCacheId = (api, args) => {
84
+ return "dajaxice-cache-" + (0, utilities_1.toHashCode)(JSON.stringify({ api, args }));
85
+ };
86
+ const checkCache = (api, args, cacheDuration) => {
87
+ const cacheId = getCacheId(api, args);
88
+ let exists = false, data = null;
89
+ if (utilities_1.StorageUtils.isStorageAvailable() && sessionStorage.getItem(cacheId)) {
90
+ const cacheData = JSON.parse(sessionStorage.getItem(cacheId));
91
+ const timeDiff = Math.floor((new Date().getTime() - new Date(cacheData.storeTime).getTime()) / 1000 / 60);
92
+ if (timeDiff <= cacheDuration) {
93
+ exists = true;
94
+ data = cacheData.data;
95
+ }
96
+ }
97
+ return { exists, data };
98
+ };
99
+ const storeCache = (api, args, data) => {
100
+ const cacheId = getCacheId(api, args);
101
+ if (utilities_1.StorageUtils.isStorageAvailable()) {
102
+ try {
103
+ const cacheData = JSON.stringify({ storeTime: new Date(), data: data });
104
+ sessionStorage.setItem(cacheId, cacheData);
105
+ }
106
+ catch (e) { }
107
+ }
108
+ };
@@ -36,3 +36,7 @@ export declare const formatDollar: (amount: number, decimal_places?: boolean) =>
36
36
  * @param authRedirectUrl The URL to redirect to if the user is not authenticated.
37
37
  */
38
38
  export declare const fnWithAuthCheck: (fn: Function, authCheckUrl: string, authRedirectUrl: string) => Promise<void>;
39
+ export declare const StorageUtils: {
40
+ isStorageAvailable: () => boolean;
41
+ };
42
+ export declare const toHashCode: (input: string) => number;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fnWithAuthCheck = exports.formatDollar = exports.useCountdown = exports.importStyleSheet = exports.importScript = exports.preventDefault = void 0;
3
+ exports.toHashCode = exports.StorageUtils = exports.fnWithAuthCheck = exports.formatDollar = exports.useCountdown = exports.importStyleSheet = exports.importScript = exports.preventDefault = void 0;
4
4
  const React = require("react");
5
5
  /**
6
6
  * Returns a function that will call the given handler and prevent the default event behavior.
@@ -105,3 +105,27 @@ const fnWithAuthCheck = (fn, authCheckUrl, authRedirectUrl) => fetch(authCheckUr
105
105
  }
106
106
  });
107
107
  exports.fnWithAuthCheck = fnWithAuthCheck;
108
+ exports.StorageUtils = {
109
+ isStorageAvailable: function () {
110
+ let available = typeof Storage !== "undefined";
111
+ try {
112
+ if (available)
113
+ sessionStorage.setItem("test", "test");
114
+ }
115
+ catch (e) {
116
+ available = false;
117
+ }
118
+ return available;
119
+ },
120
+ };
121
+ const toHashCode = (input) => {
122
+ let hash = 0;
123
+ if (input.length === 0)
124
+ return hash;
125
+ for (let i = 0, len = input.length; i < len; i++) {
126
+ hash = (hash << 5) - hash + input.charCodeAt(i);
127
+ hash |= 0; // Convert to 32bit integer
128
+ }
129
+ return hash;
130
+ };
131
+ exports.toHashCode = toHashCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dry-ux",
3
- "version": "1.35.0",
3
+ "version": "1.37.0",
4
4
  "description": "",
5
5
  "main": "dist/index",
6
6
  "scripts": {