Skip to content

Commit a443af3

Browse files
authored
fix: close #63, close #63, add logger.js (#64)
* fix: close #63, close #63, add logger.js * chore: add editor config * docs: fix issue number
1 parent 23ba9ae commit a443af3

File tree

15 files changed

+134
-106
lines changed

15 files changed

+134
-106
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 4
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true

packages/tua-mp/examples/basic/utils/tua-mp.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ var __dep__ = '__dep__';
117117
// 被框架占用的关键字,在 data 和 computed 中如果使用这些关键字,将会抛出错误
118118
var reservedKeys = ['$data', '$emit', '$computed', __TUA_PATH__];
119119

120+
/**
121+
* 统一的日志输出函数,在测试环境时不输出
122+
* @param {String} type 输出类型 log|warn|error
123+
* @param {any} out 具体的输出内容
124+
*/
125+
var logByType = function logByType(type) {
126+
return function () {
127+
var _console;
128+
129+
for (var _len = arguments.length, out = Array(_len), _key = 0; _key < _len; _key++) {
130+
out[_key] = arguments[_key];
131+
}
132+
133+
/* istanbul ignore next */
134+
(_console = console)[type].apply(_console, ['[TUA-API]:'].concat(out));
135+
};
136+
};
137+
138+
var logger = {
139+
log: logByType('log'),
140+
warn: logByType('warn'),
141+
error: logByType('error')
142+
};
143+
120144
var isFn = function isFn(fn) {
121145
return typeof fn === 'function';
122146
};
@@ -142,6 +166,9 @@ var getPathByPrefix = function getPathByPrefix(prefix, key) {
142166
return prefix === '' ? key : prefix + '.' + key;
143167
};
144168

