Skip to content

Commit

Permalink
fix: recordHook asyncing/awaiting (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbrugger authored Sep 12, 2023
1 parent f6a3a2b commit 59a8b18
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-foxes-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@flatfile/plugin-record-hook': patch
---

Bug fix to handle asynchronous handlers
94 changes: 80 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"utils/*"
],
"scripts": {
"clean": "find ./ '(' -name 'node_modules' -o -name 'dist' -o -name '.turbo' ')' -type d -exec rm -rf {} +",
"clean": "find ./ '(' -name 'node_modules' -o -name 'dist' -o -name '.turbo' -o -name '.parcel-cache' ')' -type d -exec rm -rf {} +",
"test": "turbo test --concurrency=1",
"build": "turbo build",
"lint": "prettier --check **/*.ts",
Expand Down
3 changes: 2 additions & 1 deletion plugins/record-hook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@flatfile/api": "^1.5.13",
"@flatfile/hooks": "^1.3.1",
"@flatfile/listener": "^0.3.15",
"@flatfile/util-common": "^0.2.0"
"@flatfile/util-common": "^0.2.0",
"effect": "^2.0.0-next.29"
},
"devDependencies": {
"axios": "^1.4.0"
Expand Down
15 changes: 13 additions & 2 deletions plugins/record-hook/src/RecordHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Record_, Records } from '@flatfile/api/api'
import { FlatfileRecord, FlatfileRecords } from '@flatfile/hooks'
import { FlatfileEvent } from '@flatfile/listener'
import { asyncBatch } from '@flatfile/util-common'
import { Effect } from 'effect'
import { RecordTranslater } from './record.translater'

export const RecordHook = async (
Expand All @@ -10,12 +11,22 @@ export const RecordHook = async (
record: FlatfileRecord,
event?: FlatfileEvent
) => any | Promise<any>,
options: { debug?: boolean } = {}
options: { concurrency?: number; debug?: boolean } = {}
) => {
const { concurrency } = { concurrency: 10, ...options }
return BulkRecordHook(
event,
async (records, event) => {
return records.map((record) => handler(record, event))
const handlers = await records.map((record: FlatfileRecord) =>
Effect.promise(async () => {
await handler(record, event)
})
)
return Effect.runPromise(
Effect.all(handlers, {
concurrency,
})
)
},
options
)
Expand Down
8 changes: 7 additions & 1 deletion plugins/record-hook/src/record.hook.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bulkRecordHook, recordHook } from './index'
import {
createRecords,
deleteSpace,
Expand All @@ -7,6 +6,7 @@ import {
setupSimpleWorkbook,
setupSpace,
} from '@flatfile/utils-testing'
import { bulkRecordHook, recordHook } from './index'

jest.setTimeout(10_000)

Expand All @@ -33,9 +33,15 @@ describe('recordHook() e2e', () => {

describe.each([
recordHook('test', (record) => record.set('name', 'daddy')),
recordHook('test', async (record) => await record.set('name', 'daddy')),
bulkRecordHook('test', (records) =>
records.map((record) => record.set('name', 'daddy'))
),
bulkRecordHook(
'test',
async (records) =>
await Promise.all(records.map((record) => record.set('name', 'daddy')))
),
])('record created', (fn) => {
beforeEach(async () => {
listener.use(fn)
Expand Down
2 changes: 1 addition & 1 deletion plugins/record-hook/src/record.hook.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const recordHookPlugin = (
record: FlatfileRecord,
event?: FlatfileEvent
) => any | Promise<any>,
options: { debug?: boolean } = {}
options: { concurrency?: number; debug?: boolean } = {}
) => {
return (client: FlatfileListener) => {
client.on('commit:created', { sheetSlug }, (event: FlatfileEvent) => {
Expand Down

0 comments on commit 59a8b18

Please sign in to comment.