@azrico/global 1.3.13 → 1.3.16

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.
@@ -0,0 +1,6 @@
1
+ export * as gglobal from "./packages/core";
2
+ export * as gonce from "./packages/once";
3
+ export * as gcache from "./packages/cache";
4
+ export * as gstorage from "./packages/storage";
5
+ export * as gstate from "./packages/state";
6
+ export { gfilter, gfunc, gholder, gview } from "./packages/hooks";
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export * as gglobal from "./packages/core";
2
+ export * as gonce from "./packages/once";
3
+ export * as gcache from "./packages/cache";
4
+ export * as gstorage from "./packages/storage";
5
+ export * as gstate from "./packages/state";
6
+ export { gfilter, gfunc, gholder, gview } from "./packages/hooks";
@@ -0,0 +1,19 @@
1
+ interface get_cache_params {
2
+ search?: any;
3
+ }
4
+ interface cache_async_params extends get_cache_params {
5
+ normal?: boolean;
6
+ session?: boolean;
7
+ storage?: boolean;
8
+ local_storage?: boolean;
9
+ }
10
+ type cache_getterFunction<T> = (...params: any) => Promise<T> | T;
11
+ export declare function get_cache<T>(key: string, getterFunction?: cache_getterFunction<T>, params?: cache_async_params): Promise<T>;
12
+ export declare function set_cache(key: string, value: any, params?: cache_async_params): Promise<any>;
13
+ export declare function delete_cache(key: string, params?: cache_async_params): Promise<any>;
14
+ export declare function set_cache_sync(key: string, value: any): any;
15
+ export declare function get_cache_sync<T>(key: string, getterFunction?: (...params: any) => T, params?: cache_async_params): T | undefined;
16
+ export declare function delete_cache_sync(key: string): Promise<any>;
17
+ export declare function get_cacheFunc(key: string): any;
18
+ export declare function set_cacheFunc(key: string, f: Function): Function;
19
+ export {};
@@ -0,0 +1,244 @@
1
+ import glb from "../core";
2
+ import { gstorage } from "../../index";
3
+ const sname = "cache";
4
+ const name_pending = "pending_cache_loads";
5
+ const lname = "list_cache";
6
+ const lname_f = "list_cache_func";
7
+ export async function get_cache(key, getterFunction, params) {
8
+ key = glb.fixKey(key);
9
+ const cachePending_key = `cache-pending-${key}`;
10
+ const old_pending_cache = glb.Global_get(name_pending, cachePending_key);
11
+ if (old_pending_cache)
12
+ return await old_pending_cache;
13
+ const cache_loader = _get_cache_local(key, getterFunction, params);
14
+ glb.Global_set(name_pending, cachePending_key, cache_loader);
15
+ const cacheResponse = await cache_loader;
16
+ glb.Global_set(name_pending, cachePending_key, undefined);
17
+ return cacheResponse;
18
+ }
19
+ async function _get_cache_local(key, getterFunction, params) {
20
+ if (!params)
21
+ params = {};
22
+ const storage_key = `${sname}-${key}`;
23
+ /* -------------------------------------------------------------------------- */
24
+ /* get the cache */
25
+ /* -------------------------------------------------------------------------- */
26
+ let currentCache = "";
27
+ let cacheUpdated = false;
28
+ /* ----------------------- save to session by default ----------------------- */
29
+ if (!params.session && !params.storage && !params.local_storage) {
30
+ params.session = true;
31
+ }
32
+ currentCache = await gstorage.get_var(storage_key, {
33
+ local_storage: params.local_storage,
34
+ session: params.session,
35
+ storage: params.storage,
36
+ });
37
+ /* -------------------------------------------------------------------------- */
38
+ /* if cache not found use the cache func */
39
+ /* -------------------------------------------------------------------------- */
40
+ if (!currentCache) {
41
+ let currentCacheFunction = null;
42
+ if (typeof getterFunction === "function") {
43
+ currentCacheFunction = getterFunction;
44
+ }
45
+ else if (currentCacheFunction == null) {
46
+ currentCacheFunction = get_cacheFunc(key);
47
+ }
48
+ if (typeof currentCacheFunction === "function") {
49
+ currentCache = JSON.stringify(convert_to_cacheObject(await currentCacheFunction(params)));
50
+ cacheUpdated = true;
51
+ }
52
+ }
53
+ const currentCacheObject = load_cacheObject(currentCache);
54
+ const cache_data = get_cacheValue(currentCacheObject);
55
+ if (cacheUpdated && cache_data != null)
56
+ await set_cache(key, cache_data, params);
57
+ /* -------------------------------------------------------------------------- */
58
+ /* return */
59
+ /* -------------------------------------------------------------------------- */
60
+ if (!params.search)
61
+ return cache_data;
62
+ return searchResult(cache_data, params.search);
63
+ }
64
+ export async function set_cache(key, value, params) {
65
+ if (!params)
66
+ params = {};
67
+ key = glb.fixKey(key);
68
+ const storage_key = `${sname}-${key}`;
69
+ const currentCacheObjectStr = value != null ? JSON.stringify(convert_to_cacheObject(value)) : value;
70
+ if (!params.session && !params.storage && !params.local_storage) {
71
+ params.session = true;
72
+ }
73
+ await gstorage.set_var(storage_key, currentCacheObjectStr, {
74
+ local_storage: params.local_storage,
75
+ session: params.session,
76
+ storage: params.storage,
77
+ });
78
+ return value;
79
+ }
80
+ export async function delete_cache(key, params) {
81
+ return await set_cache(key, undefined, params);
82
+ }
83
+ /* -------------------------------------------------------------------------- */
84
+ /* -------------------------------------------------------------------------- */
85
+ /* -------------------------------------------------------------------------- */
86
+ export function set_cache_sync(key, value) {
87
+ if (value == null) {
88
+ return glb.Global_set(lname, key, value);
89
+ }
90
+ const currentCacheObjectStr = JSON.stringify(convert_to_cacheObject(value));
91
+ return glb.Global_set(lname, key, currentCacheObjectStr);
92
+ }
93
+ export function get_cache_sync(key, getterFunction, params) {
94
+ if (!params)
95
+ params = {};
96
+ key = glb.fixKey(key);
97
+ /* -------------------------------------------------------------------------- */
98
+ /* get the cache */
99
+ /* -------------------------------------------------------------------------- */
100
+ let currentCache = "";
101
+ let cacheUpdated = false;
102
+ currentCache = glb.Global_get(lname, key);
103
+ /* -------------------------------------------------------------------------- */
104
+ /* if cache not found use the cache func */
105
+ /* -------------------------------------------------------------------------- */
106
+ if (!currentCache) {
107
+ let currentCacheFunction = null;
108
+ if (typeof getterFunction === "function") {
109
+ currentCacheFunction = getterFunction;
110
+ }
111
+ else if (currentCacheFunction == null) {
112
+ currentCacheFunction = get_cacheFunc(key);
113
+ }
114
+ if (typeof currentCacheFunction === "function") {
115
+ currentCache = JSON.stringify(convert_to_cacheObject(currentCacheFunction(params)));
116
+ cacheUpdated = true;
117
+ }
118
+ }
119
+ const currentCacheObject = load_cacheObject(currentCache);
120
+ const cache_data = get_cacheValue(currentCacheObject);
121
+ if (cacheUpdated && cache_data != null)
122
+ set_cache_sync(key, cache_data);
123
+ /* -------------------------------------------------------------------------- */
124
+ /* return */
125
+ /* -------------------------------------------------------------------------- */
126
+ if (!params.search)
127
+ return cache_data;
128
+ return searchResult(cache_data, params.search);
129
+ }
130
+ export async function delete_cache_sync(key) {
131
+ return await set_cache_sync(key, undefined);
132
+ }
133
+ export function get_cacheFunc(key) {
134
+ return glb.Global_get(lname_f, key);
135
+ }
136
+ export function set_cacheFunc(key, f) {
137
+ return glb.Global_set(lname_f, key, f);
138
+ }
139
+ /* -------------------------------------------------------------------------- */
140
+ function convert_to_cacheObject(data) {
141
+ const data_type = typeof data;
142
+ const data_str = stringify_cacheValue(data);
143
+ return { original_type: data_type, raw_data: data_str };
144
+ }
145
+ function load_cacheObject(data) {
146
+ if (data === null || data === undefined)
147
+ return null;
148
+ if (typeof data === "object") {
149
+ return {
150
+ original_type: data.original_type,
151
+ raw_data: data.raw_data,
152
+ };
153
+ }
154
+ else if (typeof data === "string") {
155
+ try {
156
+ return load_cacheObject(JSON.parse(data));
157
+ }
158
+ catch { }
159
+ }
160
+ return null;
161
+ }
162
+ function get_cacheValue(data) {
163
+ if (data === null || data === undefined)
164
+ return null;
165
+ if (typeof data === "object") {
166
+ const { original_type, raw_data } = data;
167
+ if (original_type === "string")
168
+ return raw_data;
169
+ else
170
+ return JSON.parse(raw_data);
171
+ }
172
+ return null;
173
+ }
174
+ function stringify_cacheValue(data) {
175
+ if (data == null)
176
+ return "";
177
+ return typeof data != "string" ? JSON.stringify(data) : data;
178
+ }
179
+ /* -------------------------------------------------------------------------- */
180
+ function searchResult(value, search) {
181
+ if (typeof value != "object" || typeof search != "object")
182
+ return value; //cant search in non objects
183
+ if (value == null)
184
+ return value;
185
+ if (!isNaN(search._per_page) && search._per_page <= 0)
186
+ return null;
187
+ let fvalue = [];
188
+ if (Array.isArray(value)) {
189
+ for (let index = 0; index < value.length; index++) {
190
+ const vElem = gMatch(value[index], search);
191
+ if (vElem)
192
+ fvalue.push(vElem);
193
+ }
194
+ }
195
+ else {
196
+ const vElem = gMatch(value, search);
197
+ if (vElem)
198
+ return vElem;
199
+ }
200
+ if (!isNaN(search._per_page))
201
+ fvalue = fvalue.slice(0, Number(search._per_page));
202
+ return fvalue;
203
+ }
204
+ function gMatch(object, search) {
205
+ if (typeof object != "object" || typeof search != "object")
206
+ return object; //cant search in non objects
207
+ let isFullMatch = true;
208
+ for (const key in search) {
209
+ if (!key || key.startsWith("_"))
210
+ continue;
211
+ if (Object.hasOwnProperty.call(search, key) &&
212
+ Object.hasOwnProperty.call(object, key)) {
213
+ let isMatch = false;
214
+ const searchValue = search[key];
215
+ const objectValue = object[key];
216
+ if (objectValue === searchValue)
217
+ continue;
218
+ if (!isNaN(objectValue) && !isNaN(searchValue)) {
219
+ isMatch = false;
220
+ isFullMatch = false;
221
+ break;
222
+ }
223
+ let searchValueStr = String(searchValue);
224
+ if (searchValueStr.startsWith("!")) {
225
+ searchValueStr = searchValueStr.substr(1);
226
+ isMatch =
227
+ String(objectValue).toLowerCase() ==
228
+ searchValueStr.toLowerCase();
229
+ }
230
+ else {
231
+ isMatch = String(objectValue)
232
+ .toLowerCase()
233
+ .includes(String(searchValueStr).toLowerCase());
234
+ }
235
+ if (!isMatch) {
236
+ isFullMatch = false;
237
+ break;
238
+ }
239
+ }
240
+ }
241
+ if (isFullMatch)
242
+ return object;
243
+ return null;
244
+ }
@@ -0,0 +1 @@
1
+ export * from "./g_cache";
@@ -0,0 +1 @@
1
+ export * from "./g_cache";
@@ -0,0 +1,20 @@
1
+ type glbKey = string | true;
2
+ export declare class GlobalStorage {
3
+ static deus_globals: any;
4
+ static initGlobal(): false | undefined;
5
+ static Global_set<T>(type: string, key: glbKey, value: T): T;
6
+ /**
7
+ *
8
+ * @param type
9
+ * @param key
10
+ * @param value
11
+ * @returns total number of elements in targeted glb array
12
+ */
13
+ static Global_add(type: string, key: glbKey, value: any): number;
14
+ static Global_remove(type: string, key: glbKey, value: any): any;
15
+ static Global_get(type: string, key: glbKey): any;
16
+ static fixKey(key: boolean): boolean;
17
+ static fixKey(key: string): string;
18
+ static loadGlobals(type: string): void;
19
+ }
20
+ export default GlobalStorage;
@@ -0,0 +1,91 @@
1
+ export class GlobalStorage {
2
+ static deus_globals;
3
+ static initGlobal() {
4
+ if (GlobalStorage == null)
5
+ return false;
6
+ if (GlobalStorage.deus_globals == undefined)
7
+ GlobalStorage.deus_globals = {};
8
+ }
9
+ /* -------------------------------------------------------------------------- */
10
+ static Global_set(type, key, value) {
11
+ GlobalStorage.loadGlobals(type);
12
+ if (key === true) {
13
+ if (value == null || value == undefined)
14
+ delete GlobalStorage.deus_globals[type];
15
+ else
16
+ GlobalStorage.deus_globals[type] = value;
17
+ GlobalStorage.deus_globals[type] = value;
18
+ }
19
+ else {
20
+ key = GlobalStorage.fixKey(key);
21
+ if (value == null || value == undefined)
22
+ delete GlobalStorage.deus_globals[type][key];
23
+ else
24
+ GlobalStorage.deus_globals[type][key] = value;
25
+ }
26
+ return value;
27
+ }
28
+ /**
29
+ *
30
+ * @param type
31
+ * @param key
32
+ * @param value
33
+ * @returns total number of elements in targeted glb array
34
+ */
35
+ static Global_add(type, key, value) {
36
+ if (typeof key !== "string") {
37
+ throw new Error("Key was not a string");
38
+ }
39
+ key = GlobalStorage.fixKey(key);
40
+ GlobalStorage.loadGlobals(type);
41
+ var cval = GlobalStorage.deus_globals[type][key];
42
+ if (cval == null || !Array.isArray(cval)) {
43
+ if (cval == null) {
44
+ cval = [];
45
+ }
46
+ else
47
+ cval = [cval];
48
+ }
49
+ cval.push(value);
50
+ GlobalStorage.Global_set(type, key, cval);
51
+ return cval.length;
52
+ }
53
+ static Global_remove(type, key, value) {
54
+ if (typeof key !== "string") {
55
+ throw new Error("Key was not a string");
56
+ }
57
+ GlobalStorage.loadGlobals(type);
58
+ key = GlobalStorage.fixKey(key);
59
+ var cval = GlobalStorage.deus_globals[type][key];
60
+ if (cval == null)
61
+ cval = [];
62
+ if (!Array.isArray(cval)) {
63
+ console.warn(`Targeted global was not array ${key}`);
64
+ return false;
65
+ }
66
+ let newValue = cval.filter((s) => s !== value);
67
+ return GlobalStorage.Global_set(type, key, newValue);
68
+ }
69
+ static Global_get(type, key) {
70
+ if (!GlobalStorage.deus_globals)
71
+ return undefined;
72
+ if (typeof key === "boolean")
73
+ return GlobalStorage.deus_globals[type];
74
+ key = GlobalStorage.fixKey(key);
75
+ return GlobalStorage.deus_globals[type]?.[key];
76
+ }
77
+ static fixKey(key) {
78
+ if (typeof key === "boolean")
79
+ return key;
80
+ return String(Array.isArray(key) ? key.join("-") : key)
81
+ .toLowerCase()
82
+ .replace(" ", "_");
83
+ }
84
+ static loadGlobals(type) {
85
+ if (GlobalStorage.deus_globals == undefined)
86
+ GlobalStorage.deus_globals = {};
87
+ if (GlobalStorage.deus_globals[type] == undefined)
88
+ GlobalStorage.deus_globals[type] = {};
89
+ }
90
+ }
91
+ export default GlobalStorage;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Adds a filter
3
+ * @param key
4
+ * @param action action of the filter
5
+ * @param priority filters with lower priority are executed first
6
+ * @returns
7
+ */
8
+ export declare function add_filter(key: string, action: Function, priority?: number): any;
9
+ export declare function remove_filter(key: string, action: Function): any;
10
+ export declare function clear_filter(key: string): any;
11
+ export declare function clear_all_filters(): any;
12
+ /**
13
+ * Apply a filter asynchronously
14
+ * @param key
15
+ * @param initialValue
16
+ * @returns
17
+ */
18
+ export declare function apply_filter(key: string, initialValue?: any): Promise<any>;
19
+ /**
20
+ * Apply a filter synchronously
21
+ * @param key
22
+ * @param initialValue
23
+ * @returns
24
+ */
25
+ export declare function apply_filter_sync(key: string, initialValue?: any): any;
26
+ /**
27
+ * Adds a filter until the current component unmounts
28
+ * @param key
29
+ * @param action
30
+ * @param priority
31
+ * @returns
32
+ */
33
+ export declare function hold_add_filter(key: string, action: (...args: any[]) => any, priority?: number): any;
@@ -0,0 +1,84 @@
1
+ import React from "react";
2
+ import glb from "../core";
3
+ /* -------------------------------------------------------------------------- */
4
+ /* Filters */
5
+ /* -------------------------------------------------------------------------- */
6
+ const lname_filter = "list_filter";
7
+ /**
8
+ * Adds a filter
9
+ * @param key
10
+ * @param action action of the filter
11
+ * @param priority filters with lower priority are executed first
12
+ * @returns
13
+ */
14
+ export function add_filter(key, action, priority = 10) {
15
+ var fobject = { key: key, action: action, priority: priority };
16
+ return glb.Global_add(lname_filter, key, fobject);
17
+ }
18
+ export function remove_filter(key, action) {
19
+ return glb.Global_remove(lname_filter, key, action);
20
+ }
21
+ export function clear_filter(key) {
22
+ return glb.Global_set(lname_filter, key, null);
23
+ }
24
+ export function clear_all_filters() {
25
+ return glb.Global_set(lname_filter, true, null);
26
+ }
27
+ /**
28
+ * Apply a filter asynchronously
29
+ * @param key
30
+ * @param initialValue
31
+ * @returns
32
+ */
33
+ export async function apply_filter(key, initialValue = null) {
34
+ var filters = glb.Global_get(lname_filter, key);
35
+ if (!Array.isArray(filters) || filters.length == 0)
36
+ return initialValue;
37
+ filters.sort((a, b) => a.priority > b.priority ? 1 : -1); /* lower priority gets executed first */
38
+ let previous_value = initialValue;
39
+ for (let index = 0; index < filters.length; index++) {
40
+ const m_filter = filters[index];
41
+ let new_value = await m_filter.action(previous_value);
42
+ //console.log("filtered ", previous_value, " --> ", new_value);
43
+ previous_value = new_value;
44
+ }
45
+ return previous_value;
46
+ }
47
+ /**
48
+ * Apply a filter synchronously
49
+ * @param key
50
+ * @param initialValue
51
+ * @returns
52
+ */
53
+ export function apply_filter_sync(key, initialValue = null) {
54
+ var filters = glb.Global_get(lname_filter, key);
55
+ if (!Array.isArray(filters) || filters.length == 0)
56
+ return initialValue;
57
+ filters.sort((a, b) => a.priority > b.priority ? 1 : -1); /* lower priority gets executed first */
58
+ let previous_value = initialValue;
59
+ for (let index = 0; index < filters.length; index++) {
60
+ const m_filter = filters[index];
61
+ let new_value = m_filter.action(previous_value);
62
+ //console.log("filtered ", previous_value, " --> ", new_value);
63
+ if (new_value !== undefined)
64
+ previous_value = new_value;
65
+ }
66
+ return previous_value;
67
+ }
68
+ /* -------------------------------------------------------------------------- */
69
+ /**
70
+ * Adds a filter until the current component unmounts
71
+ * @param key
72
+ * @param action
73
+ * @param priority
74
+ * @returns
75
+ */
76
+ export function hold_add_filter(key, action, priority = 10) {
77
+ const filterFunction = React.useCallback(action, []);
78
+ React.useEffect(() => {
79
+ add_filter(key, filterFunction, priority);
80
+ return () => {
81
+ remove_filter(key, filterFunction);
82
+ };
83
+ }, []);
84
+ }
@@ -0,0 +1,58 @@
1
+ export type actionFunction = (...args: any[]) => any | Promise<any>;
2
+ export declare function get_action(key: string): Array<actionFunction> | actionFunction | null;
3
+ /**
4
+ * set action in action list by its key , you can later run this action using the key
5
+ * @param key
6
+ * @param value
7
+ * @returns
8
+ */
9
+ export declare function set_action(key: string, func: actionFunction | null): any;
10
+ /**
11
+ * Adds an action to action list, you can later run those actions
12
+ * @param key
13
+ * @param value
14
+ * @returns
15
+ */
16
+ export declare function add_action(key: string, func: actionFunction): any;
17
+ /**
18
+ * remove an action from action list
19
+ * @param key
20
+ * @param value
21
+ * @returns
22
+ */
23
+ export declare function remove_action(key: string, value: actionFunction): any;
24
+ /**
25
+ * Runs an action or a list of actions by their key, then clear the action list
26
+ * @param key
27
+ * @param args
28
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
29
+ */
30
+ export declare function run_action_clear(key: string, ...args: any[]): Promise<any>;
31
+ /**
32
+ * Runs an action or a list of actions by their key,
33
+ * @param key
34
+ * @param args
35
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
36
+ */
37
+ export declare function run_action(key: string, ...args: any[]): Promise<any>;
38
+ /**
39
+ * Runs an action or a list of actions by their key,
40
+ * @param key
41
+ * @param args
42
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
43
+ */
44
+ export declare function run_action_sync(key: string, ...args: any[]): any;
45
+ /**
46
+ * Adds an action until the current component unmounts
47
+ * @param key
48
+ * @param value
49
+ * @returns
50
+ */
51
+ export declare function hold_add_action(key: string, func: actionFunction): any;
52
+ /**
53
+ * set an action until the current component unmounts
54
+ * @param key
55
+ * @param value
56
+ * @returns
57
+ */
58
+ export declare function hold_set_action(key: string, func: actionFunction): any;
@@ -0,0 +1,154 @@
1
+ import React from "react";
2
+ import glb from "../core";
3
+ const lname = "list_func";
4
+ /* -------------------------------------------------------------------------- */
5
+ export function get_action(key) {
6
+ const res = glb.Global_get(lname, key);
7
+ return Array.isArray(res) || typeof res === "function" ? res : null;
8
+ }
9
+ /**
10
+ * set action in action list by its key , you can later run this action using the key
11
+ * @param key
12
+ * @param value
13
+ * @returns
14
+ */
15
+ export function set_action(key, func) {
16
+ return glb.Global_set(lname, key, func);
17
+ }
18
+ /**
19
+ * Adds an action to action list, you can later run those actions
20
+ * @param key
21
+ * @param value
22
+ * @returns
23
+ */
24
+ export function add_action(key, func) {
25
+ return glb.Global_add(lname, key, func);
26
+ }
27
+ /**
28
+ * remove an action from action list
29
+ * @param key
30
+ * @param value
31
+ * @returns
32
+ */
33
+ export function remove_action(key, value) {
34
+ return glb.Global_remove(lname, key, value);
35
+ }
36
+ /**
37
+ * Runs an action or a list of actions by their key, then clear the action list
38
+ * @param key
39
+ * @param args
40
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
41
+ */
42
+ export async function run_action_clear(key, ...args) {
43
+ const result = await run_action(key, ...args);
44
+ set_action(key, null);
45
+ return result;
46
+ }
47
+ /**
48
+ * Runs an action or a list of actions by their key,
49
+ * @param key
50
+ * @param args
51
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
52
+ */
53
+ export async function run_action(key, ...args) {
54
+ let funcList = get_action(key);
55
+ if (funcList == null)
56
+ return null;
57
+ //console.log("run_action", funcList);
58
+ if (typeof funcList === "function") {
59
+ let func = funcList;
60
+ return await func(...args);
61
+ //if (func.constructor.name == "AsyncFunction") {
62
+ // //console.log("Doing async f");
63
+ //
64
+ // return await func(...args);
65
+ //} else {
66
+ // return func(...args);
67
+ //}
68
+ }
69
+ if (Array.isArray(funcList)) {
70
+ let resultObject = [];
71
+ for (let index = 0; index < funcList.length; index++) {
72
+ const func = funcList[index];
73
+ if (typeof func === "function") {
74
+ let functionResult = null;
75
+ functionResult = await func(...args);
76
+ //if (func.constructor.name == "AsyncFunction") {
77
+ // //console.log("Doing async f");
78
+ // functionResult = await func(...args);
79
+ //} else {
80
+ // functionResult = func(...args);
81
+ //}
82
+ resultObject.push(functionResult);
83
+ }
84
+ else {
85
+ console.error("[run_action]", "non function detected ", "key:", key);
86
+ }
87
+ }
88
+ return resultObject;
89
+ }
90
+ else {
91
+ console.error("[run_action]", "invalid functions detected ", "key:", key);
92
+ }
93
+ }
94
+ /**
95
+ * Runs an action or a list of actions by their key,
96
+ * @param key
97
+ * @param args
98
+ * @returns result of action if there is a single action , or an array of results if there are multiple actions
99
+ */
100
+ export function run_action_sync(key, ...args) {
101
+ let funcList = get_action(key);
102
+ if (funcList == null)
103
+ return null;
104
+ if (typeof funcList === "function") {
105
+ let func = funcList;
106
+ return func(...args);
107
+ }
108
+ if (Array.isArray(funcList)) {
109
+ let resultObject = [];
110
+ for (let index = 0; index < funcList.length; index++) {
111
+ const func = funcList[index];
112
+ if (typeof func === "function") {
113
+ let functionResult = null;
114
+ functionResult = func(...args);
115
+ resultObject.push(functionResult);
116
+ }
117
+ else {
118
+ console.error("[run_action]", "non function detected ", "key:", key);
119
+ }
120
+ }
121
+ return resultObject;
122
+ }
123
+ else {
124
+ console.error("[run_action]", "invalid functions detected ", "key:", key);
125
+ }
126
+ }
127
+ /* -------------------------------------------------------------------------- */
128
+ /* -------------------------------------------------------------------------- */
129
+ /**
130
+ * Adds an action until the current component unmounts
131
+ * @param key
132
+ * @param value
133
+ * @returns
134
+ */
135
+ export function hold_add_action(key, func) {
136
+ const actionFunction = React.useCallback(func, []);
137
+ React.useEffect(() => {
138
+ add_action(key, actionFunction);
139
+ return () => remove_action(key, actionFunction);
140
+ }, []);
141
+ }
142
+ /**
143
+ * set an action until the current component unmounts
144
+ * @param key
145
+ * @param value
146
+ * @returns
147
+ */
148
+ export function hold_set_action(key, func) {
149
+ const actionFunction = React.useCallback(func, []);
150
+ React.useEffect(() => {
151
+ set_action(key, actionFunction);
152
+ return () => set_action(key, null);
153
+ }, []);
154
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Holds a key until the current component unmounts
3
+ * you can use this to see if a certain component is alive or not
4
+ * @param key
5
+ */
6
+ export declare function hold_key(key: string, value?: string, isActive?: boolean): any;
7
+ export declare function is_key_held(key: string): any;
@@ -0,0 +1,23 @@
1
+ import React from "react";
2
+ import glb from "../core";
3
+ const lname_hold = "list_hold_keys";
4
+ /**
5
+ * Holds a key until the current component unmounts
6
+ * you can use this to see if a certain component is alive or not
7
+ * @param key
8
+ */
9
+ export function hold_key(key, value = "", isActive = true) {
10
+ const hvar = React.useMemo(() => Symbol(value), []);
11
+ React.useEffect(() => {
12
+ const hkey = key;
13
+ if (isActive)
14
+ glb.Global_add(lname_hold, hkey, hvar);
15
+ else
16
+ glb.Global_remove(lname_hold, hkey, hvar);
17
+ return () => glb.Global_remove(lname_hold, hkey, hvar);
18
+ }, [isActive]);
19
+ }
20
+ export function is_key_held(key) {
21
+ const gval = glb.Global_get(lname_hold, key);
22
+ return Array.isArray(gval) && gval.length > 0;
23
+ }
@@ -0,0 +1,32 @@
1
+ import React from "react";
2
+ type viewType = React.ReactNode | ((props: any) => React.ReactNode);
3
+ /**
4
+ * Adds a view to view list
5
+ * @param key
6
+ * @param action action of the filter
7
+ * @param priority filters with lower priority are executed first
8
+ * @returns
9
+ */
10
+ export declare function add_view(key: string, view: viewType, priority?: number): any;
11
+ /**
12
+ *
13
+ * @param view_key
14
+ * @param initialValue
15
+ * @returns
16
+ */
17
+ export declare function load_views(view_key: string, initialValue?: viewType, child_props?: any): viewType | undefined;
18
+ /**
19
+ *
20
+ * @param view_key
21
+ * @param initialValue
22
+ * @returns
23
+ */
24
+ export declare function use_views(view_key: string, initialValue?: viewType, child_props?: any): any;
25
+ interface ViewsContainerProps {
26
+ view_key: string;
27
+ initialValue?: viewType;
28
+ children?: React.ReactNode;
29
+ children_props?: any;
30
+ }
31
+ export declare function ViewsContainer(props: ViewsContainerProps): JSX.Element;
32
+ export {};
@@ -0,0 +1,57 @@
1
+ import React from "react";
2
+ import glb from "../core";
3
+ /* -------------------------------------------------------------------------- */
4
+ /* Filters */
5
+ /* -------------------------------------------------------------------------- */
6
+ const lname_views = "list_views";
7
+ /**
8
+ * Adds a view to view list
9
+ * @param key
10
+ * @param action action of the filter
11
+ * @param priority filters with lower priority are executed first
12
+ * @returns
13
+ */
14
+ export function add_view(key, view, priority = 10) {
15
+ const view_object = {
16
+ view_key: key,
17
+ view: view,
18
+ priority: priority,
19
+ };
20
+ return glb.Global_add(lname_views, key, view_object);
21
+ }
22
+ // export function remove_view(key: string, view: React.ReactNode ): any {
23
+ // const view_object = {key: key, view: view, priority: priority};
24
+ // return glb.Global_add(lname_filter, key, view);
25
+ // }
26
+ /**
27
+ *
28
+ * @param view_key
29
+ * @param initialValue
30
+ * @returns
31
+ */
32
+ export function load_views(view_key, initialValue, child_props) {
33
+ var view_list = glb.Global_get(lname_views, view_key);
34
+ if (!Array.isArray(view_list) || view_list.length == 0)
35
+ return initialValue;
36
+ view_list.sort((a, b) => a.priority > b.priority ? 1 : -1); /* lower priority gets executed first */
37
+ return view_list.map((r, i) => typeof r.view === "function"
38
+ ? r.view({ ...child_props, key: String(i) })
39
+ : r.view);
40
+ }
41
+ /**
42
+ *
43
+ * @param view_key
44
+ * @param initialValue
45
+ * @returns
46
+ */
47
+ export function use_views(view_key, initialValue, child_props) {
48
+ return React.useMemo(() => {
49
+ return load_views(view_key, initialValue, child_props);
50
+ }, []);
51
+ }
52
+ export function ViewsContainer(props) {
53
+ const viewlist = use_views(props.view_key, props.initialValue, props.children_props);
54
+ return (React.createElement("div", null,
55
+ props.children,
56
+ viewlist));
57
+ }
@@ -0,0 +1,4 @@
1
+ export * as gfunc from "./g_func";
2
+ export * as gfilter from "./g_filter";
3
+ export * as gview from "./g_view";
4
+ export * as gholder from "./g_holder";
@@ -0,0 +1,4 @@
1
+ export * as gfunc from "./g_func";
2
+ export * as gfilter from "./g_filter";
3
+ export * as gview from "./g_view";
4
+ export * as gholder from "./g_holder";
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Runs the specefic function only once
3
+ * @param key
4
+ * @param f
5
+ * @returns
6
+ */
7
+ export declare function do_once(f: Function): Promise<any>;
8
+ /**
9
+ * Runs the specefic function only once
10
+ * @param key
11
+ * @param f
12
+ * @returns
13
+ */
14
+ export declare function do_once(f: Function, key: string, ifNot?: Function): Promise<any>;
15
+ export declare function reset_once(key?: any): any;
@@ -0,0 +1,24 @@
1
+ import glb from "../core";
2
+ const lname = "list_once";
3
+ export async function do_once(f, key, ifNot) {
4
+ if (!f || typeof f !== "function")
5
+ return null;
6
+ if (!key || key == "")
7
+ key = "fn-" + f.name;
8
+ const isDone = glb.Global_get(lname, key);
9
+ if (isDone == "true") {
10
+ if (ifNot)
11
+ return await ifNot();
12
+ return undefined;
13
+ }
14
+ glb.Global_set(lname, key, "true");
15
+ return await f();
16
+ }
17
+ export function reset_once(key = "") {
18
+ if (!key || key == "") {
19
+ glb.Global_set(lname, true, null);
20
+ return true;
21
+ }
22
+ glb.Global_set(lname, key, null);
23
+ return true;
24
+ }
@@ -0,0 +1,7 @@
1
+ export * from "./g_once";
2
+ declare const _default: {
3
+ do_once(f: Function): Promise<any>;
4
+ do_once(f: Function, key: string, ifNot?: Function | undefined): Promise<any>;
5
+ reset_once(key?: any): any;
6
+ };
7
+ export default _default;
@@ -0,0 +1,3 @@
1
+ export * from "./g_once";
2
+ import * as g_once from "./g_once";
3
+ export default { ...g_once };
@@ -0,0 +1,48 @@
1
+ import { TUpdateFunction } from "pullstate";
2
+ /**
3
+ * Set state
4
+ * @param state_name update function
5
+ * @returns false if StateHandler is not defined, true otherwise
6
+ */
7
+ export declare function set_state(state_updater: TUpdateFunction<any>): boolean;
8
+ /**
9
+ * Set state
10
+ * @param state_name state name
11
+ * @param value
12
+ * @returns false if StateHandler is not defined, true otherwise
13
+ */
14
+ export declare function set_state(state_name: string, value?: any): boolean;
15
+ /**
16
+ * Add value to state array
17
+ * @param state_name
18
+ * @param value
19
+ */
20
+ export declare function add_state(state_name: string, value: any): void;
21
+ /**
22
+ * remove value from state array
23
+ * @param state_name
24
+ * @param value
25
+ */
26
+ export declare function remove_state(state_name: string, value: any): void;
27
+ /**
28
+ * Add value to state object
29
+ * @param state_name
30
+ * @param key
31
+ * @param value
32
+ */
33
+ export declare function set_keystate(state_name: string, key: string, value: any): void;
34
+ /**
35
+ * remove value from state object
36
+ * @param state_name
37
+ * @param key
38
+ */
39
+ export declare function remove_keystate(state_name: string, key: string): void;
40
+ /**
41
+ * Returns a state by its name
42
+ * @param state_name
43
+ * @returns the state
44
+ */
45
+ export declare function use_state<T>(state_name: string): T | undefined;
46
+ export declare function hold_set_state(state_name: string, value: any): any;
47
+ export declare function hold_set_keystate(state_name: string, key: string, value: any): any;
48
+ export declare function hold_add_state(state_name: string, value: any): any;
@@ -0,0 +1,128 @@
1
+ import { Store } from "pullstate";
2
+ import React from "react";
3
+ const sd = {};
4
+ const DataStore = new Store(sd);
5
+ export function set_state(key, value) {
6
+ DataStore.update((s) => {
7
+ if (!s || typeof s != "object")
8
+ s = {};
9
+ if (typeof key === "function") {
10
+ const updRes = key(s);
11
+ if (updRes != undefined)
12
+ s = updRes;
13
+ }
14
+ else {
15
+ s[key] = value;
16
+ }
17
+ });
18
+ return true;
19
+ }
20
+ /**
21
+ * Add value to state array
22
+ * @param state_name
23
+ * @param value
24
+ */
25
+ export function add_state(state_name, value) {
26
+ set_state((s) => {
27
+ if (!s || typeof s != "object")
28
+ s = {};
29
+ const oldval = Array.isArray(s[state_name]) ? s[state_name] : [];
30
+ s[state_name] = [...oldval, value];
31
+ return s;
32
+ });
33
+ }
34
+ /**
35
+ * remove value from state array
36
+ * @param state_name
37
+ * @param value
38
+ */
39
+ export function remove_state(state_name, value) {
40
+ set_state((s) => {
41
+ if (s) {
42
+ const oldval = Array.isArray(s[state_name])
43
+ ? s[state_name]
44
+ : [];
45
+ s[state_name] = oldval.filter((v) => v !== value);
46
+ }
47
+ return s;
48
+ });
49
+ }
50
+ /**
51
+ * Add value to state object
52
+ * @param state_name
53
+ * @param key
54
+ * @param value
55
+ */
56
+ export function set_keystate(state_name, key, value) {
57
+ set_state((s) => {
58
+ if (!s || typeof s != "object")
59
+ s = {};
60
+ let oldval = s[state_name];
61
+ if (oldval == null || typeof oldval !== "object") {
62
+ oldval = {};
63
+ }
64
+ else
65
+ oldval = { ...oldval };
66
+ oldval[key] = value;
67
+ s[state_name] = oldval;
68
+ return s;
69
+ });
70
+ }
71
+ /**
72
+ * remove value from state object
73
+ * @param state_name
74
+ * @param key
75
+ */
76
+ export function remove_keystate(state_name, key) {
77
+ set_state((s) => {
78
+ if (s) {
79
+ let oldval = s[state_name];
80
+ if (oldval == null || typeof oldval !== "object") {
81
+ oldval = {};
82
+ }
83
+ else {
84
+ // oldval = { ...oldval };
85
+ delete oldval[key];
86
+ }
87
+ s[state_name] = oldval;
88
+ }
89
+ return s;
90
+ });
91
+ }
92
+ /**
93
+ * Returns a state by its name
94
+ * @param state_name
95
+ * @returns the state
96
+ */
97
+ export function use_state(state_name) {
98
+ return DataStore.useState((s) => s[state_name]);
99
+ }
100
+ /* -------------------------------------------------------------------------- */
101
+ /* -------------------------------------------------------------------------- */
102
+ export function hold_set_state(state_name, value) {
103
+ React.useEffect(() => {
104
+ set_state(state_name, value);
105
+ return () => {
106
+ set_state(state_name, undefined);
107
+ };
108
+ }, [value]);
109
+ return value;
110
+ }
111
+ export function hold_set_keystate(state_name, key, value) {
112
+ React.useEffect(() => {
113
+ set_keystate(state_name, key, value);
114
+ return () => {
115
+ set_keystate(state_name, key, undefined);
116
+ };
117
+ }, [value]);
118
+ return value;
119
+ }
120
+ export function hold_add_state(state_name, value) {
121
+ React.useEffect(() => {
122
+ add_state(state_name, value);
123
+ return () => {
124
+ remove_state(state_name, undefined);
125
+ };
126
+ }, [value]);
127
+ return value;
128
+ }
@@ -0,0 +1 @@
1
+ export * from "./g_state";
@@ -0,0 +1 @@
1
+ export * from "./g_state";
@@ -0,0 +1,13 @@
1
+ export interface setVarOptions {
2
+ normal?: boolean;
3
+ session?: boolean;
4
+ storage?: boolean;
5
+ local_storage?: boolean;
6
+ json?: boolean;
7
+ }
8
+ export declare function add_var(key: string, value: any, options?: setVarOptions): Promise<any>;
9
+ export declare function get_var_sync(key: string): any;
10
+ export declare function set_var_sync(key: string, value: any): any;
11
+ export declare function get_var(key: string, options?: setVarOptions): Promise<any>;
12
+ export declare function set_var(key: string, value: any, options?: setVarOptions): Promise<any>;
13
+ export declare function hold_var<T>(key: string, value: T, options?: setVarOptions): T;
@@ -0,0 +1,124 @@
1
+ import AsyncStorage from "@react-native-async-storage/async-storage";
2
+ import React from "react";
3
+ import { gencryption } from "../../index";
4
+ import glb from "../core";
5
+ const lname = "list_var";
6
+ export async function add_var(key, value, options) {
7
+ var foundValue = await get_var(key, options);
8
+ if (!Array.isArray(foundValue))
9
+ foundValue = [];
10
+ foundValue.push(value);
11
+ await set_var(key, foundValue, options);
12
+ return foundValue;
13
+ }
14
+ export function get_var_sync(key) {
15
+ let foundValue = glb.Global_get(lname, key);
16
+ var isString = typeof foundValue == "string";
17
+ if (!isString)
18
+ return foundValue;
19
+ foundValue = gencryption.Decode_Default(foundValue);
20
+ return foundValue;
21
+ }
22
+ export function set_var_sync(key, value) {
23
+ let final_value = value;
24
+ var isString = typeof final_value == "string";
25
+ if (isString) {
26
+ /* Encode any string */
27
+ final_value = gencryption.Encode_Default(final_value);
28
+ }
29
+ glb.Global_set(lname, key, final_value);
30
+ return final_value;
31
+ }
32
+ export async function get_var(key, options) {
33
+ if (!options)
34
+ options = {};
35
+ var jsonify = options.json;
36
+ var foundValue = null;
37
+ /* -------------------------------------------------------------------------- */
38
+ foundValue = glb.Global_get(lname, key);
39
+ if (foundValue == null && options.local_storage) {
40
+ const fname = lname + "_" + key;
41
+ foundValue = localStorage[fname]; // only strings
42
+ }
43
+ if (foundValue == null && options.session) {
44
+ const sessionStorage = window.sessionStorage;
45
+ foundValue = await sessionStorage.getItem(key);
46
+ }
47
+ if (foundValue == null && options.storage) {
48
+ foundValue = await AsyncStorage.getItem(key);
49
+ }
50
+ if (foundValue == null)
51
+ return null;
52
+ /* -------------------------------------------------------------------------- */
53
+ var isString = typeof foundValue == "string";
54
+ if (!isString)
55
+ return foundValue;
56
+ foundValue = await gencryption.Decode_Default(foundValue);
57
+ if (jsonify) {
58
+ try {
59
+ foundValue = JSON.parse(foundValue);
60
+ }
61
+ catch (Ex) { }
62
+ }
63
+ return foundValue;
64
+ }
65
+ export async function set_var(key, value, options) {
66
+ if (!options)
67
+ options = {};
68
+ var final_value = value;
69
+ var jsonify = options.json && typeof final_value != "function";
70
+ if (jsonify) {
71
+ try {
72
+ final_value = JSON.stringify(final_value);
73
+ }
74
+ catch (Ex) { }
75
+ }
76
+ if (final_value === null || final_value === undefined) {
77
+ final_value = "";
78
+ }
79
+ var isString = typeof final_value == "string";
80
+ if (isString) {
81
+ /* Encode any string */
82
+ final_value = gencryption.Encode_Default(final_value);
83
+ }
84
+ /* -------------------------------------------------------------------------- */
85
+ glb.Global_set(lname, key, final_value);
86
+ if (options.local_storage) {
87
+ if (!isString)
88
+ throw new Error("You cant save non-string values in local_storage !");
89
+ const fname = lname + "_" + key;
90
+ localStorage[fname] = final_value; // only strings
91
+ }
92
+ if (options.session) {
93
+ if (!isString)
94
+ throw new Error("You cant save non-string values in session !");
95
+ const sessionStorage = window.sessionStorage;
96
+ if (final_value !== "") {
97
+ sessionStorage.setItem(key, final_value);
98
+ }
99
+ else
100
+ sessionStorage.removeItem(key);
101
+ }
102
+ if (options.storage) {
103
+ if (!isString)
104
+ throw new Error("You cant save non-string values in AsyncStorage !");
105
+ if (final_value !== "") {
106
+ await AsyncStorage.setItem(key, final_value);
107
+ }
108
+ else
109
+ await AsyncStorage.removeItem(key);
110
+ }
111
+ /* -------------------------------------------------------------------------- */
112
+ return final_value;
113
+ }
114
+ /* -------------------------------------------------------------------------- */
115
+ /* -------------------------------------------------------------------------- */
116
+ export function hold_var(key, value, options) {
117
+ React.useEffect(() => {
118
+ set_var(key, value, options);
119
+ return () => {
120
+ set_var(key, null, options);
121
+ };
122
+ }, [value]);
123
+ return value;
124
+ }
@@ -0,0 +1 @@
1
+ export * from "./g_storage";
@@ -0,0 +1 @@
1
+ export * from "./g_storage";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azrico/global",
3
- "version": "1.3.13",
3
+ "version": "1.3.16",
4
4
  "description": "Simple global functions and helpers",
5
5
  "author": "Azrideus",
6
6
  "license": "MIT",
@@ -75,13 +75,11 @@
75
75
  ],
76
76
  "dependencies": {
77
77
  "@react-native-async-storage/async-storage": "^1.15.5",
78
- "crypto-js": "^4.1.1",
79
78
  "pullstate": "^1.24.0",
80
79
  "react": "^18.0.0"
81
80
  },
82
81
  "peerDependencies": {
83
82
  "@react-native-async-storage/async-storage": "^1.15.5",
84
- "crypto-js": "^4.1.1",
85
83
  "pullstate": "^1.24.0",
86
84
  "react": "^18.0.0"
87
85
  }