Skip to content

Commit

Permalink
fix(reactive): fix batch api can not throw error (#2268)
Browse files Browse the repository at this point in the history
* fix(reactive): fix batch api can not throw error

* fix(json-schema): fix ci
  • Loading branch information
janryWang authored Oct 5, 2021
1 parent 4c1cfed commit 07227ad
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 16 deletions.
14 changes: 8 additions & 6 deletions packages/json-schema/src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,15 @@ const getUserReactions =
reaction.effects = effects?.length ? effects : DefaultFieldEffects
}
if (reaction.effects) {
untracked(() => {
each(reaction.effects, (type) => {
if (FieldEffects[type]) {
FieldEffects[type](field.address, run)
}
autorun.memo(() => {
untracked(() => {
each(reaction.effects, (type) => {
if (FieldEffects[type]) {
FieldEffects[type](field.address, run)
}
})
})
})
}, [])
} else {
run()
}
Expand Down
9 changes: 6 additions & 3 deletions packages/path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const isMatcher = Symbol('PATH_MATCHER')

const isValid = (val: any) => val !== undefined && val !== null

const isAssignable = (val: any) =>
typeof val === 'object' || typeof val === 'function'

const isNumberIndex = (val: any) =>
isStr(val) ? /^\d+$/.test(val) : isNum(val)

Expand Down Expand Up @@ -44,7 +47,7 @@ const setIn = (segments: Segments, source: any, value: any) => {
const index = segments[i]
const rules = getDestructor(index as string)
if (!rules) {
if (!isValid(source)) return
if (!isValid(source) || !isAssignable(source)) return
if (isArr(source) && !isNumberIndex(index)) {
return
}
Expand Down Expand Up @@ -77,7 +80,7 @@ const deleteIn = (segments: Segments, source: any) => {
return
}

if (!isValid(source)) return
if (!isValid(source) || !isAssignable(source)) return
source = source[index]
if (!isObj(source)) {
return
Expand Down Expand Up @@ -107,7 +110,7 @@ const existIn = (segments: Segments, source: any, start: number | Path) => {
return hasOwnProperty.call(source, index)
}

if (!isValid(source)) return false
if (!isValid(source) || !isAssignable(source)) return false
source = source[index]

if (!isObj(source)) {
Expand Down
4 changes: 1 addition & 3 deletions packages/react/src/components/SchemaField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ export function createSchemaField<Components extends SchemaReactComponents>(
function MarkupField<
Decorator extends ReactComponentPath<Components>,
Component extends ReactComponentPath<Components>
>(
props: ISchemaMarkupFieldProps<Components, Component, Decorator>
): React.ReactElement {
>(props: ISchemaMarkupFieldProps<Components, Component, Decorator>) {
const parent = useContext(SchemaMarkupContext)
if (!parent) return <Fragment />
const renderChildren = () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/reactive/src/__tests__/batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ describe('normal batch', () => {
expect(handler).toBeCalledTimes(2)
expect(obs.cc).toEqual(41)
})

test('batch error', () => {
let error = null
try {
batch(() => {
throw '123'
})
} catch (e) {
error = e
}
expect(error).toEqual('123')
})
})

describe('annotation batch', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/reactive/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ export const createBoundaryFunction = (
) => {
function boundary<F extends (...args: any) => any>(fn?: F): ReturnType<F> {
let results: ReturnType<F>
start()
try {
start()
if (isFn(fn)) {
results = fn()
}
} finally {
end()
return results
}
return results
}

boundary.bound = createBindFunction(boundary)
Expand Down
4 changes: 2 additions & 2 deletions scripts/jest.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module.exports = {
// moduleNameMapper: process.env.TEST_ENV === 'production' ? undefined : alias,
globals: {
'ts-jest': {
babelConfig: true,
tsconfig: 'tsconfig.jest.json',
babelConfig: false,
tsconfig: path.resolve(__dirname, '../tsconfig.jest.json'),
diagnostics: false,
},
},
Expand Down
5 changes: 5 additions & 0 deletions tsconfig.jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"jsx": "react",
"esModuleInterop": true,
"moduleResolution": "node",
"allowJs": true,
"module": "commonjs",
"target": "es5",
"paths": {
"@formily/*": [
"./packages/*/src",
Expand Down

0 comments on commit 07227ad

Please sign in to comment.