Skip to content

Commit

Permalink
feat: add mapParallel and mapSequential commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DarcyRayner committed Mar 2, 2019
1 parent a324f41 commit 44befcb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/core/commands/functional.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { CommandQueue } from '../command-queue';
import { mapParallel, mapSequential } from './functional';
import { sequence, waitForSeconds } from '../commands';

describe('mapParallel', () => {
it('executes all generated commands in parallel', () => {
const items = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }];
const queue = new CommandQueue();
queue.enqueue(
mapParallel(items, item =>
sequence(waitForSeconds(1), () => {
item.x++;
})
)
);
queue.update(0.5);
expect(items).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }]);
queue.update(0.5);
expect(items).toEqual([{ x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]);
});

it('skips undefined return values', () => {
const items = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }];
const queue = new CommandQueue();
queue.enqueue(
mapParallel(items, (item, index) => {
if (index % 2 === 0) {
return undefined;
}
return sequence(waitForSeconds(1), () => {
item.x++;
});
})
);
queue.update(0.5);
expect(items).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }]);
queue.update(0.5);
expect(items).toEqual([{ x: 1 }, { x: 3 }, { x: 3 }, { x: 5 }]);
});
});

describe('mapSequential', () => {
it('executes all generated commands in sequence', () => {
const items = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }];
const queue = new CommandQueue();
queue.enqueue(
mapSequential(items, item =>
sequence(waitForSeconds(1), () => {
item.x++;
})
)
);
queue.update(1);
expect(items).toEqual([{ x: 2 }, { x: 2 }, { x: 3 }, { x: 4 }]);
queue.update(1);
expect(items).toEqual([{ x: 2 }, { x: 3 }, { x: 3 }, { x: 4 }]);
queue.update(2);
expect(items).toEqual([{ x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]);
});
});
25 changes: 25 additions & 0 deletions src/core/commands/functional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CommandFactory, Command, parallel, sequence } from '../commands';

export function mapParallel<T>(items: Iterable<T>, factory: (item: T, index: number) => Command | undefined): Command {
return parallel(...mapToCommands(items, factory));
}

export function mapSequential<T>(
items: Iterable<T>,
factory: (item: T, index: number) => Command | undefined
): Command {
return sequence(...mapToCommands(items, factory));
}

function mapToCommands<T>(items: Iterable<T>, factory: (item: T, index: number) => Command | undefined): Command[] {
const commands = [];
let index = 0;
for (const item of items) {
const command = factory(item, index);
++index;
if (command !== undefined) {
commands.push(command);
}
}
return commands;
}

0 comments on commit 44befcb

Please # to comment.