UNPKG

@ducor/react

Version:

admin template ui interface

51 lines (50 loc) 2.11 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useRef, useReducer } from "react"; import { createContext } from "react"; import Loading from "../components/loading"; import MemoComponent from "../components/memo-component"; export var LoadingContext = createContext(undefined); export var LoadingProvider = function (_a) { var children = _a.children, defaultLoading = _a.defaultLoading; var _b = useReducer(function (x) { return x + 1; }, 0), forceUpdate = _b[1]; function createLoadingRef(name) { return { isLoading: defaultLoading === name, duration: null, loadingCount: 0, message: undefined, start: function () { this.isLoading = true; this.loadingCount += 1; forceUpdate(); }, update: function (options) { var _a; if (options) { this.message = options.message; this.duration = (_a = options.duration) !== null && _a !== void 0 ? _a : null; } forceUpdate(); }, finish: function () { this.loadingCount -= 1; this.isLoading = this.loadingCount > 0; forceUpdate(); }, force: function (state) { Object.assign(this, state); forceUpdate(); }, }; } var screenRef = useRef(createLoadingRef("screen")); var pageRef = useRef(createLoadingRef("page")); var backgroundRef = useRef(createLoadingRef("background")); var contextValue = { screen: screenRef, page: pageRef, background: backgroundRef, }; console.log("rerender loading"); return (_jsxs(LoadingContext.Provider, { value: contextValue, children: [screenRef.current.isLoading && _jsx(Loading.Screen, {}), pageRef.current.isLoading && _jsx(Loading.Page, {}), backgroundRef.current.isLoading && _jsx(Loading.Background, {}), _jsx(MemoComponent, { component: children })] })); };