-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Closes #83 Reviewers: O2 Material Motion, markwei, featherless Reviewed By: O2 Material Motion, markwei, featherless Subscribers: shyndman, featherless, markwei Tags: #material_motion Differential Revision: http://codereview.cc/D1722
- Loading branch information
1 parent
da1d6f2
commit a0e0ccb
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** @license | ||
* Copyright 2016 The Material Motion Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export default class TokenGenerator { | ||
protected _tokenCount = 0; | ||
protected _onTokenCountChange; | ||
|
||
constructor({ onTokenCountChange }:{ onTokenCountChange:(_:{ count:number })=>void } = {}) { | ||
if (!onTokenCountChange) { | ||
throw new Error(`TokenGenerator requires an onTokenCountChange listener to be passed in`); | ||
} | ||
|
||
this._onTokenCountChange = onTokenCountChange; | ||
} | ||
|
||
/** | ||
* Generates a token, representing a task waiting to be completed. | ||
* | ||
* Call the returned terminate function when the task is complete. | ||
*/ | ||
generateToken():{ terminate:()=>void } { | ||
let terminated = false; | ||
this._updateTokenCount(+1); | ||
|
||
return { | ||
terminate: () => { | ||
if (terminated) { | ||
throw new Error(`This token has already been terminated`); | ||
|
||
} else { | ||
terminated = true; | ||
this._updateTokenCount(-1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
_updateTokenCount(delta:number):void { | ||
this._tokenCount += delta; | ||
this._onTokenCountChange({ count: this._tokenCount }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** @license | ||
* Copyright 2016 The Material Motion Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { | ||
beforeEach, | ||
describe, | ||
it, | ||
} from 'mocha-sugar-free'; | ||
|
||
import { | ||
spy, | ||
} from 'sinon'; | ||
|
||
import TokenGenerator from '../TokenGenerator'; | ||
|
||
describe('TokenGenerator', | ||
() => { | ||
let tokenGenerator; | ||
|
||
it(`should require a callback`, | ||
() => { | ||
expect( | ||
() => { | ||
new TokenGenerator(); | ||
} | ||
).to.throw(`onTokenCountChange`); | ||
} | ||
); | ||
|
||
it(`should notify the callback when a new token is issued`, | ||
() => { | ||
const onTokenCountChange = spy(); | ||
new TokenGenerator({ onTokenCountChange }).generateToken(); | ||
|
||
expect(onTokenCountChange.lastCall.args[0].count).to.equal(1); | ||
} | ||
); | ||
|
||
it(`should notify the callback when a token is terminated`, | ||
() => { | ||
const onTokenCountChange = spy(); | ||
|
||
new TokenGenerator({ onTokenCountChange }).generateToken().terminate(); | ||
|
||
expect(onTokenCountChange.lastCall.args[0].count).to.equal(0); | ||
} | ||
); | ||
|
||
it(`should error if a token is terminated repeatedly`, | ||
() => { | ||
expect( | ||
() => { | ||
const token = new TokenGenerator({ onTokenCountChange: () => null }).generateToken(); | ||
token.terminate(); | ||
token.terminate(); | ||
} | ||
).to.throw(`terminated`); | ||
} | ||
); | ||
} | ||
); |