@indietabletop/appkit 0.2.1 → 0.2.2

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
+ import { AsyncOp } from "./async-op.js";
2
+ export declare function useAsyncOp<T, E>(): {
3
+ op: AsyncOp<T, E>;
4
+ setSuccess: (value: T) => void;
5
+ setFailure: (failure: E) => void;
6
+ };
@@ -0,0 +1,12 @@
1
+ import { useCallback, useState } from "react";
2
+ import { Failure, Pending, Success } from "./async-op.js";
3
+ export function useAsyncOp() {
4
+ const [op, setOp] = useState(new Pending());
5
+ const setSuccess = useCallback((value) => {
6
+ setOp(new Success(value));
7
+ }, []);
8
+ const setFailure = useCallback((failure) => {
9
+ setOp(new Failure(failure));
10
+ }, []);
11
+ return { op, setSuccess, setFailure };
12
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Sets a state that will automatically revert to null after specified number
3
+ * of milliseconds.
4
+ */
5
+ export declare function useRevertingState<T>(initialState: T, revertAfterMs: number): readonly [T | null, import("react").Dispatch<import("react").SetStateAction<T | null>>];
@@ -0,0 +1,26 @@
1
+ import { useEffect, useRef, useState } from "react";
2
+ /**
3
+ * Sets a state that will automatically revert to null after specified number
4
+ * of milliseconds.
5
+ */
6
+ export function useRevertingState(initialState, revertAfterMs) {
7
+ const [state, setState] = useState(initialState);
8
+ const timeoutRef = useRef(null);
9
+ useEffect(() => {
10
+ const timeoutId = timeoutRef.current;
11
+ if (timeoutId) {
12
+ clearInterval(timeoutId);
13
+ }
14
+ if (state) {
15
+ timeoutRef.current = setTimeout(() => {
16
+ setState(null);
17
+ }, revertAfterMs);
18
+ }
19
+ return () => {
20
+ if (timeoutId) {
21
+ clearTimeout(timeoutId);
22
+ }
23
+ };
24
+ }, [revertAfterMs, state]);
25
+ return [state, setState];
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indietabletop/appkit",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "A collection of modules used in apps built by Indie Tabletop Club",
5
5
  "private": false,
6
6
  "type": "module",