Skip to content

Commit

Permalink
chore: add subfolders to lint and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
pikax committed Jun 19, 2020
1 parent 5f72e26 commit 233dafa
Show file tree
Hide file tree
Showing 29 changed files with 2,426 additions and 2,293 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"scripts": {
"start": "concurrently \"tsc --emitDeclarationOnly -w\" \"cross-env TARGET=es rollup -c -w\"",
"build": "rimraf dist typings && tsc --emitDeclarationOnly && rollup -c",
"lint": "prettier --write --parser typescript \"{src,test,test-dts}/*.ts?(x)\" && prettier --write \"{src,test}/*.js\"",
"lint": "prettier --write --parser typescript \"{src,test,test-dts}/**/*.ts?(x)\" && prettier --write \"{src,test}/**/*.js\"",
"test": "yarn test-dts && yarn test-unit",
"test-unit": "cross-env NODE_ENV=test jest",
"test-dts": "tsc -p ./test-dts/tsconfig.json && yarn build && tsc -p ./test-dts/tsconfig.build.json",
Expand Down
40 changes: 20 additions & 20 deletions src/apis/computed.ts
@@ -1,34 +1,34 @@
import { getCurrentVue, getCurrentVM } from '../runtimeContext';
import { createRef, Ref } from '../reactivity';
import { defineComponentInstance } from '../helper';
import { warn } from '../utils';
import { getCurrentVue, getCurrentVM } from '../runtimeContext'
import { createRef, Ref } from '../reactivity'
import { defineComponentInstance } from '../helper'
import { warn } from '../utils'

interface Option<T> {
get: () => T;
set: (value: T) => void;
get: () => T
set: (value: T) => void
}

export interface ComputedRef<T = any> extends WritableComputedRef<T> {
readonly value: T;
readonly value: T
}

export interface WritableComputedRef<T> extends Ref<T> {}

// read-only
export function computed<T>(getter: Option<T>['get']): ComputedRef<T>;
export function computed<T>(getter: Option<T>['get']): ComputedRef<T>
// writable
export function computed<T>(options: Option<T>): WritableComputedRef<T>;
export function computed<T>(options: Option<T>): WritableComputedRef<T>
// implement
export function computed<T>(
options: Option<T>['get'] | Option<T>
): ComputedRef<T> | WritableComputedRef<T> {
const vm = getCurrentVM();
let get: Option<T>['get'], set: Option<T>['set'] | undefined;
const vm = getCurrentVM()
let get: Option<T>['get'], set: Option<T>['set'] | undefined
if (typeof options === 'function') {
get = options;
get = options
} else {
get = options.get;
set = options.set;
get = options.get
set = options.set
}

const computedHost = defineComponentInstance(getCurrentVue(), {
Expand All @@ -38,19 +38,19 @@ export function computed<T>(
set,
},
},
});
})

vm && vm.$on('hook:destroyed', () => computedHost.$destroy());
vm && vm.$on('hook:destroyed', () => computedHost.$destroy())

return createRef<T>({
get: () => (computedHost as any).$$state,
set: (v: T) => {
if (__DEV__ && !set) {
warn('Computed property was assigned to but it has no setter.', vm!);
return;
warn('Computed property was assigned to but it has no setter.', vm!)
return
}

(computedHost as any).$$state = v;
;(computedHost as any).$$state = v
},
});
})
}
56 changes: 31 additions & 25 deletions src/apis/inject.ts
@@ -1,59 +1,65 @@
import { ComponentInstance } from '../component';
import { currentVMInFn } from '../helper';
import { hasOwn, warn } from '../utils';
import { getCurrentVM } from '../runtimeContext';
import { ComponentInstance } from '../component'
import { currentVMInFn } from '../helper'
import { hasOwn, warn } from '../utils'
import { getCurrentVM } from '../runtimeContext'

const NOT_FOUND = {};
const NOT_FOUND = {}
export interface InjectionKey<T> extends Symbol {}

function resolveInject(provideKey: InjectionKey<any> | string, vm: ComponentInstance): any {
let source = vm;
function resolveInject(
provideKey: InjectionKey<any> | string,
vm: ComponentInstance
): any {
let source = vm
while (source) {
// @ts-ignore
if (source._provided && hasOwn(source._provided, provideKey)) {
//@ts-ignore
return source._provided[provideKey];
return source._provided[provideKey]
}
source = source.$parent;
source = source.$parent
}

return NOT_FOUND;
return NOT_FOUND
}

export function provide<T>(key: InjectionKey<T> | string, value: T): void {
const vm: any = currentVMInFn('provide');
if (!vm) return;
const vm: any = currentVMInFn('provide')
if (!vm) return

if (!vm._provided) {
const provideCache = {};
const provideCache = {}
Object.defineProperty(vm, '_provided', {
get: () => provideCache,
set: (v) => Object.assign(provideCache, v),
});
})
}

