Skip to content

Commit

Permalink
fix: 高德修改对象引用问题 (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvisei authored Sep 15, 2023
1 parent 916d04f commit e610cd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
11 changes: 4 additions & 7 deletions packages/li-editor/src/hooks/internal/use-immer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import type { Draft, nothing } from 'immer';
import produce, { freeze, setAutoFreeze } from 'immer';
import produce, { freeze } from 'immer';
import type { Dispatch } from 'react';
import { useCallback, useMemo, useReducer, useState } from 'react';

setAutoFreeze(false);

export type Reducer<S = any, A = any> = (
draftState: Draft<S>,
action: A,
) => void | (S extends undefined ? typeof nothing : S);
export type DraftFunction<S> = (draft: Draft<S>) => void;
export type Updater<S> = (arg: S | DraftFunction<S>) => void;
export type ImmerHook<S> = [S, Updater<S>];
export function useImmer<S = any>(initialValue: S | (() => S)): ImmerHook<S>;
export function useImmer<S = any>(initialValue: S | (() => S), freezeDeep?: boolean): ImmerHook<S>;

export function useImmer(initialValue: any) {
export function useImmer(initialValue: any, freezeDeep: boolean = true) {
const [val, updateValue] = useState(() =>
// change freeze to default
freeze(typeof initialValue === 'function' ? initialValue() : initialValue),
freeze(typeof initialValue === 'function' ? initialValue() : initialValue, freezeDeep),
);
return [
val,
Expand Down
14 changes: 12 additions & 2 deletions packages/li-editor/src/layout/RuntimeApp/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Application, DatasetSchema, LIRuntimeApp } from '@antv/li-sdk';
import { useLatest, useMemoizedFn, useUpdateEffect } from 'ahooks';
import classNames from 'classnames';
import type { WritableDraft } from 'immer/dist/internal';
import { debounce } from 'lodash-es';
import { cloneDeep, debounce } from 'lodash-es';
import type { CSSProperties } from 'react';
import React from 'react';
import type { FallbackProps } from 'react-error-boundary';
Expand All @@ -23,6 +23,13 @@ function FallbackRender({ error, resetErrorBoundary }: FallbackProps) {
);
}

// 使用 Editor 传下来的 defaultApplication 可能是被 freeze 的
// 高德地图初始化的时候,会使用 center 属性,直接引用修改变量
// 为避免修改失败报错,解冻 map 属性
function cloneDefaultApplication(defaultApplication: Application): Application {
return { ...defaultApplication, spec: { ...defaultApplication.spec, map: cloneDeep(defaultApplication.spec.map) } };
}

export type RuntimeAppProps = {
/** 默认的应用配置 */
defaultApplication: Application;
Expand All @@ -44,7 +51,10 @@ const RuntimeApp: React.FC<RuntimeAppProps> = (props) => {
const { state } = useEditorState();

// LISDK 配置项
const [sdkConfigState, updateSdkConfigState] = useImmer<Application>(defaultApplication);
const [sdkConfigState, updateSdkConfigState] = useImmer<Application>(
cloneDefaultApplication(defaultApplication),
false,
);

const latestAppConfigRef = useLatest({
...sdkConfigState,
Expand Down

0 comments on commit e610cd0

Please sign in to comment.