Skip to content

Commit

Permalink
!refactor(trigger): options for callAction
Browse files Browse the repository at this point in the history
- BREAKING
  • Loading branch information
Frederik Schmatz committed Sep 28, 2021
1 parent dfb5bc8 commit 6b5840d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
12 changes: 6 additions & 6 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ sidebarDepth: 2
<img src="/img/logo.svg" width="150">
</p>

![npm](https://img.shields.io/npm/v/feathers-trigger)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/fratzinger/feathers-trigger/Node.js%20CI)
![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/fratzinger/feathers-trigger)
![Code Climate coverage](https://img.shields.io/codeclimate/coverage/fratzinger/feathers-trigger)
![David](https://img.shields.io/david/fratzinger/feathers-trigger)
![npm](https://img.shields.io/npm/dm/feathers-trigger)
[![npm](https://img.shields.io/npm/v/feathers-trigger)](https://www.npmjs.com/package/feathers-trigger)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/fratzinger/feathers-trigger/Node.js%20CI)](https://github.com/fratzinger/feathers-trigger/actions/workflows/node.js.yml?query=branch%3Amain++)
[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/fratzinger/feathers-trigger)](https://codeclimate.com/github/fratzinger/feathers-trigger)
[![Code Climate coverage](https://img.shields.io/codeclimate/coverage/fratzinger/feathers-trigger)](https://codeclimate.com/github/fratzinger/feathers-trigger)
[![David](https://img.shields.io/david/fratzinger/feathers-trigger)](https://david-dm.org/fratzinger/feathers-trigger)
[![npm](https://img.shields.io/npm/dm/feathers-trigger)](https://www.npmjs.com/package/feathers-trigger)
[![GitHub license](https://img.shields.io/github/license/fratzinger/feathers-trigger)](https://github.com/fratzinger/feathers-trigger/blob/master/LICENSE)

## Installation
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export const afterHook = async (
conditionsResult: conditionsResultNew
});

const promise = sub.callAction(changeForSub, sub, changes, context);
const promise = sub.callAction(changeForSub, { subscription: sub, items: changes, context });

if (sub.isBlocking) {
promises.push(promise);
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ interface ViewContext<T = any> {
context: HookContext
}

export type CallAction<T = any> = (item: Change<T>, subscription: Subscription, items: Change<T>[], context: HookContext) => (Promisable<void>);
export type CallActionOptions<T = any> = {
subscription?: Subscription,
items?: Change<T>[],
context?: HookContext
}

export type CallAction<T = any> = (item: Change<T>, options?: CallActionOptions<T>) => (Promisable<void>);

export type HookTriggerOptions = Subscription | Subscription[] | ((context: HookContext) => Promisable<Subscription | Subscription[]>)

Expand Down
6 changes: 3 additions & 3 deletions test/hooks/trigger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ describe("hook - trigger", function() {

it("create: triggers on single create with custom param", async function() {
let cbCount = 0;
const callAction: CallAction = (item, sub) => {
const callAction: CallAction = (item, { subscription: sub }) => {
cbCount++;
if (sub.id === 1) {
assert.deepStrictEqual(item, { before: undefined, item: { id: 1 } }, "correct item for sub1");
Expand Down Expand Up @@ -560,7 +560,7 @@ describe("hook - trigger", function() {
const { service } = mock("patch", [
{
conditionsResult: { date: { $lt: "{{ before.date }}" } },
callAction: ({ before, item }, sub) => {
callAction: ({ before, item }, { subscription: sub }) => {
if (item.id === 0) {
assert.deepStrictEqual(sub.conditionsResult, { date: { $lt: addDays(beforeDate, 0).toISOString() } }, "conditions on id:0");
} else if (item.id === 1) {
Expand All @@ -573,7 +573,7 @@ describe("hook - trigger", function() {
},
{
conditionsResult: { test: true },
callAction: ({ before, item }, sub) => {
callAction: ({ before, item }, { subscription: sub }) => {
assert.deepStrictEqual(sub.conditionsResult, { test: true }, "has conditionsResult");
calledTrigger2ById[item.id] = true;
}
Expand Down

0 comments on commit 6b5840d

Please sign in to comment.