-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
834dee4
commit 05f7518
Showing
40 changed files
with
2,059 additions
and
1,635 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,57 @@ | ||
// SPDX-FileCopyrightText: 2022 Sveriges Television AB | ||
// SPDX-FileCopyrightText: 2025 Sveriges Television AB | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
const handlers = [] | ||
const DIController = require('../../shared/DIController') | ||
|
||
let sendQueue = [] | ||
class Transport { | ||
#handlers = [] | ||
#queue = [] | ||
|
||
/** | ||
* Replay the send queue and | ||
* sequentially call send for | ||
* every message | ||
*/ | ||
function replayQueue () { | ||
const tmpQueue = sendQueue | ||
sendQueue = [] | ||
/** | ||
* Replay the send queue and | ||
* sequentially call send for | ||
* every message | ||
*/ | ||
replayQueue () { | ||
const tmpQueue = this.#queue | ||
this.#queue = [] | ||
|
||
for (const message of tmpQueue) { | ||
this.send(message) | ||
for (const message of tmpQueue) { | ||
this.send(message) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Send a message with the | ||
* transport, for messages | ||
* leaving the api | ||
* | ||
* Unless overridden | ||
* messages will be queued | ||
* | ||
* @param { Object } message | ||
*/ | ||
function send (message) { | ||
sendQueue.push(message) | ||
} | ||
/** | ||
* The entrypoint for messages | ||
* coming into the api | ||
* | ||
* This should be called by the application | ||
* when a new message is to be handled | ||
* | ||
* @param { Object } msg | ||
*/ | ||
receive (msg) { | ||
this.#handlers.forEach(handler => handler(msg)) | ||
} | ||
|
||
/** | ||
* Add a handler to be called when | ||
* this transport receives a message | ||
* @param { (Any) -> Void } handler | ||
*/ | ||
function onMessage (handler) { | ||
handlers.push(handler) | ||
} | ||
onMessage (handler) { | ||
this.#handlers.push(handler) | ||
} | ||
|
||
/** | ||
* The entrypoint for messages | ||
* coming into the api | ||
* | ||
* This should be called by the application | ||
* when a new message is to be handled | ||
* | ||
* @param { Object } message | ||
*/ | ||
function receive (message) { | ||
handlers.forEach(handler => handler(message)) | ||
/** | ||
* Send a message with the | ||
* transport, for messages | ||
* leaving the api | ||
* | ||
* Unless overridden | ||
* messages will be queued | ||
* | ||
* @param { Object } msg | ||
*/ | ||
send (msg) { | ||
this.#queue.push(msg) | ||
} | ||
} | ||
|
||
/** | ||
* @type { import('../transport').Communicator } | ||
*/ | ||
module.exports = { | ||
send, | ||
receive, | ||
replayQueue, | ||
onMessage | ||
} | ||
DIController.main.register('Transport', Transport) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.