Skip to content

Commit

Permalink
remove mpx utils directly imports from mpx core
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Aug 22, 2024
1 parent 1628db9 commit 4c12cc7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/platform/export/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { set, del, reactive } from '../../observer/reactive'
import { set, del, reactive, isReactive } from '../../observer/reactive'
import { isRef } from '../../observer/ref'
import { watch } from '../../observer/watch'
import { injectMixins } from '../../core/injectMixins'

Expand All @@ -8,7 +9,9 @@ const APIs = {
observable: reactive,
watch,
set,
delete: del
delete: del,
isReactive,
isRef
}

const InstanceAPIs = {
Expand Down
21 changes: 12 additions & 9 deletions packages/core/src/platform/export/api.web.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import Vue from 'vue'
import {
watch,
reactive,
isReactive,
set,
del,
isRef
} from 'vue'
import { injectMixins } from '../../core/injectMixins'

const vm = new Vue()
const observable = Vue.observable.bind(Vue)
const watch = vm.$watch.bind(vm)
const set = Vue.set.bind(Vue)
const del = Vue.delete.bind(Vue)

const APIs = {
injectMixins,
mixin: injectMixins,
observable,
observable: reactive,
watch,
set,
delete: del
delete: del,
isReactive,
isRef
}

const InstanceAPIs = {}
Expand Down
25 changes: 17 additions & 8 deletions packages/utils/src/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isRef, isReactive } from '@mpxjs/core'
import { type, noop } from './base'

const hasOwnProperty = Object.prototype.hasOwnProperty
Expand Down Expand Up @@ -113,25 +112,35 @@ function diffAndCloneA (a, b) {
}

function proxy (target, source, keys, readonly, onConflict) {
if (!global.__mpx) {
console.warn('[Mpx utils warn]: Can not find "global.__mpx", "proxy" may encounter some potential problems!')
}
keys = keys || Object.keys(source)
keys.forEach((key) => {
const descriptor = {
get () {
const val = source[key]
return !isReactive(source) && isRef(val) ? val.value : val
if (global.__mpx) {
return !global.__mpx.isReactive(source) && global.__mpx.isRef(val) ? val.value : val
} else {
return val
}
},
configurable: true,
enumerable: true
}
descriptor.set = readonly
? noop
: function (val) {
// 对reactive对象代理时不需要处理ref解包
if (!isReactive(source)) {
const oldVal = source[key]
if (isRef(oldVal) && !isRef(val)) {
oldVal.value = val
return
if (global.__mpx) {
const isRef = global.__mpx.isRef
// 对reactive对象代理时不需要处理ref解包
if (!global.__mpx.isReactive(source)) {
const oldVal = source[key]
if (isRef(oldVal) && !isRef(val)) {
oldVal.value = val
return
}
}
}
source[key] = val
Expand Down
11 changes: 8 additions & 3 deletions packages/utils/src/path.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { set } from '@mpxjs/core'

let curStack
let targetStacks
let property
Expand Down Expand Up @@ -155,9 +153,16 @@ function getByPath (data, pathStrOrArr, defaultVal, errTip) {
}

function setByPath (data, pathStrOrArr, value) {
if (!global.__mpx) {
console.warn('[Mpx utils warn]: Can not find "global.__mpx", "setByPath" may encounter some potential problems!')
}
doGetByPath(data, pathStrOrArr, (current, key, meta) => {
if (meta.isEnd) {
set(current, key, value)
if (global.__mpx) {
global.__mpx.set(current, key, value)
} else {
current[key] = value
}
} else if (!current[key]) {
current[key] = {}
}
Expand Down

0 comments on commit 4c12cc7

Please sign in to comment.