Skip to content

Commit f5423f1

Browse files
committed
Change the way the dynamic host driver function is created
1 parent 5fad875 commit f5423f1

File tree

3 files changed

+27
-31
lines changed

3 files changed

+27
-31
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ Looks like this:
6464

6565
![img](http://i.imgur.com/1LCZxrg.png)
6666

67-
<a id="makeDynamicHostCanvasDriverExample"></a>
67+
<a id="canvasDriverExample"></a>
6868
Or in case the canvas element appears later, for example, when it's created by DOM driver:
6969

7070
```jsx
7171
import {run} from '@cycle/rxjs-run';
7272
import {makeDOMDriver} from '@cycle/dom'
73-
import {makeDynamicHostCanvasDriver, rect} from 'cycle-canvas';
73+
import {canvasDriver, rect} from 'cycle-canvas';
7474
import {Observable} from 'rxjs'
7575

7676
function main (sources) {
@@ -94,7 +94,7 @@ function main (sources) {
9494

9595
const drivers = {
9696
DOM: makeDOMDriver('body'),
97-
Canvas: makeDynamicHostCanvasDriver()
97+
Canvas: canvasDriver
9898
};
9999

100100
run(main, drivers);
@@ -109,7 +109,7 @@ You can find the source for flappy bird [here](https://github.com/cyclejs-commun
109109
#### Creating a canvas driver
110110

111111
- [`makeCanvasDriver`](#makeCanvasDriver)
112-
- [`makeDynamicHostCanvasDriver`](#makeDynamicHostCanvasDriver)
112+
- [`canvasDriver`](#canvasDriver)
113113

114114
#### Drawing shapes and text
115115

@@ -181,11 +181,11 @@ const drivers = {
181181
run(main, drivers);
182182
```
183183

184-
### <a id="makeDynamicHostCanvasDriver"></a> `makeDynamicHostCanvasDriver()`
184+
### <a id="canvasDriver"></a> `canvasDriver`
185185

186-
An alternative factory for the canvas driver function.
186+
An alternative canvas driver function to be used directly without a factory.
187187

188-
Does not take any arguments, but requires events in the sink to be of this format:
188+
It requires events in the sink to be of this format:
189189
```js
190190
{
191191
hostCanvas
@@ -194,7 +194,7 @@ Does not take any arguments, but requires events in the sink to be of this forma
194194
```
195195
where `hostCanvas` is a `<canvas>` DOM element and `rootElement` is the element to draw on the `hostCanvas`.
196196

197-
You can find an [example](#makeDynamicHostCanvasDriverExample) at the top.
197+
You can find an [example](#canvasDriverExample) at the top.
198198

199199
## Drawing shapes and text
200200

src/canvas-driver.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -433,29 +433,25 @@ export function makeCanvasDriver (selector, canvasSize = null) {
433433
return driver
434434
}
435435

436-
export function makeDynamicHostCanvasDriver () {
437-
let driver = function dynamicHostCanvasDriver (sink$) {
438-
sink$.addListener({
439-
next: ({hostCanvas, rootElement}) => {
440-
const context = hostCanvas.getContext('2d')
441-
442-
const rootElementWithDefaults = getRootElementWithDefaults(hostCanvas, rootElement)
436+
export function canvasDriver (sink$) {
437+
sink$.addListener({
438+
next: ({hostCanvas, rootElement}) => {
439+
const context = hostCanvas.getContext('2d')
443440

444-
const instructions = translateVtreeToInstructions(rootElementWithDefaults)
441+
const rootElementWithDefaults = getRootElementWithDefaults(hostCanvas, rootElement)
445442

446-
renderInstructionsToCanvas(instructions, context)
447-
},
448-
error: e => { throw e },
449-
complete: () => null
450-
})
443+
const instructions = translateVtreeToInstructions(rootElementWithDefaults)
451444

452-
return adapt(xs.empty())
453-
}
445+
renderInstructionsToCanvas(instructions, context)
446+
},
447+
error: e => { throw e },
448+
complete: () => null
449+
})
454450

455-
return driver
451+
return adapt(xs.empty())
456452
}
457453

458-
function getRootElementWithDefaults(hostCanvas, rootElement) {
454+
function getRootElementWithDefaults (hostCanvas, rootElement) {
459455
const defaults = {
460456
kind: 'rect',
461457
x: 0,

test/driver-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* globals describe, it */
2-
import {translateVtreeToInstructions, renderInstructionsToCanvas, rect, line, arc, text, polygon, image, makeCanvasDriver, makeDynamicHostCanvasDriver} from '../src/canvas-driver'
2+
import {translateVtreeToInstructions, renderInstructionsToCanvas, rect, line, arc, text, polygon, image, makeCanvasDriver, canvasDriver} from '../src/canvas-driver'
33
import assert from 'assert'
44
import root from 'window-or-global'
55
import {JSDOM} from 'jsdom'
@@ -12,7 +12,7 @@ function methodSpy () {
1212
const stub = (...args) => {
1313
called += 1
1414
callArgs.push(args)
15-
};
15+
}
1616

1717
stub.callCount = () => called
1818
stub.callArgs = () => callArgs
@@ -964,8 +964,8 @@ describe('canvasDriver', () => {
964964
})
965965
})
966966

967-
describe('makeDynamicHostCanvasDriver', () => {
968-
it('produces a canvas driver, a sink to which must emit hostCanvas DOM element along with rootElement to draw on that canvas', () => {
967+
describe('canvasDriver', () => {
968+
it('is a canvas driver function, a sink to which must emit hostCanvas DOM element along with rootElement to draw on that hostCanvas', () => {
969969
const jsdom = new JSDOM('<!DOCTYPE html><html><body><canvas width="100" height="100"></canvas></body></html>')
970970

971971
const actualContextOperations = []
@@ -987,7 +987,7 @@ describe('canvasDriver', () => {
987987
rootElement
988988
})
989989

990-
makeDynamicHostCanvasDriver()(vcanvas$)
990+
canvasDriver(vcanvas$)
991991

992992
const expectedContextOperations = [
993993
{call: 'save', args: []},
@@ -1001,7 +1001,7 @@ describe('canvasDriver', () => {
10011001
})
10021002
})
10031003

1004-
function mockContext(operationCallback) {
1004+
function mockContext (operationCallback) {
10051005
return new Proxy({}, {
10061006
get: (_, methodName) => {
10071007
return (...args) => operationCallback({

0 commit comments

Comments
 (0)