From ba87aaf77089f22f92ac2e5b15ed6fe240008dd7 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 12:27:05 -0600 Subject: [PATCH] Update superheroes to functional runtime syntax Blocked by recursive definitions --- examples/motoko_examples/superheroes/dfx.json | 3 +- .../superheroes/src/superheroes/index.ts | 87 +++++++++---------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/examples/motoko_examples/superheroes/dfx.json b/examples/motoko_examples/superheroes/dfx.json index 012a5b0010..4272d1e0c7 100644 --- a/examples/motoko_examples/superheroes/dfx.json +++ b/examples/motoko_examples/superheroes/dfx.json @@ -6,7 +6,8 @@ "root": "src/superheroes", "ts": "src/superheroes/index.ts", "candid": "src/superheroes/index.did", - "wasm": ".azle/superheroes/superheroes.wasm.gz", + "wasm": ".azle/superheroes/superheroes.wasm", + "gzip": true, "declarations": { "output": "src/declarations", "node_compatibility": true diff --git a/examples/motoko_examples/superheroes/src/superheroes/index.ts b/examples/motoko_examples/superheroes/src/superheroes/index.ts index 1495dcfada..d9b206d629 100644 --- a/examples/motoko_examples/superheroes/src/superheroes/index.ts +++ b/examples/motoko_examples/superheroes/src/superheroes/index.ts @@ -1,11 +1,11 @@ import { bool, - candid, nat32, None, Opt, query, Record, + Recursive, Service, Some, text, @@ -13,72 +13,71 @@ import { update } from 'azle'; -// Note: This won't be reflected in the candid until +// Note: This won't be reflected in the candid export type SuperheroId = nat32; const SuperheroId = nat32; -export type List = [text, Opt]; -const List: List = Tuple(text, Opt(List)); +const List = Tuple( + text, + Recursive(() => Opt(List)) +); // The type of a superhero. -class Superhero extends Record { - @candid(text) - name: text; - - @candid(Opt(List)) - superpowers: Opt; -} +const Superhero = Record({ + name: text, + superpowers: Opt(List) +}); /** - * High-Level API + * Application State */ -export default class extends Service { - /** - * Application State - */ - // The next available superhero identifier. - next: SuperheroId = 0; +// The next available superhero identifier. +let next: SuperheroId = 0; - // The superhero data store. - superheroes: Map = new Map(); +// The superhero data store. +let superheroes: Map = new Map(); +/** + * High-Level API + */ +export default Service({ // Create a superhero. - @update([Superhero], SuperheroId) - create(superhero: Superhero): SuperheroId { - let superheroId = this.next; - this.next += 1; - this.superheroes.set(superheroId, superhero); + create: update([Superhero], SuperheroId, (superhero) => { + let superheroId = next; + next += 1; + superheroes.set(superheroId, superhero); return superheroId; - } + }), // Read a superhero. - @query([SuperheroId], Opt(Superhero)) - read(superheroId: SuperheroId): Opt { - const superheroOrUndefined = this.superheroes.get(superheroId); + read: query([SuperheroId], Opt(Superhero), (superheroId) => { + const superheroOrUndefined = superheroes.get(superheroId); return superheroOrUndefined ? Some(superheroOrUndefined) : None; - } + }), // Update a superhero. - @update([SuperheroId, Superhero], bool) - update(superheroId: SuperheroId, superhero: Superhero): bool { - let result = this.superheroes.get(superheroId); - if (result) { - this.superheroes.set(superheroId, superhero); - } + update: update( + [SuperheroId, Superhero], + bool, + (superheroId: SuperheroId, superhero) => { + let result = superheroes.get(superheroId); + if (result) { + superheroes.set(superheroId, superhero); + } - return !!result; - } + return !!result; + } + ), // Delete a superhero. - @update([SuperheroId], bool) - deleteHero(superheroId: SuperheroId): bool { - let result = this.superheroes.get(superheroId); + deleteHero: update([SuperheroId], bool, (superheroId) => { + let result = superheroes.get(superheroId); if (result) { - this.superheroes.delete(superheroId); + superheroes.delete(superheroId); } return !!result; - } -} + }) +});