@leancodepl/utils 7.3.3 → 7.3.4

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.
package/index.cjs.js CHANGED
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var invariant = require('tiny-invariant');
6
+ var react = require('react');
6
7
 
7
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
9
 
@@ -81,6 +82,73 @@ function ensureNotEmpty(value, message) {
81
82
  return value;
82
83
  }
83
84
 
85
+ function useRunInTask() {
86
+ const [runningTasks, setRunningTasks] = react.useState(0);
87
+ const runInTask = react.useCallback(async (task)=>{
88
+ setRunningTasks((runningTasks)=>runningTasks + 1);
89
+ try {
90
+ return await task();
91
+ } finally{
92
+ setRunningTasks((runningTasks)=>runningTasks - 1);
93
+ }
94
+ }, []);
95
+ return [
96
+ runningTasks > 0,
97
+ runInTask
98
+ ];
99
+ }
100
+
101
+ function useBoundRunInTask(block) {
102
+ const [isRunning, runInTask] = useRunInTask();
103
+ const runBlockInTask = react.useMemo(()=>block ? (...args)=>runInTask(()=>block(...args)) : undefined, [
104
+ block,
105
+ runInTask
106
+ ]);
107
+ return [
108
+ isRunning,
109
+ runBlockInTask
110
+ ];
111
+ }
112
+
113
+ function useKeyByRoute(routeMatches) {
114
+ const keys = [];
115
+ for(const key in routeMatches){
116
+ const matches = routeMatches[key];
117
+ if (Array.isArray(matches) ? matches.some((match)=>match !== null) : matches !== null) {
118
+ keys.push(key);
119
+ }
120
+ }
121
+ return keys;
122
+ }
123
+
124
+ function useSetUnset(set) {
125
+ return [
126
+ react.useCallback(()=>set(true), [
127
+ set
128
+ ]),
129
+ react.useCallback(()=>set(false), [
130
+ set
131
+ ])
132
+ ];
133
+ }
134
+
135
+ function useDialog(onAfterClose) {
136
+ const [isDialogOpen, setIsDialogOpen] = react.useState(false);
137
+ const [openDialog, closeDialog] = useSetUnset(setIsDialogOpen);
138
+ const close = react.useCallback(()=>{
139
+ closeDialog();
140
+ onAfterClose && setTimeout(onAfterClose);
141
+ }, [
142
+ closeDialog,
143
+ onAfterClose
144
+ ]);
145
+ return {
146
+ isDialogOpen,
147
+ openDialog,
148
+ closeDialog: close
149
+ };
150
+ }
151
+
84
152
  exports.assertDefined = assertDefined;
85
153
  exports.assertNotEmpty = assertNotEmpty;
86
154
  exports.assertNotNull = assertNotNull;
@@ -91,3 +159,8 @@ exports.ensureNotNull = ensureNotNull;
91
159
  exports.toLowerFirst = toLowerFirst;
92
160
  exports.toUpperFirst = toUpperFirst;
93
161
  exports.uncapitalizeDeep = uncapitalizeDeep;
162
+ exports.useBoundRunInTask = useBoundRunInTask;
163
+ exports.useDialog = useDialog;
164
+ exports.useKeyByRoute = useKeyByRoute;
165
+ exports.useRunInTask = useRunInTask;
166
+ exports.useSetUnset = useSetUnset;
package/index.esm.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import invariant from 'tiny-invariant';
2
+ import { useState, useCallback, useMemo } from 'react';
2
3
 
