adding where-am-i as a cs019 example, using signatures
This commit is contained in:
parent
a717bde102
commit
c4ec1ae744
|
@ -74,6 +74,10 @@
|
||||||
(define View$ (Sig: view?))
|
(define View$ (Sig: view?))
|
||||||
(provide View$)
|
(provide View$)
|
||||||
|
|
||||||
|
(define Event$ (Sig: event?))
|
||||||
|
(provide Event$)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(require "../resource.rkt")
|
(require "../resource.rkt")
|
||||||
(provide (all-from-out "../resource.rkt"))
|
(provide (all-from-out "../resource.rkt"))
|
||||||
|
|
9
examples/cs019/where-am-i/index.html
Normal file
9
examples/cs019/where-am-i/index.html
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<html>
|
||||||
|
<head><title>Where in the world am I?</title></head>
|
||||||
|
<body>
|
||||||
|
<p>
|
||||||
|
I am at: <span id="real-location">dunno</span>.
|
||||||
|
The mock location says: <span id="mock-location">dunno</span>.
|
||||||
|
</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
59
examples/cs019/where-am-i/where-am-i.rkt
Normal file
59
examples/cs019/where-am-i/where-am-i.rkt
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#lang planet dyoo/whalesong/cs019
|
||||||
|
|
||||||
|
(define-resource index.html)
|
||||||
|
|
||||||
|
|
||||||
|
(define-struct: coord ([lat : Number$]
|
||||||
|
[lng : Number$]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define (coord/unknown? x)
|
||||||
|
(or (coord? x)
|
||||||
|
(and (symbol? x)
|
||||||
|
(symbol=? x 'unknown))))
|
||||||
|
|
||||||
|
(define Coord/Unknown$ (Sig: coord/unknown?))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define-struct: world ([real : Coord/Unknown$]
|
||||||
|
[mock : Coord/Unknown$]))
|
||||||
|
(define World$ (Sig: world?))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define: (location-change [world : World$] [dom : View$] [evt : Event$]) -> World$
|
||||||
|
(make-world (make-coord (event-ref evt "latitude")
|
||||||
|
(event-ref evt "longitude"))
|
||||||
|
(world-mock world)))
|
||||||
|
|
||||||
|
|
||||||
|
(define: (mock-location-change [world : World$] [dom : View$] [evt : Event$]) -> World$
|
||||||
|
(make-world (world-real world)
|
||||||
|
(make-coord (event-ref evt "latitude")
|
||||||
|
(event-ref evt "longitude"))))
|
||||||
|
|
||||||
|
|
||||||
|
(define: (draw [world : World$] [dom : View$]) -> View$
|
||||||
|
(local [(define v1 (if (coord? (world-real world))
|
||||||
|
(update-view-text (view-focus dom "real-location")
|
||||||
|
(format "lat=~a, lng=~a"
|
||||||
|
(coord-lat (world-real world))
|
||||||
|
(coord-lng (world-real world))))
|
||||||
|
dom))
|
||||||
|
(define v2 (if (coord? (world-mock world))
|
||||||
|
(update-view-text (view-focus v1 "mock-location")
|
||||||
|
(format "lat=~a, lng=~a"
|
||||||
|
(coord-lat (world-mock world))
|
||||||
|
(coord-lng (world-mock world))))
|
||||||
|
v1))]
|
||||||
|
v2))
|
||||||
|
|
||||||
|
|
||||||
|
(big-bang (make-world 'unknown 'unknown)
|
||||||
|
(initial-view index.html)
|
||||||
|
(to-draw draw)
|
||||||
|
(on-location-change location-change)
|
||||||
|
(on-mock-location-change mock-location-change))
|
|
@ -26,7 +26,8 @@
|
||||||
quasiquote
|
quasiquote
|
||||||
bitmap/url
|
bitmap/url
|
||||||
current-output-port
|
current-output-port
|
||||||
lambda)))
|
lambda
|
||||||
|
true false)))
|
||||||
|
|
||||||
@(define-runtime-path whalesong-path "..")
|
@(define-runtime-path whalesong-path "..")
|
||||||
|
|
||||||
|
@ -197,6 +198,11 @@ consumes the DOM as an argument, since the DOM, too, is a source of
|
||||||
external state in a program.}]
|
external state in a program.}]
|
||||||
|
|
||||||
|
|
||||||
|
This program cannot be executed directly in Racket/DrRacket,
|
||||||
|
unfortunately, but it can be compiled through Whalesong and @link["http://hashcollision.org/whalesong/examples/cs019/tick-tock/tick-tock.html"]{run in the
|
||||||
|
browser}.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@subsection{More web-world examples}
|
@subsection{More web-world examples}
|
||||||
|
@ -251,7 +257,13 @@ Uses @racket[on-location-change] and @racket[on-mock-location-change] to demonst
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
These examples are written in a less featureful language level
|
||||||
|
(@litchar{#lang planet dyoo/whalesong}), which is why it uses explicit
|
||||||
|
@racket[require] statements to pull in support for
|
||||||
|
@racketmodname/this-package[web-world] and
|
||||||
|
@racketmodname/this-package[resource]. As long as you use
|
||||||
|
@litchar{#lang planet dyoo/whalesong/cs019}, you shouldn't need to
|
||||||
|
require those particular libraries.
|
||||||
|
|
||||||
|
|
||||||
@section{API}
|
@section{API}
|
||||||
|
@ -286,11 +298,18 @@ to a web page.
|
||||||
(big-bang ...
|
(big-bang ...
|
||||||
(initial-view page1.html))
|
(initial-view page1.html))
|
||||||
}|
|
}|
|
||||||
|
|
||||||
|
|
||||||
|
If both the @racket[initial-view] and @racket[to-draw] are omitted
|
||||||
|
from a @racket[big-bang], then @racket[big-bang] will render the world
|
||||||
|
value itself.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@defproc[(stop-when [stop? ([w world] [dom view] -> boolean)]) big-bang-handler]{
|
@defproc[(stop-when [stop? ([w world] [dom view] -> boolean)]) big-bang-handler]{
|
||||||
Tells @racket[big-bang] when to stop.
|
Tells @racket[big-bang] when to stop.
|
||||||
|
|
||||||
|
For example,
|
||||||
@codeblock|{
|
@codeblock|{
|
||||||
...
|
...
|
||||||
(define-struct world (given expected))
|
(define-struct world (given expected))
|
||||||
|
@ -303,6 +322,8 @@ Tells @racket[big-bang] when to stop.
|
||||||
(big-bang ...
|
(big-bang ...
|
||||||
(stop-when stop?))
|
(stop-when stop?))
|
||||||
}|
|
}|
|
||||||
|
|
||||||
|
will stop the computation as soon as @racket[stop?] returns @racket[true].
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -520,6 +541,18 @@ Dom nodes can be created by using @racket[xexp->dom], which converts a
|
||||||
nodes whose DOM representation will be the way they print. This
|
nodes whose DOM representation will be the way they print. This
|
||||||
including images.}
|
including images.}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@defproc[(view-insert-left [v view] [d dom]) view]{
|
||||||
|
Add the dom node @racket[d] as the previous sibling of the focused node.
|
||||||
|
Focus moves to the inserted node.}
|
||||||
|
|
||||||
|
@defproc[(view-insert-right [v view] [d dom]) view]{
|
||||||
|
Add the dom node @racket[d] as the next sibling of the focused node.
|
||||||
|
Focus moves to the inserted node.}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@defproc[(view-remove [v view]) view]{
|
@defproc[(view-remove [v view]) view]{
|
||||||
Remove the dom node at the focus from the view @racket[v]. Focus tries to move
|
Remove the dom node at the focus from the view @racket[v]. Focus tries to move
|
||||||
to the right, if there's a next sibling. If that fails, focus then
|
to the right, if there's a next sibling. If that fails, focus then
|
||||||
|
@ -542,6 +575,8 @@ can accept the event as an argument.
|
||||||
|
|
||||||
@defstruct[event ([kvpairs (listof (list symbol (or/c string number)))])]{}
|
@defstruct[event ([kvpairs (listof (list symbol (or/c string number)))])]{}
|
||||||
|
|
||||||
|
@defthing[Event$ sig]{The signature for an event.}
|
||||||
|
|
||||||
@defproc[(event-ref [evt event?] [name (or/c symbol string)]) value]{
|
@defproc[(event-ref [evt event?] [name (or/c symbol string)]) value]{
|
||||||
Get an value from the event, given its @racket[name].
|
Get an value from the event, given its @racket[name].
|
||||||
}
|
}
|
||||||
|
@ -692,7 +727,7 @@ The signature of a resource.
|
||||||
|
|
||||||
|
|
||||||
@defproc[(resource->url [a-resource resource?]) string?]{
|
@defproc[(resource->url [a-resource resource?]) string?]{
|
||||||
Given a resource, gets a URL.
|
Given a resource, gets its URL.
|
||||||
|
|
||||||
For example,
|
For example,
|
||||||
@codeblock|{
|
@codeblock|{
|
||||||
|
@ -704,8 +739,8 @@ For example,
|
||||||
(bitmap/url (resource->url my-whale-image-resource)))
|
(bitmap/url (resource->url my-whale-image-resource)))
|
||||||
}|
|
}|
|
||||||
|
|
||||||
(This particular example is somewhat redundant, because image
|
This particular example is somewhat redundant, because image resources
|
||||||
resources behave already as images.)
|
behave already as images.
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user