Skip to content

Commit 4976afd

Browse files
committed
Update erlang types dep
1 parent 0b7adb5 commit 4976afd

9 files changed

Lines changed: 66 additions & 75 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Experiment to reproduce Erlang style processes in browser. The api follows the o
88

99
```javascript
1010
const Processes = require('erlang-processes')
11-
let system = new Processes.default.ProcessSystem()
11+
let system = new Processes.ProcessSystem()
1212
```
1313

1414
- Now you can spawn processes using the system.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"typescript": "^2.9.2"
2525
},
2626
"dependencies": {
27-
"erlang-types": "^1.1.0"
27+
"erlang-types": "^1.1.3"
2828
},
2929
"ava": {
3030
"compileEnhancements": false,

src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import ProcessSystem from "./processes/process_system";
1+
import ProcessSystem from './processes/process_system'
22

3-
export default {
4-
ProcessSystem
5-
};
3+
export {ProcessSystem}

src/processes/mailbox.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ class Mailbox {
44
this.messages = []
55
}
66

7-
deliver(message: any) {
7+
deliver(message: any): any {
88
this.messages.push(message)
99
return message
1010
}
1111

12-
get() {
12+
get(): any[] {
1313
return this.messages
1414
}
1515

16-
isEmpty() {
16+
isEmpty(): boolean {
1717
return this.messages.length === 0
1818
}
1919

20-
removeAt(index: number) {
20+
removeAt(index: number): any {
2121
this.messages.splice(index, 1)
2222
}
2323
}

src/processes/process.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ErlangTypes from 'erlang-types'
1+
import {PID} from 'erlang-types'
22
import States from './states'
33
import Mailbox from './mailbox'
44
import System from './process_system'
@@ -16,7 +16,7 @@ function receive_timed_out(value: any) {
1616
}
1717

1818
class Process {
19-
pid: ErlangTypes.PID
19+
pid: PID
2020
func: Function
2121
args: any[]
2222
mailbox: Mailbox
@@ -27,7 +27,7 @@ class Process {
2727
monitors: any[]
2828

2929
constructor(
30-
pid: ErlangTypes.PID,
30+
pid: PID,
3131
func: Function,
3232
args: any[],
3333
mailbox: Mailbox,

src/processes/process_system.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@ import Mailbox from './mailbox'
55
import Process from './process'
66
import States from './states'
77
import Scheduler from './scheduler'
8-
import * as ErlangTypes from 'erlang-types'
8+
import {PID, Reference, Tuple} from 'erlang-types'
99

1010
class ProcessSystem {
11-
pids: Map<ErlangTypes.PID, Process>
12-
mailboxes: Map<ErlangTypes.PID, Mailbox>
13-
names: Map<any, ErlangTypes.PID>
14-
links: Map<ErlangTypes.PID, Set<ErlangTypes.PID>>
15-
monitors: Map<
16-
ErlangTypes.Reference,
17-
{monitor: ErlangTypes.PID; monitee: ErlangTypes.PID}
18-
>
11+
pids: Map<PID, Process>
12+
mailboxes: Map<PID, Mailbox>
13+
names: Map<any, PID>
14+
links: Map<PID, Set<PID>>
15+
monitors: Map<Reference, {monitor: PID; monitee: PID}>
1916
current_process: Process | null
2017
scheduler: Scheduler
21-
suspended: Map<ErlangTypes.PID, Function>
18+
suspended: Map<PID, Function>
2219
main_process_pid: Process
2320

2421
constructor() {
@@ -78,12 +75,12 @@ class ProcessSystem {
7875
}
7976
}
8077

81-
link(pid: ErlangTypes.PID) {
78+
link(pid: PID) {
8279
this.links.get(this.pid()).add(pid)
8380
this.links.get(pid).add(this.pid())
8481
}
8582

86-
unlink(pid: ErlangTypes.PID) {
83+
unlink(pid: PID) {
8784
this.links.get(this.pid()).delete(pid)
8885
this.links.get(pid).delete(this.pid())
8986
}
@@ -103,7 +100,7 @@ class ProcessSystem {
103100
}
104101
}
105102

106-
monitor(pid: ErlangTypes.PID) {
103+
monitor(pid: PID) {
107104
const real_pid = this.pidof(pid)
108105
const ref = this.make_ref()
109106

@@ -117,13 +114,13 @@ class ProcessSystem {
117114
} else {
118115
this.send(
119116
this.current_process.pid,
120-
new ErlangTypes.Tuple('DOWN', ref, pid, real_pid, Symbol.for('noproc'))
117+
new Tuple('DOWN', ref, pid, real_pid, Symbol.for('noproc'))
121118
)
122119
return ref
123120
}
124121
}
125122

126-
demonitor(ref: ErlangTypes.Reference) {
123+
demonitor(ref: Reference) {
127124
if (this.monitors.has(ref)) {
128125
this.monitors.delete(ref)
129126
return true
@@ -149,7 +146,7 @@ class ProcessSystem {
149146
linked: boolean,
150147
monitored: boolean
151148
) {
152-
let newpid = new ErlangTypes.PID()
149+
let newpid = new PID()
153150
let mailbox = new Mailbox()
154151
let newproc = new Process(newpid, fun, args, mailbox, this)
155152

@@ -169,7 +166,7 @@ class ProcessSystem {
169166
return newproc
170167
}
171168

172-
remove_proc(pid: ErlangTypes.PID, exitreason: any) {
169+
remove_proc(pid: PID, exitreason: any) {
173170
this.pids.delete(pid)
174171
this.unregister(pid)
175172
this.scheduler.removePid(pid)
@@ -184,7 +181,7 @@ class ProcessSystem {
184181
}
185182
}
186183

187-
register(name: any, pid: ErlangTypes.PID) {
184+
register(name: any, pid: PID) {
188185
if (!this.names.has(name)) {
189186
this.names.set(name, pid)
190187
} else {
@@ -200,7 +197,7 @@ class ProcessSystem {
200197
return this.names.keys()
201198
}
202199

203-
unregister(pid: ErlangTypes.PID) {
200+
unregister(pid: PID) {
204201
for (let name of this.names.keys()) {
205202
if (this.names.has(name) && this.names.get(name) === pid) {
206203
this.names.delete(name)
@@ -213,7 +210,7 @@ class ProcessSystem {
213210
}
214211

215212
pidof(id: any) {
216-
if (id instanceof ErlangTypes.PID) {
213+
if (id instanceof PID) {
217214
return this.pids.has(id) ? id : null
218215
} else if (id instanceof Process) {
219216
return id.pid
@@ -274,14 +271,14 @@ class ProcessSystem {
274271
}
275272
}
276273

277-
schedule(fun: () => any, pid?: ErlangTypes.PID): void {
274+
schedule(fun: () => any, pid?: PID): void {
278275
if (this.current_process) {
279276
const the_pid = pid != null ? pid : this.current_process.pid
280277
this.scheduler.schedule(the_pid, fun)
281278
}
282279
}
283280

284-
exit(one: ErlangTypes.PID | any, two?: any): void {
281+
exit(one: PID | any, two?: any): void {
285282
let pid = null
286283
let reason = null
287284
let process = null
@@ -298,7 +295,7 @@ class ProcessSystem {
298295
) {
299296
this.mailboxes
300297
.get(process.pid)
301-
.deliver(new ErlangTypes.Tuple(States.EXIT, this.pid(), reason))
298+
.deliver(new Tuple(States.EXIT, this.pid(), reason))
302299
} else {
303300
process.signal(reason)
304301
}
@@ -318,13 +315,7 @@ class ProcessSystem {
318315
if (mons) {
319316
this.send(
320317
mons['monitor'],
321-
new ErlangTypes.Tuple(
322-
'DOWN',
323-
ref,
324-
mons['monitee'],
325-
mons['monitee'],
326-
reason
327-
)
318+
new Tuple('DOWN', ref, mons['monitee'], mons['monitee'], reason)
328319
)
329320
}
330321
}
@@ -395,12 +386,12 @@ class ProcessSystem {
395386
return real_pid != null
396387
}
397388

398-
list(): ErlangTypes.PID[] {
389+
list(): PID[] {
399390
return Array.from(this.pids.keys())
400391
}
401392

402-
make_ref(): ErlangTypes.Reference {
403-
return new ErlangTypes.Reference()
393+
make_ref(): Reference {
394+
return new Reference()
404395
}
405396
}
406397

src/processes/scheduler.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import ErlangTypes from 'erlang-types'
1+
import {PID} from 'erlang-types'
22

33
class ProcessQueue {
4-
pid: ErlangTypes.PID
4+
pid: PID
55
tasks: Function[]
66

7-
constructor(pid: ErlangTypes.PID) {
7+
constructor(pid: PID) {
88
this.pid = pid
99
this.tasks = []
1010
}
@@ -26,7 +26,7 @@ class Scheduler {
2626
isRunning: boolean
2727
invokeLater: (callback: () => void) => void
2828
reductions_per_process: number
29-
queues: Map<ErlangTypes.PID, ProcessQueue>
29+
queues: Map<PID, ProcessQueue>
3030
constructor(throttle: number = 0, reductions_per_process: number = 8) {
3131
this.isRunning = false
3232
this.invokeLater = function(callback) {
@@ -40,7 +40,7 @@ class Scheduler {
4040
this.run()
4141
}
4242

43-
addToQueue(pid: ErlangTypes.PID, task: () => any) {
43+
addToQueue(pid: PID, task: () => any) {
4444
if (!this.queues.has(pid)) {
4545
this.queues.set(pid, new ProcessQueue(pid))
4646
}
@@ -51,7 +51,7 @@ class Scheduler {
5151
}
5252
}
5353

54-
removePid(pid: ErlangTypes.PID) {
54+
removePid(pid: PID) {
5555
this.isRunning = true
5656

5757
this.queues.delete(pid)
@@ -78,7 +78,9 @@ class Scheduler {
7878
let result
7979

8080
try {
81-
result = task()
81+
if (task) {
82+
result = task()
83+
}
8284
} catch (e) {
8385
console.error(e)
8486
result = e
@@ -100,7 +102,7 @@ class Scheduler {
100102
}
101103
}
102104

103-
addToScheduler(pid: ErlangTypes.PID, task: () => any, dueTime: number = 0) {
105+
addToScheduler(pid: PID, task: () => any, dueTime: number = 0) {
104106
if (dueTime === 0) {
105107
this.invokeLater(() => {
106108
this.addToQueue(pid, task)
@@ -112,13 +114,13 @@ class Scheduler {
112114
}
113115
}
114116

115-
schedule(pid: ErlangTypes.PID, task: () => any) {
117+
schedule(pid: PID, task: () => any) {
116118
this.addToScheduler(pid, () => {
117119
task()
118120
})
119121
}
120122

121-
scheduleFuture(pid: ErlangTypes.PID, dueTime: number, task: () => any) {
123+
scheduleFuture(pid: PID, dueTime: number, task: () => any) {
122124
this.addToScheduler(
123125
pid,
124126
() => {

test/processes_test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import test from 'ava';
2-
import Processes from '../src/index.js';
1+
import test from 'ava'
2+
import {ProcessSystem} from '../src/index.js'
33

4-
let system = null;
4+
let system = null
55

66
test.beforeEach(t => {
7-
system = new Processes.ProcessSystem();
8-
});
7+
system = new ProcessSystem()
8+
})
99

1010
test(function* testSpawn(t) {
1111
const pid = system.spawn(function*() {
12-
yield 1;
13-
});
12+
yield 1
13+
})
1414

15-
t.is(system.list().length, 2);
16-
t.is(system.list()[1], pid);
17-
});
15+
t.is(system.list().length, 2)
16+
t.is(system.list()[1], pid)
17+
})
1818

1919
test(function* testSpawnLink(t) {
2020
const pid = system.spawn_link(function*() {
21-
yield 1;
22-
});
21+
yield 1
22+
})
2323

24-
t.is(system.list().length, 2);
25-
t.true(system.links.get(pid).has(system.list()[0]));
26-
t.true(system.links.get(system.list()[0]).has(pid));
27-
});
24+
t.is(system.list().length, 2)
25+
t.true(system.links.get(pid).has(system.list()[0]))
26+
t.true(system.links.get(system.list()[0]).has(pid))
27+
})

0 commit comments

Comments
 (0)