Skip to content

Commit

Permalink
chore: 使用on-before-unmount替换on-unmouted
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 15, 2023
1 parent f212be1 commit b72e487
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 38 deletions.
8 changes: 5 additions & 3 deletions packages/design/src/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { computed, nextTick, ref, watch } from 'vue';
import { getConfig } from './config';
import type { SelectProps } from './types';
Expand Down Expand Up @@ -49,14 +49,16 @@ const visibleHandler = (...args: any[]) => {
const scrollbarWrap = ref<HTMLDivElement | undefined>();
const unWacth = watch(
const unWatch = watch(
() => select.value?.scrollbar?.wrap$ || select.value?.scrollbar?.wrapRef,
(wrap) => {
if (!wrap) {
return;
}
nextTick(() => unWatch());
scrollbarWrap.value = wrap;
unWacth();
},
{
immediate: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/CodeBlockEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</template>

<script lang="ts" setup>
import { computed, inject, onUnmounted, ref } from 'vue';
import { computed, inject, onBeforeUnmount, ref } from 'vue';
import { TMagicButton, TMagicDialog, tMagicMessage, tMagicMessageBox, TMagicTag } from '@tmagic/design';
import { ColumnConfig, FormConfig, FormState, MFormBox, MFormDrawer } from '@tmagic/form';
Expand Down Expand Up @@ -85,7 +85,7 @@ const windowReizehandler = () => {
globalThis.addEventListener('resize', windowReizehandler);
onUnmounted(() => {
onBeforeUnmount(() => {
globalThis.removeEventListener('resize', windowReizehandler);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/editor/src/components/ContentMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</template>

<script lang="ts" setup>
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue';
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
import { MenuButton, MenuComponent } from '@editor/type';
Expand Down Expand Up @@ -99,7 +99,7 @@ const clickHandler = () => {
hide();
};
const outsideClickhideHandler = (e: MouseEvent) => {
const outsideClickHideHandler = (e: MouseEvent) => {
if (!props.autoHide) return;
const target = e.target as HTMLElement | undefined;
Expand Down Expand Up @@ -173,13 +173,13 @@ const mouseenterHandler = () => {
onMounted(() => {
if (props.isSubMenu) return;
globalThis.addEventListener('mousedown', outsideClickhideHandler, true);
globalThis.addEventListener('mousedown', outsideClickHideHandler, true);
});
onUnmounted(() => {
onBeforeUnmount(() => {
if (props.isSubMenu) return;
globalThis.removeEventListener('mousedown', outsideClickhideHandler, true);
globalThis.removeEventListener('mousedown', outsideClickHideHandler, true);
});
defineExpose({
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/ScrollBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</template>

<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import Gesto from 'gesto';
defineOptions({
Expand Down Expand Up @@ -53,7 +53,7 @@ onMounted(() => {
bar.value?.addEventListener('wheel', wheelHandler, false);
});
onUnmounted(() => {
onBeforeUnmount(() => {
if (gesto) gesto.off();
bar.value?.removeEventListener('wheel', wheelHandler, false);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/ScrollViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</template>

<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { isNumber } from '@tmagic/utils';
Expand Down Expand Up @@ -94,7 +94,7 @@ onMounted(() => {
});
});
onUnmounted(() => {
onBeforeUnmount(() => {
scrollViewer.destroy();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/SplitView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</template>

<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref } from 'vue';
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { OnDrag } from 'gesto';
import Resizer from './Resizer.vue';
Expand Down Expand Up @@ -110,7 +110,7 @@ onMounted(() => {
}
});
onUnmounted(() => {
onBeforeUnmount(() => {
resizerObserver.disconnect();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/hooks/use-getso.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onMounted, onUnmounted, type Ref, ref } from 'vue';
import { onBeforeUnmount, onMounted, type Ref, ref } from 'vue';
import Gesto, { type OnDrag } from 'gesto';

export const useGetSo = (target: Ref<HTMLElement | undefined>, emit: (evt: 'change', e: OnDrag<Gesto>) => void) => {
Expand All @@ -24,7 +24,7 @@ export const useGetSo = (target: Ref<HTMLElement | undefined>, emit: (evt: 'chan
});
});

onUnmounted(() => {
onBeforeUnmount(() => {
getso?.unset();
isDraging.value = false;
});
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/initService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { onUnmounted, reactive, toRaw, watch } from 'vue';
import { onBeforeUnmount, reactive, toRaw, watch } from 'vue';
import { cloneDeep } from 'lodash-es';

import type { EventOption } from '@tmagic/core';
Expand Down Expand Up @@ -165,7 +165,7 @@ export const initServiceState = (
},
);

onUnmounted(() => {
onBeforeUnmount(() => {
editorService.resetState();
historyService.resetState();
propsService.resetState();
Expand Down Expand Up @@ -405,7 +405,7 @@ export const initServiceEvents = (
depService.addTarget(createRelatedCompTarget(props.collectorOptions));
}

onUnmounted(() => {
onBeforeUnmount(() => {
depService.off('add-target', targetAddHandler);
depService.off('remove-target', targetRemoveHandler);
depService.off('dep-update', depUpdateHandler);
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/layouts/CodeEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</template>

<script lang="ts" setup>
import { onMounted, onUnmounted, ref, watch } from 'vue';
import { onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { FullScreen } from '@element-plus/icons-vue';
import { throttle } from 'lodash-es';
import * as monaco from 'monaco-editor';
Expand Down Expand Up @@ -188,7 +188,7 @@ onMounted(async () => {
init();
});
onUnmounted(() => {
onBeforeUnmount(() => {
resizeObserver.disconnect();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/layouts/PropsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</template>

<script lang="ts" setup>
import { computed, getCurrentInstance, inject, onMounted, onUnmounted, ref, watchEffect } from 'vue';
import { computed, getCurrentInstance, inject, onBeforeUnmount, onMounted, ref, watchEffect } from 'vue';
import { tMagicMessage } from '@tmagic/design';
import type { FormState, FormValue } from '@tmagic/form';
Expand Down Expand Up @@ -64,7 +64,7 @@ onMounted(() => {
emit('mounted', internalInstance);
});
onUnmounted(() => {
onBeforeUnmount(() => {
services?.propsService.off('props-configs-change', init);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</template>

<script setup lang="ts">
import { computed, inject, nextTick, onMounted, onUnmounted, ref, toRaw, watch } from 'vue';
import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref, toRaw, watch } from 'vue';
import { ArrowLeftBold, ArrowRightBold, Plus } from '@element-plus/icons-vue';
import { NodeType } from '@tmagic/schema';
Expand Down Expand Up @@ -72,7 +72,7 @@ onMounted(() => {
pageBar.value && resizeObserver.observe(pageBar.value);
});
onUnmounted(() => {
onBeforeUnmount(() => {
resizeObserver.disconnect();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/layouts/workspace/viewer/NodeListMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const unWatch = watch(
(stage) => {
if (!stage) return;
nextTick(() => unWatch());
stage.on('select', (el: HTMLElement, event: MouseEvent) => {
const els = stage.renderer.getElementsFromPoint(event) || [];
const ids = els.map((el) => el.id).filter((id) => Boolean(id));
Expand All @@ -64,8 +66,6 @@ const unWatch = watch(
filterTextChangeHandler(ids);
});
unWatch();
},
{
immediate: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/layouts/workspace/viewer/Stage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</template>

<script lang="ts" setup>
import { computed, inject, markRaw, nextTick, onMounted, onUnmounted, ref, toRaw, watch, watchEffect } from 'vue';
import { computed, inject, markRaw, nextTick, onBeforeUnmount, onMounted, ref, toRaw, watch, watchEffect } from 'vue';
import { cloneDeep } from 'lodash-es';
import type { MApp, MContainer } from '@tmagic/schema';
Expand Down Expand Up @@ -140,7 +140,7 @@ onMounted(() => {
}
});
onUnmounted(() => {
onBeforeUnmount(() => {
stage?.destroy();
resizeObserver.disconnect();
services?.editorService.set('stage', null);
Expand Down
8 changes: 5 additions & 3 deletions packages/form/src/fields/Select.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</template>

<script lang="ts" setup>
import { inject, onBeforeMount, Ref, ref, watch, watchEffect } from 'vue';
import { inject, nextTick, onBeforeMount, Ref, ref, watch, watchEffect } from 'vue';
import { TMagicSelect } from '@tmagic/design';
import { getValueByKeyPath } from '@tmagic/utils';
Expand Down Expand Up @@ -331,12 +331,15 @@ if (typeof props.config.options === 'function') {
}
if (props.config.remote) {
const unWacth = watch(
const unWatch = watch(
() => tMagicSelect.value?.scrollbarWrap,
(scrollbarWrap) => {
if (!scrollbarWrap) {
return;
}
nextTick(() => unWatch());
scrollbarWrap.addEventListener('scroll', async (e: Event) => {
const el = e.currentTarget as HTMLDivElement;
if (moreLoadingVisible.value) {
Expand All @@ -353,7 +356,6 @@ if (props.config.remote) {
options.value = await getOptions();
moreLoadingVisible.value = false;
});
unWacth();
},
{
immediate: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/ui-vue2/src/useApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import { getCurrentInstance, inject, onMounted, onUnmounted } from 'vue';
import { getCurrentInstance, inject, onBeforeUnmount, onMounted } from 'vue';

import Core from '@tmagic/core';

Expand All @@ -32,7 +32,7 @@ export default (props: any) => {
node?.emit('mounted', vm);
});

onUnmounted(() => {
onBeforeUnmount(() => {
node?.emit('destroy', vm);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/useApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* limitations under the License.
*/

import { inject, onMounted, onUnmounted } from 'vue';
import { inject, onBeforeUnmount, onMounted } from 'vue';

import Core from '@tmagic/core';
import type { MComponent } from '@tmagic/schema';
Expand All @@ -43,7 +43,7 @@ export default ({ config, methods }: UseAppOptions) => {
node?.emit('mounted', emitData);
});

onUnmounted(() => {
onBeforeUnmount(() => {
node?.emit('destroy', emitData);
});

Expand Down

0 comments on commit b72e487

Please sign in to comment.