Skip to content

Commit

Permalink
fix decorators for init and post upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Dec 30, 2024
1 parent 2052715 commit cc43919
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 67 deletions.
60 changes: 25 additions & 35 deletions src/lib/stable/canister_methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IDL } from '@dfinity/candid';

import { Method, MethodMeta } from '../../../build/stable/utils/types';
import { handleUncaughtError } from '../error';
import {
CanisterMethodMode,
Expand All @@ -14,7 +13,6 @@ export type MethodType<This, Args extends any[], Return> = (

export function decoratorArgumentsHandler<This, Args extends any[], Return>(
canisterMethodMode: CanisterMethodMode,
methodMetaKey: keyof MethodMeta,
param1?: MethodType<This, Args, Return> | IDL.Type[],
param2?: ClassMethodDecoratorContext | IDL.Type,
param3?: { composite?: boolean; manual?: boolean }
Expand All @@ -26,7 +24,6 @@ export function decoratorArgumentsHandler<This, Args extends any[], Return>(

return decoratorImplementation(
canisterMethodMode,
methodMetaKey,
originalMethod,
context
);
Expand All @@ -43,7 +40,6 @@ export function decoratorArgumentsHandler<This, Args extends any[], Return>(
): MethodType<This, Args, Return> => {
return decoratorImplementation(
canisterMethodMode,
methodMetaKey,
originalMethod,
context,
paramIdlTypes,
Expand All @@ -56,7 +52,6 @@ export function decoratorArgumentsHandler<This, Args extends any[], Return>(

function decoratorImplementation<This, Args extends any[], Return>(
canisterMethodMode: CanisterMethodMode,
methodMetaKey: keyof MethodMeta,
originalMethod: MethodType<This, Args, Return>,
context: ClassMethodDecoratorContext,
paramIdlTypes?: IDL.Type[],
Expand All @@ -68,29 +63,13 @@ function decoratorImplementation<This, Args extends any[], Return>(
const index = globalThis._azleCanisterMethodsIndex++;
const indexString = index.toString();

if (canisterMethodMode !== 'heartbeat') {
if (Array.isArray(globalThis._azleMethodMeta[methodMetaKey])) {
if (canisterMethodMode === 'query') {
(globalThis._azleMethodMeta[methodMetaKey] as Method[]).push({
name,
index,
composite: options?.composite ?? false
});
} else {
(globalThis._azleMethodMeta[methodMetaKey] as Method[]).push({
name,
index
});
}
} else {
(globalThis._azleMethodMeta[methodMetaKey] as Method) = {
name,
index
};
}
}

if (canisterMethodMode === 'query') {
globalThis._azleMethodMeta.queries?.push({
name,
index,
composite: options?.composite ?? false
});

globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes ?? [],
returnIdlType === undefined ? [] : [returnIdlType],
Expand All @@ -99,25 +78,36 @@ function decoratorImplementation<This, Args extends any[], Return>(
}

if (canisterMethodMode === 'update') {
globalThis._azleMethodMeta.updates?.push({
name,
index
});

globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes ?? [],
returnIdlType === undefined ? [] : [returnIdlType]
);
}

if (canisterMethodMode === 'init') {
globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes ?? [],
[],
['init']
globalThis._azleMethodMeta.init = {
name,
index
};

globalThis._azleInitAndPostUpgradeIdlTypes.push(
IDL.Func(paramIdlTypes ?? [], [], ['init'])
);
}

if (canisterMethodMode === 'postUpgrade') {
globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes ?? [],
[],
['post_upgrade']
globalThis._azleMethodMeta.post_upgrade = {
name,
index
};

globalThis._azleInitAndPostUpgradeIdlTypes.push(
IDL.Func(paramIdlTypes ?? [], [], ['post_upgrade'])
);
}

Expand Down
8 changes: 2 additions & 6 deletions src/lib/stable/canister_methods/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function init<This, Args extends any[], Return>(
): MethodType<This, Args, Return>;

export function init<This, Args extends any[], Return>(
paramIdlTypes?: IDL.Type[],
returnIdlType?: IDL.Type,
options?: {
manual?: boolean;
}
paramIdlTypes?: IDL.Type[]
): (
originalMethod: MethodType<This, Args, Return>,
context: ClassMethodDecoratorContext
Expand All @@ -23,5 +19,5 @@ export function init<This, Args extends any[], Return>(
param2?: ClassMethodDecoratorContext | IDL.Type,
param3?: { manual?: boolean }
): any {
return decoratorArgumentsHandler('init', 'init', param1, param2, param3);
return decoratorArgumentsHandler('init', param1, param2, param3);
}
14 changes: 2 additions & 12 deletions src/lib/stable/canister_methods/post_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function postUpgrade<This, Args extends any[], Return>(
): MethodType<This, Args, Return>;

export function postUpgrade<This, Args extends any[], Return>(
paramIdlTypes?: IDL.Type[],
returnIdlType?: IDL.Type,
options?: {
manual?: boolean;
}
paramIdlTypes?: IDL.Type[]
): (
originalMethod: MethodType<This, Args, Return>,
context: ClassMethodDecoratorContext
Expand All @@ -23,11 +19,5 @@ export function postUpgrade<This, Args extends any[], Return>(
param2?: ClassMethodDecoratorContext | IDL.Type,
param3?: { manual?: boolean }
): any {
return decoratorArgumentsHandler(
'postUpgrade',
'post_upgrade',
param1,
param2,
param3
);
return decoratorArgumentsHandler('postUpgrade', param1, param2, param3);
}
8 changes: 1 addition & 7 deletions src/lib/stable/canister_methods/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,5 @@ export function query<This, Args extends any[], Return>(
param2?: ClassMethodDecoratorContext | IDL.Type,
param3?: { manual?: boolean }
): any {
return decoratorArgumentsHandler(
'query',
'queries',
param1,
param2,
param3
);
return decoratorArgumentsHandler('query', param1, param2, param3);
}
8 changes: 1 addition & 7 deletions src/lib/stable/canister_methods/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,5 @@ export function update<This, Args extends any[], Return>(
param2?: ClassMethodDecoratorContext | IDL.Type,
param3?: { manual?: boolean }
): any {
return decoratorArgumentsHandler(
'update',
'updates',
param1,
param2,
param3
);
return decoratorArgumentsHandler('update', param1, param2, param3);
}

0 comments on commit cc43919

Please sign in to comment.