-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hook up query and service for new functional syntax
- Loading branch information
Showing
16 changed files
with
160 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { query, Service, text } from 'azle'; | ||
|
||
export default class extends Service { | ||
@query([], text) | ||
simpleQuery(): text { | ||
export default Service({ | ||
simpleQuery: query([], text, () => { | ||
return 'This is a query function'; | ||
} | ||
} | ||
}) | ||
}); |
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
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './reference'; |
Empty file.
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 './service'; |
Empty file.
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,55 @@ | ||
import { CanisterMethods } from '../../../compiler/utils/types'; | ||
import { CanisterMethodInfo, query } from '../../canister_methods/query'; | ||
|
||
// type CanisterMethodType = 'query'; | ||
// type CanisterMethods = (typeof query & { | ||
// canisterMethodType: CanisterMethodType; | ||
// })[]; | ||
|
||
type ServiceOptions = { | ||
[key: string]: CanisterMethodInfo; | ||
}; | ||
|
||
export function Service(serviceOptions: ServiceOptions): CanisterMethods { | ||
const callbacks = Object.entries(serviceOptions).reduce((acc, entry) => { | ||
const key = entry[0]; | ||
const value = entry[1]; | ||
|
||
return { | ||
...acc, | ||
[key]: value.callback | ||
}; | ||
}, {}); | ||
|
||
const queries = Object.entries(serviceOptions) | ||
.filter((entry) => { | ||
const key = entry[0]; | ||
const value = entry[1]; | ||
|
||
return value.type === 'query'; | ||
}) | ||
.map((entry) => { | ||
const key = entry[0]; | ||
const value = entry[1]; | ||
|
||
return { | ||
name: key | ||
}; | ||
}); | ||
|
||
// TODO loop through each key and simply grab the candid off | ||
// TODO grab the init/post_upgrade candid as well | ||
return { | ||
candid: `service: () -> { | ||
${Object.entries(serviceOptions) | ||
.map((entry) => { | ||
return `${entry[0]}: ${entry[1].candid}`; | ||
}) | ||
.join('')} | ||
} | ||
`, | ||
queries, | ||
updates: [], | ||
callbacks | ||
}; | ||
} |
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,49 @@ | ||
import { IDL } from '../../lib_new/index'; | ||
import { ic } from '../../lib_new/ic'; | ||
|
||
export * from './query'; | ||
|
||
export function executeMethod( | ||
paramCandid: any, | ||
returnCandid: any, | ||
args: any[], | ||
callback: any | ||
) { | ||
const decoded = IDL.decode(paramCandid[0] as any, args[0]); | ||
|
||
const result = callback(decoded); | ||
|
||
if ( | ||
result !== undefined && | ||
result !== null && | ||
typeof result.then === 'function' | ||
) { | ||
result | ||
.then((result: any) => { | ||
// TODO this won't be accurate because we have most likely had | ||
// TODO cross-canister calls | ||
console.log(`final instructions: ${ic.instructionCounter()}`); | ||
|
||
// if (!manual) { | ||
const encodeReadyResult = result === undefined ? [] : [result]; | ||
const encoded = IDL.encode( | ||
returnCandid[0] as any, | ||
encodeReadyResult | ||
); | ||
ic.replyRaw(new Uint8Array(encoded)); | ||
// } | ||
}) | ||
.catch((error: any) => { | ||
ic.trap(error.toString()); | ||
}); | ||
} else { | ||
const encodeReadyResult = result === undefined ? [] : [result]; | ||
|
||
// if (!manual) { | ||
const encoded = IDL.encode(returnCandid[0] as any, encodeReadyResult); | ||
ic.replyRaw(new Uint8Array(encoded)); | ||
// } | ||
|
||
console.log(`final instructions: ${ic.instructionCounter()}`); | ||
} | ||
} |
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,31 @@ | ||
import { | ||
handleRecursiveParams, | ||
handleRecursiveReturn | ||
} from '../../lib_new/method_decorators'; | ||
import { IDL } from '../../lib_new/index'; | ||
import { ic } from '../../lib_new/ic'; | ||
import { executeMethod } from '.'; | ||
|
||
export type CanisterMethodInfo = { | ||
type: 'query'; | ||
callback: (...args: any) => any; | ||
candid: string; | ||
}; | ||
|
||
// TODO execute the candid stuff and just store it on the function itself? | ||
export function query( | ||
paramsIdls: any, | ||
returnIdl: any, | ||
callback: any | ||
): CanisterMethodInfo { | ||
const paramCandid = handleRecursiveParams(paramsIdls); | ||
const returnCandid = handleRecursiveReturn(returnIdl, paramCandid[2]); | ||
|
||
return { | ||
type: 'query', | ||
callback: (...args) => { | ||
executeMethod(paramCandid, returnCandid, args, callback); | ||
}, | ||
candid: `(${paramCandid[1].join(', ')}) -> (${returnCandid[1]}) query;` | ||
}; | ||
} |
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 function update() {} |
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,4 @@ | ||
export * from './canister_methods'; | ||
export * from './candid'; | ||
export * from '../lib_new/primitives'; | ||
export * from '../lib_new/ic'; |
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