Skip to content

Commit

Permalink
Merge pull request #2415 from demergent-labs/canister_methods_only_fr…
Browse files Browse the repository at this point in the history
…om_export

Canister methods only from export
  • Loading branch information
lastmjs authored Jan 4, 2025
2 parents 7d57683 + f274940 commit 0755b66
Show file tree
Hide file tree
Showing 26 changed files with 786 additions and 360 deletions.
Binary file modified canister_templates/experimental.wasm
Binary file not shown.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default Canister({
repeatCrossCanister: repeatCrossCanisterId
};
}),
statusReport: query([], StatusReport, () => {
getStatusReport: query([], StatusReport, () => {
return statusReport;
})
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let timerIds = {
export function getTests(timersCanister: ActorSubclass<_SERVICE>): Test {
return () => {
it('gets initial timer values', async () => {
const result = await timersCanister.statusReport();
const result = await timersCanister.getStatusReport();

const expectedResult = {
single: false,
Expand All @@ -39,7 +39,7 @@ export function getTests(timersCanister: ActorSubclass<_SERVICE>): Test {
wait('for repeated timer to be called once', 7_000);

it('checks that only the repeated timers were called', async () => {
const result = await timersCanister.statusReport();
const result = await timersCanister.getStatusReport();

const expectedResult = {
single: false,
Expand All @@ -60,7 +60,7 @@ export function getTests(timersCanister: ActorSubclass<_SERVICE>): Test {
wait('for the single timer to finish', 5_000);

it('checks that everything got called (and the repeated timers ran a second time)', async () => {
const result = await timersCanister.statusReport();
const result = await timersCanister.getStatusReport();

const expectedResult = {
single: true,
Expand Down Expand Up @@ -93,7 +93,7 @@ export function getTests(timersCanister: ActorSubclass<_SERVICE>): Test {
wait('for the repeating call interval', 7_000);

it('checks that the repeating timers stopped', async () => {
const result = await timersCanister.statusReport();
const result = await timersCanister.getStatusReport();

const expectedResult = {
single: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ import { IDL, query, update } from 'azle';
import { Post, Reaction, ReactionType, Thread, User } from './candid_types';
import * as posts from './posts';
import * as reactions from './reactions';
import { State } from './state';
import * as threads from './threads';
import * as users from './users';

export default class {
export default class Canister {
state: State = {
posts: {},
reactions: {},
threads: {},
users: {}
};

@update([IDL.Text, IDL.Text, IDL.Text, IDL.Nat32], Post)
createPost(
authorId: string,
text: string,
threadId: string,
joinDepth: number
): Post {
return posts.createPost(authorId, text, threadId, joinDepth);
return posts.createPost(this, authorId, text, threadId, joinDepth);
}

@query([IDL.Nat32], IDL.Vec(Post))
getAllPosts(joinDepth: number): Post[] {
return posts.getAllPosts(joinDepth);
return posts.getAllPosts(this, joinDepth);
}

@update([IDL.Text, IDL.Text, ReactionType, IDL.Nat32], Reaction)
Expand All @@ -30,6 +38,7 @@ export default class {
joinDepth: number
): Reaction {
return reactions.createReaction(
this,
authorId,
postId,
reactionType,
Expand All @@ -39,26 +48,26 @@ export default class {

@query([IDL.Nat32], IDL.Vec(Reaction))
getAllReactions(joinDepth: number): Reaction[] {
return reactions.getAllReactions(joinDepth);
return reactions.getAllReactions(this, joinDepth);
}

@update([IDL.Text, IDL.Text, IDL.Nat32], Thread)
createThread(title: string, authorId: string, joinDepth: number): Thread {
return threads.createThread(title, authorId, joinDepth);
return threads.createThread(this, title, authorId, joinDepth);
}

@query([IDL.Nat32], IDL.Vec(Thread))
getAllThreads(joinDepth: number): Thread[] {
return threads.getAllThreads(joinDepth);
return threads.getAllThreads(this, joinDepth);
}

@update([IDL.Text, IDL.Nat32], User)
createUser(username: string, joinDepth: number): User {
return users.createUser(username, joinDepth);
return users.createUser(this, username, joinDepth);
}

@query([IDL.Nat32], IDL.Vec(User))
getAllUsers(joinDepth: number): User[] {
return users.getAllUsers(joinDepth);
return users.getAllUsers(this, joinDepth);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import Canister from '.';
import { Post } from './candid_types';
import { getReactionFromStateReaction } from './reactions';
import { state, StatePost, StateThread, StateUser } from './state';
import { StatePost, StateThread, StateUser } from './state';
import { getThreadFromStateThread } from './threads';
import { getUserFromStateUser } from './users';

export function createPost(
canister: Canister,
authorId: string,
text: string,
threadId: string,
joinDepth: number
): Post {
const id = Object.keys(state.posts).length.toString();
const id = Object.keys(canister.state.posts).length.toString();

const statePost: StatePost = {
id,
Expand All @@ -19,43 +21,52 @@ export function createPost(
text,
threadId
};
const updatedStateAuthor = getUpdatedStateAuthor(authorId, statePost.id);
const updatedStateThread = getUpdatedStateThread(threadId, statePost.id);
const updatedStateAuthor = getUpdatedStateAuthor(
canister,
authorId,
statePost.id
);
const updatedStateThread = getUpdatedStateThread(
canister,
threadId,
statePost.id
);

state.posts[id] = statePost;
state.users[authorId] = updatedStateAuthor;
state.threads[threadId] = updatedStateThread;
canister.state.posts[id] = statePost;
canister.state.users[authorId] = updatedStateAuthor;
canister.state.threads[threadId] = updatedStateThread;

const post = getPostFromStatePost(statePost, joinDepth);
const post = getPostFromStatePost(canister, statePost, joinDepth);

return post;
}

export function getAllPosts(joinDepth: number): Post[] {
return Object.values(state.posts).map((statePost) =>
getPostFromStatePost(statePost!, joinDepth)
export function getAllPosts(canister: Canister, joinDepth: number): Post[] {
return Object.values(canister.state.posts).map((statePost) =>
getPostFromStatePost(canister, statePost!, joinDepth)
);
}

export function getPostFromStatePost(
canister: Canister,
statePost: StatePost,
joinDepth: number
): Post {
const stateAuthor = state.users[statePost.authorId];
const stateAuthor = canister.state.users[statePost.authorId];

if (stateAuthor === undefined) {
throw new Error('Author not found');
}

const author = getUserFromStateUser(stateAuthor, joinDepth);
const author = getUserFromStateUser(canister, stateAuthor, joinDepth);

const stateThread = state.threads[statePost.threadId];
const stateThread = canister.state.threads[statePost.threadId];

if (stateThread === undefined) {
throw new Error('Thread not found');
}

const thread = getThreadFromStateThread(stateThread, joinDepth);
const thread = getThreadFromStateThread(canister, stateThread, joinDepth);

if (joinDepth === 0) {
return {
Expand All @@ -67,9 +78,13 @@ export function getPostFromStatePost(
};
} else {
const reactions = statePost.reactionIds
.map((reactionId) => state.reactions[reactionId])
.map((reactionId) => canister.state.reactions[reactionId])
.map((stateReaction) =>
getReactionFromStateReaction(stateReaction!, joinDepth - 1)
getReactionFromStateReaction(
canister,
stateReaction!,
joinDepth - 1
)
);

return {
Expand All @@ -82,8 +97,12 @@ export function getPostFromStatePost(
}
}

function getUpdatedStateAuthor(authorId: string, postId: string): StateUser {
const stateAuthor = state.users[authorId];
function getUpdatedStateAuthor(
canister: Canister,
authorId: string,
postId: string
): StateUser {
const stateAuthor = canister.state.users[authorId];

if (stateAuthor === undefined) {
throw new Error('Author not found');
Expand All @@ -97,8 +116,12 @@ function getUpdatedStateAuthor(authorId: string, postId: string): StateUser {
return updatedStateAuthor;
}

function getUpdatedStateThread(threadId: string, postId: string): StateThread {
const stateThread = state.threads[threadId];
function getUpdatedStateThread(
canister: Canister,
threadId: string,
postId: string
): StateThread {
const stateThread = canister.state.threads[threadId];

if (stateThread === undefined) {
throw new Error('Thread not found');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import Canister from '.';
import { Reaction, ReactionType } from './candid_types';
import { getPostFromStatePost } from './posts';
import { state, StatePost, StateReaction, StateUser } from './state';
import { StatePost, StateReaction, StateUser } from './state';
import { getUserFromStateUser } from './users';

export function createReaction(
canister: Canister,
authorId: string,
postId: string,
reactionType: ReactionType,
joinDepth: number
): Reaction {
const id = Object.keys(state.reactions).length.toString();
const id = Object.keys(canister.state.reactions).length.toString();

const stateReaction: StateReaction = {
id,
Expand All @@ -18,45 +20,58 @@ export function createReaction(
reactionType
};
const updatedStateAuthor = getUpdatedStateAuthor(
canister,
authorId,
stateReaction.id
);
const updatedStatePost = getUpdatedStatePost(postId, stateReaction.id);
const updatedStatePost = getUpdatedStatePost(
canister,
postId,
stateReaction.id
);

state.reactions[id] = stateReaction;
state.users[authorId] = updatedStateAuthor;
state.posts[postId] = updatedStatePost;
canister.state.reactions[id] = stateReaction;
canister.state.users[authorId] = updatedStateAuthor;
canister.state.posts[postId] = updatedStatePost;

const reaction = getReactionFromStateReaction(stateReaction, joinDepth);
const reaction = getReactionFromStateReaction(
canister,
stateReaction,
joinDepth
);

return reaction;
}

export function getAllReactions(joinDepth: number): Reaction[] {
return Object.values(state.reactions).map((stateReaction) =>
getReactionFromStateReaction(stateReaction!, joinDepth)
export function getAllReactions(
canister: Canister,
joinDepth: number
): Reaction[] {
return Object.values(canister.state.reactions).map((stateReaction) =>
getReactionFromStateReaction(canister, stateReaction!, joinDepth)
);
}

export function getReactionFromStateReaction(
canister: Canister,
stateReaction: StateReaction,
joinDepth: number
): Reaction {
const stateAuthor = state.users[stateReaction.authorId];
const stateAuthor = canister.state.users[stateReaction.authorId];

if (stateAuthor === undefined) {
throw new Error('Author not found');
}

const author = getUserFromStateUser(stateAuthor, joinDepth);
const author = getUserFromStateUser(canister, stateAuthor, joinDepth);

const statePost = state.posts[stateReaction.postId];
const statePost = canister.state.posts[stateReaction.postId];

if (statePost === undefined) {
throw new Error('Post not found');
}

const post = getPostFromStatePost(statePost, joinDepth);
const post = getPostFromStatePost(canister, statePost, joinDepth);

return {
id: stateReaction.id,
Expand All @@ -67,10 +82,11 @@ export function getReactionFromStateReaction(
}

function getUpdatedStateAuthor(
canister: Canister,
authorId: string,
reactionId: string
): StateUser {
const stateAuthor = state.users[authorId];
const stateAuthor = canister.state.users[authorId];

if (stateAuthor === undefined) {
throw new Error('Author not found');
Expand All @@ -84,8 +100,12 @@ function getUpdatedStateAuthor(
return updatedStateAuthor;
}

function getUpdatedStatePost(postId: string, reactionId: string): StatePost {
const statePost = state.posts[postId];
function getUpdatedStatePost(
canister: Canister,
postId: string,
reactionId: string
): StatePost {
const statePost = canister.state.posts[postId];

if (statePost === undefined) {
throw new Error('Post not found');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import { ReactionType } from './candid_types';

// TODO this state should go on the class
export let state: State = {
posts: {},
reactions: {},
threads: {},
users: {}
};

export type State = {
posts: {
[id: string]: StatePost | undefined;
Expand Down
Loading

0 comments on commit 0755b66

Please sign in to comment.