Skip to content

Fix: make __ob__ unenumerable and enable createComponent() accept tuple props. #147

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 10 additions & 4 deletions src/component/componentProps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Data } from './component';

export type ComponentPropsOptions<P = Data> = {
[K in keyof P]: Prop<P[K], true | false> | null;
};
export type ComponentPropsOptions<P = Data> =
| {
[K in keyof P]: Prop<P[K], true | false> | null;
}
| readonly string[];

type Prop<T, Required extends boolean> = PropOptions<T, Required> | PropType<T>;

Expand All @@ -15,6 +17,8 @@ export interface PropOptions<T = any, Required extends boolean = false> {

export type PropType<T> = PropConstructor<T> | PropConstructor<T>[];

type PropKeys<O extends readonly string[]> = O[number];

type PropConstructor<T> = { new (...args: any[]): T & object } | { (): T };

type RequiredKeys<T, MakeDefaultRequired> = {
Expand All @@ -39,7 +43,9 @@ type InferPropType<T> = T extends null
: T;

// prettier-ignore
export type ExtractPropTypes<O, MakeDefaultRequired extends boolean = true> = {
export type ExtractPropTypes<O, MakeDefaultRequired extends boolean = true> = O extends readonly string[] ? {
[K in PropKeys<O>]: any;
} : {
readonly [K in RequiredKeys<O, MakeDefaultRequired>]: InferPropType<O[K]>;
} & {
readonly [K in OptionalKeys<O, MakeDefaultRequired>]?: InferPropType<O[K]>;
Expand Down
7 changes: 6 additions & 1 deletion src/reactivity/reactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ export function nonReactive<T = any>(obj: T): T {
}

// set the vue observable flag at obj
(obj as any).__ob__ = (observe({}) as any).__ob__;
Object.defineProperty(obj, '__ob__', {
value: (observe({}) as any).__ob__,
enumerable: false,
configurable: true,
writable: true,
});
// mark as nonReactive
def(obj, NonReactiveIdentifierKey, NonReactiveIdentifier);

Expand Down
20 changes: 19 additions & 1 deletion test/setup.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Vue = require('vue/dist/vue.common.js');
const { ref, computed, createElement: h } = require('../src');
const { ref, computed, createElement: h, createComponent } = require('../src');

describe('setup', () => {
beforeEach(() => {
Expand Down Expand Up @@ -276,6 +276,24 @@ describe('setup', () => {
.then(done);
});

it("should put a unenumerable '__ob__' for non-reactive object", () => {
const clone = obj => JSON.parse(JSON.stringify(obj));
const componentSetup = jest.fn(props => {
const internalOptions = clone(props.options);
return { internalOptions };
});
const ExternalComponent = {
props: ['options'],
setup: componentSetup,
};
new Vue({
components: { ExternalComponent },
setup: () => ({ options: {} }),
template: `<external-component ref="comp" :options="options"></external-component>`,
}).$mount();
expect(componentSetup).toReturn();
});

it('current vue should exist in nested setup call', () => {
const spy = jest.fn();
new Vue({
Expand Down
11 changes: 11 additions & 0 deletions test/types/createComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ describe('createComponent', () => {
expect.assertions(2);
});

it('should accept tuple props', () => {
const App = createComponent({
props: ['p1', 'p2'] as const,
setup(props) {
props.p1;
props.p2;
},
});
new Vue(App);
});

describe('compatible with vue router', () => {
it('RouteConfig.component', () => {
new Router({
Expand Down