Skip to content

Commit 0922b1d

Browse files
kazuponyyx990803
authored andcommitted
improve set/delete API (#5050)
1 parent d6999c1 commit 0922b1d

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

flow/component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ declare interface Component {
3535
$mount: (el?: Element | string, hydrating?: boolean) => Component;
3636
$forceUpdate: () => void;
3737
$destroy: () => void;
38-
$set: (obj: Array<mixed> | Object, key: mixed, val: mixed) => void;
39-
$delete: (obj: Object, key: string) => void;
38+
$set: <T>(target: Object | Array<T>, key: string | number, val: T) => T;
39+
$delete: <T>(target: Object | Array<T>, key: string | number) => void;
4040
$watch: (expOrFn: string | Function, cb: Function, options?: Object) => Function;
4141
$on: (event: string | Array<string>, fn: Function) => Component;
4242
$once: (event: string, fn: Function) => Component;

flow/global-api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ declare interface GlobalAPI {
55
util: Object;
66

77
extend: (options: Object) => Function;
8-
set: (obj: Object, key: string, value: any) => void;
9-
delete: (obj: Object, key: string) => void;
8+
set: <T>(target: Object | Array<T>, key: string | number, value: T) => T;
9+
delete: <T>(target: Object| Array<T>, key: string | number) => void;
1010
nextTick: (fn: Function, context?: Object) => void;
1111
use: (plugin: Function | Object) => void;
1212
mixin: (mixin: Object) => void;

src/core/observer/index.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -188,27 +188,27 @@ export function defineReactive (
188188
* triggers change notification if the property doesn't
189189
* already exist.
190190
*/
191-
export function set (obj: Array<any> | Object, key: any, val: any) {
192-
if (Array.isArray(obj)) {
193-
obj.length = Math.max(obj.length, key)
194-
obj.splice(key, 1, val)
191+
export function set (target: Array<any> | Object, key: any, val: any): any {
192+
if (Array.isArray(target)) {
193+
target.length = Math.max(target.length, key)
194+
target.splice(key, 1, val)
195195
return val
196196
}
197-
if (hasOwn(obj, key)) {
198-
obj[key] = val
199-
return
197+
if (hasOwn(target, key)) {
198+
target[key] = val
199+
return val
200200
}
201-
const ob = obj.__ob__
202-
if (obj._isVue || (ob && ob.vmCount)) {
201+
const ob = target.__ob__
202+
if (target._isVue || (ob && ob.vmCount)) {
203203
process.env.NODE_ENV !== 'production' && warn(
204204
'Avoid adding reactive properties to a Vue instance or its root $data ' +
205205
'at runtime - declare it upfront in the data option.'
206206
)
207-
return
207+
return val
208208
}
209209
if (!ob) {
210-
obj[key] = val
211-
return
210+
target[key] = val
211+
return val
212212
}
213213
defineReactive(ob.value, key, val)
214214
ob.dep.notify()
@@ -218,23 +218,23 @@ export function set (obj: Array<any> | Object, key: any, val: any) {
218218
/**
219219
* Delete a property and trigger change if necessary.
220220
*/
221-
export function del (obj: Array<any> | Object, key: any) {
222-
if (Array.isArray(obj)) {
223-
obj.splice(key, 1)
221+
export function del (target: Array<any> | Object, key: any) {
222+
if (Array.isArray(target)) {
223+
target.splice(key, 1)
224224
return
225225
}
226-
const ob = obj.__ob__
227-
if (obj._isVue || (ob && ob.vmCount)) {
226+
const ob = target.__ob__
227+
if (target._isVue || (ob && ob.vmCount)) {
228228
process.env.NODE_ENV !== 'production' && warn(
229229
'Avoid deleting properties on a Vue instance or its root $data ' +
230230
'- just set it to null.'
231231
)
232232
return
233233
}
234-
if (!hasOwn(obj, key)) {
234+
if (!hasOwn(target, key)) {
235235
return
236236
}
237-
delete obj[key]
237+
delete target[key]
238238
if (!ob) {
239239
return
240240
}

test/unit/modules/observer/observer.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ describe('Observer', () => {
312312
data
313313
}).$mount()
314314
expect(vm.$el.outerHTML).toBe('<div>1</div>')
315-
Vue.set(data, 'a', 2)
315+
expect(Vue.set(data, 'a', 2)).toBe(2)
316316
waitForUpdate(() => {
317317
expect(vm.$el.outerHTML).toBe('<div>2</div>')
318318
expect('Avoid adding reactive properties to a Vue instance').not.toHaveBeenWarned()
319319
Vue.delete(data, 'a')
320320
}).then(() => {
321321
expect('Avoid deleting properties on a Vue instance').toHaveBeenWarned()
322322
expect(vm.$el.outerHTML).toBe('<div>2</div>')
323-
Vue.set(data, 'b', 123)
323+
expect(Vue.set(data, 'b', 123)).toBe(123)
324324
expect('Avoid adding reactive properties to a Vue instance').toHaveBeenWarned()
325325
}).then(done)
326326
})

0 commit comments

Comments
 (0)