Skip to content

Commit

Permalink
Update simple-to-do to functional runtime syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dansteren committed Sep 25, 2023
1 parent a5b6f47 commit 9465d4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# "examples/generics",
# "examples/ledger_canister",
# "examples/manual_reply",
# "examples/motoko_examples/simple-to-do",
# "examples/motoko_examples/superheroes",
# "examples/motoko_examples/threshold_ecdsa",
# "examples/motoko_examples/whoami",
Expand Down Expand Up @@ -113,6 +112,7 @@ jobs:
"examples/motoko_examples/persistent-storage",
"examples/motoko_examples/phone-book",
"examples/motoko_examples/quicksort",
"examples/motoko_examples/simple-to-do",
"examples/primitive_types",
"examples/principal",
"examples/query",
Expand Down
3 changes: 2 additions & 1 deletion examples/motoko_examples/simple-to-do/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/simple_to_do/simple_to_do.wasm.gz",
"wasm": ".azle/simple_to_do/simple_to_do.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/simple_to_do",
"node_compatibility": true
Expand Down
67 changes: 28 additions & 39 deletions examples/motoko_examples/simple-to-do/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
bool,
candid,
nat,
query,
Record,
Expand All @@ -11,72 +10,62 @@ import {
Void
} from 'azle';

export class ToDo extends Record {
@candid(text)
description: text;
export const ToDo = Record({
description: text,
completed: bool
});

@candid(bool)
completed: bool;
}
let todos: Map<nat, typeof ToDo> = new Map();
let nextId: nat = 0n;

export default class extends Service {
todos: Map<nat, ToDo> = new Map();
nextId: nat = 0n;

@query([], Vec(ToDo))
getTodos(): Vec<ToDo> {
return Array.from(this.todos.values());
}
export default Service({
getTodos: query([], Vec(ToDo), () => {
return Array.from(todos.values());
}),

// Returns the ID that was given to the ToDo item
@update([text], nat)
addTodo(description: text): nat {
const id = this.nextId;
this.todos.set(id, {
addTodo: update([text], nat, (description: text) => {
const id = nextId;
todos.set(id, {
description: description,
completed: false
});
this.nextId += 1n;
nextId += 1n;

return id;
}
}),

@update([nat], Void)
completeTodo(id: nat): void {
let todo = this.todos.get(id);
completeTodo: update([nat], Void, (id: nat) => {
let todo = todos.get(id);

if (todo !== undefined) {
this.todos.set(id, {
todos.set(id, {
description: todo.description,
completed: true
});
}
}
}),

@query([], text)
showTodos(): text {
showTodos: query([], text, () => {
let output = '\n___TO-DOs___';
for (const todoEntry of [...this.todos]) {
for (const todoEntry of [...todos]) {
output += `\n${todoEntry[1].description}`;
if (todoEntry[1].completed) {
output += ' ✔';
}
}
return output;
}
}),

@update([], Void)
clearCompleted(): void {
clearCompleted: update([], Void, () => {
// NOTE: this syntax isn't supported in Boa. If we revert to using Boa
// we'll need to revert the syntax to:
// ```ts
// this.todos = new Map(
// [...this.todos].filter((value) => !value[1].completed)
// todos = new Map(
// [...todos].filter((value) => !value[1].completed)
// );
// ```
// See: https://github.com/demergent-labs/azle/issues/574
this.todos = new Map(
[...this.todos].filter(([key, value]) => !value.completed)
);
}
}
todos = new Map([...todos].filter(([key, value]) => !value.completed));
})
});

0 comments on commit 9465d4f

Please sign in to comment.