Skip to content

Commit fa54d68

Browse files
committed
Create README.md
1 parent 177b925 commit fa54d68

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

README.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Inter Process Object Sharing [IPOS]
2+
3+
_Share objects across different Node.js processes. Write and read on both sides._
4+
This package manages objects via IPC for you. When you create an object, it creates an equivalent on connected
5+
processes. When you update that object, it updates the equivalent accordingly. And if you delete the object, it will
6+
also delete the equivalent. This works both ways, so if you change the equivalent object in a subprocess, the original
7+
will be changes as well.
8+
9+
# Install
10+
11+
```shell
12+
npm i ipos
13+
```
14+
15+
# Usage
16+
17+
In the main process:
18+
19+
```javascript
20+
import child_process from 'child_process'
21+
import IPOS from 'ipos'
22+
23+
// create a shared object instance
24+
const sharedObject = IPOS.new()
25+
// spawn a subprocess
26+
// the ipc channel (4th stdio argument) is important
27+
const subProcess = child_process.spawn('node', ['sub-process.js'], {
28+
stdio: ['inherit', 'inherit', 'inherit', 'ipc']
29+
})
30+
// register the subprocess with IPOS
31+
// await: to wait for the connection to be established
32+
await sharedObject.addProcess(subProcess)
33+
```
34+
35+
In the subprocess:
36+
37+
```javascript
38+
import IPOS from 'ipos'
39+
40+
// await: to wait for the connection to be established (await only in subprocess)
41+
const sharedObject = await IPOS.new()
42+
```
43+
44+
See [`example/main-process.js`](https://github.com/drinking-code/inter-process-object-sharing/blob/main/example/main-process.js)
45+
and [`example/sub-process.js`](https://github.com/drinking-code/inter-process-object-sharing/blob/main/example/sub-process.js)
46+
.
47+
48+
## `IPOS()`
49+
50+
The main class. Don't use the `new` keyword (when creating an instance in a subprocess). Instead, use the
51+
static [`IPOS.new()`](#static-iposnew-ipos--promiseipos) method to create an instance.
52+
53+
### `static IPOS.new(): IPOS | Promise<IPOS>`
54+
55+
Creates a new instance. Multiple instances are not yet supported, so only create one instance per process.
56+
57+
**Returns:**
58+
59+
- `<IPOS>` instance, if called in a normal process
60+
- `Promise<IPOS>`, if called in a subprocess.
61+
Use await to wait for the connection to the main process to be established.
62+
63+
### `ipos.addProcess(process: ChildProcess): Promise<void>`
64+
65+
Connect a subprocess to the IPOS instance. The subprocess must also
66+
call [`IPOS.new()`](#static-iposnew-ipos--promiseipos) for the two processes' IPOS to connect.
67+
68+
**Parameters:**
69+
70+
- `process: ChildProcess` The object of a subprocess IPOS should connect with. What gets returned
71+
by `child_process.exec()`, `child_process.execFile()`, `child_process.fork()`, or `child_process.spawn()`
72+
73+
**Returns:** `Promise<void>`. Use await to wait for the connection to the subprocess to be established.
74+
75+
### `ipos.create(key: string, value: any)`
76+
77+
Create a field on the IPOS instance. This value can later be accessed or updated (See
78+
[`ipos.get()`](#iposgetkey-string-any)). After creating a field, you can access and update it (even change the type)
79+
with `ipos[key: string] = value`. See:
80+
81+
```javascript
82+
sharedObject.create('myValue', 23)
83+
sharedObject.myValue = 'foo'
84+
console.log(sharedObject.myValue) // -> 'foo'
85+
```
86+
87+
`ipos.create()` can be called multiple times with the same key. Each time the old value will be overwritten.
88+
89+
**Parameters:**
90+
91+
- `key: string` A unique key.
92+
- `value: any` The value to be stored.
93+
94+
### `ipos.get(key: string): any`
95+
96+
Get an already created field from the IPOS instance. You can also use `ipos[key: string]` to access the value. If you
97+
use a method on the value that changes the value, this change will also be reflected in the connected IPOS instances.
98+
See:
99+
100+
```javascript
101+
sharedObject.create('myArray', [])
102+
sharedObject.myArray.push('myString')
103+
console.log(sharedObject.get('myArray')) // -> ['myString']
104+
console.log(sharedObject.myArray) // -> ['myString']
105+
```
106+
107+
And in a connected process, after `'myString'` was pushed:
108+
109+
```javascript
110+
console.log(sharedObject.myArray) // -> ['myString']
111+
```
112+
113+
**Parameters:**
114+
115+
- `key: string` A unique key of an already created field.
116+
117+
**Returns:** `any`. The stored value.
118+
119+
### `ipos.delete(key: string): boolean`
120+
121+
Deletes the field with the specified key.
122+
123+
**Parameters:**
124+
125+
- `key: string` A unique key of an already created field.
126+
127+
**Returns:** `boolean`. `true` if a field existed and has been removed, or `false` if the element does not exist.

0 commit comments

Comments
 (0)