Skip to content

Commit

Permalink
fix: buffer commands until template is bootstrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
indr committed Jan 25, 2019
1 parent 77572c0 commit c3c7714
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ const init = function (window) {
console.warn('[webcg-adobe-animate-adapter] expected window.createjs to be an object')
return
}

// The template host may already call template commands such as update() and play() before the template is ready to
// receive calls. We ask WebCG to buffer these commands and flush them after we have instantiated the adapter.
window.webcg.bufferCommands()
window.AdobeAn.bootstrapCallback(() => {
/* eslint-disable no-new */
new Adapter(window.webcg, window.exportRoot, window.createjs)
// The template is bootstrapped and the adapter is ready to receive and process calls
window.webcg.flushCommands()
})
}

Expand Down
1 change: 1 addition & 0 deletions test/createjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class DisplayObject {
}

class MovieClip extends DisplayObject {
stop () {}
}

class Text extends DisplayObject {
Expand Down
15 changes: 13 additions & 2 deletions test/main.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import init from '../src/main.js'
import createjs from 'createjs.js'

describe('init', () => {
let window, AdobeAn
let window, AdobeAn, webcg

beforeEach(() => {
window = { location: { search: '' } }
window.AdobeAn = AdobeAn = {}
window.exportRoot = new createjs.MovieClip()
window.createjs = {}
window.webcg = {}
window.webcg = webcg = { bufferCommands: () => {}, flushCommands: () => {}, addEventListener: () => {} }
AdobeAn.bootstrapCallback = sinon.spy()
})

Expand All @@ -33,4 +35,13 @@ describe('init', () => {
init(window)
expect(AdobeAn.bootstrapCallback.called).to.equal(false)
})

it('should buffer and flush commands', () => {
const mock = sinon.mock(webcg)
mock.expects('bufferCommands').once()
mock.expects('flushCommands').once()
init(window)
AdobeAn.bootstrapCallback.args[0][0]()
mock.verify()
})
})

0 comments on commit c3c7714

Please sign in to comment.