[improvement] optimize utils (#3104)

This commit is contained in:
neverland 2019-04-08 17:21:05 +08:00 committed by GitHub
parent 1a7daecdbc
commit d3054fd4b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 11 deletions

View File

@ -1,7 +1,7 @@
/* eslint-disable no-empty */
/* eslint-disable getter-return */
/* eslint-disable import/no-mutable-exports */
import { noop, isServer } from '.';
import { isServer } from '.';
type EventHanlder = (event?: Event) => void;
@ -16,18 +16,29 @@ if (!isServer) {
supportsPassive = true;
}
});
window.addEventListener('test-passive', noop, opts);
window.addEventListener('test-passive', null as any, opts);
} catch (e) {}
}
export function on(target: HTMLElement, event: string, handler: EventHanlder, passive = false) {
export function on(
target: HTMLElement,
event: string,
handler: EventHanlder,
passive = false
) {
if (!isServer) {
target.addEventListener(event, handler, supportsPassive ? { capture: false, passive } : false);
target.addEventListener(
event,
handler,
supportsPassive ? { capture: false, passive } : false
);
}
}
export function off(target: HTMLElement, event: string, handler: EventHanlder) {
!isServer && target.removeEventListener(event, handler);
if (!isServer) {
target.removeEventListener(event, handler);
}
}
export function stop(event: Event) {

View File

@ -19,10 +19,10 @@ function fallback(fn: FrameRequestCallback): number {
const root = <Window>(isServer ? global : window);
/* istanbul ignore next */
const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || fallback;
const iRaf = root.requestAnimationFrame || fallback;
/* istanbul ignore next */
const iCancel = root.cancelAnimationFrame || root.webkitCancelAnimationFrame || root.clearTimeout;
const iCancel = root.cancelAnimationFrame || root.clearTimeout;
export function raf(fn: FrameRequestCallback): number {
return iRaf.call(root, fn);

View File

@ -85,10 +85,8 @@ function defaultProps(props: any) {
function install(this: ComponentOptions<Vue>, Vue: VueConstructor) {
const { name } = this;
if (name) {
Vue.component(name, this);
Vue.component(name as string, this);
Vue.component(camelize(`-${name}`), this);
}
}
// unify slots & scopedSlots