@paroicms/bo-app-log
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -8,2 +8,4 @@ import { type AppLog, type FullAppLog } from "./app-log"; | ||
export declare function useWrapAsync(): WrapAsync; | ||
export type AsyncEffect = Promise<void | (() => void | Promise<void>)>; | ||
export declare function useAsyncEffect(effect: () => AsyncEffect, deps: readonly unknown[]): void; | ||
export declare const AppLogProvider: import("react").Provider<FullAppLog>; |
@@ -1,2 +0,2 @@ | ||
import { createContext, useContext } from "react"; | ||
import { createContext, useContext, useEffect, useRef } from "react"; | ||
import { createNoAppLog } from "./app-log"; | ||
@@ -12,2 +12,31 @@ export * from "./app-log"; | ||
} | ||
export function useAsyncEffect(effect, deps) { | ||
const logger = useContext(AppLogContext); | ||
const enqueueCalling = useRef(); // to resist to React strict mode | ||
useEffect(() => { | ||
let readyCleanup; | ||
let promise; | ||
if (enqueueCalling.current) { | ||
promise = enqueueCalling.current.then(callEffect); | ||
} | ||
else { | ||
promise = callEffect(); | ||
} | ||
enqueueCalling.current = promise; | ||
function callEffect() { | ||
return effect().then((cleanup) => { | ||
readyCleanup = cleanup ?? (() => { }); | ||
return cleanup; | ||
}, logger.error); | ||
} | ||
return () => { | ||
if (readyCleanup) { | ||
readyCleanup()?.catch(logger.error); // cleanup synchronously if possible | ||
} | ||
else { | ||
promise.then((cleanup) => cleanup?.()).catch(logger.error); | ||
} | ||
}; | ||
}, deps); | ||
} | ||
export const AppLogProvider = AppLogContext.Provider; |
{ | ||
"name": "@paroicms/bo-app-log", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Logger for the BO front of a Paroi CMS.", | ||
@@ -30,5 +30,2 @@ "keywords": [ | ||
}, | ||
"peerDependencies": { | ||
"react": "~18.3.1" | ||
}, | ||
"files": [ | ||
@@ -35,0 +32,0 @@ "dist" |
9960
0
283