Skip to content

Commit

Permalink
Merge pull request #22 from ecency/use-replace
Browse files Browse the repository at this point in the history
Replace use functions
  • Loading branch information
feruzm authored Apr 4, 2024
2 parents 453c49f + 79a9904 commit f15aba7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/nostr/core/keys-query.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { useQueries } from "@tanstack/react-query";
import { EncryptionTools } from "../../utils";
import { EncryptionTools, useMount } from "../../utils";
import { useContext, useMemo } from "react";
import { NostrQueries } from "../queries";
import { ChatContext } from "../../chat-context-provider";
import { useGetKeysQuery } from "../../api";
import { useMount } from "react-use";

export function useKeysQuery() {
const { activeUsername, activeUserData } = useContext(ChatContext);
Expand Down
2 changes: 1 addition & 1 deletion lib/nostr/core/use-live-listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Event, Filter } from "nostr-tools";
import { useContext, useEffect, useRef } from "react";
import { NostrContext } from "../nostr-context";
import { useTimeoutFn } from "react-use";
import { useTimeoutFn } from "../../utils";

export function useLiveListener<DATA extends object>(
filters: Filter[],
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ export * from "./get-relative-date";
export * from "./encryption-tools";
export * from "./is-single-emoji";
export * from "./is-community";
export * from "./useEffectOnce";
export * from "./useMount";
export * from "./useTimeoutFn";
5 changes: 5 additions & 0 deletions lib/utils/useEffectOnce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EffectCallback, useEffect } from 'react';

export const useEffectOnce = (effect: EffectCallback) => {
useEffect(effect, []);
};
7 changes: 7 additions & 0 deletions lib/utils/useMount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import useEffectOnce from './useEffectOnce';

export const useMount = (fn: () => void) => {
useEffectOnce(() => {
fn();
});
};
40 changes: 40 additions & 0 deletions lib/utils/useTimeoutFn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useCallback, useEffect, useRef } from 'react';

export type UseTimeoutFnReturn = [() => boolean | null, () => void, () => void];

export const useTimeoutFn = (fn: Function, ms: number = 0): UseTimeoutFnReturn => {
const ready = useRef<boolean | null>(false);
const timeout = useRef<ReturnType<typeof setTimeout>>();
const callback = useRef(fn);

const isReady = useCallback(() => ready.current, []);

const set = useCallback(() => {
ready.current = false;
timeout.current && clearTimeout(timeout.current);

timeout.current = setTimeout(() => {
ready.current = true;
callback.current();
}, ms);
}, [ms]);

const clear = useCallback(() => {
ready.current = null;
timeout.current && clearTimeout(timeout.current);
}, []);

// update ref when function changes
useEffect(() => {
callback.current = fn;
}, [fn]);

// set on mount, clear on unmount
useEffect(() => {
set();

return clear;
}, [ms]);

return [isReady, clear, set];
}
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ecency/ns-query",
"description": "React-query based Nostr protocol SDK for Ecency vision and mobile",
"version": "1.1.9",
"version": "1.2.0",
"repository": "https://github.com/ecency/ns-query",
"author": "ildar.timerbaev <dkildar@gmail.com>",
"license": "MIT",
Expand All @@ -26,16 +26,14 @@
"@tanstack/react-query": "^4.29.7",
"axios": "^0.21.2",
"date-fns": "^2.30.0",
"react": "^16.8.6",
"react-use": "^17.4.1"
"react": "^16.8.6"
},
"dependencies": {
"@tanstack/react-query": "^4.29.7",
"axios": "^0.21.2",
"date-fns": "^2.30.0",
"nostr-tools": "^1.17.0",
"react": "^16.8.6",
"react-use": "^17.4.1"
"react": "^16.8.6"
},
"scripts": {
"build": "rimraf dist & tsc & vite build",
Expand Down
1 change: 0 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default defineConfig({
"react/jsx-runtime",
"@tanstack/react-query",
"date-fns",
"react-use",
"crypto",
],
},
Expand Down

0 comments on commit f15aba7

Please sign in to comment.