Skip to content

Commit

Permalink
Merge pull request #687 from orbitjs/request-strategy-args
Browse files Browse the repository at this point in the history
Ensure that RequestStrategy handler functions receive all args
  • Loading branch information
dgeb authored Sep 5, 2019
2 parents eefafd5 + 080808f commit da00029
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
22 changes: 12 additions & 10 deletions packages/@orbit/coordinator/src/strategies/request-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,45 @@ export class RequestStrategy extends ConnectionStrategy {
protected generateListener(): Listener {
const target = this.target as any;

return (data: any, hints: any): any => {
return (...args: any[]): any => {
let result;

if (this._filter) {
if (!this._filter(data, hints)) {
if (!this._filter(...args)) {
return;
}
}

if (typeof this._action === 'string') {
result = target[this._action](data);
result = target[this._action](args[0]);
} else {
result = this._action(data);
result = this._action(...args);
}

if (this._catch && result && result.catch) {
result = result.catch((e: Error) => {
return this._catch(e, data);
return this._catch(e, ...args);
});
}

if (result && result.then) {
let blocking = false;
if (typeof this._blocking === 'function') {
if (this._blocking(data)) {
if (this._blocking(...args)) {
blocking = true;
}
} else if (this._blocking) {
blocking = true;
}

if (blocking) {
if (this.passHints && typeof hints === 'object') {
return this.applyHint(hints, result);
} else {
return result;
if (this.passHints) {
const hints = args[1];
if (typeof hints === 'object') {
return this.applyHint(hints, result);
}
}
return result;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,49 @@ module('RequestStrategy', function(hooks) {
await s1.update(tA);
});

test('for events that are invoked with more than one arg, pass all args to any custom handler functions', async function(assert) {
assert.expect(9);

strategy = new RequestStrategy({
source: 's1',
on: 'updateFail',
async action(transform: Transform, e: Error): Promise<void> {
assert.ok(
this instanceof RequestStrategy,
'`action` is bound to the strategy'
);
assert.strictEqual(transform, tA, 'transform is passed to `action`');
assert.equal(e.message, ':(', 'error is passed to `action`');
},
blocking(transform: Transform, e: Error): boolean {
assert.ok(
this instanceof RequestStrategy,
'`blocking` is bound to the strategy'
);
assert.strictEqual(transform, tA, 'transform is passed to `blocking`');
assert.equal(e.message, ':(', 'error is passed to `blocking`');
return false;
},
filter(transform: Transform, e: Error): boolean {
assert.ok(
this instanceof RequestStrategy,
'`filter` is bound to the strategy'
);
assert.strictEqual(transform, tA, 'transform is passed to `filter`');
assert.equal(e.message, ':(', 'error is passed to `filter`');
return true;
}
});

coordinator = new Coordinator({
sources: [s1, s2],
strategies: [strategy]
});

await coordinator.activate();

s1.emit('updateFail', tA, new Error(':('));
});

// TODO - test blocking option
});

0 comments on commit da00029

Please sign in to comment.