UNPKG

rpc_ts

Version:

Remote Procedure Calls in TypeScript made simple

39 lines 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Return a handler where the methods of the BankingService are actually implemented. * * We wrap it in a function to allow for closures, so that you can have variables scoped * to the lifetime of the server (such as the mock store `users` in this instance). */ exports.getNumberHandler = () => { return { async increment({ value }) { return { value: value + 1 }; }, /** Server-side streams are offered with a slightly different signature */ async streamNumbers({ max, sleepMs }, { onReady, onMessage }) { // Calling 'onReady' is mandatory before any call to 'onMessage'. // 'onReady' allows us to pass a callback for early cancellation. let closed = false; onReady(() => { closed = true; }); for (let counter = 0; counter < max; ++counter) { // If the stream has been closed, exit early if (closed) { break; } onMessage({ counter }); await sleep(sleepMs); } }, }; }; /** * Like setTimeout but with promises */ async function sleep(timeInMillis) { return new Promise(y => setTimeout(y, timeInMillis)); } //# sourceMappingURL=handler.js.map