3
4
  function transformFirst(value, transformFn) {
4
5
  if (value.length === 0) {
@@ -73,4 +74,71 @@ function ensureNotEmpty(value, message) {
73
74
  return value;
74
75
  }
75
76
 
76
- export { assertDefined, assertNotEmpty, assertNotNull, capitalizeDeep, ensureDefined, ensureNotEmpty, ensureNotNull, toLowerFirst, toUpperFirst, uncapitalizeDeep };
77
+ function useRunInTask() {
78
+ const [runningTasks, setRunningTasks] = useState(0);
79
+ const runInTask = useCallback(async (task)=>{
80
+ setRunningTasks((runningTasks)=>runningTasks + 1);
81
+ try {
82
+ return await task();
83
+ } finally{
84
+ setRunningTasks((runningTasks)=>runningTasks - 1);
85
+ }
86
+ }, []);
87
+ return [
88
+ runningTasks > 0,
89
+ runInTask
90
+ ];
91
+ }
92
+
93
+ function useBoundRunInTask(block) {
94
+ const [isRunning, runInTask] = useRunInTask();
95
+ const runBlockInTask = useMemo(()=>block ? (...args)=>runInTask(()=>block(...args)) : undefined, [
96
+ block,
97
+ runInTask
98
+ ]);
99
+ return [
100
+ isRunning,
101
+ runBlockInTask
102
+ ];
103
+ }
104
+
105
+ function useKeyByRoute(routeMatches) {
106
+ const keys = [];
107
+ for(const key in routeMatches){
108
+ const matches = routeMatches[key];
109
+ if (Array.isArray(matches) ? matches.some((match)=>match !== null) : matches !== null) {
110
+ keys.push(key);
111
+ }
112
+ }
113
+ return keys;
114
+ }
115
+
116
+ function useSetUnset(set) {
117
+ return [
118
+ useCallback(()=>set(true), [
119
+ set
120
+ ]),
121
+ useCallback(()=>set(false), [
122
+ set
123
+ ])
124
+ ];
125
+ }
126
+
127
+ function useDialog(onAfterClose) {
128
+ const [isDialogOpen, setIsDialogOpen] = useState(false);
129
+ const [openDialog, closeDialog] = useSetUnset(setIsDialogOpen);
130
+ const close = useCallback(()=>{
131
+ closeDialog();
132
+ onAfterClose && setTimeout(onAfterClose);
133
+ }, [
134
+ closeDialog,
135
+ onAfterClose
136
+ ]);
137
+ return {
138
+ isDialogOpen,
139
+ openDialog,
140
+ closeDialog: close
141
+ };
142
+ }
143
+
144
+ export { assertDefined, assertNotEmpty, assertNotNull, capitalizeDeep, ensureDefined, ensureNotEmpty, ensureNotNull, toLowerFirst, toUpperFirst, uncapitalizeDeep, useBoundRunInTask, useDialog, useKeyByRoute, useRunInTask, useSetUnset };
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@leancodepl/utils",
3
- "version": "7.3.3",
3
+ "version": "7.3.4",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/api-date": "7.3.3",
6
+ "@leancodepl/api-date": "7.3.4",
7
7
  "tiny-invariant": ">=1.3.1"
8
8
  },
9
+ "peerDependencies": {
10
+ "react": ">=17.0.0",
11
+ "react-router-dom": ">=6.0.0"
12
+ },
9
13
  "exports": {
10
14
  "./package.json": "./package.json",
11
15
  ".": {
package/src/index.d.ts CHANGED
@@ -7,3 +7,8 @@ export * from "./lib/assertNotEmpty";
7
7
  export * from "./lib/ensureDefined";
8
8
  export * from "./lib/ensureNotNull";
9
9
  export * from "./lib/ensureNotEmpty";
10
+ export * from "./lib/hooks/useRunInTask";
11
+ export * from "./lib/hooks/useBoundRunInTask";
12
+ export * from "./lib/hooks/useKeyByRoute";
13
+ export * from "./lib/hooks/useSetUnset";
14
+ export * from "./lib/hooks/useDialog";
@@ -0,0 +1,4 @@
1
+ type AnyFunction = (...args: any[]) => any;
2
+ export declare function useBoundRunInTask<T extends AnyFunction>(block: T): [boolean, T];
3
+ export declare function useBoundRunInTask<T extends AnyFunction>(block: T | undefined): [boolean, T | undefined];
4
+ export {};
@@ -0,0 +1,5 @@
1
+ export declare function useDialog(onAfterClose?: () => void): {
2
+ isDialogOpen: boolean;
3
+ openDialog: () => void;
4
+ closeDialog: () => void;
5
+ };
@@ -0,0 +1,2 @@
1
+ import { PathMatch } from "react-router-dom";
2
+ export declare function useKeyByRoute<TKey extends string>(routeMatches: Record<TKey, PathMatch | null | [PathMatch | null] | never>): TKey[];
@@ -0,0 +1 @@
1
+ export declare function useRunInTask(): readonly [boolean, <T>(task: () => T | Promise<T>) => Promise<T>];
@@ -0,0 +1,2 @@
1
+ import { Dispatch, SetStateAction } from "react";
2
+ export declare function useSetUnset(set: Dispatch<SetStateAction<boolean>>): readonly [() => void, () => void];
@@ -1 +1,2 @@
1
1
  export * from "./transformDeep";
2
+ export * from "./unpromisify";
@@ -0,0 +1 @@
1
+ export type Unpromisify<T> = T extends Promise<infer TResult> ? TResult : T;