Skip to content

feat: adding missing reactivity api from vue-next #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8297872
fix(watch): component should runCleaup() on unmount
LinusBorg Mar 30, 2020
2f3ec44
feat: add unref API to match API with vue3
pikax Apr 19, 2020
2957417
types: making ComputedRef types inline with vue3 and provide Computed…
pikax Apr 19, 2020
b697dd1
feat: bring API to vue-3 compatible
pikax Apr 19, 2020
1e1424c
fix rollup build
pikax Apr 19, 2020
e604038
ref tests, add some sort of effect api
pikax Apr 19, 2020
296ef63
Merge branch 'feat/unref_api' into feat/vue3_API
pikax Apr 19, 2020
0c4cbe0
Merge branch 'types/computedRef' into feat/vue3_API
pikax Apr 19, 2020
0ac2500
add unref test
pikax Apr 19, 2020
e4c9d62
more tests
pikax Apr 19, 2020
6f58691
fix computed.spec.ts
pikax Apr 19, 2020
d6af20e
watch/watchEffect making the apis inline with vue3
pikax Apr 20, 2020
db65ae0
Merge branch 'master' into feat/vue3_API
pikax Apr 24, 2020
1c3ce8b
fix state.spec.js tests
pikax Apr 24, 2020
a660cf7
Merge remote-tracking branch 'upstream/fix-293-watch_cleanup-on-unmou…
pikax Apr 24, 2020
e4265b2
tests
pikax Apr 24, 2020
2a9e674
apiWatch.spec.ts passing
pikax Apr 24, 2020
d7ec18b
fix computed.spec.ts
pikax Apr 24, 2020
74dc967
removing reactivity collection tests :(
pikax Apr 24, 2020
daa1955
implementing `shallowReactive`
pikax Apr 24, 2020
e4a3f1d
removing bad import :/
pikax Apr 24, 2020
fe08cf8
rename nonReactive to markRaw and add toRaw
pikax Apr 24, 2020
de0b7c6
removing readonly.spec.ts
pikax Apr 24, 2020
027200b
add shallowRef
pikax Apr 24, 2020
8e06d81
clean stuff
pikax Apr 24, 2020
0fd591a
Merge branch 'master' into feat/vue3_API
pikax Apr 24, 2020
58c3429
cleanup
pikax Apr 24, 2020
b125885
computed Ref
pikax Apr 24, 2020
f907214
Add toRefs type
pikax Apr 24, 2020
f0e55a8
inline ref, reactive, computed, watch api to v3.0.0-beta.8
pikax May 4, 2020
618adff
code-review fixes
pikax May 16, 2020
9ee6013
feat(lifeCycleHook): `getCurrentInstance` inside lifecycle hook
pikax May 30, 2020
93e0c82
feat: warn when calling lifecycleHook outside setup() instead of thro…
pikax May 30, 2020
afab8ec
fix(test): capture warning properly
antfu May 31, 2020
d2eca4e
fix: apiLifecycleTests
pikax Jun 2, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"homepage": "https://github.com/vuejs/composition-api#readme",
"devDependencies": {
"@types/jest": "^24.0.13",
"@types/jest": "^25.2.1",
"@types/node": "^12.0.2",
"cross-env": "^5.2.0",
"husky": "^2.7.0",
Expand Down Expand Up @@ -76,6 +76,9 @@
},
"jest": {
"verbose": true,
"globals": {
"__DEV__": true
},
"setupFiles": [
"<rootDir>/test/setupTest.js"
],
Expand Down
10 changes: 9 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ function genConfig({ outFile, format, mode }) {
typescript: require('typescript'),
}),
resolve(),
replace({ 'process.env.NODE_ENV': JSON.stringify(isProd ? 'production' : 'development') }),
replace({
'process.env.NODE_ENV': JSON.stringify(isProd ? 'production' : 'development'),
__DEV__:
format === 'es'
? // preserve to be handled by bundlers
`(__DEV__)`
: // hard coded dev/prod builds
!isProd,
}),
isProd && terser(),
].filter(Boolean),
};
Expand Down
14 changes: 10 additions & 4 deletions src/apis/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ interface Option<T> {
set: (value: T) => void;
}

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

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

