Skip to content

Commit

Permalink
feat: publish fixes and introducing performant correct **publish** me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
dalisoft committed May 30, 2021
1 parent 043fd99 commit 116e369
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
35 changes: 25 additions & 10 deletions src/App.js
Expand Up @@ -42,7 +42,6 @@ export default class App {
this._router = new FindRoute(config);

this._ws = [];
this._pubs = [];

this.time = Date.now();

Expand Down Expand Up @@ -72,20 +71,27 @@ export default class App {
}
middlewares.forEach((handler) => {
if (handler instanceof Route) {
const { _routers, _pubs } = handler;
const { _routers, _ws } = handler;
_routers.forEach(({ method, path, handler: routeHandler }) => {
const routePath =
// eslint-disable-next-line no-nested-ternary
basePath === '*' ? '*' : path === '/' ? basePath : basePath + path;
this._router.on(method, routePath, routeHandler);
});
_pubs.forEach(({ topic, string, isBinary, compress }) => {
this._app.publish(topic, string, isBinary, compress);
_ws.forEach((websocket) => {
websocket.path =
// eslint-disable-next-line no-nested-ternary
basePath === '*'
? '*'
: websocket.path === '/'
? basePath
: basePath + websocket.path;
this._ws.push(websocket);
});
handler._app = this;
handler._basePath = basePath;
_routers.length = 0;
_pubs.length = 0;
_ws.length = 0;
} else {
this._router.on('ANY', basePath, handler);
}
Expand All @@ -100,20 +106,23 @@ export default class App {
return this;
}

ws(path, handler, options) {
ws(path, handler, options = {}) {
if (typeof handler === 'object') {
options = handler;
handler = null;
}

this._ws.push({ path, handler, options });

return this;
}

publish(topic, string, isBinary, compress) {
this._app.publish(topic, string, isBinary, compress);

return this;
return this._app.publish(topic, string, isBinary, compress);
}

listen(port, host) {
const { _config: config, _app: app, _router: router, _console } = this;
const { _config: config, _app: app, _router: router, _ws, _console } = this;

if (typeof port === 'string') {
if (port.indexOf('.') !== -1) {
Expand Down Expand Up @@ -200,6 +209,12 @@ export default class App {
router.lookup(req, res);
});

_ws.forEach(({ path, handler, options }) => {
this._app.ws(path, handler, options);
});
// Cleanup GC
_ws.length = 0;

return new Promise((resolve, reject) => {
if (port === undefined) {
const _errorContext = _console.error ? _console : console;
Expand Down
11 changes: 5 additions & 6 deletions src/Route.js
@@ -1,10 +1,9 @@
import { httpMethods } from './helpers/index.js';
import { httpMethods, invalid } from './helpers/index.js';

export default class Route {
constructor() {
this._routers = [];
this._ws = [];
this._pubs = [];
this._app = null;
this._basePath = '';

Expand Down Expand Up @@ -59,11 +58,11 @@ for (let i = 0, len = httpMethods.length; i < len; i += 1) {
// PubSub methods expose
Route.prototype.publish = (topic, message, isBinary, compress) => {
if (this._app) {
this._app.publish(topic, message, isBinary, compress);
} else {
this._pubs.push({ topic, message, isBinary, compress });
return this._app.publish(topic, message, isBinary, compress);
}
return this;
invalid(
'nanoexpress [Router]: Please attach to `Application` before using publish'
);
};

Route.prototype.ws = function wsExpose(path, handler, options = {}) {
Expand Down

0 comments on commit 116e369

Please sign in to comment.