Skip to content

Commit c87e225

Browse files
authored
Merge pull request #30 from igagen/master
Change withImage to tryLoadImage returning Maybe to handle failure
2 parents 35c0c4a + 2e39f42 commit c87e225

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

docs/Graphics/Canvas.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ Opaque object describing a gradient.
6565
canvasElementToImageSource :: CanvasElement -> CanvasImageSource
6666
```
6767

68-
#### `withImage`
68+
#### `tryLoadImage`
6969

7070
``` purescript
71-
withImage :: forall eff. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
71+
tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
7272
```
7373

7474
Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage

src/Graphics/Canvas.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ exports.canvasElementToImageSource = function(e) {
55
return e;
66
};
77

8-
exports.withImage = function (src) {
9-
return function(f) {
10-
return function () {
11-
var img = new Image();
12-
img.src = src;
13-
img.addEventListener("load", function() {
14-
f(img)();
15-
}, false);
16-
17-
return {};
8+
exports.tryLoadImageImpl = function (src) {
9+
return function(e) {
10+
return function(f) {
11+
return function () {
12+
var img = new Image();
13+
img.src = src;
14+
img.addEventListener("load", function() {
15+
f(img)();
16+
}, false);
17+
img.addEventListener("error", function(error) {
18+
e();
19+
}, false);
20+
21+
return {};
22+
}
1823
}
1924
};
2025
};

src/Graphics/Canvas.purs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ module Graphics.Canvas
8484
, restore
8585
, withContext
8686

87-
, withImage
87+
, tryLoadImage
8888
, getImageData
8989
, putImageData
9090
, putImageDataFull
@@ -139,8 +139,12 @@ foreign import data CanvasGradient :: *
139139

140140
foreign import canvasElementToImageSource :: CanvasElement -> CanvasImageSource
141141

142+
foreign import tryLoadImageImpl :: forall eff. String -> Eff (canvas :: Canvas | eff) Unit -> (CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
143+
142144
-- | Wrapper for asynchronously loading a image file by path and use it in callback, e.g. drawImage
143-
foreign import withImage :: forall eff. String -> (CanvasImageSource -> Eff eff Unit) -> Eff eff Unit
145+
tryLoadImage :: forall eff. String -> (Maybe CanvasImageSource -> Eff (canvas :: Canvas | eff) Unit) -> Eff (canvas :: Canvas | eff) Unit
146+
tryLoadImage path k = tryLoadImageImpl path (k Nothing) (k <<< Just)
147+
144148

145149
foreign import getCanvasElementByIdImpl ::
146150
forall r eff. Fn3 String

0 commit comments

Comments
 (0)