Skip to content

Commit

Permalink
feat: add ctx.useReactive
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasticsoul committed Dec 6, 2023
1 parent 59e873f commit dbe9904
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helux",
"version": "3.5.0",
"version": "3.5.1",
"description": "A state library core that integrates atom, signal, collection dep, derive and watch, it supports all react like frameworks( including react 18 ).",
"keywords": [],
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/helux-core/src/consts/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VER as limuVer } from 'limu';

export const VER = '3.5.0';
export const VER = '3.5.1';

export const LIMU_VER = limuVer;

Expand Down
2 changes: 2 additions & 0 deletions packages/helux-core/src/factory/createShared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { noop } from '@helux/utils';
import { FROM, STATE_TYPE } from '../consts';
import { useAtom, useShared } from '../hooks/useShared';
import { useReactive } from '../hooks/useReactive';
import type { CoreApiCtx } from '../types/api-ctx';
import type { Dict, Fn, IAtomCreateOptions, IAtomCtx, ICreateOptions, IRunMutateOptions, ISharedCtx } from '../types/base';
import { action, actionAsync, atomAction, atomActionAsync } from './createAction';
Expand Down Expand Up @@ -63,6 +64,7 @@ export function createSharedLogic(innerOptions: IInnerOptions, createOptions?: a
call: (fn: Fn, ...args: any[]) => actionCreator(fn)(...args),
callAsync: (fn: Fn, ...args: any[]) => actionAsyncCreator(fn)(...args),
useState: (options?: any) => useFn(apiCtx, state, options),
useReactive: (options?: any) => useReactive(apiCtx, options),
getMutateLoading: ldMutate.getLoading,
useMutateLoading: ldMutate.useLoading,
getActionLoading: ldAction.getLoading,
Expand Down
4 changes: 2 additions & 2 deletions packages/helux-core/src/types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
|------------------------------------------------------------------------------------------------
| [email protected].0
| [email protected].1
| A state library core that integrates atom, signal, collection dep, derive and watch,
| it supports all react like frameworks ( including react 18 ).
|------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -72,7 +72,7 @@ import type {
WatchOptionsType,
} from './base';

export declare const VER: '3.5.0';
export declare const VER: '3.5.1';

export declare const LIMU_VER: string;

Expand Down
1 change: 1 addition & 0 deletions packages/helux-core/src/types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ export interface ISharedStateCtxBase<T = any> {
sharedKeyStr: string;
rootValKey: string;
reactive: T;
useReactive: (options?: IUseSharedStateOptions<T>) => [InsReactiveState<T>, IInsRenderInfo];
}

export interface ISharedCtx<T = SharedDict, O extends ICreateOptions<T> = ICreateOptions<T>> extends ISharedStateCtxBase<T> {
Expand Down
7 changes: 5 additions & 2 deletions packages/helux/__tests__/helux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { act } from '@testing-library/react';
import * as React from 'react';
import { initHeluxContext } from '../../helux-core/src/index';

window.alert = () => {};
window.alert = () => { };

// pass act to avoid Warning:
// An update to TestComponent inside a test was not wrapped in act(...).
Expand All @@ -27,6 +27,7 @@ export const {
// hooks api
useAtom,
useShared,
useReactive,
useDerived,
useDerivedAtom,
useWatch,
Expand Down Expand Up @@ -84,5 +85,7 @@ export const {
addMiddleware,
addPlugin,
EVENT_NAME,
LOADING_MODE,
RECORD_LOADING,
VER,
LIMU_VER,
} = api;
19 changes: 19 additions & 0 deletions packages/helux/__tests__/hooks/useReactive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import '@testing-library/jest-dom';
import { renderHook } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { derive, share, useDerived, useReactive } from '../helux';
import { delay, dictFictory } from '../util';

describe('useReactive', () => {
test('simple use', async () => {
const [state, setState] = share(dictFictory);

const { result } = renderHook(() => {
const [shared] = useReactive(state);
return shared.a.b.c;
});

expect(result.current).toBe(1);
});

});

0 comments on commit dbe9904

Please sign in to comment.