diff --git a/packages/constants/README.md b/packages/constants/README.md index 2e792d41f1a94ad397acc38543cc77764f062d8e..40f9fea8ef7d352c886a0c913067d1aab43da431 100755 --- a/packages/constants/README.md +++ b/packages/constants/README.md @@ -171,6 +171,7 @@ Please consult the detail break down below. - ENABLE_SPLIT_TASK_PROP_KEY - CONNECTED_PROP_KEY - CACHE_STORE_PROP_KEY +- EVENT_EMITTER_PROP_KEY ### SOCKET @@ -202,6 +203,8 @@ Please consult the detail break down below. - EMIT_REPLY_TYPE - EMIT_SEND_TYPE - ACKNOWLEDGE_REPLY_TYPE +- INTER_EMIT_SEND_TYPE +- INTER_EMIT_REPLY_TYPE - NSP_GROUP - PUBLIC_NAMESPACE - JS_WS_SOCKET_IO_NAME diff --git a/packages/constants/browser.js b/packages/constants/browser.js index bdd8764049a762bd3067566e631822c58160cb30..ccd312a9fe503bedca818ca0998e35174f6bbe1d 100644 --- a/packages/constants/browser.js +++ b/packages/constants/browser.js @@ -172,6 +172,7 @@ var jsonqlConstants = { "ENABLE_SPLIT_TASK_PROP_KEY": "enableSplitTask", "CONNECTED_PROP_KEY": "connected", "CACHE_STORE_PROP_KEY": "cacheStore", + "EVENT_EMITTER_PROP_KEY": "eventEmitter", "SOCKET_PING_EVENT_NAME": "__ping__", "SWITCH_USER_EVENT_NAME": "__switch__", "LOGIN_EVENT_NAME": "__login__", @@ -208,6 +209,8 @@ var jsonqlConstants = { "EMIT_REPLY_TYPE": "emit_reply", "EMIT_SEND_TYPE": "emit_send", "ACKNOWLEDGE_REPLY_TYPE": "emit_acknowledge", + "INTER_EMIT_SEND_TYPE": "inter_emit_send", + "INTER_EMIT_REPLY_TYPE": "inter_emit_reply", "NSP_GROUP": "nspGroup", "PUBLIC_NAMESPACE": "publicNamespace", "JS_WS_SOCKET_IO_NAME": "socket.io", diff --git a/packages/constants/constants.json b/packages/constants/constants.json index 59eaca791a753794f719d8cf71c925ac2ddc735b..8136611fd8ae151a01c868cb4c61fff6cd379573 100644 --- a/packages/constants/constants.json +++ b/packages/constants/constants.json @@ -172,6 +172,7 @@ "ENABLE_SPLIT_TASK_PROP_KEY": "enableSplitTask", "CONNECTED_PROP_KEY": "connected", "CACHE_STORE_PROP_KEY": "cacheStore", + "EVENT_EMITTER_PROP_KEY": "eventEmitter", "SOCKET_PING_EVENT_NAME": "__ping__", "SWITCH_USER_EVENT_NAME": "__switch__", "LOGIN_EVENT_NAME": "__login__", @@ -208,6 +209,8 @@ "EMIT_REPLY_TYPE": "emit_reply", "EMIT_SEND_TYPE": "emit_send", "ACKNOWLEDGE_REPLY_TYPE": "emit_acknowledge", + "INTER_EMIT_SEND_TYPE": "inter_emit_send", + "INTER_EMIT_REPLY_TYPE": "inter_emit_reply", "NSP_GROUP": "nspGroup", "PUBLIC_NAMESPACE": "publicNamespace", "JS_WS_SOCKET_IO_NAME": "socket.io", diff --git a/packages/constants/index.js b/packages/constants/index.js index 16fe46fc0b8ace2f6db6f2b4cd5cd30c3863065d..5a40f8457f1bc68431f3b339b04473c89469411d 100644 --- a/packages/constants/index.js +++ b/packages/constants/index.js @@ -247,7 +247,8 @@ export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' export const CONNECTED_PROP_KEY = 'connected' // share naming to id the cache store object -export const CACHE_STORE_PROP_KEY = 'cacheStore' /* socket.js */ +export const CACHE_STORE_PROP_KEY = 'cacheStore' +export const EVENT_EMITTER_PROP_KEY = 'eventEmitter' /* socket.js */ // the constants file is gettig too large // we need to split up and group the related constant in one file @@ -318,6 +319,9 @@ export const EMIT_REPLY_TYPE = 'emit_reply' // this is a new event for a may be feature export const EMIT_SEND_TYPE = 'emit_send' export const ACKNOWLEDGE_REPLY_TYPE = 'emit_acknowledge' +// this is for inter communication +export const INTER_EMIT_SEND_TYPE = 'inter_emit_send' +export const INTER_EMIT_REPLY_TYPE = 'inter_emit_reply' export const NSP_GROUP = 'nspGroup' export const PUBLIC_NAMESPACE = 'publicNamespace' diff --git a/packages/constants/main.js b/packages/constants/main.js index bb059e788ebfe506bff305d6372240daf581d64b..dfa27981d598ca392361e87a1b4f13ff19a8c66d 100644 --- a/packages/constants/main.js +++ b/packages/constants/main.js @@ -172,6 +172,7 @@ module.exports = { "ENABLE_SPLIT_TASK_PROP_KEY": "enableSplitTask", "CONNECTED_PROP_KEY": "connected", "CACHE_STORE_PROP_KEY": "cacheStore", + "EVENT_EMITTER_PROP_KEY": "eventEmitter", "SOCKET_PING_EVENT_NAME": "__ping__", "SWITCH_USER_EVENT_NAME": "__switch__", "LOGIN_EVENT_NAME": "__login__", @@ -208,6 +209,8 @@ module.exports = { "EMIT_REPLY_TYPE": "emit_reply", "EMIT_SEND_TYPE": "emit_send", "ACKNOWLEDGE_REPLY_TYPE": "emit_acknowledge", + "INTER_EMIT_SEND_TYPE": "inter_emit_send", + "INTER_EMIT_REPLY_TYPE": "inter_emit_reply", "NSP_GROUP": "nspGroup", "PUBLIC_NAMESPACE": "publicNamespace", "JS_WS_SOCKET_IO_NAME": "socket.io", diff --git a/packages/constants/package.json b/packages/constants/package.json index 1f22f4e3a284083d2c58d10d17fd0f5319f249d6..073e0f8875abece09c3f8481dc83c8519d61e625 100755 --- a/packages/constants/package.json +++ b/packages/constants/package.json @@ -1,6 +1,6 @@ { "name": "jsonql-constants", - "version": "2.0.12", + "version": "2.0.13", "description": "All the share constants for jsonql modules", "main": "main.js", "module": "index.js", diff --git a/packages/constants/prop.js b/packages/constants/prop.js index b871f24aa8dbba4706396a0e3f80f5a2ff5aebce..0744a21fb52514412ef19daa0afd094c857a798c 100644 --- a/packages/constants/prop.js +++ b/packages/constants/prop.js @@ -102,4 +102,5 @@ export const ENABLE_SPLIT_TASK_PROP_KEY = 'enableSplitTask' export const CONNECTED_PROP_KEY = 'connected' // share naming to id the cache store object -export const CACHE_STORE_PROP_KEY = 'cacheStore' \ No newline at end of file +export const CACHE_STORE_PROP_KEY = 'cacheStore' +export const EVENT_EMITTER_PROP_KEY = 'eventEmitter' \ No newline at end of file diff --git a/packages/constants/socket.js b/packages/constants/socket.js index 0ba76834e2ac7fd933f0570967de430a51e8fe1f..dda8b17d2c97399c53c9ee447a398397cc4226e2 100644 --- a/packages/constants/socket.js +++ b/packages/constants/socket.js @@ -67,6 +67,9 @@ export const EMIT_REPLY_TYPE = 'emit_reply' // this is a new event for a may be feature export const EMIT_SEND_TYPE = 'emit_send' export const ACKNOWLEDGE_REPLY_TYPE = 'emit_acknowledge' +// this is for inter communication +export const INTER_EMIT_SEND_TYPE = 'inter_emit_send' +export const INTER_EMIT_REPLY_TYPE = 'inter_emit_reply' export const NSP_GROUP = 'nspGroup' export const PUBLIC_NAMESPACE = 'publicNamespace' diff --git a/packages/ws-server-core/package.json b/packages/ws-server-core/package.json index d90f381acfc72053b90d74133550c502857b38fe..fb8a990ce864d81784234913f76a89331e162d65 100644 --- a/packages/ws-server-core/package.json +++ b/packages/ws-server-core/package.json @@ -33,7 +33,7 @@ "debug": "^4.1.1", "esm": "^3.2.25", "fs-extra": "^9.0.0", - "jsonql-constants": "^2.0.11", + "jsonql-constants": "^2.0.12", "jsonql-errors": "^1.2.1", "jsonql-params-validator": "^1.6.2", "jsonql-resolver": "^1.2.2", diff --git a/packages/ws-server-core/src/handles/get-socket-handler.js b/packages/ws-server-core/src/handles/get-socket-handler.js index b780cf07a8946dde8258f03db847a4e87c4e7d33..bd0f5669ca861f9c6c0d7cea6440eb7795d24827 100644 --- a/packages/ws-server-core/src/handles/get-socket-handler.js +++ b/packages/ws-server-core/src/handles/get-socket-handler.js @@ -48,6 +48,9 @@ function getSocketHandler(config, ws, deliverFn, req, connectedNamespace, payloa const nspGroup = config.nspInfo[NSP_GROUP] const resolverName = getResolverFromPayload(payload) const args = payload[resolverName] + // break up from previous setup so we get a cache version + const nspHandlerFn = handleNspResolver(deliverFn, ws, config) + // it might be the special internal event? switch (true) { case resolverName === INTERCOM_RESOLVER_NAME: @@ -64,9 +67,14 @@ function getSocketHandler(config, ws, deliverFn, req, connectedNamespace, payloa // just return an error return handleUnknownPayload(deliverFn, resolverName, args) } - - handleNspResolvers(deliverFn, ws, resolverName, args, contractParam.params, config, userdata) - + // we change the interface a bit + return nspHandlerFn( + connectedNamespace, + resolverName, + args.args, + contractParam.params, + userdata + ) } } diff --git a/packages/ws-server-core/src/handles/handle-hello.js b/packages/ws-server-core/src/handles/handle-hello.js index 5e60d066f88181dba17e79821a1bb0507d36c300..a82447a25dfbf9aa6627052bd138e003ca237f52 100644 --- a/packages/ws-server-core/src/handles/handle-hello.js +++ b/packages/ws-server-core/src/handles/handle-hello.js @@ -1,5 +1,5 @@ // create a hello world method to test the installation -const { deliverMsg } = require('../share/resolver-methods') +const { deliverMsg } = require('../resolver/resolver-methods') const { HELLO_FN, HELLO } = require('jsonql-constants') function handleHello(deliverFn, connectedNamespace, userdata) { diff --git a/packages/ws-server-core/src/handles/handle-init-ping.js b/packages/ws-server-core/src/handles/handle-init-ping.js index 18fbbcf99e274728c72ede35837796b29e55998b..3488822e512ac5a9e2f8fcd1b0c16b7ed9fae44f 100644 --- a/packages/ws-server-core/src/handles/handle-init-ping.js +++ b/packages/ws-server-core/src/handles/handle-init-ping.js @@ -11,7 +11,7 @@ const { CSRF_HEADER_KEY, SOCKET_PING_EVENT_NAME } = require('jsonql-constants') -const { deliverIntercomMsg } = require('../share/resolver-methods') +const { deliverIntercomMsg } = require('../resolver/resolver-methods') const { getDebug } = require('../share/helpers') const debug = getDebug('handles:init-ping') /** diff --git a/packages/ws-server-core/src/handles/handle-nsp-resolvers.js b/packages/ws-server-core/src/handles/handle-nsp-resolvers.js index 35271ba00b16a73799fbd5b98bd82cf33ac71dad..28edf9fb1f3f2c1914297a2cbfb42dcd73d57134 100644 --- a/packages/ws-server-core/src/handles/handle-nsp-resolvers.js +++ b/packages/ws-server-core/src/handles/handle-nsp-resolvers.js @@ -2,46 +2,65 @@ // @TODO this property still not create correctly // const { TIMESTAMP_PARAM_NAME } = require('jsonql-constants') // get the resolver to handle the request -const { validateInput, deliverMsg, handleError } = require('../share/resolver-methods') -const { resolveSocketMethod } = require('../share/resolve-socket-method') +const { + validateInput, + deliverMsg, + handleError, + resolveSocketMethod +} = require('../resolver') + const { getRainbowDebug } = require('../share/helpers') const debug = getRainbowDebug('handles:handle-nsp-resolver', 'x') +// for caching +let nspHandlerFn /** - * handle resolvers + * We pass those params that is not change once it's set to this method + * Then we get back the actual handler that will return the resolver to take the call + * * @param {function} deliverFn the final delivery message method * @param {object} ws the socket instance get add to the resolver as property - * @param {string} resolverName name of resolver - * @param {*} args from payload - * @param {object} params from contract.json * @param {object} opts configuration - * @param {object} userdata userdata - * @return {promise} resolve the result + * @return {function} */ -const handleNspResolvers = (deliverFn, ws, resolverName, args, params, opts, userdata) => { - debug('handleNspResolvers', resolverName, args, params) - // debug('TIMESTAMP_PARAM_NAME', json[TIMESTAMP_PARAM_NAME], TIMESTAMP_PARAM_NAME) - // const ts = json[TIMESTAMP_PARAM_NAME] // keep this for use later - // @NOTE the args is in {args: [ value ]} structure and we only need the value - return validateInput(args.args, params) - .then(_args => resolveSocketMethod( - deliverFn, - resolverName, - _args, // this is the clean value from validateAsync - opts, - ws, - userdata +function handleNspResolver(deliverFn, ws, opts) { + // check the cache method first + if (typeof nspHandlerFn === 'function') { + return nspHandlerFn + } + + /** + * @param {string} resolverName name of resolver + * @param {*} args from payload + * @param {object} params from contract.json + * @param {object} userdata userdata + * @return {promise} resolve the result + */ + nspHandlerFn = function nspHandlerAction(namespace, resolverName, args, params, userdata) { + + return validateInput(args, params) + .then(_args => resolveSocketMethod( + namespace, + resolverName, + deliverFn, + _args, // this is the clean value from validateAsync + opts, + ws, + userdata + ) ) - ) - .then(result => { - debug(`${resolverName} return result`, result) - return deliverMsg(deliverFn, resolverName, result) - }) - .catch(err => { - // By the time the error gets here already become JsonqlError??? - // rdebug('in the catch', err) - handleError(deliverFn, resolverName, err) - }) + .then(result => { + debug(`${resolverName} return result`, result) + return deliverMsg(deliverFn, resolverName, result) + }) + .catch(err => { + // By the time the error gets here already become JsonqlError??? + // rdebug('in the catch', err) + handleError(deliverFn, resolverName, err) + }) + } + + return nspHandlerFn } -module.exports = { handleNspResolvers } +module.exports = { handleNspResolver } diff --git a/packages/ws-server-core/src/handles/handle-unknown-payload.js b/packages/ws-server-core/src/handles/handle-unknown-payload.js index 5d065699a274f0f86287c318ab25234425a55288..c687639ccb48196ab78a09faaaabe47d5fb70083 100644 --- a/packages/ws-server-core/src/handles/handle-unknown-payload.js +++ b/packages/ws-server-core/src/handles/handle-unknown-payload.js @@ -1,6 +1,6 @@ const { getDebug } = require('../share/helpers') const debug = getDebug('handle-intercome') -const { handleError } = require('../share/resolver-methods') +const { handleError } = require('../resolver/resolver-methods') const { ERROR_KEY } = require('jsonql-constants') /** diff --git a/packages/ws-server-core/src/options/index.js b/packages/ws-server-core/src/options/index.js index a893b103e3f1c604526c4157e3cccefac1b903d0..c9b1ca5ae3c5936b0a84e7c1f741a08d03c4920f 100644 --- a/packages/ws-server-core/src/options/index.js +++ b/packages/ws-server-core/src/options/index.js @@ -8,7 +8,6 @@ const { getNodeCache, isNodeCache } = require('@jsonql/security') - const { checkConfig, checkConfigAsync, diff --git a/packages/ws-server-core/src/resolver/add-property.js b/packages/ws-server-core/src/resolver/add-property.js deleted file mode 100644 index 98c77d7338b955ec9cafe2e359bac2553ff383db..0000000000000000000000000000000000000000 --- a/packages/ws-server-core/src/resolver/add-property.js +++ /dev/null @@ -1,48 +0,0 @@ -// add required properties to the resolver -const { - SOCKET_NAME, - INIT_CLIENT_PROP_KEY -} = require('jsonql-constants') -const { injectToFn } = require('jsonql-utils') -const { provideUserdata } = require('@jsonql/security') -const { injectNodeClient } = require('jsonql-resolver') - -const { setupSendMethod } = require('./setup-send-method') -const { getDebug } = require('../share/helpers') - -const debug = getDebug(`share:addProperty`) - -/** - * using the serverType to provide different addProperty method to this - * change to return a promise on 1.4.4 - * @param {function} deliverFn framework specific method - * @param {function} fn the resolver function - * @param {string} resolverName resolver name - * @param {object} ws the different context object - * @param {object|boolean} userdata false when there is none - * @param {object} opts configuration added in 1.4.4 for the client configs - * @return {function} the applied function - */ -const addProperty = (deliverFn, fn, resolverName, ws, userdata, opts) => { - return Promise - .resolve(injectToFn(fn, SOCKET_NAME, ws)) // @0.6.2 use a generic name `socket` - // define the send method - .then(resolver => { - return setupSendMethod(deliverFn, resolver, resolverName) - }) - .then(resolver => { - debug(`addProperty ---> userdata --->`, userdata) - return userdata ? provideUserdata(resolver, userdata) : resolver - }) - .then(resolver => { - if (opts[INIT_CLIENT_PROP_KEY] && opts[INIT_CLIENT_PROP_KEY].then) { - debug(`using INIT_CLIENT_PROP_KEY to add clients to the resolver`) - return opts[INIT_CLIENT_PROP_KEY] - .then(clients => injectNodeClient(resolver, clients)) - } - // @TODO what if we run this standalone and we want to inject clients to it? - return resolver - }) -} - -module.exports = { addProperty } diff --git a/packages/ws-server-core/src/resolver/get-injectors.js b/packages/ws-server-core/src/resolver/get-injectors.js new file mode 100644 index 0000000000000000000000000000000000000000..09c8c97c7f3997069cfe8f608bf50d364d9cfc32 --- /dev/null +++ b/packages/ws-server-core/src/resolver/get-injectors.js @@ -0,0 +1,99 @@ +// add required properties to the resolver +const { + SOCKET_NAME, + INIT_CLIENT_PROP_KEY +} = require('jsonql-constants') +const { injectToFn } = require('jsonql-utils') +const { provideUserdata } = require('@jsonql/security') +const { injectNodeClient } = require('jsonql-resolver') +const { setupOnMethod } = require('./setup-on-method') +const { setupSendMethod } = require('./setup-send-method') +const { getDebug } = require('../share/helpers') + +const debug = getDebug(`share:addProperty`) + +/* + This method change from injecting the property via the promise chain + to organize injectors to fns for the resolver method to work with + */ + +/** + * We just pass the whole lot, use some then passing them back with additonal prop to the resolver + * @param {*} fn + * @param {*} deliverFn + * @param {*} resolverName + * @param {*} ws + * @param {*} userdata + * @param {*} opts + * @return {array} params for the next chain call + */ +const injectWsProp = (fn, deliverFn, resolverName, ws, userdata, opts) => [ + injectToFn(fn, SOCKET_NAME, ws), + deliverFn, + resolverName, + ws, + userdata, + opts +] + +const injectUserdata = (fn, deliverFn, resolverName, ws, userdata, opts) => [ + userdata ? provideUserdata(fn, userdata) : fn, + deliverFn, + resolverName, + ws, + userdata, + opts +] + +const injectSendFn = (fn, deliverFn, resolverName, ws, userdata, opts) => [ + setupSendMethod(deliverFn, resolver, resolverName, opts), + deliverFn, + resolverName, + ws, + userdata, + opts +] + +const injectOnFn = (deliverFn, resolverName, ws, userdata, opts) => [ + setupOnMethod(deliverFn, resolver, resolverName, opts), + deliverFn, + resolverName, + ws, + userdata, + opts +] + +/** + * This is the last in the chain, and we just return the resolver itself + * @param {*} deliverFn + * @param {*} resolverName + * @param {*} ws + * @param {*} userdata + * @param {*} opts + * @return {function} resolver with all the props added + */ +const injectClient = (deliverFn, resolverName, ws, userdata, opts) => { + if (opts[INIT_CLIENT_PROP_KEY] && opts[INIT_CLIENT_PROP_KEY].then) { + debug(`using INIT_CLIENT_PROP_KEY to add clients to the resolver`) + return opts[INIT_CLIENT_PROP_KEY] + .then(clients => injectNodeClient(resolver, clients)) + } + + return resolver +} + + +/** + * @return {array} list of injectors + */ +const getInjectors = () => { + return [ + injectWsProp, + injectUserdata, + injectSendFn, + injectOnFn, + injectClient + ] +} + +module.exports = { getInjectors } diff --git a/packages/ws-server-core/src/resolver/index.js b/packages/ws-server-core/src/resolver/index.js index 6715167f30670f9f758da044b892532c3d22d21a..78df5f6aef96ff748a6b191d90c2d1814e2eec15 100644 --- a/packages/ws-server-core/src/resolver/index.js +++ b/packages/ws-server-core/src/resolver/index.js @@ -1,3 +1,18 @@ // all these methods were in the share folder before // now all group under the resolver because they are all related to resolver +const { + validateInput, + deliverMsg, + handleError, + deliverIntercomMsg +} = require('./resolver-methods') +const { resolveSocketMethod } = require('./resolver-methods') + +module.exports = { + validateInput, + deliverMsg, + handleError, + deliverIntercomMsg, + resolveSocketMethod +} \ No newline at end of file diff --git a/packages/ws-server-core/src/resolver/resolve-socket-method.js b/packages/ws-server-core/src/resolver/resolve-socket-method.js index 31ed229aa4658d1b92a347e9b9863b665e258fcc..2a54ae15b4deca8cb76901feb2f96ad4daaa8a27 100644 --- a/packages/ws-server-core/src/resolver/resolve-socket-method.js +++ b/packages/ws-server-core/src/resolver/resolve-socket-method.js @@ -4,29 +4,39 @@ const { CACHE_STORE_PROP_KEY } = require('jsonql-constants') const { JsonqlResolverAppError } = require('jsonql-errors') -const { getResolver } = require('jsonql-resolver') +const { getCompleteSocketResolver } = require('jsonql-resolver') +const { getInjectors } = require('./get-injectors') const { getRainbowDebug } = require('../share/helpers') -const { addProperty } = require('./add-property') - const debug = getRainbowDebug('share:resolve-method') /** * similiar to the one in Koa-middleware without the ctx - * @param {function} deliverFn framework specific method + * @param {string} namespace connected namespace * @param {string} resolverName name to call + * @param {function} deliverFn framework specific method * @param {array} args arguments * @param {object} opts for search later * @param {object} ws the WebSocket instance * @param {object} [userdata=false] userdata * @return {promise} depends on the contract */ -const resolveSocketMethod = function(deliverFn, resolverName, args, opts, ws, userdata) { - debug('resolverSocketMethod') - const { contract } = opts - // @TODO add a cache method to store the constructed resolver - const fn = getResolver(resolverName, SOCKET_NAME, contract, opts) +const resolveSocketMethod = function( + namespace, + resolverName, + deliverFn, + args, // this is the clean value from validateAsync + opts, + ws, + userdata) { + // resolverName, opts, key = null, store = null + const key = [namespace, resolverName].join('-') + const store = opts[CACHE_STORE_PROP_KEY] + const actionFn = getCompleteSocketResolver(resolverName, opts, key, store) + + const injectorFns = getInjectors() + const args = [ deliverFn, resolverName, ws, userdata, opts ] - return addProperty(deliverFn, fn, resolverName, ws, userdata, opts) + return Promise.resolve(actionFn(injectorFns, args)) .then(resolver => { try { return Reflect.apply(resolver, null, args) diff --git a/packages/ws-server-core/src/share/resolver-methods.js b/packages/ws-server-core/src/resolver/resolver-methods.js similarity index 93% rename from packages/ws-server-core/src/share/resolver-methods.js rename to packages/ws-server-core/src/resolver/resolver-methods.js index 3109b8b31bfa089e4baf481164b645ce20378f90..1b8f7f020a71c3c2d0dfb62426dc811d23739254 100644 --- a/packages/ws-server-core/src/share/resolver-methods.js +++ b/packages/ws-server-core/src/resolver/resolver-methods.js @@ -2,7 +2,7 @@ // const { JsonqlValidationError } = require('jsonql-errors') const { packError } = require('jsonql-utils') const { isNotEmpty, validateAsync } = require('jsonql-params-validator') -const { getRainbowDebug, createWsReply } = require('./helpers') +const { getRainbowDebug, createWsReply } = require('../share/helpers') const { INTERCOM_RESOLVER_NAME, ACKNOWLEDGE_REPLY_TYPE, @@ -64,16 +64,17 @@ function handleError(deliverFn, resolverName, err) { /** * The final step to delivery the message - * @param {function} deliverFn framework specific method to delivery + * @param {function} deliverFn framework specific method to delivery * @param {string} resolverName that perform this method * @param {*} result from the resolver process - * @return {void} + * @param {string} type what type of message + * @return {promise} resolve the final result */ function deliverMsg(deliverFn, resolverName, result, type = ACKNOWLEDGE_REPLY_TYPE) { return Promise .resolve(result) - .then(result => handleResult(deliverFn, resolverName, result, type)) + .then(result => handleResult(deliverFn, resolverName, result, type)) } /** diff --git a/packages/ws-server-core/src/resolver/setup-on-method.js b/packages/ws-server-core/src/resolver/setup-on-method.js index d534482510f320b52873425eafadebfa9ee271c6..db884010e6829491996c608761620d88ce151436 100644 --- a/packages/ws-server-core/src/resolver/setup-on-method.js +++ b/packages/ws-server-core/src/resolver/setup-on-method.js @@ -1,7 +1,37 @@ // this is brand new resolver.on method to listen to the message +const { SEND_ON_FN_NAME, EMIT_REPLY_TYPE } = require('jsonql-constants') +const { objDefineProps } = require('jsonql-utils') +const { deliverMsg } = require('./resolver-methods') +const { nil, getRainbowDebug } = require('../share/helpers') +const debug = getRainbowDebug('create-send-method', 'x') - - +/** + * @TODO setup a new resolver.on method that can listen to reply message also + * This will serve as the new inter communication method as well + * when call with just a cb resolver.on(cb) it listen to the client + * when call with resolver.on(resolverName, cb) then it will become the inter communication tool + * @param {*} deliverFn + * @param {*} resolver + * @param {*} resolverName + * @return {function} resolver + */ function setupOnMethod(deliverFn, resolver, resolverName) { + return objDefineProps( + resolver, + SEND_ON_FN_NAME, + nil, + function sendHandler() { + // @NOTE we don't need to validate here, if we need to in the future, + // we should validate it against the return params + return function sendOnback(...args) { + + debug('onCallback', args) + + // deliverMsg(deliverFn, resolverName, args, EMIT_REPLY_TYPE) + } + } + ) +} + -} \ No newline at end of file +module.exports = { setupOnMethod } \ No newline at end of file diff --git a/packages/ws-server-core/src/resolver/setup-send-method.js b/packages/ws-server-core/src/resolver/setup-send-method.js index 82ff7cd200ae840c525ff0c8ef19738e03328c99..9d58e89721ca8d48edebe0635548547e7c828178 100644 --- a/packages/ws-server-core/src/resolver/setup-send-method.js +++ b/packages/ws-server-core/src/resolver/setup-send-method.js @@ -1,6 +1,6 @@ const { SEND_MSG_FN_NAME, EMIT_REPLY_TYPE } = require('jsonql-constants') const { objDefineProps } = require('jsonql-utils') -const { deliverMsg } = require('../share/resolver-methods') +const { deliverMsg } = require('./resolver-methods') const { nil, getRainbowDebug } = require('../share/helpers') const debug = getRainbowDebug('create-send-method', 'x') @@ -10,9 +10,10 @@ const debug = getRainbowDebug('create-send-method', 'x') * @param {function} deliveryFn framework specific to deliver message to client * @param {function} resolver the function itself * @param {string} resolverName the name of this resolver + * @param {object} opts configuration * @return {function} resolver with a `send` method property */ -const setupSendMethod = function(deliverFn, resolver, resolverName) { +const setupSendMethod = function(deliverFn, resolver, resolverName, opts) { return objDefineProps( resolver, SEND_MSG_FN_NAME,