Skip to content
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

add mobx plugin scoped bundle and a common bundle builder. #310

Merged
merged 2 commits into from
Feb 15, 2023
Merged
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
3 changes: 2 additions & 1 deletion packages/ast/src/state/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './react-query';
export * from './react-query';
export * from './mobx';
28 changes: 28 additions & 0 deletions packages/ast/src/state/mobx/__snapshots__/mobx.scoped.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`builds stores. 1`] = `
"import * as _ProtoCosmosBankV1beta1A from "./proto/cosmos/bank/v1beta1/a.lcd";
import * as _ProtoCosmosBankV1beta1B from "./proto/cosmos/bank/v1beta1/b.lcd";
import * as _ProtoCosmosBankV1beta1C from "./proto/cosmos/bank/v1beta1/c.lcd";
export const createRpcStores = ({
rpc
}: {
rpc: ProtobufRpcClient | undefined;
}) => {
return {
cosmos: {
bank: {
v1beta1: _ProtoCosmosBankV1beta1A.createRpcStores(rpc)
},
gov: {
v1beta1: _ProtoCosmosBankV1beta1B.createRpcStores(rpc)
}
},
osmosis: {
gamm: {
v1beta1: _ProtoCosmosBankV1beta1C.createRpcStores(rpc)
}
}
};
};"
`;
1 change: 1 addition & 0 deletions packages/ast/src/state/mobx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './mobx';
33 changes: 33 additions & 0 deletions packages/ast/src/state/mobx/mobx.scoped.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
expectCode,
getGenericParseContext,
getTestProtoStore
} from '../../../test-utils';
import * as t from '@babel/types';
import { build } from './scoped-bundle';

const store = getTestProtoStore();
store.traverseAll();

it('builds stores.', async () => {
const context = getGenericParseContext();
expectCode(
t.program(
build(context, {
cosmos: {
bank: {
v1beta1: './proto/cosmos/bank/v1beta1/a.lcd'
},
gov: {
v1beta1: './proto/cosmos/bank/v1beta1/b.lcd'
}
},
osmosis: {
gamm: {
v1beta1: './proto/cosmos/bank/v1beta1/c.lcd'
}
}
}) as t.Statement[]
)
);
});
23 changes: 23 additions & 0 deletions packages/ast/src/state/mobx/mobx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as t from '@babel/types';
import { ProtoService, ProtoServiceMethod } from '@osmonauts/types';
import { getNestedProto, isRefIncluded } from '@osmonauts/proto-parser';
import { GenericParseContext } from '../../encoding';
import { objectPattern } from '../../utils';
import { variableSlug } from '@osmonauts/utils';

/**
* Entry for building stores.
*/
export const build = (
context: GenericParseContext,
name: string,
svc: ProtoServiceMethod
) => {
const isIncluding =
context.pluginValue('mobx.enabled') &&
isRefIncluded(context.ref, context.pluginValue('mobx.include'));

if (isIncluding) {

}
};
14 changes: 14 additions & 0 deletions packages/ast/src/state/mobx/scoped-bundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GenericParseContext } from '../../encoding';
import { buildExportCreators } from '../../utils';

const CREATOR_NAME = 'createRpcStores';

export const build = (context: GenericParseContext, obj: object) => {
return buildExportCreators(
context,
obj,
CREATOR_NAME,
['ProtobufRpcClient'],
CREATOR_NAME
);
};
247 changes: 71 additions & 176 deletions packages/ast/src/state/react-query/scoped-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,30 @@ import * as t from '@babel/types';
import { GenericParseContext } from '../../encoding';
import { objectPattern } from '../../utils';
import { variableSlug } from '@osmonauts/utils';
import { buildExportCreators } from '../../utils';

export const rpcHookFuncArguments = (): t.ObjectPattern[] => {
return [
objectPattern(
[
t.objectProperty(
t.identifier('rpc'),
t.identifier('rpc'),
false,
true
)
],
t.tsTypeAnnotation(
t.tsTypeLiteral(
[
t.tsPropertySignature(
t.identifier('rpc'),
t.tsTypeAnnotation(
t.tsTypeReference(
t.identifier('Rpc')
)
)
)
]
)
)
)
];
return [
objectPattern(
[t.objectProperty(t.identifier('rpc'), t.identifier('rpc'), false, true)],
t.tsTypeAnnotation(
t.tsTypeLiteral([
t.tsPropertySignature(
t.identifier('rpc'),
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Rpc')))
)
])
)
)
];
};

export const rpcHookClassArguments = (): t.ObjectExpression[] => {
return [
t.objectExpression(
[
t.objectProperty(
t.identifier('rpc'),
t.identifier('rpc'),
false,
true
)
]
)
];
return [
t.objectExpression([
t.objectProperty(t.identifier('rpc'), t.identifier('rpc'), false, true)
])
];
};