169+
var jsonParse = JSON.parse.bind(JSON);
170+
var stringify = JSON.stringify.bind(JSON);
171+
145172
/**
146173
* 将 source 上的属性代理到 target 上
147174
* @param {Object} source 被代理对象
@@ -203,7 +230,7 @@ var setObjByPath = function setObjByPath(_ref) {
203230
);
204231
}, 'this');
205232

206-
error('Property "' + cur + '" is not found in "' + parentStr + '": ' + 'Make sure that this property has initialized in the data option.');
233+
logger.error('Property "' + cur + '" is not found in "' + parentStr + '": ' + 'Make sure that this property has initialized in the data option.');
207234
}
208235

209236
if (idx === arr.length - 1) {
@@ -258,28 +285,6 @@ var assertType = function assertType(value, type) {
258285
return { valid: valid, expectedType: expectedType };
259286
};
260287

261-
/**
262-
* 统一的日志输出函数,在测试环境时不输出
263-
* @param {String} type 输出类型 log|warn|error
264-
* @param {any} out 输出的内容
265-
*/
266-
var logByType = function logByType(type) {
267-
return function () {
268-
var _console;
269-
270-
for (var _len = arguments.length, out = Array(_len), _key = 0; _key < _len; _key++) {
271-
out[_key] = arguments[_key];
272-
}
273-
274-
/* istanbul ignore next */
275-
(_console = console)[type].apply(_console, ['[TUA-MP]:'].concat(out));
276-
};
277-
};
278-
279-
var log = logByType('log');
280-
var warn = logByType('warn');
281-
var error = logByType('error');
282-
283288
// reserved keys
284289
var isReservedKeys = function isReservedKeys(str) {
285290
return reservedKeys.indexOf(str) !== -1;
@@ -358,7 +363,7 @@ var assertProp = function assertProp(_ref) {
358363
}
359364

360365
if (!valid) {
361-
warn('Invalid prop: type check failed for prop "' + name + '".' + (' Expected ' + expectedTypes.join(', ')) + (', got ' + toRawType(value) + '.'));
366+
logger.warn('Invalid prop: type check failed for prop "' + name + '".' + (' Expected ' + expectedTypes.join(', ')) + (', got ' + toRawType(value) + '.'));
362367
}
363368

364369
return valid;
@@ -373,19 +378,15 @@ var assertProp = function assertProp(_ref) {
373378
var getObserver = function getObserver(name) {
374379
return function (prop) {
375380
return function observer(value) {
376-
var _this = this;
377-
378381
// 触发 setter
379-
Promise.resolve().then(function () {
380-
_this[name] = value;
381-
});
382+
this[name] = value;
382383

383384
var valid = assertProp({ prop: prop, name: name, value: value });
384385
var validator = prop.validator;
385386

386387

387388
if (validator && !validator(value)) {
388-
warn('Invalid prop: custom validator check failed for prop "' + name + '".');
389+
logger.warn('Invalid prop: custom validator check failed for prop "' + name + '".');
389390
return false;
390391
}
391392

@@ -491,7 +492,7 @@ var hackSetData = function hackSetData(vm) {
491492
});
492493
};
493494

494-
var version = "0.8.1";
495+
var version = "0.8.2";
495496

496497
/**
497498
* 根据 vm 生成 key
@@ -620,6 +621,10 @@ var VmStatus = function () {
620621
// 更新数据
621622
vm.updated ? setData.call(vm, newState, vm.updated) : setData.call(vm, newState);
622623

624+
// 干掉原生 setData 触发的 setter,不然会死循环
625+
delete _this.newStateByVM[vmKey];
626+
delete _this.oldStateByVM[vmKey];
627+
623628
// 触发 watch
624629
Object.keys(newState).map(function (key) {
625630
var newVal = newState[key];
@@ -1051,7 +1056,7 @@ var bindComputed = function bindComputed(vm, computed, asyncSetData) {
10511056
},
10521057
set: function set$$1() {
10531058
if (typeof computed[key].set === 'undefined') {
1054-
warn('Computed property "' + key + '" was assigned to but it has no setter.');
1059+
logger.warn('Computed property "' + key + '" was assigned to but it has no setter.');
10551060
} else {
10561061
var setVal = computed[key].set.bind(vm);
10571062
setVal.apply(undefined, arguments);
@@ -1264,6 +1269,6 @@ var TuaPage = function TuaPage(_ref) {
12641269
}));
12651270
};
12661271

1267-
log('Version ' + version);
1272+
logger.log('Version ' + version);
12681273

12691274
export { TuaComp, TuaPage };

packages/tua-mp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tua-mp",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "A progressive miniprogram framework for coding like Vue",
55
"main": "examples/basic/utils/tua-mp.js",
66
"files": [

packages/tua-mp/src/TuaComp.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export const TuaComp = ({
4545
created (...options) {
4646
rest.beforeCreate && rest.beforeCreate.apply(this, options)
4747
rest.created && rest.created.apply(this, options)
48+
49+
if (process.env.NODE_ENV === 'test') {
50+
this.$props = {
51+
...properties,
52+
...getPropertiesFromProps(props),
53+
}
54+
}
4855
},
4956
attached (...options) {
5057
rest.beforeMount && rest.beforeMount.apply(this, options)

packages/tua-mp/src/VmStatus.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ export default class VmStatus {
109109
? setData.call(vm, newState, vm.updated)
110110
: setData.call(vm, newState)
111111

112+
// 干掉原生 setData 触发的 setter,不然会死循环
113+
delete this.newStateByVM[vmKey]
114+
delete this.oldStateByVM[vmKey]
115+
112116
// 触发 watch
113117
Object.keys(newState)
114118
.map((key) => {

packages/tua-mp/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { log } from './utils/index'
1+
import { logger } from './utils/index'
22
import { version } from '../package.json'
33

4-
log(`Version ${version}`)
4+
logger.log(`Version ${version}`)
55

66
export * from './TuaComp'
77
export * from './TuaPage'

packages/tua-mp/src/init.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import {
22
isFn,
3-
warn,
3+
logger,
44
proxyData,
55
getValByPath,
66
} from './utils/index'
7-
import {
8-
COMMON_PROP,
9-
} from './constants'
107
import Dep from './observer/dep'
8+
import { COMMON_PROP } from './constants'
119

1210
/**
1311
* 遍历观察 vm.data 中的所有属性,并将其直接挂到 vm 上
@@ -82,7 +80,7 @@ export const bindComputed = (vm, computed, asyncSetData) => {
8280
},
8381
set (...options) {
8482
if (typeof computed[key].set === 'undefined') {
85-
warn(`Computed property "${key}" was assigned to but it has no setter.`)
83+
logger.warn(`Computed property "${key}" was assigned to but it has no setter.`)
8684
} else {
8785
const setVal = computed[key].set.bind(vm)
8886
setVal(...options)

packages/tua-mp/src/utils/basic.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
reservedKeys,
66
__TUA_PATH__,
77
} from '../constants'
8+
import { logger } from './logger'
89

910
export const isFn = fn => typeof fn === 'function'
1011

@@ -19,8 +20,12 @@ export const isPlainObject = value =>
1920
_toString.call(value) === '[object Object]'
2021

2122
// 根据路径前缀和 key 得到当前路径
22-
export const getPathByPrefix = (prefix, key) =>
23-
prefix === '' ? key : `${prefix}.${key}`
23+
export const getPathByPrefix = (prefix, key) => prefix === ''
24+
? key
25+
: `${prefix}.${key}`
26+
27+
export const jsonParse = JSON.parse.bind(JSON)
28+
export const stringify = JSON.stringify.bind(JSON)
2429

2530
/**
2631
* 将 source 上的属性代理到 target 上
@@ -79,7 +84,7 @@ export const setObjByPath = ({ obj, path, val, isCheckDef = false }) => pathStr2
7984
'this'
8085
)
8186

82-
error(
87+
logger.error(
8388
`Property "${cur}" is not found in "${parentStr}": ` +
8489
`Make sure that this property has initialized in the data option.`
8590
)
@@ -138,23 +143,6 @@ export const assertType = (value, type) => {
138143
return { valid, expectedType }
139144
}
140145

141-
/**
142-
* 统一的日志输出函数,在测试环境时不输出
143-
* @param {String} type 输出类型 log|warn|error
144-
* @param {any} out 输出的内容
145-
*/
146-
const logByType = (type) => (...out) => {
147-
/* istanbul ignore else */
148-
if (process.env.NODE_ENV === 'test') return
149-
150-
/* istanbul ignore next */
151-
console[type](`[TUA-MP]:`, ...out)
152-
}
153-
154-
export const log = logByType('log')
155-
export const warn = logByType('warn')
156-
export const error = logByType('error')
157-
158146
// reserved keys
159147
const isReservedKeys = str => reservedKeys.indexOf(str) !== -1
160148
const getObjHasReservedKeys = obj => Object.keys(obj).filter(isReservedKeys)

packages/tua-mp/src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './comp'
22
export * from './basic'
33
export * from './props'
4+
export * from './logger'
45
export * from './hackSetData'

packages/tua-mp/src/utils/logger.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 统一的日志输出函数,在测试环境时不输出
3+
* @param {String} type 输出类型 log|warn|error
4+
* @param {any} out 具体的输出内容
5+
*/
6+
const logByType = (type) => (...out) => {
7+
/* istanbul ignore else */
8+
if (process.env.NODE_ENV === 'test') return
9+
10+
/* istanbul ignore next */
11+
console[type](`[TUA-API]:`, ...out)
12+
}
13+
14+
export const logger = {
15+
log: logByType('log'),
16+
warn: logByType('warn'),
17+
error: logByType('error'),
18+
}

0 commit comments

Comments
 (0)