vm._provided[key as string] = value;
vm._provided[key as string] = value
}

export function inject<T>(key: InjectionKey<T> | string): T | undefined;
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T;
export function inject(key: InjectionKey<any> | string, defaultValue?: unknown) {
export function inject<T>(key: InjectionKey<T> | string): T | undefined
export function inject<T>(key: InjectionKey<T> | string, defaultValue: T): T
export function inject(
key: InjectionKey<any> | string,
defaultValue?: unknown
) {
if (!key) {
return defaultValue;
return defaultValue
}

const vm = getCurrentVM();
const vm = getCurrentVM()
if (vm) {
const val = resolveInject(key, vm);
const val = resolveInject(key, vm)
if (val !== NOT_FOUND) {
return val;
return val
} else {
if (defaultValue === undefined && process.env.NODE_ENV !== 'production') {
warn(`Injection "${String(key)}" not found`, vm);
warn(`Injection "${String(key)}" not found`, vm)
}
return defaultValue;
return defaultValue
}
} else {
warn(`inject() can only be used inside setup() or functional components.`);
warn(`inject() can only be used inside setup() or functional components.`)
}
}
59 changes: 32 additions & 27 deletions src/apis/lifecycle.ts
@@ -1,44 +1,49 @@
import { VueConstructor } from 'vue';
import { ComponentInstance } from '../component';
import { getCurrentVue, setCurrentVM, getCurrentVM } from '../runtimeContext';
import { currentVMInFn } from '../helper';
import { VueConstructor } from 'vue'
import { ComponentInstance } from '../component'
import { getCurrentVue, setCurrentVM, getCurrentVM } from '../runtimeContext'
import { currentVMInFn } from '../helper'

const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`;
const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`
function createLifeCycle(lifeCyclehook: string) {
return (callback: Function) => {
const vm = currentVMInFn(genName(lifeCyclehook));
const vm = currentVMInFn(genName(lifeCyclehook))
if (vm) {
injectHookOption(getCurrentVue(), vm, lifeCyclehook, callback);
injectHookOption(getCurrentVue(), vm, lifeCyclehook, callback)
}
};
}
}

function injectHookOption(Vue: VueConstructor, vm: ComponentInstance, hook: string, val: Function) {
const options = vm.$options as any;
const mergeFn = Vue.config.optionMergeStrategies[hook];
options[hook] = mergeFn(options[hook], wrapHookCall(vm, val));
function injectHookOption(
Vue: VueConstructor,
vm: ComponentInstance,
hook: string,
val: Function
) {
const options = vm.$options as any
const mergeFn = Vue.config.optionMergeStrategies[hook]
options[hook] = mergeFn(options[hook], wrapHookCall(vm, val))
}

function wrapHookCall(vm: ComponentInstance, fn: Function) {
return (...args: any) => {
let preVm = getCurrentVM();
setCurrentVM(vm);
let preVm = getCurrentVM()
setCurrentVM(vm)
try {
return fn(...args);
return fn(...args)
} finally {
setCurrentVM(preVm);
setCurrentVM(preVm)
}
};
}
}

// export const onCreated = createLifeCycle('created');
export const onBeforeMount = createLifeCycle('beforeMount');
export const onMounted = createLifeCycle('mounted');
export const onBeforeUpdate = createLifeCycle('beforeUpdate');
export const onUpdated = createLifeCycle('updated');
export const onBeforeUnmount = createLifeCycle('beforeDestroy');
export const onUnmounted = createLifeCycle('destroyed');
export const onErrorCaptured = createLifeCycle('errorCaptured');
export const onActivated = createLifeCycle('activated');
export const onDeactivated = createLifeCycle('deactivated');
export const onServerPrefetch = createLifeCycle('serverPrefetch');
export const onBeforeMount = createLifeCycle('beforeMount')
export const onMounted = createLifeCycle('mounted')
export const onBeforeUpdate = createLifeCycle('beforeUpdate')
export const onUpdated = createLifeCycle('updated')
export const onBeforeUnmount = createLifeCycle('beforeDestroy')
export const onUnmounted = createLifeCycle('destroyed')
export const onErrorCaptured = createLifeCycle('errorCaptured')
export const onActivated = createLifeCycle('activated')
export const onDeactivated = createLifeCycle('deactivated')
export const onServerPrefetch = createLifeCycle('serverPrefetch')
2 changes: 1 addition & 1 deletion src/apis/state.ts
Expand Up @@ -14,4 +14,4 @@ export {
toRaw,
shallowRef,
triggerRef,
} from '../reactivity';
} from '../reactivity'

0 comments on commit 233dafa

Please sign in to comment.