Skip to content

Commit 5cc7e1b

Browse files
committed
add timeout to new() #9
1 parent f1d3c99 commit 5cc7e1b

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ipos",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Share objects across different Node.js processes. Write and read on both sides.",
55
"license": "MIT",
66
"main": "lib/main.js",

src/__test__/initialize.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ describe('Initialising IPOS', () => {
8080

8181
sub_process.destroy()
8282
})
83+
84+
it('Connect subprocess without main process', async () => {
85+
await expect(IPOS.new({syncTimeout: 200})).resolves.not.toThrow()
86+
})
8387
})

src/init-child.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import IPOS from './main.js'
22

3-
export default function initChild(this: IPOS) {
3+
export default function initChild(this: IPOS, timeout: number) {
44
let resolve: Function
5-
const promise = new Promise(res => resolve = res)
5+
const promise = new Promise((res, reject) => {
6+
resolve = res
7+
setTimeout(reject, timeout)
8+
})
69

710
// @ts-ignore Object is possibly 'undefined'
811
this.messaging.listenForType('sync', message => {

src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ export default class IPOS {
1414

1515
[field: string]: unknown;
1616

17-
static new(): IPOS | Promise<IPOS> {
17+
static new(config?: { syncTimeout: number }): IPOS | Promise<IPOS> {
1818
const ipos = new IPOS()
1919
// was called on child process
2020
if (process.send) {
2121
return new Promise(async resolve => {
22-
await initChild.call(ipos)
22+
await initChild.call(ipos, config?.syncTimeout || 100)
23+
.catch(() => 0)
2324
resolve(ipos)
2425
})
2526
}

0 commit comments

Comments
 (0)