Skip to content

Commit

Permalink
fix: rename useEffect to useWatch and add useMount
Browse files Browse the repository at this point in the history
  • Loading branch information
shuta13 committed Sep 6, 2022
1 parent 6f37750 commit e5d9edf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
23 changes: 11 additions & 12 deletions packages/core/__tests__/smoke/dev-server/src/plugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EditorJS, {
InlineToolConstructorOptions,
ToolConfig,
} from '@editorjs/editorjs';
import { h, useState, useEffect, createPlugin } from '../../../../src';
import { h, useState, useWatch, createPlugin, useMount } from '../../../../src';
import type { PDJSX } from '../../../../src/';

const WithHooks = () => {
Expand All @@ -18,21 +18,20 @@ const WithHooks = () => {
const [api, setApi] = useState<API | null>(null);
const [config, setConfig] = useState<ToolConfig | null>(null);

useEffect(() => {
// FIXME: In this useEffect(called useMount), it rewrite state each state updater executed.
useMount(() => {
setText('Pong');
}, []);
});

useEffect(() => {
console.log('[useEffect] show changed!: ', show);
useWatch(() => {
console.log('[useWatch] show changed!: ', show);
}, [show]);

useEffect(() => {
console.log('[useEffect] value changed!: ', value);
useWatch(() => {
console.log('[useWatch] value changed!: ', value);
}, [value]);

useEffect(() => {
console.log('[useEffect] api changed!: ', api);
useWatch(() => {
console.log('[useWatch] api changed!: ', api);
}, [api]);

const initializer = ({ api, config }: InlineToolConstructorOptions) => {
Expand Down Expand Up @@ -114,8 +113,8 @@ const WithContentEdiable = () => {
const initializer = ({ data }: BlockToolConstructorOptions) => {
setData(data);
};
useEffect(() => {
console.log('[useEffect data] ', data?.value);
useWatch(() => {
console.log('[useWatch data] ', data?.value);
}, [data]);
return (
<tool
Expand Down
29 changes: 21 additions & 8 deletions packages/core/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ export const useState = <S = undefined>(initialState: S) => {
return useReducer<S>(invokeOrReturn, initialState) as [S, StateUpdater<S>];
};

export const useEffect = (callback: Effect, args: any[]) => {
export const useWatch = (callback: Effect, args: any[]) => {
const state = getHookState(currentIndex++, 3);
/**
* NOTE: We suppress to call useEffect(cb, []) twice through calledUseMount.
Expand All @@ -319,13 +319,26 @@ export const useEffect = (callback: Effect, args: any[]) => {

currentComponent?.renderCallbacks.push(state as Component);
} else {
if (!options.calledUseMount) {
state.value = callback;
state.pendingArgs = args;

currentComponent?.renderCallbacks.push(state as Component);
options.calledUseMount = true;
}
throw new Error(
'args should be array with at least one element. Use `useMount` instead.'
);
}
}
};

export const useMount = (callback: Effect) => {
const state = getHookState(currentIndex++, 4);
let called = !!state?.value;

if (state && !called) {
state.value = callback;
state.pendingArgs = [];

currentComponent?.renderCallbacks.push(state as Component);
currentComponent?.forceUpdate();

called = true;
}
};

export const useConstruct = () => {};

0 comments on commit e5d9edf

Please sign in to comment.