-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update superheroes to functional runtime syntax
Blocked by recursive definitions
- Loading branch information
Showing
2 changed files
with
45 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 43 additions & 44 deletions
87
examples/motoko_examples/superheroes/src/superheroes/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,83 @@ | ||
import { | ||
bool, | ||
candid, | ||
nat32, | ||
None, | ||
Opt, | ||
query, | ||
Record, | ||
Recursive, | ||
Service, | ||
Some, | ||
text, | ||
Tuple, | ||
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<List>]; | ||
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<List>; | ||
} | ||
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<SuperheroId, Superhero> = new Map(); | ||
// The superhero data store. | ||
let superheroes: Map<SuperheroId, typeof Superhero> = 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<Superhero> { | ||
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; | ||
} | ||
} | ||
}) | ||
}); |