/**
Expand All @@ -57,47 +38,37 @@ export const rpcHookClassArguments = (): t.ObjectExpression[] => {
* @returns {ParseResult} created AST
*/
export const rpcHookNewTmRequire = (
imports: HookImport[],
path: string,
methodName: string
imports: HookImport[],
path: string,
methodName: string
) => {
imports.push({
as: variableSlug(path),
path
});

return t.callExpression(
t.memberExpression(
t.identifier(variableSlug(path)),
t.identifier(methodName)
),
[t.identifier('rpc')]
);
};

imports.push({
as: variableSlug(path),
path
});

return t.callExpression(
t.memberExpression(
t.identifier(variableSlug(path)),
t.identifier(methodName)
),
[
t.identifier('rpc')
]
)

}
export const rpcHookRecursiveObjectProps = (names: string[], leaf?: any) => {
const [name, ...rest] = names;

export const rpcHookRecursiveObjectProps = (
names: string[],
leaf?: any
) => {
const [name, ...rest] = names;
let baseComponent;
if (names.length === 1) {
baseComponent = leaf ? leaf : t.identifier(name);
} else {
baseComponent = rpcHookRecursiveObjectProps(rest, leaf);
}

let baseComponent;
if (names.length === 1) {
baseComponent = leaf ? leaf : t.identifier(name)
} else {
baseComponent = rpcHookRecursiveObjectProps(rest, leaf)
}

return t.objectExpression([
t.objectProperty(
t.identifier(name),
baseComponent
)
])
return t.objectExpression([
t.objectProperty(t.identifier(name), baseComponent)
]);
};

/**
Expand All @@ -110,30 +81,31 @@ export const rpcHookRecursiveObjectProps = (
* @returns {ParseResult} created AST
*/
export const rpcHookTmNestedImportObject = (
imports: HookImport[],
obj: object,
methodName: string
imports: HookImport[],
obj: object,
methodName: string
) => {

//if obj is a path, end recursion and get the mapping.
if (typeof obj === 'string') {
return rpcHookNewTmRequire(imports, obj, methodName);
}

const keys = Object.keys(obj);

// get hooks for keys of the obj.
return t.objectExpression(keys.map(name => {
return t.objectProperty(
t.identifier(name),
rpcHookTmNestedImportObject(imports, obj[name], methodName)
)
}))
//if obj is a path, end recursion and get the mapping.
if (typeof obj === 'string') {
return rpcHookNewTmRequire(imports, obj, methodName);
}

const keys = Object.keys(obj);

// get hooks for keys of the obj.
return t.objectExpression(
keys.map((name) => {
return t.objectProperty(
t.identifier(name),
rpcHookTmNestedImportObject(imports, obj[name], methodName)
);
})
);
};

interface HookImport {
as: string;
path: string;
as: string;
path: string;
}

/**
Expand All @@ -147,86 +119,9 @@ interface HookImport {
* @returns {ParseResult} created AST
*/
export const createScopedRpcHookFactory = (
context: GenericParseContext,
obj: object,
identifier: string
context: GenericParseContext,
obj: object,
identifier: string
) => {

// add imports
context.addUtil('ProtobufRpcClient');

const hookImports: HookImport[] = [];

const ast = t.exportNamedDeclaration(
t.variableDeclaration(
'const',
[
t.variableDeclarator(
// createRPCQueryHooks
t.identifier(identifier),
t.arrowFunctionExpression(
[
objectPattern([
t.objectProperty(
t.identifier('rpc'),
t.identifier('rpc'),
false,
true
)
], t.tsTypeAnnotation(
t.tsTypeLiteral([
t.tsPropertySignature(
t.identifier('rpc'),
t.tsTypeAnnotation(
t.tsUnionType([
t.tsTypeReference(
t.identifier('ProtobufRpcClient')
),
t.tsUndefinedKeyword()
])
)
)
])
))
],
t.blockStatement([

t.returnStatement(
rpcHookTmNestedImportObject(
hookImports,
obj,
'createRpcQueryHooks'
)
)

]),
false
)
)
]
)
);

// generate imports for packages.
const imports = hookImports.map(hookport => {
return {
"type": "ImportDeclaration",
"importKind": "value",
"specifiers": [
{
"type": "ImportNamespaceSpecifier",
"local": {
"type": "Identifier",
"name": hookport.as
}
}
],
"source": {
"type": "StringLiteral",
"value": hookport.path
}
};
});

return [...imports, ast];
return buildExportCreators(context, obj, identifier, ['ProtobufRpcClient']);
};
Loading