summaryrefslogtreecommitdiff
path: root/starling/asset.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@vistahigherlearning.com>2021-01-29 21:01:16 -0500
committerDavid Thompson <dthompson@vistahigherlearning.com>2021-01-29 21:01:16 -0500
commit7ea3e69142f3a9f4d843f9afcad593d7c7a972f0 (patch)
tree7f40acc2314eafc6c648790fa937fd09b5da5fc4 /starling/asset.scm
parent93f5905a8bccaa4820cc30b8fe93a21244ba915b (diff)
Make asset access transparent and add reload handler method.
Diffstat (limited to 'starling/asset.scm')
-rw-r--r--starling/asset.scm31
1 files changed, 25 insertions, 6 deletions
diff --git a/starling/asset.scm b/starling/asset.scm
index c1a05a1..8ddf945 100644
--- a/starling/asset.scm
+++ b/starling/asset.scm
@@ -32,7 +32,10 @@
artifact
file-name
loader
- args
+ loader-args
+ on-asset-reload
+ add-subscriber
+ remove-subscriber
watch-assets
watching-assets?
watch-asset-directory
@@ -57,7 +60,8 @@
(file-name #:getter file-name #:init-keyword #:file-name)
(loader #:getter loader #:init-keyword #:loader)
(loader-args #:getter loader-args #:init-form '()
- #:init-keyword #:loader-args))
+ #:init-keyword #:loader-args)
+ (subscribers #:getter subscribers #:init-form (make-weak-key-hash-table)))
(define (absolute-file-name file-name)
(if (absolute-file-name? file-name)
@@ -80,6 +84,23 @@
wt))))
(hash-set! sub-table asset asset)))
+(define-method (on-asset-reload obj slot-name)
+ #t)
+
+(define-method (add-subscriber (asset <asset>) obj slot-name)
+ (let ((subs (subscribers asset)))
+ (hashq-set! subs obj (cons slot-name (hashq-ref subs obj '())))))
+
+(define-method (remove-subscriber (asset <asset>) obj slot-name)
+ (let* ((subs (subscribers asset))
+ (slot-names (delq slot-name (hashq-ref subs obj '()))))
+ (if (null? slot-names)
+ (hashq-remove! subs obj)
+ (hashq-set! subs obj slot-names))))
+
+(define-method (notify-subscribers (asset <asset>))
+ (hash-for-each on-asset-reload (subscribers asset)))
+
(define-method (asset-purge (asset <asset>))
(hash-remove! (hash-ref (asset-file-map) (file-name asset)) asset))
@@ -151,7 +172,8 @@
(expire-cached-artifact (cache-key asset)))
assets)
(hash-for-each (lambda (key asset)
- (load! asset))
+ (load! asset)
+ (notify-subscribers asset))
assets))))))))
(define (cache-key asset)
@@ -192,9 +214,6 @@
(or (hashq-ref (asset-artifact-map) asset)
(load! asset #t)))
-;; Make assets that are outside of the cache "just work".
-(define-method (asset-ref x) x)
-
;; Handy syntax for defining new assets.
(define-syntax-rule (define-asset name
(loader file-name loader-args ...))