Skip to content

Commit

Permalink
cleanup!
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderer committed Jul 5, 2017
1 parent 4f7f80f commit 2fc16a0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 70 deletions.
26 changes: 25 additions & 1 deletion kernel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Message = require('primea-message')
const BN = require('bn.js')
const PortManager = require('./portManager.js')
const DeleteMessage = require('./deleteMessage')

Expand Down Expand Up @@ -141,6 +142,29 @@ module.exports = class Kernel {
return message
}

/**
* creates a new container. Returning a port to it.
* @param {String} type
* @param {*} data - the data to populate the initail state with
* @returns {Object}
*/
createInstance (type, message) {
let nonce = this.state.nonce

const id = {
nonce: nonce,
parent: this.id
}

// incerment the nonce
nonce = new BN(nonce)
nonce.iaddn(1)
this.state.nonce = nonce.toArray()
this.ports.removeSentPorts(message)

this.hypervisor.createInstance(type, message, id)
}

/**
* sends a message to a given port
* @param {Object} portRef - the port
Expand All @@ -149,7 +173,7 @@ module.exports = class Kernel {
async send (port, message) {
// set the port that the message came from
message._fromTicks = this.ticks
message.ports.forEach(port => this.ports._unboundPorts.delete(port))
this.ports.removeSentPorts(message)

// if (this.currentMessage !== message && !message.responsePort) {
// this.currentMessage._addSubMessage(message)
Expand Down
28 changes: 4 additions & 24 deletions portManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const BN = require('bn.js')
const DeleteMessage = require('./deleteMessage')

// decides which message to go first
Expand Down Expand Up @@ -165,29 +164,6 @@ module.exports = class PortManager {
return this.ports[name]
}

/**
* creates a new container. Returning a port to it.
* @param {String} type
* @param {*} data - the data to populate the initail state with
* @returns {Object}
*/
create (type, message) {
let nonce = this.state.nonce

const id = {
nonce: nonce,
parent: this.id
}

// incerment the nonce
nonce = new BN(nonce)
nonce.iaddn(1)
this.state.nonce = nonce.toArray()
message.ports.forEach(port => this._unboundPorts.delete(port))

this.hypervisor.createInstance(type, message, id)
}

/**
* creates a channel returns the created ports in an Array
* @returns {array}
Expand Down Expand Up @@ -270,4 +246,8 @@ module.exports = class PortManager {
this._messageTickThreshold = message ? message._fromTicks : 0
return this._oldestMessagePromise
}

removeSentPorts (message) {
message.ports.forEach(port => this._unboundPorts.delete(port))
}
}

0 comments on commit 2fc16a0

Please sign in to comment.