react-usespinner 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/README.md +79 -0
- package/dist/{index.ts → hooks/index.d.ts} +1 -4
- package/dist/hooks/index.js +6 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/usespinner.d.ts +16 -0
- package/dist/hooks/usespinner.js +97 -0
- package/dist/hooks/usespinner.js.map +1 -0
- package/package.json +3 -3
- package/dist/usespinner.tsx +0 -89
package/dist/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# useSpinner
|
2
|
+
|
3
|
+
Wraps action calls that can be grouped together to display a spinner while 1 or more is active. By default an action will be marked as complete after 10 seconds.
|
4
|
+
|
5
|
+
Any item that may require an indication of processing may be considered an action. For example a fetch call, or a long running process.
|
6
|
+
|
7
|
+
Typically before a fetch is started, an action would be started for the fetch. Once the fetch rresolves or is in error then the action can be ended
|
8
|
+
|
9
|
+
```javascript
|
10
|
+
start("slowapi") // start action
|
11
|
+
console.log("Starting slowapi action")
|
12
|
+
fetch("https://flash-the-slow-api.herokuapp.com/delay/3000")
|
13
|
+
.then(res => res.text())
|
14
|
+
.then(data => console.log(data))
|
15
|
+
.finally(() => {
|
16
|
+
console.log("Ending slowapi action")
|
17
|
+
end("slowapi") // finally end action
|
18
|
+
})
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```javascript
|
24
|
+
import useSpinner from './hooks/usespinner';
|
25
|
+
|
26
|
+
const { start, end, clear, busy, SpinnerContainer } = useSpinner();
|
27
|
+
|
28
|
+
// To start a new action
|
29
|
+
start("10 Second timer")
|
30
|
+
|
31
|
+
// To end an existing action
|
32
|
+
end("10 Second timer")
|
33
|
+
|
34
|
+
// clear all actions
|
35
|
+
clear();
|
36
|
+
|
37
|
+
// Check if any actions currently running
|
38
|
+
if (busy()) {
|
39
|
+
console.log("There are actions running")
|
40
|
+
}
|
41
|
+
|
42
|
+
// Display a spinner (if any actions are running)
|
43
|
+
<SpinnerContainer>
|
44
|
+
LOADING
|
45
|
+
</SpinnerContainer>
|
46
|
+
```
|
47
|
+
|
48
|
+
## Configuration
|
49
|
+
|
50
|
+
A global timeout can be configured, by default this is 10 seconds
|
51
|
+
|
52
|
+
```javascript
|
53
|
+
// to set a global delay of 1 second
|
54
|
+
const { start, end, clear, SpinnerContainer } = useSpinner({ delay: 1000 });
|
55
|
+
```
|
56
|
+
|
57
|
+
Each action can also be configurred with a configurable timeout
|
58
|
+
```javascript
|
59
|
+
// To start a new action that may take up to 60 seconds
|
60
|
+
start("10 Second timer", { delay: 60000 })
|
61
|
+
```
|
62
|
+
|
63
|
+
The action will be kept active based on (in order), action delay, global delay, 10 seconds
|
64
|
+
|
65
|
+
## ending actions
|
66
|
+
|
67
|
+
calling the end action method will immediatly close all actions with the given name, irrespective to when they were started, if mutiple actions with the same name are to be started each must be uniquely identified by name
|
68
|
+
|
69
|
+
```javascript
|
70
|
+
start("genericname", { delay: 10000})
|
71
|
+
start("genericname", { delay: 20000})
|
72
|
+
start("genericname", { delay: 30000})
|
73
|
+
|
74
|
+
end("genericname"); // will mark all three actions as complete as they have the same name
|
75
|
+
```
|
76
|
+
|
77
|
+
## Custom fetch
|
78
|
+
|
79
|
+
useSpinner also exports it's own fetch method. This method is compatible with a normal browser fetch but creates a unique action id that is tracked by the useSpinner actions
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":";;;AAAA,2CAAsC;AAGlC,qBAHG,oBAAU,CAGH"}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
/// <reference types="react" />
|
2
|
+
interface Options {
|
3
|
+
delay: number;
|
4
|
+
}
|
5
|
+
interface SpinnerProps {
|
6
|
+
children: React.ReactNode;
|
7
|
+
}
|
8
|
+
declare const useSpinner: (globalOptions?: Options) => {
|
9
|
+
start: (name: string, options?: Options) => void;
|
10
|
+
end: (name: string) => void;
|
11
|
+
clear: () => void;
|
12
|
+
busy: () => boolean;
|
13
|
+
fetch: (...params: any) => Promise<unknown>;
|
14
|
+
SpinnerContainer: ({ children }: SpinnerProps) => JSX.Element | null;
|
15
|
+
};
|
16
|
+
export default useSpinner;
|
@@ -0,0 +1,97 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
3
|
+
__assign = Object.assign || function(t) {
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5
|
+
s = arguments[i];
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7
|
+
t[p] = s[p];
|
8
|
+
}
|
9
|
+
return t;
|
10
|
+
};
|
11
|
+
return __assign.apply(this, arguments);
|
12
|
+
};
|
13
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
15
|
+
if (ar || !(i in from)) {
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
17
|
+
ar[i] = from[i];
|
18
|
+
}
|
19
|
+
}
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
21
|
+
};
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
23
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
24
|
+
var react_1 = require("react");
|
25
|
+
var useSpinner = function (globalOptions) {
|
26
|
+
// store a list of actions
|
27
|
+
var _a = (0, react_1.useState)([]), actions = _a[0], setActions = _a[1];
|
28
|
+
// set a timer that every second checks if any actions can be closed
|
29
|
+
(0, react_1.useEffect)(function () {
|
30
|
+
var timer = setTimeout(function () {
|
31
|
+
if (actions) {
|
32
|
+
setActions(function (prev) { return prev.filter(function (item) { return item.endtime > Date.now(); }); });
|
33
|
+
}
|
34
|
+
}, 1000);
|
35
|
+
return function () { return clearTimeout(timer); };
|
36
|
+
}, [actions]);
|
37
|
+
// Allow the start of a new Action to be tracked
|
38
|
+
var start = function (name, options) {
|
39
|
+
var delay = (options === null || options === void 0 ? void 0 : options.delay) || (globalOptions === null || globalOptions === void 0 ? void 0 : globalOptions.delay) || 10000;
|
40
|
+
var currentAction = { name: name, endtime: Date.now() + delay };
|
41
|
+
setActions(function (prev) { return __spreadArray(__spreadArray([], prev, true), [currentAction], false); });
|
42
|
+
};
|
43
|
+
// remove any action that has the selected name
|
44
|
+
var end = function (name) {
|
45
|
+
if (actions) {
|
46
|
+
setActions(function (prev) { return prev.filter(function (item) { return item.name !== name; }); });
|
47
|
+
}
|
48
|
+
};
|
49
|
+
// clear all current actions
|
50
|
+
var clear = function () {
|
51
|
+
setActions([]);
|
52
|
+
};
|
53
|
+
var busy = function () {
|
54
|
+
return actions && actions.length > 0;
|
55
|
+
};
|
56
|
+
var createUUID = function () {
|
57
|
+
// https://www.arungudelli.com/tutorial/javascript/how-to-create-uuid-guid-in-javascript-with-examples/
|
58
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
59
|
+
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
60
|
+
return v.toString(16);
|
61
|
+
});
|
62
|
+
};
|
63
|
+
var fetch = function () {
|
64
|
+
var params = [];
|
65
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
66
|
+
params[_i] = arguments[_i];
|
67
|
+
}
|
68
|
+
var id = createUUID();
|
69
|
+
start(id);
|
70
|
+
return new Promise(function (resolve, reject) {
|
71
|
+
var config = params[1] || {};
|
72
|
+
fetch(params[0], config)
|
73
|
+
.then(function (res) {
|
74
|
+
resolve(res);
|
75
|
+
})
|
76
|
+
.catch(function (err) {
|
77
|
+
reject(err);
|
78
|
+
})
|
79
|
+
.finally(function () {
|
80
|
+
end(id);
|
81
|
+
});
|
82
|
+
});
|
83
|
+
};
|
84
|
+
// JSX component used to wrap spinner of choice
|
85
|
+
var SpinnerContainer = function (_a) {
|
86
|
+
var children = _a.children;
|
87
|
+
// If no active Actions then dont display anything
|
88
|
+
if (actions && actions.length === 0) {
|
89
|
+
return null;
|
90
|
+
}
|
91
|
+
return (0, jsx_runtime_1.jsx)("div", __assign({ className: "useSpinner" }, { children: children }));
|
92
|
+
};
|
93
|
+
// return hook values
|
94
|
+
return { start: start, end: end, clear: clear, busy: busy, fetch: fetch, SpinnerContainer: SpinnerContainer };
|
95
|
+
};
|
96
|
+
exports.default = useSpinner;
|
97
|
+
//# sourceMappingURL=usespinner.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"usespinner.js","sourceRoot":"","sources":["../../src/hooks/usespinner.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA,+BAA4C;AAa5C,IAAM,UAAU,GAAG,UAAC,aAAuB;IACzC,0BAA0B;IACpB,IAAA,KAAwB,IAAA,gBAAQ,EAAW,EAAE,CAAC,EAA7C,OAAO,QAAA,EAAE,UAAU,QAA0B,CAAC;IAErD,oEAAoE;IACpE,IAAA,iBAAS,EAAC;QACR,IAAM,KAAK,GAAG,UAAU,CAAC;YACvB,IAAI,OAAO,EAAE;gBACX,UAAU,CAAC,UAAC,IAAI,IAAK,OAAA,IAAI,CAAC,MAAM,CAAC,UAAC,IAAI,IAAK,OAAA,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,EAAzB,CAAyB,CAAC,EAAhD,CAAgD,CAAC,CAAC;aACxE;QACH,CAAC,EAAE,IAAI,CAAC,CAAC;QACT,OAAO,cAAM,OAAA,YAAY,CAAC,KAAK,CAAC,EAAnB,CAAmB,CAAC;IACnC,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAEd,gDAAgD;IAChD,IAAM,KAAK,GAAG,UAAC,IAAY,EAAE,OAAiB;QAC5C,IAAM,KAAK,GAAW,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,MAAI,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,KAAK,CAAA,IAAI,KAAK,CAAC;QACtE,IAAM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ACT,OAAO,IAAI,OAAO,CAAC,UAAC,OAAO,EAAE,MAAM;YACjC,IAAI,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;YAC7B,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC;iBACrB,IAAI,CAAC,UAAC,GAAG;gBACR,OAAO,CAAC,GAAG,CAAC,CAAC;YACf,CAAC,CAAC;iBACD,KAAK,CAAC,UAAC,GAAG;gBACT,MAAM,CAAC,GAAG,CAAC,CAAC;YACd,CAAC,CAAC;iBACD,OAAO,CAAC;gBACP,GAAG,CAAC,EAAE,CAAC,CAAC;YACV,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;IAEF,+CAA+C;IAC/C,IAAM,gBAAgB,GAAG,UAAC,EAA0B;YAAxB,QAAQ,cAAA;QAClC,kDAAkD;QAClD,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;YACnC,OAAO,IAAI,CAAC;SACb;QAED,OAAO,yCAAK,SAAS,EAAC,YAAY,gBAAE,QAAQ,IAAO,CAAC;IACtD,CAAC,CAAC;IAEF,qBAAqB;IACrB,OAAO,EAAE,KAAK,OAAA,EAAE,GAAG,KAAA,EAAE,KAAK,OAAA,EAAE,IAAI,MAAA,EAAE,KAAK,OAAA,EAAE,gBAAgB,kBAAA,EAAE,CAAC;AAC9D,CAAC,CAAC;AAEF,kBAAe,UAAU,CAAC"}
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-usespinner",
|
3
|
-
"version": "1.0.
|
4
|
-
"description": "
|
3
|
+
"version": "1.0.6",
|
4
|
+
"description": "Track actions in progress to know if a spinner should be display. Actions expire within 10 seconds if not expired in code",
|
5
5
|
"private": false,
|
6
6
|
"license": "MIT",
|
7
7
|
"main": "dist/index.ts",
|
@@ -52,7 +52,7 @@
|
|
52
52
|
"test": "react-scripts test",
|
53
53
|
"eject": "react-scripts eject",
|
54
54
|
"clean": "rimraf dist",
|
55
|
-
"compile": "npm run clean && cross-env NODE_ENV=production
|
55
|
+
"compile": "npm run clean && cross-env NODE_ENV=production npx tsc && cross-env copy ./README.md ./dist/README.md"
|
56
56
|
},
|
57
57
|
"eslintConfig": {
|
58
58
|
"extends": [
|
package/dist/usespinner.tsx
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
import React, { useState, useEffect, useRef } from "react";
|
2
|
-
|
3
|
-
interface Options {
|
4
|
-
delay: number;
|
5
|
-
}
|
6
|
-
interface SpinnerProps {
|
7
|
-
children: React.ReactNode;
|
8
|
-
}
|
9
|
-
type Action = {
|
10
|
-
name: string;
|
11
|
-
endtime: number;
|
12
|
-
};
|
13
|
-
|
14
|
-
const useSpinner = (globalOptions?: Options) => {
|
15
|
-
// store a list of actions
|
16
|
-
const [actions, setActions] = useState<Action[]>([]);
|
17
|
-
|
18
|
-
// set a timer that every second checks if any actions can be closed
|
19
|
-
useEffect(() => {
|
20
|
-
const timer = setTimeout(() => {
|
21
|
-
if (actions) {
|
22
|
-
setActions((prev) => prev.filter((item) => item.endtime > Date.now()));
|
23
|
-
}
|
24
|
-
}, 1000);
|
25
|
-
return () => clearTimeout(timer);
|
26
|
-
}, [actions]);
|
27
|
-
|
28
|
-
// Allow the start of a new Action to be tracked
|
29
|
-
const start = (name: string, options?: Options) => {
|
30
|
-
const delay: number = options?.delay || globalOptions?.delay || 10000;
|
31
|
-
const currentAction: Action = { name: name, endtime: Date.now() + delay };
|
32
|
-
setActions((prev) => [...prev, currentAction]);
|
33
|
-
};
|
34
|
-
|
35
|
-
// remove any action that has the selected name
|
36
|
-
const end = (name: string) => {
|
37
|
-
if (actions) {
|
38
|
-
setActions((prev) => prev.filter((item) => item.name !== name));
|
39
|
-
}
|
40
|
-
};
|
41
|
-
// clear all current actions
|
42
|
-
const clear = () => {
|
43
|
-
setActions([]);
|
44
|
-
};
|
45
|
-
|
46
|
-
const busy = () => {
|
47
|
-
return actions && actions.length > 0
|
48
|
-
}
|
49
|
-
|
50
|
-
const createUUID = () => {
|
51
|
-
// https://www.arungudelli.com/tutorial/javascript/how-to-create-uuid-guid-in-javascript-with-examples/
|
52
|
-
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
53
|
-
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
|
54
|
-
return v.toString(16);
|
55
|
-
});
|
56
|
-
}
|
57
|
-
const fetch = (...params: any) => {
|
58
|
-
const id = createUUID();
|
59
|
-
start(id)
|
60
|
-
return new Promise((resolve, reject) => {
|
61
|
-
let config = params[1] || {};
|
62
|
-
fetch(params[0], config)
|
63
|
-
.then((res) => {
|
64
|
-
resolve(res);
|
65
|
-
})
|
66
|
-
.catch((err) => {
|
67
|
-
reject(err);
|
68
|
-
})
|
69
|
-
.finally(()=>{
|
70
|
-
end(id);
|
71
|
-
});
|
72
|
-
});
|
73
|
-
};
|
74
|
-
|
75
|
-
// JSX component used to wrap spinner of choice
|
76
|
-
const SpinnerContainer = ({ children }: SpinnerProps) => {
|
77
|
-
// If no active Actions then dont display anything
|
78
|
-
if (actions && actions.length === 0) {
|
79
|
-
return null;
|
80
|
-
}
|
81
|
-
|
82
|
-
return <div className="useSpinner">{children}</div>;
|
83
|
-
};
|
84
|
-
|
85
|
-
// return hook values
|
86
|
-
return { start, end, clear, busy, fetch, SpinnerContainer };
|
87
|
-
};
|
88
|
-
|
89
|
-
export default useSpinner;
|