Skip to content

Commit

Permalink
[added] TokenGenerator
Browse files Browse the repository at this point in the history
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
appsforartists committed Oct 17, 2016
1 parent da1d6f2 commit a0e0ccb
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/runtime/src/TokenGenerator.ts
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 });
}
}
76 changes: 76 additions & 0 deletions packages/runtime/src/__tests__/TokenGenerator.test.ts
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`);
}
);
}
);

0 comments on commit a0e0ccb

Please sign in to comment.