Skip to content

Commit

Permalink
heartbeat refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Sep 25, 2023
1 parent 1587ba4 commit 830beb4
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# "examples/complex_types",
# "examples/func_types",
# "examples/generics",
# "examples/heartbeat",
# "examples/ic_api",
# "examples/imports",
# "examples/init",
Expand Down Expand Up @@ -114,6 +113,7 @@ jobs:
"examples/date",
"examples/ethereum_json_rpc",
"examples/guard_functions",
"examples/heartbeat",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
6 changes: 4 additions & 2 deletions examples/heartbeat/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/heartbeat_async/index.ts",
"candid": "src/heartbeat_async/index.did",
"wasm": ".azle/heartbeat_async/heartbeat_async.wasm.gz",
"wasm": ".azle/heartbeat_async/heartbeat_async.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/heartbeat_async",
"node_compatibility": true
Expand All @@ -18,7 +19,8 @@
"root": "src",
"ts": "src/heartbeat_sync/index.ts",
"candid": "src/heartbeat_sync/index.did",
"wasm": ".azle/heartbeat_sync/heartbeat_sync.wasm.gz",
"wasm": ".azle/heartbeat_sync/heartbeat_sync.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/heartbeat_sync",
"node_compatibility": true
Expand Down
238 changes: 174 additions & 64 deletions examples/heartbeat/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions examples/heartbeat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"azle": "0.17.1"
},
"devDependencies": {
"@dfinity/agent": "0.11.1",
"ts-node": "10.7.0",
"typescript": "4.6.3"
"@dfinity/agent": "^0.19.2",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
21 changes: 9 additions & 12 deletions examples/heartbeat/src/heartbeat_async/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { blob, ic, heartbeat, query, Service } from 'azle';
import { managementCanister } from 'azle/canisters/management';

export default class extends Service {
initialized: blob = Uint8Array.from([]);
let initialized: blob = Uint8Array.from([]);

@heartbeat
async heartbeat() {
export default Service({
heartbeat: heartbeat(async () => {
const randomness = await ic.call(managementCanister.raw_rand);

this.initialized = randomness;
initialized = randomness;
console.log('heartbeat initialized', randomness.length);
}

@query([], blob)
getInitialized(): blob {
return this.initialized;
}
}
}),
getInitialized: query([], blob, () => {
return initialized;
})
});
23 changes: 10 additions & 13 deletions examples/heartbeat/src/heartbeat_sync/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { bool, heartbeat, query, Service } from 'azle';

export default class extends Service {
initialized = false;
let initialized = false;

@heartbeat
heartbeat() {
this.initialized = true;
console.log('heartbeat initialized', this.initialized);
}

@query([], bool)
getInitialized(): bool {
return this.initialized;
}
}
export default Service({
heartbeat: heartbeat(() => {
initialized = true;
console.log('heartbeat initialized', initialized);
}),
getInitialized: query([], bool, () => {
return initialized;
})
});
6 changes: 3 additions & 3 deletions examples/heartbeat/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createActor as create_actor_heartbeat_async } from './dfx_generated/hea
import { createActor as create_actor_heartbeat_sync } from './dfx_generated/heartbeat_sync';
import { getTests } from './tests';

const heartbeat_async_canister = create_actor_heartbeat_async(
const heartbeatAsyncCanister = create_actor_heartbeat_async(
getCanisterId('heartbeat_async'),
{
agentOptions: {
Expand All @@ -12,7 +12,7 @@ const heartbeat_async_canister = create_actor_heartbeat_async(
}
);

const heartbeat_sync_canister = create_actor_heartbeat_sync(
const heartbeatSyncCanister = create_actor_heartbeat_sync(
getCanisterId('heartbeat_sync'),
{
agentOptions: {
Expand All @@ -21,4 +21,4 @@ const heartbeat_sync_canister = create_actor_heartbeat_sync(
}
);

runTests(getTests(heartbeat_async_canister, heartbeat_sync_canister));
runTests(getTests(heartbeatAsyncCanister, heartbeatSyncCanister));
11 changes: 11 additions & 0 deletions src/lib_functional/candid/reference/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export function Service<T extends ServiceOptions>(
name: initOption[0]
};

const heartbeatOption = Object.entries(serviceOptions).find(
([key, value]) => value.mode === 'heartbeat'
);
const heartbeat =
heartbeatOption === undefined
? undefined
: {
name: heartbeatOption[0]
};

const queries = Object.entries(serviceOptions)
.filter((entry) => {
const key = entry[0];
Expand Down Expand Up @@ -179,6 +189,7 @@ export function Service<T extends ServiceOptions>(
`;

returnFunction.init = init;
returnFunction.heartbeat = heartbeat;
returnFunction.queries = queries;
returnFunction.updates = updates;
returnFunction.callbacks = callbacks;
Expand Down
31 changes: 31 additions & 0 deletions src/lib_functional/canister_methods/heartbeat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { isAsync } from '../../lib_new/method_decorators';
import { CanisterMethodInfo, executeMethod } from '.';
import { Void } from '../../lib_new';

export function heartbeat(
callback: () => void | Promise<void>
): CanisterMethodInfo<[], Void> {
const finalCallback = (...args: any[]) => {
executeMethod(
'heartbeat',
undefined,
undefined,
args,
callback,
[],
Void,
false
);
};

return {
mode: 'heartbeat',
callback: finalCallback,
candid: '',
candidTypes: [],
paramsIdls: [],
returnIdl: Void,
async: isAsync(callback),
guard: undefined
};
}
19 changes: 18 additions & 1 deletion src/lib_functional/canister_methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
EncodeVisitor
} from '../../lib_new/visitors/encode_decode';

export * from './heartbeat';
export * from './init';
export * from './query';
export * from './update';

export type CanisterMethodInfo<T extends ReadonlyArray<any>, K> = {
mode: 'query' | 'update' | 'init';
mode: 'query' | 'update' | 'init' | 'heartbeat';
async: boolean;
callback?: (...args: any) => any;
candid: string;
Expand All @@ -35,6 +36,22 @@ export function executeMethod(
returnIdl: any,
manual: boolean
) {
if (mode === 'heartbeat') {
const result = callback();

if (
result !== undefined &&
result !== null &&
typeof result.then === 'function'
) {
result.catch((error: any) => {
ic.trap(error.toString());
});
}

return;
}

const decoded = IDL.decode(paramCandid[0] as any, args[0]);

const myDecodedObject = paramCandid[0].map((idl: any, index: any) => {
Expand Down
3 changes: 2 additions & 1 deletion src/lib_functional/canister_methods/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function init<
candidTypes: newTypesToStingArr(returnCandid[2]),
paramsIdls: paramsIdls as any,
returnIdl: Void,
async: false
async: false,
guard: undefined
};
}

0 comments on commit 830beb4

Please sign in to comment.