// read-only
export function computed<T>(getter: Option<T>['get']): Readonly<Ref<Readonly<T>>>;
export function computed<T>(getter: Option<T>['get']): ComputedRef<T>;
// writable
export function computed<T>(options: Option<T>): Ref<Readonly<T>>;
export function computed<T>(options: Option<T>): WritableComputedRef<T>;
// implement
export function computed<T>(
options: Option<T>['get'] | Option<T>
): Readonly<Ref<Readonly<T>>> | Ref<Readonly<T>> {
): ComputedRef<T> | WritableComputedRef<T> {
const vm = getCurrentVM();
let get: Option<T>['get'], set: Option<T>['set'] | undefined;
if (typeof options === 'function') {
Expand All @@ -37,7 +43,7 @@ export function computed<T>(
return createRef<T>({
get: () => (computedHost as any).$$state,
set: (v: T) => {
if (process.env.NODE_ENV !== 'production' && !set) {
if (__DEV__ && !set) {
warn('Computed property was assigned to but it has no setter.', vm!);
return;
}
Expand Down
31 changes: 19 additions & 12 deletions src/apis/inject.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ComponentInstance } from '../component';
import { ensureCurrentVMInFn } from '../helper';
import { currentVMInFn } from '../helper';
import { hasOwn, warn } from '../utils';
import { getCurrentVM } from '../runtimeContext';

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

function resolveInject(provideKey: InjectionKey<any>, vm: ComponentInstance): any {
function resolveInject(provideKey: InjectionKey<any> | string, vm: ComponentInstance): any {
let source = vm;
while (source) {
// @ts-ignore
Expand All @@ -20,7 +21,9 @@ function resolveInject(provideKey: InjectionKey<any>, vm: ComponentInstance): an
}

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

if (!vm._provided) {
const provideCache = {};
Object.defineProperty(vm, '_provided', {
Expand All @@ -34,19 +37,23 @@ export function provide<T>(key: InjectionKey<T> | string, value: T): void {

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

const vm = ensureCurrentVMInFn('inject');
const val = resolveInject(key as InjectionKey<T>, vm);
if (val !== NOT_FOUND) {
return val;
} else {
if (defaultValue === undefined && process.env.NODE_ENV !== 'production') {
warn(`Injection "${String(key)}" not found`, vm);
const vm = getCurrentVM();
if (vm) {
const val = resolveInject(key, vm);
if (val !== NOT_FOUND) {
return val;
} else {
if (defaultValue === undefined && process.env.NODE_ENV !== 'production') {
warn(`Injection "${String(key)}" not found`, vm);
}
return defaultValue;
}
return defaultValue;
} else {
warn(`inject() can only be used inside setup() or functional components.`);
}
}
24 changes: 19 additions & 5 deletions src/apis/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import { VueConstructor } from 'vue';
import { ComponentInstance } from '../component';
import { getCurrentVue } from '../runtimeContext';
import { ensureCurrentVMInFn } from '../helper';
import { getCurrentVue, setCurrentVM, getCurrentVM } from '../runtimeContext';
import { currentVMInFn } from '../helper';

const genName = (name: string) => `on${name[0].toUpperCase() + name.slice(1)}`;
function createLifeCycle(lifeCyclehook: string) {
return (callback: Function) => {
const vm = ensureCurrentVMInFn(genName(lifeCyclehook));
injectHookOption(getCurrentVue(), vm, lifeCyclehook, callback);
const vm = currentVMInFn(genName(lifeCyclehook));
if (vm) {
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], val);
options[hook] = mergeFn(options[hook], wrapHookCall(vm, val));
}

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

// export const onCreated = createLifeCycle('created');
Expand Down
18 changes: 17 additions & 1 deletion src/apis/state.ts
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export { reactive, ref, Ref, isRef, toRefs, set } from '../reactivity';
export {
reactive,
ref,
Ref,
isRef,
toRefs,
set,
toRef,
isReactive,
UnwrapRef,
markRaw,
unref,
shallowReactive,
toRaw,
shallowRef,
triggerRef,
} from '../reactivity';
Loading