Skip to content

Commit

Permalink
Add types to promisify-port.js (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc136 authored May 11, 2023
1 parent 9fa20ed commit 1667809
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/promisify-port.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
/**
* Allows to treat a pair of ports as one promise.
* It sends a message into `sendTrough` and resolves the promise with the first "response" data on `subscribeTo`.
* @template DataIn,DataOut
* @param {import("./types/promisify-port").PortsToPromise<DataIn, DataOut>} obj
* @returns {PromiseLike<DataOut>}
*/
function promisifyPort({subscribeTo, sendThrough, data}) {
return new Promise((resolve) => {
/**
* @param {DataOut} result
* @returns void
*/
const handler = (result) => {
subscribeTo.unsubscribe(handler);
resolve(result);
Expand Down
16 changes: 16 additions & 0 deletions lib/types/promisify-port.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type PortsToPromise<DataIn, DataOut> = {
subscribeTo: PortFromElm<DataOut>;
sendThrough: PortToElm<DataIn>;
data: DataIn;
};

export type PortFromElm<DataOut> = {
subscribe: CallbackFn<DataOut>;
unsubscribe: CallbackFn<DataOut>;
};

type CallbackFn<T> = (cb: (data: T) => void) => void;

export type PortToElm<DataIn> = {
send: (data: DataIn) => void;
};
1 change: 1 addition & 0 deletions tsconfig.no-implicit-any.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lib/hash.js",
"lib/help.js",
"lib/os-helpers.js",
"lib/promisify-port.js",
"jest.config.js"
]
}

0 comments on commit 1667809

Please sign in to comment.