Skip to content

Commit

Permalink
fix(play/stop): prevent scenario from messing up when calling play/st…
Browse files Browse the repository at this point in the history
…op successively
  • Loading branch information
zhouzi committed Mar 11, 2016
1 parent c22f5d9 commit 10d4152
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
11 changes: 10 additions & 1 deletion dist/theater.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ return /******/ (function(modules) { // webpackBootstrap
}

function play() {
if (props.status === 'stopping') {
props.status = 'playing';
}

if (props.status === 'ready') {
props.status = 'playing';
playNextScene();
Expand All @@ -206,11 +210,16 @@ return /******/ (function(modules) { // webpackBootstrap
}

function stop() {
props.status = 'ready';
props.status = 'stopping';
return this;
}

function playNextScene() {
if (props.status === 'stopping') {
props.status = 'ready';
return this;
}

if (props.status !== 'playing') return this;

var currentScene = props.scenario[props.currentScene];
Expand Down
2 changes: 1 addition & 1 deletion dist/theater.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/theater.min.js.map

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion src/theaterJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ function theaterJS (options = {}) {
}

function play () {
if (props.status === 'stopping') {
props.status = 'playing'
}

if (props.status === 'ready') {
props.status = 'playing'
playNextScene()
Expand All @@ -118,11 +122,16 @@ function theaterJS (options = {}) {
}

function stop () {
props.status = 'ready'
props.status = 'stopping'
return this
}

function playNextScene () {
if (props.status === 'stopping') {
props.status = 'ready'
return this
}

if (props.status !== 'playing') return this

let currentScene = props.scenario[props.currentScene]
Expand Down
49 changes: 45 additions & 4 deletions test/TheaterJS.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,55 @@ describe('theaterJS', function () {
})

describe('has a stop method that', function () {
it('sets the status to ready', function () {
theater = theaterJS({ autoplay: false })
beforeEach(function () {
jasmine.clock().install()

theater.addActor('vader').addScene('vader:Hello!').play()
theater = theaterJS()
theater.addActor('vader').addScene('vader:Hello!', 'vader:How u doing?')
})

afterEach(function () {
jasmine.clock().uninstall()
})

it('should stop the scenario', function () {
expect(theater.status).toBe('playing')

theater.stop()
expect(theater.status).toBe('ready')

expect(theater.status).toBe('stopping')

jasmine.clock().tick(LONG_TIME)
expect(theater.getCurrentActor().displayValue).toBe('Hello!')
})

it('should be resume-able', function () {
theater.stop()

jasmine.clock().tick(LONG_TIME)
expect(theater.getCurrentActor().displayValue).toBe('Hello!')

theater.play()

jasmine.clock().tick(LONG_TIME)
expect(theater.getCurrentActor().displayValue).toBe('How u doing?')
})

it('shouldn\'t be conflicting if called several times alternatively', function () {
theater.stop()
jasmine.clock().tick(200)
theater.play()
jasmine.clock().tick(200)
theater.stop()
jasmine.clock().tick(200)
theater.play()
jasmine.clock().tick(200)
theater.stop()
jasmine.clock().tick(200)
theater.play()

jasmine.clock().tick(LONG_TIME)
expect(theater.getCurrentActor().displayValue).toBe('How u doing?')
})
})

Expand Down

0 comments on commit 10d4152

Please sign in to comment.