mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 10:59:42 +08:00
更换 autobind (#1433)
This commit is contained in:
parent
56fa20ed27
commit
5d05967028
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "amis",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.2",
|
||||
"description": "一种MIS页面生成工具",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
@ -38,7 +38,6 @@
|
||||
"dependencies": {
|
||||
"async": "2.6.0",
|
||||
"attr-accept": "2.2.2",
|
||||
"autobind-decorator": "2.4.0",
|
||||
"blueimp-canvastoblob": "2.1.0",
|
||||
"classnames": "2.2.5",
|
||||
"dom-helpers": "^3.3.1",
|
||||
|
@ -197,6 +197,8 @@ export class AsideNav extends React.Component<AsideNavProps, AsideNavState> {
|
||||
|
||||
if (!dom) {
|
||||
return;
|
||||
} else if (key === 'subHeader') {
|
||||
return dom;
|
||||
}
|
||||
|
||||
return (
|
||||
|
111
src/utils/autobind.ts
Normal file
111
src/utils/autobind.ts
Normal file
@ -0,0 +1,111 @@
|
||||
const {defineProperty, getPrototypeOf} = Object;
|
||||
|
||||
export function bind(fn: Function, context: any) {
|
||||
if (fn.bind) {
|
||||
return fn.bind(context);
|
||||
} else {
|
||||
return function __autobind__() {
|
||||
return fn.apply(context, arguments);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let mapStore: WeakMap<Object, any>;
|
||||
function getBoundSuper(obj: Object, fn: Function) {
|
||||
if (typeof WeakMap === 'undefined') {
|
||||
throw new Error(
|
||||
`Using @autobind on ${fn.name}() requires WeakMap support due to its use of super.${fn.name}()
|
||||
See https://github.com/jayphelps/core-decorators.js/issues/20`
|
||||
);
|
||||
}
|
||||
|
||||
if (!mapStore) {
|
||||
mapStore = new WeakMap();
|
||||
}
|
||||
|
||||
if (mapStore.has(obj) === false) {
|
||||
mapStore.set(obj, new WeakMap());
|
||||
}
|
||||
|
||||
const superStore = mapStore.get(obj);
|
||||
|
||||
if (superStore.has(fn) === false) {
|
||||
superStore.set(fn, bind(fn, obj));
|
||||
}
|
||||
|
||||
return superStore.get(fn);
|
||||
}
|
||||
|
||||
function createDefaultSetter(key: string) {
|
||||
return function set(this: any, newValue: any) {
|
||||
Object.defineProperty(this, key, {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
// IS enumerable when reassigned by the outside word
|
||||
enumerable: true,
|
||||
value: newValue
|
||||
});
|
||||
|
||||
return newValue;
|
||||
};
|
||||
}
|
||||
|
||||
export function autobindMethod(
|
||||
target: Object,
|
||||
key: string,
|
||||
{value: fn, configurable, enumerable}: TypedPropertyDescriptor<Function>
|
||||
) {
|
||||
if (typeof fn !== 'function') {
|
||||
throw new SyntaxError(
|
||||
`@autobind can only be used on functions, not: ${fn}`
|
||||
);
|
||||
}
|
||||
|
||||
const {constructor} = target;
|
||||
|
||||
return {
|
||||
configurable,
|
||||
enumerable,
|
||||
|
||||
get() {
|
||||
// Class.prototype.key lookup
|
||||
// Someone accesses the property directly on the prototype on which it is
|
||||
// actually defined on, i.e. Class.prototype.hasOwnProperty(key)
|
||||
if (this === target) {
|
||||
return fn;
|
||||
}
|
||||
|
||||
// Class.prototype.key lookup
|
||||
// Someone accesses the property directly on a prototype but it was found
|
||||
// up the chain, not defined directly on it
|
||||
// i.e. Class.prototype.hasOwnProperty(key) == false && key in Class.prototype
|
||||
if (
|
||||
this.constructor !== constructor &&
|
||||
getPrototypeOf(this).constructor === constructor
|
||||
) {
|
||||
return fn;
|
||||
}
|
||||
|
||||
// Autobound method calling super.sameMethod() which is also autobound and so on.
|
||||
if (
|
||||
this.constructor !== constructor &&
|
||||
key in this.constructor.prototype
|
||||
) {
|
||||
return getBoundSuper(this, fn);
|
||||
}
|
||||
|
||||
const boundFn = bind(fn, this);
|
||||
|
||||
defineProperty(this, key, {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
// NOT enumerable when it's a bound method
|
||||
enumerable: false,
|
||||
value: boundFn
|
||||
});
|
||||
|
||||
return boundFn;
|
||||
},
|
||||
set: createDefaultSetter(key)
|
||||
};
|
||||
}
|
@ -3,10 +3,10 @@ import isEqual from 'lodash/isEqual';
|
||||
import uniq from 'lodash/uniq';
|
||||
import {Schema, PlainObject, FunctionPropertyNames} from '../types';
|
||||
import {evalExpression} from './tpl';
|
||||
import {boundMethod} from 'autobind-decorator';
|
||||
import qs from 'qs';
|
||||
import {IIRendererStore} from '../store';
|
||||
import {IFormStore} from '../store/form';
|
||||
import {autobindMethod} from './autobind';
|
||||
|
||||
// 方便取值的时候能够把上层的取到,但是获取的时候不会全部把所有的数据获取到。
|
||||
export function createObject(
|
||||
@ -1141,7 +1141,7 @@ export function pickEventsProps(props: any) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
export const autobind = boundMethod;
|
||||
export const autobind = autobindMethod;
|
||||
|
||||
export const bulkBindFunctions = function <
|
||||
T extends {
|
||||
|
Loading…
Reference in New Issue
Block a user