Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): app.provide throws a warning when assigning the same value. #10337

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ describe('api: createApp', () => {
expect(serializeInner(root)).toBe(`3,2`)
expect('[Vue warn]: injection "__proto__" not found.').toHaveBeenWarned()

app.provide('baz', 1)
app.provide('baz', 1)
expect(
`App already provides property with key "baz".`,
).not.toHaveBeenWarned()

const app2 = createApp(Root)
app2.provide('bar', 1)
app2.provide('bar', 2)
Expand Down
23 changes: 14 additions & 9 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { warn } from './warning'
import { type VNode, cloneVNode, createVNode } from './vnode'
import type { RootHydrateFunction } from './hydration'
import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
import { NO, extend, isFunction, isObject } from '@vue/shared'
import { NO, extend, hasChanged, isFunction, isObject } from '@vue/shared'
import { version } from '.'
import { installAppCompatProperties } from './compat/global'
import type { NormalizedPropsOptions } from './componentProps'
Expand Down Expand Up @@ -373,15 +373,20 @@ export function createAppAPI<HostElement>(
}
},

provide(key, value) {
if (__DEV__ && (key as string | symbol) in context.provides) {
warn(
`App already provides property with key "${String(key)}". ` +
`It will be overwritten with the new value.`,
)
}
provide(key, newVal) {
const oldValue = context.provides[key as string | symbol]
const isValueChanged = hasChanged(oldValue, newVal)

if (isValueChanged) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (isValueChanged) { could be merged with previous if

Copy link
Contributor Author

@OnlyWick OnlyWick Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you suggesting to put it inside an else block? Since the value is already the same, there is no need to perform the assignment again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

if (isValueChanged) {
    if (...) warn()
  
    context.provides[key as string | symbol] = newVal
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it.

if (__DEV__ && (key as string | symbol) in context.provides) {
warn(
`App already provides property with key "${String(key)}". ` +
`It will be overwritten with the new value.`,
)
}

context.provides[key as string | symbol] = value
context.provides[key as string | symbol] = newVal
}

return app
},
Expand Down
Loading