Skip to content

Commit

Permalink
fix: use iterator properly
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchristian committed Jul 18, 2017
1 parent 31b1066 commit 8f4c91c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"scripts": {
"clean": "rimraf lib && rimraf coverage && rimraf .nyc_output && rimraf lib_test",
"prettier": "prettier --single-quote true --write \"src/**/*.ts\"",
"lint": "tslint --format verbose \"src/**/!(*.spec|*.e2e).ts\"",
"lint": "tslint --format verbose \"src/**/*.ts\"",
"commitmsg": "validate-commit-msg",
"prepublish": "npm run build",
"prebuild": "npm run clean && npm run prettier && npm run lint && echo Using TypeScript && tsc --version",
Expand Down
27 changes: 23 additions & 4 deletions src/core/crosslytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ interface TestEventArgs {
}

class TestEvent extends TrackedEvent<TestEventArgs> {
name = 'Test Event';
category = 'Test Category';
organizationId = 'abc123';
argPriority = new Array<keyof TestEventArgs>();
public name = 'Test Event';
public category = 'Test Category';
public organizationId = 'abc123';
public argPriority = new Array<keyof TestEventArgs>();
}

// tslint:disable-next-line:max-classes-per-file
class TestTracker implements Tracker {
public trackedCalled = 0;
constructor(public id: string) {}
public identify(identity: Identity) {
return;
}
public track<T>(event: TrackedEvent<T>) {
this.trackedCalled++;
return Promise.resolve();
}
}
Expand Down Expand Up @@ -53,3 +56,19 @@ test('Should deregister a Tracker once', t => {
cl.trackers.delete(trackerA.id);
t.is(cl.trackers.size, 1);
});

test('Should call track() once per Tracker', async t => {
const cl = new Crosslytics();
const trackerA = new TestTracker('test');
const trackerB = new TestTracker('test2');
const trackerC = new TestTracker('test3');
cl.trackers.set(trackerA.id, trackerA);
cl.trackers.set(trackerB.id, trackerB);
cl.trackers.set(trackerC.id, trackerC);
t.is(cl.trackers.size, 3);
const ev = new TestEvent({ 'Color': 'Green' });
await cl.track(ev);
t.is(trackerA.trackedCalled, 1);
t.is(trackerB.trackedCalled, 1);
t.is(trackerC.trackedCalled, 1);
});
5 changes: 3 additions & 2 deletions src/core/crosslytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export class Crosslytics {
public async track<T>(event: TrackedEvent<T>) {
const promises = [];

let next = this.trackers.values().next();
const iterable = this.trackers.values();
let next = iterable.next();
while (!next.done) {
promises.push(next.value.track(event));
next = this.trackers.values().next();
next = iterable.next();
}

return Promise.all(promises);
Expand Down

0 comments on commit 8f4c91c

Please sign in to comment.