-
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.
Merge pull request #1849 from demergent-labs/class_syntax_heartbeat
Class syntax heartbeat
- Loading branch information
Showing
26 changed files
with
4,169 additions
and
16 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
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,33 @@ | ||
export function heartbeat<T>( | ||
_target: object, | ||
propertyKey: string | symbol, | ||
descriptor: TypedPropertyDescriptor<T> | ||
): TypedPropertyDescriptor<T> | void { | ||
globalThis._azleCanisterMethods.heartbeat = { | ||
name: propertyKey as string | ||
}; | ||
|
||
globalThis._azleCanisterMethods.callbacks[propertyKey as string] = | ||
descriptor.value as any; | ||
|
||
return descriptor; | ||
} | ||
|
||
// TODO do we need this? | ||
// TODO it would be nice if QuickJS would just panic | ||
// TODO on all uncaught exceptions | ||
// function executeHeartbeat(callback: any) { | ||
// const result = callback(); | ||
|
||
// if ( | ||
// result !== undefined && | ||
// result !== null && | ||
// typeof result.then === 'function' | ||
// ) { | ||
// result.catch((error: any) => { | ||
// ic.trap(error.toString()); | ||
// }); | ||
// } | ||
|
||
// return; | ||
// } |
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,76 @@ | ||
import { v4 } from 'uuid'; // TODO is uuid experimental? | ||
|
||
import { IDL, Principal } from '../'; | ||
|
||
export async function call( | ||
canisterId: Principal | string, | ||
method: string, | ||
options?: { | ||
paramIdls?: IDL.Type[]; | ||
returnIdl?: IDL.Type; | ||
args?: any[]; | ||
payment?: bigint; | ||
} | ||
): Promise<any> { | ||
// TODO this should use a Result remember | ||
return new Promise((resolve, reject) => { | ||
if (globalThis._azleIc === undefined) { | ||
return undefined as any; | ||
} | ||
|
||
const promiseId = v4(); | ||
const globalResolveId = `_resolve_${promiseId}`; | ||
const globalRejectId = `_reject_${promiseId}`; | ||
|
||
const returnIdl = options?.returnIdl; | ||
|
||
// TODO perhaps we should be more robust | ||
// TODO for example, we can keep the time with these | ||
// TODO if they are over a certain amount old we can delete them | ||
globalThis._azleResolveIds[globalResolveId] = (result: ArrayBuffer) => { | ||
if (returnIdl === undefined) { | ||
resolve(undefined); | ||
} else { | ||
resolve(IDL.decode([returnIdl], result)[0]); | ||
} | ||
|
||
delete globalThis._azleResolveIds[globalResolveId]; | ||
delete globalThis._azleRejectIds[globalRejectId]; | ||
}; | ||
|
||
globalThis._azleRejectIds[globalRejectId] = (error: any) => { | ||
reject(error); | ||
|
||
delete globalThis._azleResolveIds[globalResolveId]; | ||
delete globalThis._azleRejectIds[globalRejectId]; | ||
}; | ||
|
||
const paramIdls = options?.paramIdls ?? []; | ||
const args = options?.args ?? []; | ||
const payment = options?.payment ?? 0n; | ||
|
||
const canisterIdPrincipal = | ||
typeof canisterId === 'string' | ||
? Principal.fromText(canisterId) | ||
: canisterId; | ||
const canisterIdBytes = canisterIdPrincipal.toUint8Array().buffer; | ||
const argsRawBuffer = new Uint8Array(IDL.encode(paramIdls, args)) | ||
.buffer; | ||
const paymentString = payment.toString(); | ||
|
||
// TODO consider finally, what if deletion goes wrong | ||
try { | ||
globalThis._azleIc.callRaw( | ||
promiseId, | ||
canisterIdBytes, | ||
method, | ||
argsRawBuffer, | ||
paymentString | ||
); | ||
} catch (error) { | ||
delete globalThis._azleResolveIds[globalResolveId]; | ||
delete globalThis._azleRejectIds[globalRejectId]; | ||
throw error; | ||
} | ||
}); | ||
} |
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 { call } from './call'; |
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
4 changes: 4 additions & 0 deletions
4
tests/end_to_end/candid_rpc/class_syntax/heartbeat/.gitignore
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 @@ | ||
.azle | ||
.dfx | ||
dfx_generated | ||
node_modules |
25 changes: 25 additions & 0 deletions
25
tests/end_to_end/candid_rpc/class_syntax/heartbeat/dfx.json
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,25 @@ | ||
{ | ||
"canisters": { | ||
"heartbeat_async": { | ||
"type": "azle", | ||
"main": "src/heartbeat_async/index.ts", | ||
"candid": "src/heartbeat_async/index.did", | ||
"candid_gen": "custom", | ||
"env": ["AZLE_TEST_FETCH"], | ||
"declarations": { | ||
"output": "test/dfx_generated/heartbeat_async", | ||
"node_compatibility": true | ||
} | ||
}, | ||
"heartbeat_sync": { | ||
"type": "azle", | ||
"main": "src/heartbeat_sync/index.ts", | ||
"candid": "src/heartbeat_sync/index.did", | ||
"candid_gen": "custom", | ||
"declarations": { | ||
"output": "test/dfx_generated/heartbeat_sync", | ||
"node_compatibility": true | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.