Skip to content

Commit 4e02cd4

Browse files
authored
Merge pull request #1039 from dfinity/fix-counter
Update Superheroes example to use ordered map
2 parents 9f4cbb9 + f1916ef commit 4e02cd4

File tree

5 files changed

+60
-85
lines changed

5 files changed

+60
-85
lines changed

motoko/minimal-counter-dapp/package-lock.json

+36-39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

motoko/minimal-counter-dapp/src/minimal_dapp_backend/main.mo

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
actor {
22

3-
var counter : Nat = 0;
3+
stable var counter : Nat = 0;
44

55
public func increment() : async Nat {
66
counter += 1;

motoko/minimal-counter-dapp/src/minimal_dapp_frontend/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"vitest": "^0.32.2"
2222
},
2323
"dependencies": {
24-
"@dfinity/agent": "^1.4.0",
25-
"@dfinity/candid": "^1.4.0",
26-
"@dfinity/principal": "^1.4.0",
24+
"@dfinity/agent": "^2.1.3",
25+
"@dfinity/candid": "^2.1.3",
26+
"@dfinity/principal": "^2.1.3",
2727
"lit-html": "^2.8.0"
2828
}
2929
}

motoko/minimal-counter-dapp/src/minimal_dapp_frontend/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default defineConfig({
3030
environment("all", { prefix: "DFX_" }),
3131
],
3232
resolve: {
33+
dedupe: ['@dfinity/agent'],
3334
alias: [
3435
{
3536
find: "declarations",
+19-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import List "mo:base/List";
22
import Option "mo:base/Option";
3-
import Trie "mo:base/Trie";
3+
import Map "mo:base/OrderedMap";
44
import Nat32 "mo:base/Nat32";
55

66
actor Superheroes {
@@ -15,18 +15,19 @@ actor Superheroes {
1515
// The type of a superhero.
1616
public type Superhero = {
1717
name : Text;
18-
superpowers : List.List<Text>;
18+
superpowers : List.List<Text>
1919
};
2020

2121
/**
2222
* Application State
2323
*/
2424

2525
// The next available superhero identifier.
26-
private stable var next : SuperheroId = 0;
26+
stable var next : SuperheroId = 0;
2727

2828
// The superhero data store.
29-
private stable var superheroes : Trie.Trie<SuperheroId, Superhero> = Trie.empty();
29+
let Ops = Map.Make<SuperheroId>(Nat32.compare);
30+
stable var map : Map.Map<SuperheroId, Superhero> = Ops.empty();
3031

3132
/**
3233
* High-Level API
@@ -36,57 +37,33 @@ actor Superheroes {
3637
public func create(superhero : Superhero) : async SuperheroId {
3738
let superheroId = next;
3839
next += 1;
39-
superheroes := Trie.replace(
40-
superheroes,
41-
key(superheroId),
42-
Nat32.equal,
43-
?superhero,
44-
).0;
45-
return superheroId;
40+
map := Ops.put(map, superheroId, superhero);
41+
return superheroId
4642
};
4743

4844
// Read a superhero.
4945
public query func read(superheroId : SuperheroId) : async ?Superhero {
50-
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
51-
return result;
46+
let result = Ops.get(map, superheroId);
47+
return result
5248
};
5349

5450
// Update a superhero.
5551
public func update(superheroId : SuperheroId, superhero : Superhero) : async Bool {
56-
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
57-
let exists = Option.isSome(result);
52+
let (result, old_value) = Ops.replace(map, superheroId, superhero);
53+
let exists = Option.isSome(old_value);
5854
if (exists) {
59-
superheroes := Trie.replace(
60-
superheroes,
61-
key(superheroId),
62-
Nat32.equal,
63-
?superhero,
64-
).0;
55+
map := result
6556
};
66-
return exists;
57+
return exists
6758
};
6859

6960
// Delete a superhero.
7061
public func delete(superheroId : SuperheroId) : async Bool {
71-
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
72-
let exists = Option.isSome(result);
62+
let (result, old_value) = Ops.remove(map, superheroId);
63+
let exists = Option.isSome(old_value);
7364
if (exists) {
74-
superheroes := Trie.replace(
75-
superheroes,
76-
key(superheroId),
77-
Nat32.equal,
78-
null,
79-
).0;
65+
map := result
8066
};
81-
return exists;
82-
};
83-
84-
/**
85-
* Utilities
86-
*/
87-
88-
// Create a trie key from a superhero identifier.
89-
private func key(x : SuperheroId) : Trie.Key<SuperheroId> {
90-
return { hash = x; key = x };
91-
};
92-
};
67+
return exists
68+
}
69+
}

0 commit comments

Comments
 (0)