Skip to content

Commit d2332d6

Browse files
authored
Merge branch 'integration/typescript' into PLAT-13699-derecursify
2 parents 10940d3 + 0852a76 commit d2332d6

8 files changed

Lines changed: 49 additions & 45 deletions

File tree

packages/core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
},
1212
"./lib/es-utils/assign": "./src/lib/es-utils/assign.js",
1313
"./lib/es-utils/includes": "./src/lib/es-utils/includes.js",
14-
"./lib/callback-runner": "./src/lib/callback-runner.js",
1514
"./lib/path-normalizer": "./src/lib/path-normalizer.js"
1615
},
1716
"description": "Core classes and utilities for Bugsnag notifiers",

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { default as extractObject } from './lib/extract-object'
1212
export { default as intRange } from './lib/validators/int-range'
1313
export { default as isError } from './lib/iserror'
1414
export { default as listOfFunctions } from './lib/validators/list-of-functions'
15+
export { default as runCallbacks } from "./lib/callback-runner";
1516
export { default as runSyncCallbacks } from './lib/sync-callback-runner'
1617
export { default as stringWithLength } from './lib/validators/string-with-length'
1718

packages/core/src/lib/async-every.d.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export type NodeCallbackType<T = any> = (error?: Error | null, result?: T) => void;
2+
13
// This is a heavily modified/simplified version of
24
// https://github.com/othiym23/async-some
35
// with the logic flipped so that it is akin to the
@@ -9,7 +11,11 @@
911
// - or the end of the array is reached
1012
// the callback (cb) will be passed (null, false) if any of the items in arr
1113
// caused fn to call back with false, otherwise it will be passed (null, true)
12-
module.exports = (arr, fn, cb) => {
14+
const every = <T>(
15+
arr: T[],
16+
fn: (item: T, cb: NodeCallbackType) => void,
17+
cb: NodeCallbackType<boolean>
18+
): void => {
1319
let index = 0
1420

1521
const next = () => {
@@ -24,3 +30,5 @@ module.exports = (arr, fn, cb) => {
2430

2531
next()
2632
}
33+
34+
export default every

packages/core/src/lib/callback-runner.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
const some = require('./async-every')
1+
import every from './async-every'
2+
import type { NodeCallbackType } from './async-every'
23

3-
module.exports = (callbacks, event, onCallbackError, cb) => {
4+
const runCallbacks = <T>(
5+
callbacks: any[],
6+
event: T,
7+
onCallbackError: (err: Error) => void,
8+
cb: NodeCallbackType<boolean>
9+
): void => {
410
// This function is how we support different kinds of callback:
511
// - synchronous - return value
612
// - node-style async with callback - cb(err, value)
713
// - promise/thenable - resolve(value)
814
// It normalises each of these into the lowest common denominator – a node-style callback
9-
const runMaybeAsyncCallback = (fn, cb) => {
15+
const runMaybeAsyncCallback = (fn: any, cb: NodeCallbackType<boolean>) => {
1016
if (typeof fn !== 'function') return cb(null)
1117
try {
1218
// if function appears sync…
@@ -16,9 +22,9 @@ module.exports = (callbacks, event, onCallbackError, cb) => {
1622
if (ret && typeof ret.then === 'function') {
1723
return ret.then(
1824
// resolve
19-
val => setTimeout(() => cb(null, val)),
25+
(val: boolean | undefined ) => setTimeout(() => cb(null, val)),
2026
// reject
21-
err => {
27+
(err: Error) => {
2228
setTimeout(() => {
2329
onCallbackError(err)
2430
return cb(null, true)
@@ -29,18 +35,20 @@ module.exports = (callbacks, event, onCallbackError, cb) => {
2935
return cb(null, ret)
3036
}
3137
// if function is async…
32-
fn(event, (err, result) => {
38+
fn(event, (err: Error, result: any) => {
3339
if (err) {
3440
onCallbackError(err)
3541
return cb(null)
3642
}
3743
cb(null, result)
3844
})
39-
} catch (e) {
45+
} catch (e: any) {
4046
onCallbackError(e)
4147
cb(null)
4248
}
4349
}
4450

45-
some(callbacks, runMaybeAsyncCallback, cb)
51+
return every(callbacks, runMaybeAsyncCallback, cb)
4652
}
53+
54+
export default runCallbacks

packages/plugin-electron-ipc/bugsnag-ipc-main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
const { Breadcrumb, Event, featureFlagDelegate } = require('@bugsnag/core')
2-
const runCallbacks = require('@bugsnag/core/lib/callback-runner')
1+
const { Breadcrumb, Event, featureFlagDelegate, runCallbacks } = require('@bugsnag/core')
32

43
module.exports = class BugsnagIpcMain {
54
constructor (client) {

packages/plugin-electron-ipc/test/bugsnag-ipc-main.test.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Schema } from '../../core/dist/types/config'
12
import BugsnagIpcMain from '../bugsnag-ipc-main'
23
import { Client, User, Plugin, Event, FeatureFlag } from '@bugsnag/core'
34

@@ -14,17 +15,20 @@ const Notifier = {
1415
url: 'https://github.com/bugsnag/bugsnag-js'
1516
}
1617

18+
// @ts-expect-error invalid schema expected for testing
19+
const testSchema: Schema = {}
20+
1721
describe('BugsnagIpcMain', () => {
1822
describe('constructor()', () => {
1923
it('should throw if the state manager plugin is not loaded first', () => {
20-
const client = new Client({ apiKey: '123' }, {}, [], Notifier)
24+
const client = new Client({ apiKey: '123' }, testSchema, [], Notifier)
2125
expect(() => {
2226
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2327
const bugsnagIpcMain = new BugsnagIpcMain(client)
2428
}).toThrowError('Expected @bugsnag/plugin-electron-client-state-manager to be loaded first')
2529
})
2630
it('should work when the state manager plugin is loaded first', () => {
27-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
31+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
2832
expect(() => {
2933
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3034
const bugsnagIpcMain = new BugsnagIpcMain(client)
@@ -34,15 +38,15 @@ describe('BugsnagIpcMain', () => {
3438

3539
describe('handle()', () => {
3640
it('works for updating context', () => {
37-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
41+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
3842
client.setContext = jest.fn()
3943
const bugsnagIpcMain = new BugsnagIpcMain(client)
4044
bugsnagIpcMain.handle({}, 'setContext', JSON.stringify('new context'))
4145
expect(client.setContext).toHaveBeenCalledWith('new context')
4246
})
4347

4448
it('returns the current context', () => {
45-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
49+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
4650
client.setContext('today')
4751
const bugsnagIpcMain = new BugsnagIpcMain(client)
4852
const event = { returnValue: undefined }
@@ -51,7 +55,7 @@ describe('BugsnagIpcMain', () => {
5155
})
5256

5357
it('works for updating user', () => {
54-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
58+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
5559
client.setUser = jest.fn()
5660
const bugsnagIpcMain = new BugsnagIpcMain(client)
5761
// all fields set
@@ -63,7 +67,7 @@ describe('BugsnagIpcMain', () => {
6367
})
6468

6569
it('returns the current user', () => {
66-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
70+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
6771
client.setUser('81676', undefined, 'Cal')
6872
const bugsnagIpcMain = new BugsnagIpcMain(client)
6973
const event = { returnValue: undefined }
@@ -72,7 +76,7 @@ describe('BugsnagIpcMain', () => {
7276
})
7377

7478
it('works for adding metadata', () => {
75-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
79+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
7680
client.addMetadata = jest.fn()
7781
const bugsnagIpcMain = new BugsnagIpcMain(client)
7882
const stubWebContents = { /* this would be a WebContents instance */ }
@@ -82,15 +86,15 @@ describe('BugsnagIpcMain', () => {
8286
})
8387

8488
it('works for removing metadata', () => {
85-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
89+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
8690
client.clearMetadata = jest.fn()
8791
const bugsnagIpcMain = new BugsnagIpcMain(client)
8892
bugsnagIpcMain.handle({}, 'clearMetadata', JSON.stringify('section'))
8993
expect(client.clearMetadata).toHaveBeenCalledWith('section')
9094
})
9195

9296
it('returns metadata content', () => {
93-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
97+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
9498
client.addMetadata('section', 'content', 'X')
9599
const bugsnagIpcMain = new BugsnagIpcMain(client)
96100
const event = { returnValue: undefined }
@@ -104,7 +108,7 @@ describe('BugsnagIpcMain', () => {
104108
})
105109

106110
it('works for adding a single feature flag', () => {
107-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
111+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
108112
client.addFeatureFlag = jest.fn()
109113

110114
const bugsnagIpcMain = new BugsnagIpcMain(client)
@@ -116,7 +120,7 @@ describe('BugsnagIpcMain', () => {
116120
})
117121

118122
it('works for adding multiple feature flags', () => {
119-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
123+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
120124
client.addFeatureFlags = jest.fn()
121125

122126
const bugsnagIpcMain = new BugsnagIpcMain(client)
@@ -136,7 +140,7 @@ describe('BugsnagIpcMain', () => {
136140
})
137141

138142
it('works for clearing a single feature flag', () => {
139-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
143+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
140144
client.clearFeatureFlag = jest.fn()
141145

142146
const bugsnagIpcMain = new BugsnagIpcMain(client)
@@ -148,7 +152,7 @@ describe('BugsnagIpcMain', () => {
148152
})
149153

150154
it('works for clearing all feature flags', () => {
151-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
155+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
152156
client.clearFeatureFlags = jest.fn()
153157

154158
const bugsnagIpcMain = new BugsnagIpcMain(client)
@@ -160,7 +164,7 @@ describe('BugsnagIpcMain', () => {
160164
})
161165

162166
it('works for managing sessions', () => {
163-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
167+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
164168
client._sessionDelegate = { startSession: jest.fn(), resumeSession: jest.fn(), pauseSession: jest.fn() }
165169
const bugsnagIpcMain = new BugsnagIpcMain(client)
166170
// start
@@ -175,7 +179,7 @@ describe('BugsnagIpcMain', () => {
175179
})
176180

177181
it('works for breadcrumbs', (done) => {
178-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
182+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
179183
client.addOnBreadcrumb(b => {
180184
expect(b.message).toBe('hi IPC')
181185
expect(b.type).toBe('manual')
@@ -191,7 +195,7 @@ describe('BugsnagIpcMain', () => {
191195
})
192196

193197
it('works for bulk updates', done => {
194-
const client = new Client({ apiKey: '123' }, {}, [{
198+
const client = new Client({ apiKey: '123' }, testSchema, [{
195199
name: 'clientStateManager',
196200
load: () => ({
197201
bulkUpdate: ({ context, user, metadata, features }: { context?: string, user?: User, metadata: Record<string, unknown>, features: FeatureFlag | null[]}) => {
@@ -219,13 +223,13 @@ describe('BugsnagIpcMain', () => {
219223
})
220224

221225
it('is resilient to unknown methods', () => {
222-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
226+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
223227
const bugsnagIpcMain = new BugsnagIpcMain(client)
224228
expect(() => bugsnagIpcMain.handle({}, 'explodePlease', JSON.stringify({ data: 123 }))).not.toThrowError()
225229
})
226230

227231
it('is resilient to bad JSON', () => {
228-
const client = new Client({ apiKey: '123' }, {}, [mockClientStateManagerPlugin], Notifier)
232+
const client = new Client({ apiKey: '123' }, testSchema, [mockClientStateManagerPlugin], Notifier)
229233
const bugsnagIpcMain = new BugsnagIpcMain(client)
230234
expect(() => bugsnagIpcMain.handle({}, 'leaveBreadcrumb', 'not json')).not.toThrowError()
231235
})

0 commit comments

Comments
 (0)