[go: up one dir, main page]

Skip to content

Commit

Permalink
feat: upgrade @vue/reactivity to v3.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
yangmingshan committed Sep 10, 2024
1 parent 3890999 commit 3bbc7b8
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 330 deletions.
7 changes: 6 additions & 1 deletion packages/core/__tests__/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe('component', () => {
}
})
component.lifetimes.attached.call(component)
expect(component.__scope__.effects.length).toBe(3)
expect(component.__scope__.effects.length).toBe(2)

component.increment()
component.lifetimes.detached.call(component)
Expand Down Expand Up @@ -281,6 +281,11 @@ describe('component', () => {
})
component.lifetimes.attached.call(component)
await nextTick()
expect(foo).toBe(undefined)
expect(bar).toBe(undefined)
expect(component.data.count).toBe(0)

renderCb()
expect(foo).toBe(0)
expect(bar).toBe(undefined)
expect(component.data.count).toBe(0)
Expand Down
7 changes: 6 additions & 1 deletion packages/core/__tests__/page.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('page', () => {
}
})
page.onLoad()
expect(page.__scope__.effects.length).toBe(3)
expect(page.__scope__.effects.length).toBe(2)

page.increment()
page.onUnload()
Expand Down Expand Up @@ -271,6 +271,11 @@ describe('page', () => {
})
page.onLoad()
await nextTick()
expect(foo).toBe(undefined)
expect(bar).toBe(undefined)
expect(page.data.count).toBe(0)

renderCb()
expect(foo).toBe(0)
expect(bar).toBe(undefined)
expect(page.data.count).toBe(0)
Expand Down
9 changes: 0 additions & 9 deletions packages/core/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,13 +1007,4 @@ describe('watch', () => {
// Should trigger on array self mutation
expect(cb).toBeCalledTimes(1)
})

it('warn if deep option is number', async () => {
const count = ref(0)
watch(count, () => {}, {
// @ts-expect-error
deep: 1,
})
expect(`"deep" option with number value`).toHaveBeenWarned()
})
})
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"小程序"
],
"dependencies": {
"@vue/reactivity": "3.4.37",
"@vue/reactivity": "3.5.4",
"miniprogram-api-typings": "^3.12.3"
}
}
8 changes: 7 additions & 1 deletion packages/core/src/component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { shallowReactive, shallowReadonly, EffectScope } from '@vue/reactivity'
import { flushPostFlushCbs } from './scheduler'
import type { Config } from './page'
import { PageLifecycle } from './page'
import { deepToRaw, deepWatch } from './shared'
Expand Down Expand Up @@ -177,16 +178,21 @@ export function defineComponent(optionsOrSetup: any, config?: Config): string {
context,
)
if (bindings !== undefined) {
let data: Record<string, unknown> | undefined
Object.keys(bindings).forEach((key) => {
const value = bindings[key]
if (isFunction(value)) {
this[key] = value
return
}

this.setData({ [key]: deepToRaw(value) })
data = data || {}
data[key] = deepToRaw(value)
deepWatch.call(this, key, value)
})
if (data !== undefined) {
this.setData(data, flushPostFlushCbs)
}
}

unsetCurrentComponent()
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export {
// Effect
effect,
stop,
getCurrentWatcher,
onWatcherCleanup,
ReactiveEffect,
// Effect scope
effectScope,
Expand Down Expand Up @@ -103,11 +105,13 @@ export type {
Reactive,
} from '@vue/reactivity'
export type {
MultiWatchSources,
WatchEffect,
WatchOptions,
WatchOptionsBase,
WatchEffectOptions as WatchOptionsBase,
WatchCallback,
WatchSource,
WatchHandle,
WatchStopHandle,
} from './watch'
export type { InjectionKey } from './inject'
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EffectScope } from '@vue/reactivity'
import { flushPostFlushCbs } from './scheduler'
import type { Bindings, PageInstance } from './instance'
import { setCurrentPage, unsetCurrentPage } from './instance'
import { deepToRaw, deepWatch } from './shared'
Expand Down Expand Up @@ -113,16 +114,21 @@ export function definePage(optionsOrSetup: any, config?: Config): void {
}
const bindings = setup(query, context)
if (bindings !== undefined) {
let data: Record<string, unknown> | undefined
Object.keys(bindings).forEach((key) => {
const value = bindings[key]
if (isFunction(value)) {
this[key] = value
return
}

this.setData({ [key]: deepToRaw(value) })
data = data || {}
data[key] = deepToRaw(value)
deepWatch.call(this, key, value)
})
if (data !== undefined) {
this.setData(data, flushPostFlushCbs)
}
}

unsetCurrentPage()
Expand Down
25 changes: 4 additions & 21 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export const EMPTY_OBJ: Readonly<Record<string, any>> =
__DEV__ ? Object.freeze({}) : /* istanbul ignore next -- @preserve */ {}

/* istanbul ignore next -- @preserve */
// eslint-disable-next-line @typescript-eslint/no-empty-function
export const NOOP = () => {}

Expand Down Expand Up @@ -39,27 +43,6 @@ export function isFunction(x: unknown): x is Function {
return typeof x === 'function'
}

export function isMap(x: unknown): x is Map<any, any> {
return getType(x) === 'Map'
}

export function isSet(x: unknown): x is Set<any> {
return getType(x) === 'Set'
}

// Compare whether a value has changed, accounting for NaN.
export function hasChanged(value: unknown, oldValue: unknown): boolean {
// eslint-disable-next-line no-self-compare
return value !== oldValue && (value === value || oldValue === oldValue)
}

export function remove<T>(arr: T[], el: T): void {
const i = arr.indexOf(el)
if (i > -1) {
arr.splice(i, 1)
}
}

export function toHiddenField(name: string): string {
return `__${name}__`
}
Loading

0 comments on commit 3bbc7b8

Please sign in to comment.