Skip to content

Commit

Permalink
feat: upgrade to @vue/composition-api v0.6.1
Browse files Browse the repository at this point in the history
BREAKING CHANGE: There are a number of breaking changes in the latest upstream composition-api. See https://github.com/vuejs/composition-api/releases/tag/v0.6.0 for details.
  • Loading branch information
danielroe committed Jun 10, 2020
1 parent 0d11992 commit 3d0bdc6
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 163 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"pre-commit": "lint-staged"
},
"dependencies": {
"@vue/composition-api": "^0.5.0",
"@vue/composition-api": "^0.6.1",
"defu": "^2.0.4"
},
"devDependencies": {
Expand All @@ -71,7 +71,7 @@
"@types/jest": "^26.0.0",
"@typescript-eslint/eslint-plugin": "^3.2.0",
"@typescript-eslint/parser": "^3.2.0",
"@vue/composition-api": "^0.5.0",
"@vue/composition-api": "^0.6.1",
"babel-loader": "^8.1.0",
"codecov": "^3.7.0",
"eslint": "^7.2.0",
Expand Down
8 changes: 4 additions & 4 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isRef, onServerPrefetch } from '@vue/composition-api'
import type { Ref } from '@vue/composition-api'
import type { Ref, UnwrapRef } from '@vue/composition-api'

import { globalNuxt } from './globals'
import { ssrRef } from './ssr-ref'
Expand Down Expand Up @@ -51,14 +51,14 @@ export const useAsync = <T>(
if (p instanceof Promise) {
if (process.server) {
onServerPrefetch(async () => {
_ref.value = await p
_ref.value = (await p) as UnwrapRef<T>
})
} else {
// eslint-disable-next-line
p.then(res => (_ref.value = res))
p.then(res => (_ref.value = res as UnwrapRef<T>))
}
} else {
_ref.value = p
_ref.value = p as UnwrapRef<T>
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ export { useStatic } from './static'

export type {
ComponentRenderProxy,
ComputedRef,
FlushMode,
InjectionKey,
PropOptions,
PropType,
Ref,
SetupContext,
UnwrapRef,
VueWatcher,
WatchCallback,
WatchEffect,
WatchOptions,
WatchOptionsBase,
WatchSource,
WatchStopHandle,
WritableComputedRef,
} from '@vue/composition-api'

export {
Expand All @@ -27,7 +37,9 @@ export {
createElement,
getCurrentInstance,
inject,
isReactive,
isRef,
markRaw,
onActivated,
onBeforeMount,
onBeforeUnmount,
Expand All @@ -42,7 +54,13 @@ export {
reactive,
ref,
set,
shallowReactive,
shallowRef,
toRaw,
toRef,
toRefs,
triggerRef,
unref,
watch,
watchEffect,
} from '@vue/composition-api'
3 changes: 2 additions & 1 deletion src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export const useMeta = <T extends MetaInfo>(init?: T) => {

const refs = toRefs(_head) as ToRefs<ReturnType<typeof createEmptyMeta> & T>

if (process.client) watch(Object.values(refs), vm.$meta().refresh)
if (process.client)
watch(Object.values(refs), vm.$meta().refresh, { immediate: true })

return refs
}
19 changes: 7 additions & 12 deletions src/ssr-ref.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, computed } from '@vue/composition-api'
import type { Ref } from '@vue/composition-api'
import { ref, computed, shallowRef } from '@vue/composition-api'
import type { Ref, UnwrapRef } from '@vue/composition-api'

import { globalContext, globalNuxt } from './globals'

Expand Down Expand Up @@ -40,7 +40,10 @@ const sanitise = (val: unknown) =>
const val2 = ssrRef(myExpensiveSetterFunction)
```
*/
export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
export const ssrRef = <T>(
value: T | (() => T),
key?: string
): Ref<UnwrapRef<T>> => {
if (!key) {
throw new Error(
"You must provide a key. You can have it generated automatically by adding 'nuxt-composition-api/babel' to your Babel plugins."
Expand Down Expand Up @@ -85,15 +88,7 @@ export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
},
})

return proxy as Ref<T>
}

// TODO: remove when https://github.com/vuejs/composition-api/pull/311 is merged
function shallowRef<T>(value: T): Ref<T> {
return computed({
get: () => value,
set: v => (value = v),
})
return proxy
}

/**
Expand Down
57 changes: 32 additions & 25 deletions src/static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ssrRef } from './ssr-ref'
import { Ref, onServerPrefetch, watch, computed } from '@vue/composition-api'
import { onServerPrefetch, watch, computed, ref } from '@vue/composition-api'
import type { Ref, UnwrapRef } from '@vue/composition-api'

const staticPath = '<%= options.staticPath %>'
const staticCache: Record<string, any> = {}
Expand Down Expand Up @@ -67,7 +68,7 @@ async function writeFile(key: string) {
*/
export const useStatic = <T>(
factory: (param: string, key: string) => Promise<T>,
param: Ref<string> = { value: '' },
param: Ref<string> = ref(''),
keyBase: string
): Ref<T | null> => {
const key = computed(() => `${keyBase}-${param.value}`)
Expand All @@ -79,40 +80,46 @@ export const useStatic = <T>(
const onFailure = () =>
factory(param.value, key.value).then(r => {
staticCache[key.value] = r
result.value = r
result.value = r as UnwrapRef<T>
return
})
watch(key, key => {
if (key in staticCache) {
result.value = staticCache[key]
return
watch(
key,
key => {
if (key in staticCache) {
result.value = staticCache[key]
return
}
/* eslint-disable promise/always-return */
if (!process.static) onFailure()
else
fetch(`<%= options.publicPath %>${key}.json`)
.then(response => {
if (!response.ok) throw new Error('Response invalid.')
return response.json()
})
.then(json => {
staticCache[key] = json
result.value = json
})
.catch(onFailure)
/* eslint-enable */
},
{
immediate: true,
}
/* eslint-disable promise/always-return */
if (!process.static) onFailure()
else
fetch(`<%= options.publicPath %>${key}.json`)
.then(response => {
if (!response.ok) throw new Error('Response invalid.')
return response.json()
})
.then(json => {
staticCache[key] = json
result.value = json
})
.catch(onFailure)
/* eslint-enable */
})
)
} else {
if (key.value in staticCache) {
result.value = staticCache[key.value]
return result
return result as Ref<T | null>
}
onServerPrefetch(async () => {
result.value = await factory(param.value, key.value)
result.value = (await factory(param.value, key.value)) as UnwrapRef<T>
staticCache[key.value] = result.value
writeFile(key.value)
})
}

return result
return result as Ref<T | null>
}
10 changes: 7 additions & 3 deletions test/fixture/pages/context/_slug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ export default defineComponent({
setup() {
const { route, query, params } = useContext()
const called = ref(0)
watch(route, () => {
called.value++
})
watch(
route,
() => {
called.value++
},
{ immediate: true }
)
return { route, query, params, called }
},
Expand Down
Loading

1 comment on commit 3d0bdc6

@vercel
Copy link

@vercel vercel bot commented on 3d0bdc6 Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.