This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
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
4290610
commit 2db7633
Showing
33 changed files
with
1,325 additions
and
332 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { SocketLocation, Response, SocketInterface } from '../interface/socket-interface'; | ||
/** | ||
* Mixin for shared socket functionality between the server and client. | ||
*/ | ||
export declare abstract class SocketMixin<API extends SocketInterface, Loc extends SocketLocation> { | ||
/** | ||
* @internal | ||
*/ | ||
private waiters; | ||
/** | ||
* Expected by the mixin. | ||
*/ | ||
protected abstract socketHandlers: any; | ||
/** | ||
* Handles a Response that requires a reply. | ||
*/ | ||
protected abstract reply(ctx: any, response: Response<string, API[Loc], Loc>): any; | ||
/** | ||
* Processes an incoming event with the appropriate socketHandler. If the handler returns an EventResponse, the | ||
* proper event will automatically be emitted. | ||
*/ | ||
protected handleEvent(ctx: unknown, event: string, ...args: any[]): void; | ||
/** | ||
* Gives the ability to block and wait for an event. Usage: `await this.blockEvent('some-event');` | ||
*/ | ||
blockEvent<Event extends string>(event: Event): Promise<any>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Mixin for shared socket functionality between the server and client. | ||
*/ | ||
var SocketMixin = /** @class */ (function () { | ||
function SocketMixin() { | ||
/** | ||
* @internal | ||
*/ | ||
this.waiters = {}; | ||
} | ||
/** | ||
* Processes an incoming event with the appropriate socketHandler. If the handler returns an EventResponse, the | ||
* proper event will automatically be emitted. | ||
*/ | ||
SocketMixin.prototype.handleEvent = function (ctx, event) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
var _a; | ||
// Process the response if there is one | ||
var response = (_a = this.socketHandlers[event]).call.apply(_a, [ctx].concat(args)); | ||
if (response) | ||
this.reply(ctx, response); | ||
// Process any waiters | ||
var waiters = this.waiters[event]; | ||
if (waiters) | ||
for (var _b = 0, waiters_1 = waiters; _b < waiters_1.length; _b++) { | ||
var waiter = waiters_1[_b]; | ||
waiter(); | ||
} | ||
this.waiters[event] = []; | ||
}; | ||
/** | ||
* Gives the ability to block and wait for an event. Usage: `await this.blockEvent('some-event');` | ||
*/ | ||
SocketMixin.prototype.blockEvent = function (event) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
if (!_this.waiters[event]) | ||
_this.waiters[event] = []; | ||
_this.waiters[event].push(resolve); | ||
}); | ||
}; | ||
return SocketMixin; | ||
}()); | ||
export { SocketMixin }; |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.