Skip to content

Commit

Permalink
Merge pull request #1039 from dfinity/fix-counter
Browse files Browse the repository at this point in the history
Update Superheroes example to use ordered map
  • Loading branch information
marc0olo authored Dec 3, 2024
2 parents 9f4cbb9 + f1916ef commit 4e02cd4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 85 deletions.
75 changes: 36 additions & 39 deletions motoko/minimal-counter-dapp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
actor {

var counter : Nat = 0;
stable var counter : Nat = 0;

public func increment() : async Nat {
counter += 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"vitest": "^0.32.2"
},
"dependencies": {
"@dfinity/agent": "^1.4.0",
"@dfinity/candid": "^1.4.0",
"@dfinity/principal": "^1.4.0",
"@dfinity/agent": "^2.1.3",
"@dfinity/candid": "^2.1.3",
"@dfinity/principal": "^2.1.3",
"lit-html": "^2.8.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineConfig({
environment("all", { prefix: "DFX_" }),
],
resolve: {
dedupe: ['@dfinity/agent'],
alias: [
{
find: "declarations",
Expand Down
61 changes: 19 additions & 42 deletions motoko/superheroes/src/superheroes/Main.mo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import List "mo:base/List";
import Option "mo:base/Option";
import Trie "mo:base/Trie";
import Map "mo:base/OrderedMap";
import Nat32 "mo:base/Nat32";

actor Superheroes {
Expand All @@ -15,18 +15,19 @@ actor Superheroes {
// The type of a superhero.
public type Superhero = {
name : Text;
superpowers : List.List<Text>;
superpowers : List.List<Text>
};

/**
* Application State
*/

// The next available superhero identifier.
private stable var next : SuperheroId = 0;
stable var next : SuperheroId = 0;

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

/**
* High-Level API
Expand All @@ -36,57 +37,33 @@ actor Superheroes {
public func create(superhero : Superhero) : async SuperheroId {
let superheroId = next;
next += 1;
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
?superhero,
).0;
return superheroId;
map := Ops.put(map, superheroId, superhero);
return superheroId
};

// Read a superhero.
public query func read(superheroId : SuperheroId) : async ?Superhero {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
return result;
let result = Ops.get(map, superheroId);
return result
};

// Update a superhero.
public func update(superheroId : SuperheroId, superhero : Superhero) : async Bool {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
let exists = Option.isSome(result);
let (result, old_value) = Ops.replace(map, superheroId, superhero);
let exists = Option.isSome(old_value);
if (exists) {
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
?superhero,
).0;
map := result
};
return exists;
return exists
};

// Delete a superhero.
public func delete(superheroId : SuperheroId) : async Bool {
let result = Trie.find(superheroes, key(superheroId), Nat32.equal);
let exists = Option.isSome(result);
let (result, old_value) = Ops.remove(map, superheroId);
let exists = Option.isSome(old_value);
if (exists) {
superheroes := Trie.replace(
superheroes,
key(superheroId),
Nat32.equal,
null,
).0;
map := result
};
return exists;
};

/**
* Utilities
*/

// Create a trie key from a superhero identifier.
private func key(x : SuperheroId) : Trie.Key<SuperheroId> {
return { hash = x; key = x };
};
};
return exists
}
}

0 comments on commit 4e02cd4

Please sign in to comment.