Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vuejs/composition-api
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.6.4
Choose a base ref
...
head repository: vuejs/composition-api
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.6.5
Choose a head ref
  • 7 commits
  • 33 files changed
  • 3 contributors

Commits on Jun 19, 2020

  1. fix: not unwrapping markRaw objects (#386)

    * fix: not unwrapping `markRaw` objects
    
    * chore: add falsy check
    pikax authored Jun 19, 2020
    Copy the full SHA
    575d100 View commit details
  2. fix: unwrap refs returned by data (#387)

    * fix: unwrap refs returned by `data`
    
    * chore: add a new test
    pikax authored Jun 19, 2020
    Copy the full SHA
    1f07075 View commit details
  3. Merge tag 'v0.6.4'

    antfu committed Jun 19, 2020
    Copy the full SHA
    5f72e26 View commit details
  4. Copy the full SHA
    233dafa View commit details
  5. fix(watchEffect): prevent recursive calls when using flush:sync (#389)

    * fix(watchEffect): prevent recursive calls when using `flush:sync`
    
    * chore: better comment
    pikax authored Jun 19, 2020
    Copy the full SHA
    f7f1e77 View commit details
  6. chore: fix release script

    antfu committed Jun 19, 2020
    Copy the full SHA
    e96f4ad View commit details
  7. chore: release v0.6.5

    antfu committed Jun 19, 2020
    Copy the full SHA
    2a2629e View commit details
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<a name="0.6.5"></a>
## [0.6.5](https://github.com/vuejs/composition-api/compare/v0.6.4...v0.6.5) (2020-06-19)


### Bug Fixes

* **watchEffect:** prevent recursive calls when using `flush:sync` ([#389](https://github.com/vuejs/composition-api/issues/389)) ([f7f1e77](https://github.com/vuejs/composition-api/commit/f7f1e77))
* not unwrapping `markRaw` objects ([#386](https://github.com/vuejs/composition-api/issues/386)) ([575d100](https://github.com/vuejs/composition-api/commit/575d100))
* unwrap refs returned by `data` ([#387](https://github.com/vuejs/composition-api/issues/387)) ([1f07075](https://github.com/vuejs/composition-api/commit/1f07075))



<a name="0.6.4"></a>
## [0.6.4](https://github.com/vuejs/composition-api/compare/v0.6.3...v0.6.4) (2020-06-16)

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue/composition-api",
"version": "0.6.4",
"version": "0.6.5",
"description": "Provide logic composition capabilities for Vue.",
"keywords": [
"vue",
@@ -27,15 +27,15 @@
"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",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"prepublish": "yarn test",
"postpublish": "yarn release-gh",
"version": "yarn changelog && git add CHANGELOG.md",
"release": "yarn version && git push --tags && yarn publish --non-interactive",
"release": "yarn version && git push --follow-tags && yarn publish --non-interactive",
"release-gh": "conventional-github-releaser -p angular"
},
"bugs": {
40 changes: 20 additions & 20 deletions src/apis/computed.ts
Original file line number Diff line number Diff line change
@@ -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(), {
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -14,4 +14,4 @@ export {
toRaw,
shallowRef,
triggerRef,
} from '../reactivity';
} from '../reactivity'
Loading