forked from hyperweb-io/telescope
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update definition files of ast package
add helpers for mobx
- Loading branch information
Showing
13 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './react-query'; | ||
export * from './mobx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './mobx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as t from '@babel/types'; | ||
import { ProtoService } from '@osmonauts/types'; | ||
import { GenericParseContext } from '../../encoding'; | ||
/** | ||
* Entry for building stores. | ||
* @param {Object=} context - context of generating the file | ||
* @param {Object=} service - method details | ||
*/ | ||
export declare const createMobxQueryStores: (context: GenericParseContext, service: ProtoService) => void; | ||
/** | ||
* Create an AST to generate creating store functions. | ||
* eg: | ||
* export const createRpcStores = (rpc: ProtobufRpcClient | undefined) => { | ||
* const queryService = getQueryService(rpc); | ||
* | ||
* class BalanceStoreInherited extends QueryStore< | ||
* QueryBalanceRequest, | ||
* QueryBalanceResponse | ||
* > { | ||
* constructor() { | ||
* super(queryService?.balance); | ||
* makeObservable(this, { | ||
* state: override, | ||
* request: override, | ||
* response: override, | ||
* isLoading: override, | ||
* isSuccess: override, | ||
* refetch: override, | ||
* getData: override | ||
* }); | ||
* } | ||
* | ||
* balance(request: QueryBalanceRequest): MobxResponse<QueryBalanceResponse> { | ||
* return this.getData(request); | ||
* } | ||
* } | ||
* | ||
* return { | ||
* BalanceStoreInherited | ||
* }; | ||
* }; | ||
* @param {Object=} context - context of generating the file | ||
* @param {Object=} service - method details | ||
* @returns {ParseResult} created AST | ||
*/ | ||
export declare const buildRpcStores: (context: GenericParseContext, service: ProtoService) => t.ExportNamedDeclaration; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { GenericParseContext } from '../../encoding'; | ||
export declare const createMobxQueryFactory: (context: GenericParseContext, obj: object) => (import("@babel/types").ExportNamedDeclaration | { | ||
type: string; | ||
importKind: string; | ||
specifiers: { | ||
type: string; | ||
local: { | ||
type: string; | ||
name: string; | ||
}; | ||
}[]; | ||
source: { | ||
type: string; | ||
value: string; | ||
}; | ||
})[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './babel'; | ||
export * from './utils'; | ||
export * from './scoped-bundle-builder'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as t from '@babel/types'; | ||
import { GenericParseContext } from '../encoding'; | ||
/** | ||
* Create an AST for a certain key and method. | ||
* eg: __fixtures__/output1/hooks.ts | ||
* v1beta2: _AkashAuditV1beta2Queryrpc.createRpcQueryHooks(rpc) | ||
* @param {Object=} imports - imports array reference for generating imports. | ||
* @param {Object=} path - filename of a package. | ||
* @param {string} methodName - hook method name of packages | ||
* @returns {ParseResult} created AST | ||
*/ | ||
export declare const buildSingleCreator: (imports: HookImport[], path: string, methodName: string) => t.CallExpression; | ||
/** | ||
* Create an ASTs for method creators of packages recursively, and get imports of packages. | ||
* eg: __fixtures__/output1/hooks.ts | ||
* export const createRpcQueryHooks = ... | ||
* @param {Object=} imports - imports array reference for generating imports. | ||
* @param {Object=} obj - mapping of packages and rpc query filenames | ||
* @param {string} methodName - hook method name of packages | ||
* @returns {ParseResult} created AST | ||
*/ | ||
export declare const buildNestedCreator: (imports: HookImport[], obj: object, methodName: string) => any; | ||
interface HookImport { | ||
as: string; | ||
path: string; | ||
} | ||
/** | ||
* Create an ASTs for export creators. | ||
* Generating files like: | ||
* __fixtures__/output1/hooks.ts | ||
* @param {Object=} context - context of generating the file | ||
* @param {Object=} obj - mapping of packages and rpc query filenames | ||
* @param {string} identifier - name of function creating hooks. eg: createRpcQueryHooks | ||
* @param {string[]} utils - name of imported utils. | ||
* @param {string} methodName - name of a certain method that creates a store or hook. eg: createRpcQueryHooks | ||
* @returns {ParseResult} created AST | ||
*/ | ||
export declare const buildExportCreators: (context: GenericParseContext, obj: object, identifier: string, utils: string[], methodName?: string) => (t.ExportNamedDeclaration | { | ||
type: string; | ||
importKind: string; | ||
specifiers: { | ||
type: string; | ||
local: { | ||
type: string; | ||
name: string; | ||
}; | ||
}[]; | ||
source: { | ||
type: string; | ||
value: string; | ||
}; | ||
})[]; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './external'; | ||
export * from './internal'; | ||
export * from './react-query'; | ||
export * from './mobx'; | ||
export * from './grpc-gateway'; | ||
export * from './grpc-web'; | ||
export * from './grpc-web'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export const mobx = ` | ||
import { | ||
action, | ||
computed, | ||
makeObservable, | ||
observable, | ||
runInAction | ||
} from 'mobx'; | ||
import { QueryStatus } from '@tanstack/react-query'; | ||
interface MobxResponse<T> { | ||
data: T | undefined; | ||
isSuccess: boolean; | ||
isLoading: boolean; | ||
refetch: () => Promise<void>; | ||
} | ||
class QueryStore<Request, Response> { | ||
state?: QueryStatus; | ||
request?: Request; | ||
response?: Response; | ||
fetchFunc?: (request: Request) => Promise<Response>; | ||
constructor(fetchFunc?: (request: Request) => Promise<Response>) { | ||
this.fetchFunc = fetchFunc; | ||
makeObservable(this, { | ||
state: observable, | ||
request: observable.ref, | ||
response: observable.ref, | ||
isLoading: computed, | ||
isSuccess: computed, | ||
refetch: action.bound, | ||
getData: action.bound, | ||
}); | ||
} | ||
get isLoading() { | ||
return this.state === 'loading'; | ||
} | ||
get isSuccess() { | ||
return this.state === 'success'; | ||
} | ||
async refetch(): Promise<void> { | ||
runInAction(() => { | ||
this.response = void 0; | ||
this.state = 'loading'; | ||
}); | ||
try { | ||
if (!this.fetchFunc) | ||
throw new Error( | ||
'Query Service not initialized or request function not implemented' | ||
); | ||
if (!this.request) throw new Error('Request not provided'); | ||
const response = await this.fetchFunc(this.request); | ||
runInAction(() => { | ||
this.response = response; | ||
this.state = 'success'; | ||
}); | ||
console.log( | ||
'%cquery.rpc.Query.ts line:572 this.state', | ||
'color: #007acc;', | ||
this.state, | ||
this.response | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
runInAction(() => { | ||
this.state = 'error'; | ||
}); | ||
} | ||
} | ||
getData(request: Request): MobxResponse<Response> { | ||
runInAction(() => { | ||
this.request = request; | ||
}); | ||
return { | ||
data: this.response, | ||
isSuccess: this.isSuccess, | ||
isLoading: this.isLoading, | ||
refetch: this.refetch, | ||
}; | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters