Skip to content

Commit

Permalink
added use-event
Browse files Browse the repository at this point in the history
  • Loading branch information
atellmer committed Nov 14, 2022
1 parent 12c0db2 commit 0e7751b
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 93 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
forwardRef,
Suspense,
useCallback,
useEvent,
useContext,
useEffect,
useError,
Expand Down Expand Up @@ -607,6 +608,22 @@ Suitable for memoizing handler functions descending down the component tree:
);
```

#### useEvent

```tsx
import { useEvent } from '@dark-engine/core';
```

Similar to useCallback but has no dependencies. Ensures the return of the same function, with the closures always corresponding to the last render. In most cases, it eliminates the need to track dependencies in useCallback.

```tsx
const handleClick = useEvent(() => setCount(count + 1));

return (
<button onClick={handleClick}>add</button>
);
```

#### useDeferredValue

Dark under the hood performs all recalculations in asynchronous mode so as not to block the main thread. Due to the task scheduler, you can achieve prioritization of interface updates. For example, make some updates more important than others, and vice versa - less important.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dark-engine/core",
"version": "0.4.4",
"version": "0.5.0",
"description": "Dark is lightweight (10 Kb gzipped) component-and-hook-based UI rendering engine for javascript apps without dependencies and written in Typescript 💫",
"author": "AlexPlex",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './use-context';
export * from './use-deferred-value';
export * from './use-effect';
export * from './use-error';
export * from './use-event';
export * from './use-imperative-handle';
export * from './use-memo';
export * from './use-reducer';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/use-callback/use-callback.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ test('use-callback works correctly by default', () => {

render(App(), host);
waitNextIdle();
expect(handlers.every(x => x && x === handlers[0])).toBeTruthy();
render(App(), host);
waitNextIdle();
render(App(), host);
waitNextIdle();
expect(handlers.every(x => x && x === handlers[0])).toBeTruthy();
Expand Down
31 changes: 3 additions & 28 deletions packages/core/src/use-callback/use-callback.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import { detectIsUndefined, detectIsDepsDifferent } from '../helpers';
import { componentFiberHelper } from '../scope';
import { useMemo } from '../use-memo';

function useCallback<T = Function>(callback: T, deps: Array<any>): T {
const fiber = componentFiberHelper.get();
const { hook } = fiber;
const { idx, values } = hook;
const value = useMemo(() => callback, deps);

if (detectIsUndefined(values[idx])) {
values[idx] = {
deps,
value: callback,
};

hook.idx++;

return callback;
}

const hookValue = values[idx];
const prevDeps = hookValue.deps as Array<any>;
const isDepsDifferent = detectIsDepsDifferent(deps, prevDeps);

if (isDepsDifferent) {
hookValue.deps = deps;
hookValue.value = callback;
}

hook.idx++;

return hookValue.value;
return value;
}

export { useCallback };
1 change: 1 addition & 0 deletions packages/core/src/use-event/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './use-event';
16 changes: 16 additions & 0 deletions packages/core/src/use-event/use-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMemo } from '../use-memo';
import { useCallback } from '../use-callback';

function useEvent<T extends (...args) => any>(fn: T) {
const scope = useMemo(() => ({ fn }), []);

scope.fn = fn;

const callback = useCallback((...args) => {
return scope.fn(...args);
}, []);

return callback as unknown as T;
}

export { useEvent };
173 changes: 114 additions & 59 deletions packages/core/umd/dark-core.development.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/umd/dark-core.development.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/umd/dark-core.production.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/umd/dark-core.production.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/platform-browser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dark-engine/platform-browser",
"version": "0.4.4",
"version": "0.5.0",
"description": "Dark is lightweight (10 Kb gzipped) component-and-hook-based UI rendering engine for javascript apps without dependencies and written in Typescript 💫",
"author": "AlexPlex",
"license": "MIT",
Expand Down

0 comments on commit 0e7751b

Please sign in to comment.