Skip to content

Commit

Permalink
Fix Provide/ProvideReactive compatibility (kaorun343#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguimbal committed Sep 24, 2019
1 parent 56d8db4 commit eedebf4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
16 changes: 8 additions & 8 deletions src/vue-property-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function Provide(key?: string | symbol) {
return createDecorator((componentOptions, k) => {
let provide: any = componentOptions.provide
if (typeof provide !== 'function' || !provide.managed) {
const original = componentOptions.provide
const original: any = componentOptions.provide
provide = componentOptions.provide = function(this: any) {
let rv = Object.create(
(typeof original === 'function' ? original.call(this) : original) ||
Expand All @@ -87,26 +87,26 @@ export function Provide(key?: string | symbol) {
export function ProvideReactive(key?: string | symbol) {
return createDecorator((componentOptions, k) => {
let provide: any = componentOptions.provide
if (typeof provide !== 'function' || !provide.managed) {
const original = componentOptions.provide
if (typeof provide !== 'function' || !provide.managedReactive) {
const original: any = componentOptions.provide
provide = componentOptions.provide = function(this: any) {
let rv = Object.create(
(typeof original === 'function' ? original.call(this) : original) ||
null,
)
rv[reactiveInjectKey] = {}
for (let i in provide.managed) {
rv[provide.managed[i]] = this[i] // Duplicates the behavior of `@Provide`
Object.defineProperty(rv[reactiveInjectKey], provide.managed[i], {
for (let i in provide.managedReactive) {
rv[provide.managedReactive[i]] = this[i] // Duplicates the behavior of `@Provide`
Object.defineProperty(rv[reactiveInjectKey], provide.managedReactive[i], {
enumerable: true,
get: () => this[i],
})
}
return rv
}
provide.managed = {}
provide.managedReactive = {}
}
provide.managed[k] = key || k
provide.managedReactive[k] = key || k
})
}

Expand Down
24 changes: 23 additions & 1 deletion tests/ProvideReactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Vue from 'vue'
import {
Component,
ProvideReactive,
InjectReactive
InjectReactive,
Provide
} from '../src/vue-property-decorator'

describe(ProvideReactive, () => {
Expand Down Expand Up @@ -39,6 +40,27 @@ describe(ProvideReactive, () => {
})
})


describe('is compatible with @Provide()', () => {
@Component
class ParentComponent extends Vue {
@Provide('first') first = 'whatever'
@ProvideReactive() one = 'one'
}
@Component
class ChildComponent extends Vue {
@InjectReactive() one!: string
}

const parent = new ParentComponent()
const component = new ChildComponent({ parent })

test('provides value', () => {
expect(component.one).toBe('one')
})
})


describe('when key is given', () => {
const key = 'KEY'
const value = 'VALUE'
Expand Down

0 comments on commit eedebf4

Please sign in to comment.