Skip to content

Commit a9d2f69

Browse files
authored
refactor: update scheduler (#465)
* refactor(@uform/core): update onFormSubmit position * fix(@uform/shared): fix shared * refactor(@uform/react): remove react scheduler deps * refactor(@uform/core): move scheduler
1 parent 1278ea3 commit a9d2f69

File tree

9 files changed

+31
-53
lines changed

9 files changed

+31
-53
lines changed

packages/core/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
FormPath,
1010
FormPathPattern,
1111
each,
12-
isObj
12+
isObj,
13+
scheduler
1314
} from '@uform/shared'
1415
import {
1516
FormValidator,
@@ -19,7 +20,6 @@ import {
1920
} from '@uform/validator'
2021
import { FormHeart } from './shared/lifecycle'
2122
import { FormGraph } from './shared/graph'
22-
import { scheduler } from './shared/scheduler'
2323
import { FormState } from './state/form'
2424
import { VirtualFieldState } from './state/virtual-field'
2525
import { FieldState } from './state/field'

packages/react/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"react": ">=16.8.0",
2828
"react-dom": ">=16.8.0",
2929
"react-eva": "^1.0.0-alpha.0",
30-
"rxjs": "^6.5.1",
31-
"scheduler": ">=0.11.2"
30+
"rxjs": "^6.5.1"
3231
},
3332
"dependencies": {
3433
"@uform/core": "^1.0.0-alpha.6",

packages/react/src/shared.ts

+1-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFn, FormPath, globalThisPolyfill, Subscribable } from '@uform/shared'
1+
import { isFn, FormPath, Subscribable } from '@uform/shared'
22
import { IFormEffect, IFormActions, IFormAsyncActions } from './types'
33
import { Observable } from 'rxjs/internal/Observable'
44
import { filter } from 'rxjs/internal/operators/filter'
@@ -124,30 +124,6 @@ export const getValueFromEvent = (event: any) => {
124124
return event
125125
}
126126

127-
const compactScheduler = ([raf, caf, priority], fresh: boolean) => {
128-
return [fresh ? callback => raf(priority, callback) : raf, caf]
129-
}
130-
131-
const getScheduler = () => {
132-
if (!globalThisPolyfill.requestAnimationFrame) {
133-
return [globalThisPolyfill.setTimeout, globalThisPolyfill.clearTimeout]
134-
}
135-
try {
136-
// eslint-disable-next-line @typescript-eslint/no-var-requires
137-
const scheduler = require('scheduler') as any
138-
return compactScheduler(
139-
[
140-
scheduler.scheduleCallback || scheduler.unstable_scheduleCallback,
141-
scheduler.cancelCallback || scheduler.unstable_cancelCallback,
142-
scheduler.NormalPriority || scheduler.unstable_NormalPriority
143-
],
144-
!!scheduler.unstable_requestPaint
145-
)
146-
} catch (err) {
147-
return [self.requestAnimationFrame, self.cancelAnimationFrame]
148-
}
149-
}
150-
151127
export class Broadcast extends Subscribable {
152128
context: any
153129

@@ -166,8 +142,6 @@ export const env = {
166142
currentActions: null
167143
}
168144

169-
export const [raf, caf] = getScheduler()
170-
171145
export const createFormEffects = <Payload = any, Actions = any>(
172146
effects: IFormEffect<Payload, Actions> | null,
173147
actions: Actions

packages/shared/src/clone.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isFn } from './types'
2-
import { globalThisPolyfill } from './global'
2+
import { instOf } from './instanceof'
33

44
type Filter = (value: any, key: string) => boolean
55

@@ -24,17 +24,11 @@ const isNativeObject = (values: any): any => {
2424
for (let i = 0; i < NATIVE_KEYS.length; i++) {
2525
const item = NATIVE_KEYS[i]
2626
if (Array.isArray(item) && item[0]) {
27-
if (
28-
globalThisPolyfill[item[0] as string] &&
29-
values instanceof globalThisPolyfill[item[0] as string]
30-
) {
27+
if (instOf(values, item[0])) {
3128
return item[1] ? item[1] : item[0]
3229
}
3330
} else {
34-
if (
35-
globalThisPolyfill[item as string] &&
36-
values instanceof globalThisPolyfill[item as string]
37-
) {
31+
if (instOf(values, item)) {
3832
return item
3933
}
4034
}

packages/shared/src/compare.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { isFn, isArr } from './types'
2+
import { instOf } from './instanceof'
23
const isArray = isArr
34
const keyList = Object.keys
45
const hasProp = Object.prototype.hasOwnProperty
@@ -44,8 +45,8 @@ function equal(a: any, b: any, filter?: Filter) {
4445
const immutableB = b && b.toJS
4546
if (immutableA !== immutableB) return false
4647
if (immutableA) return a.is ? a.is(b) : a === b
47-
const dateA = a instanceof Date
48-
const dateB = b instanceof Date
48+
const dateA = instOf(a, 'Date')
49+
const dateB = instOf(b, 'Date')
4950
if (dateA !== dateB) {
5051
return false
5152
}
@@ -56,21 +57,18 @@ function equal(a: any, b: any, filter?: Filter) {
5657
const schemaB = b && b.toJSON
5758
if (schemaA !== schemaB) return false
5859
if (schemaA && schemaB) return equal(a.toJSON(), b.toJSON(), filter)
59-
const regexpA = a instanceof RegExp
60-
const regexpB = b instanceof RegExp
60+
const regexpA = instOf(a, 'RegExp')
61+
const regexpB = instOf(b, 'RegExp')
6162
if (regexpA !== regexpB) {
6263
return false
6364
}
6465
if (regexpA && regexpB) {
6566
return a.toString() === b.toString()
6667
}
67-
// fix: Not in the browser
68-
if(typeof URL !== void(0)){
69-
const urlA = a instanceof URL
70-
const urlB = b instanceof URL
71-
if (urlA && urlB) {
72-
return a.href === b.href
73-
}
68+
const urlA = instOf(a, 'URL')
69+
const urlB = instOf(b, 'URL')
70+
if (urlA && urlB) {
71+
return a.href === b.href
7472
}
7573

7674
const keys = keyList(a)

packages/shared/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ export * from './path'
1010
export * from './deprecate'
1111
export * from './subscribable'
1212
export * from './merge'
13+
export * from './instanceof'
14+
export * from './scheduler'

packages/shared/src/instanceof.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { globalThisPolyfill } from './global'
2+
import { isStr, isFn } from './types'
3+
export const instOf = (value: any, cls: any) => {
4+
if (isFn(cls)) return value instanceof cls
5+
if (isStr(cls))
6+
return globalThisPolyfill[cls]
7+
? value instanceof globalThisPolyfill[cls]
8+
: false
9+
return false
10+
}

packages/shared/src/isEmpty.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { instOf } from './instanceof'
12
const has = Object.prototype.hasOwnProperty
23

34
const toString = Object.prototype.toString
@@ -49,7 +50,7 @@ export function isEmpty(val: any): boolean {
4950
}
5051

5152
// Errors...
52-
if (val instanceof Error) {
53+
if (instOf(val, 'Error')) {
5354
return val.message === ''
5455
}
5556

packages/core/src/shared/scheduler.ts packages/shared/src/scheduler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isFn } from '@uform/shared'
1+
import { isFn } from './types'
22

33
export const scheduler = (concurrent = 360) => {
44
let operating = false

0 commit comments

Comments
 (0)