From 8c3f9916349978e5fa30d911a4e22a81027b6844 Mon Sep 17 00:00:00 2001 From: Marcos Spessatto Defendi Date: Thu, 10 Jan 2019 20:09:27 -0200 Subject: [PATCH] Move rocketchat promises (#13039) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * Merge branch 'develop' into globals/move-rocketchat-callbacks --- .meteor/packages | 1 + .meteor/versions | 1 + packages/rocketchat-lib/lib/promises.js | 85 +------------------- packages/rocketchat-lib/package.js | 1 + packages/rocketchat-promises/client/index.js | 5 ++ packages/rocketchat-promises/lib/promises.js | 84 +++++++++++++++++++ packages/rocketchat-promises/package.js | 14 ++++ packages/rocketchat-promises/server/index.js | 5 ++ 8 files changed, 113 insertions(+), 83 deletions(-) create mode 100644 packages/rocketchat-promises/client/index.js create mode 100644 packages/rocketchat-promises/lib/promises.js create mode 100644 packages/rocketchat-promises/package.js create mode 100644 packages/rocketchat-promises/server/index.js diff --git a/.meteor/packages b/.meteor/packages index 5be1056f7b58..6c4d4c801a7f 100644 --- a/.meteor/packages +++ b/.meteor/packages @@ -207,3 +207,4 @@ rocketchat:models rocketchat:metrics rocketchat:callbacks rocketchat:notifications +rocketchat:promises diff --git a/.meteor/versions b/.meteor/versions index fc28e8eeed36..9719721101ae 100644 --- a/.meteor/versions +++ b/.meteor/versions @@ -211,6 +211,7 @@ rocketchat:oauth2-server@2.0.0 rocketchat:oauth2-server-config@1.0.0 rocketchat:oembed@0.0.1 rocketchat:otr@0.0.1 +rocketchat:promises@0.0.1 rocketchat:push@3.3.1 rocketchat:push-notifications@0.0.1 rocketchat:reactions@0.0.1 diff --git a/packages/rocketchat-lib/lib/promises.js b/packages/rocketchat-lib/lib/promises.js index a306ccceeef6..875676508810 100644 --- a/packages/rocketchat-lib/lib/promises.js +++ b/packages/rocketchat-lib/lib/promises.js @@ -1,84 +1,3 @@ -import { Meteor } from 'meteor/meteor'; -import { Random } from 'meteor/random'; -import _ from 'underscore'; +import { promises } from 'meteor/rocketchat:promises'; -/* -* Callback hooks provide an easy way to add extra steps to common operations. -* @namespace RocketChat.promises -*/ - -RocketChat.promises = {}; - - -/* -* Callback priorities -*/ - -RocketChat.promises.priority = { - HIGH: -1000, - MEDIUM: 0, - LOW: 1000, -}; - -const getHook = (hookName) => RocketChat.promises[hookName] || []; - -/* -* Add a callback function to a hook -* @param {String} hook - The name of the hook -* @param {Function} callback - The callback function -*/ - -RocketChat.promises.add = function(hook, callback, p = RocketChat.promises.priority.MEDIUM, id) { - callback.priority = _.isNumber(p) ? p : RocketChat.promises.priority.MEDIUM; - callback.id = id || Random.id(); - RocketChat.promises[hook] = getHook(hook); - if (RocketChat.promises[hook].find((cb) => cb.id === callback.id)) { - return; - } - RocketChat.promises[hook].push(callback); - RocketChat.promises[hook] = _.sortBy(RocketChat.promises[hook], (callback) => callback.priority || RocketChat.promises.priority.MEDIUM); -}; - - -/* -* Remove a callback from a hook -* @param {string} hook - The name of the hook -* @param {string} id - The callback's id -*/ - -RocketChat.promises.remove = function(hook, id) { - RocketChat.promises[hook] = getHook(hook).filter((callback) => callback.id !== id); -}; - - -/* -* Successively run all of a hook's callbacks on an item -* @param {String} hook - The name of the hook -* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks -* @param {Object} [constant] - An optional constant that will be passed along to each callback -* @returns {Object} Returns the item after it's been through all the callbacks for this hook -*/ - -RocketChat.promises.run = function(hook, item, constant) { - const callbacks = RocketChat.promises[hook]; - if (callbacks == null || callbacks.length === 0) { - return Promise.resolve(item); - } - return callbacks.reduce((previousPromise, callback) => previousPromise.then((result) => callback(result, constant)), Promise.resolve(item)); -}; - - -/* -* Successively run all of a hook's callbacks on an item, in async mode (only works on server) -* @param {String} hook - The name of the hook -* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks -* @param {Object} [constant] - An optional constant that will be passed along to each callback -*/ - -RocketChat.promises.runAsync = function(hook, item, constant) { - const callbacks = RocketChat.promises[hook]; - if (!Meteor.isServer || callbacks == null || callbacks.length === 0) { - return item; - } - Meteor.defer(() => callbacks.forEach((callback) => callback(item, constant))); -}; +RocketChat.promises = promises; diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index e5a2fcaeb2d5..6100d0a41b9f 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -30,6 +30,7 @@ Package.onUse(function(api) { api.use('rocketchat:metrics'); api.use('rocketchat:callbacks'); api.use('rocketchat:notifications'); + api.use('rocketchat:promises'); api.use('rocketchat:accounts'); api.use('modules'); api.use('rocketchat:i18n'); diff --git a/packages/rocketchat-promises/client/index.js b/packages/rocketchat-promises/client/index.js new file mode 100644 index 000000000000..999b1f62bc34 --- /dev/null +++ b/packages/rocketchat-promises/client/index.js @@ -0,0 +1,5 @@ +import { promises } from '../lib/promises'; + +export { + promises, +}; diff --git a/packages/rocketchat-promises/lib/promises.js b/packages/rocketchat-promises/lib/promises.js new file mode 100644 index 000000000000..62ad6b51af37 --- /dev/null +++ b/packages/rocketchat-promises/lib/promises.js @@ -0,0 +1,84 @@ +import { Meteor } from 'meteor/meteor'; +import { Random } from 'meteor/random'; +import _ from 'underscore'; + +/* +* Callback hooks provide an easy way to add extra steps to common operations. +* @namespace RocketChat.promises +*/ + +export const promises = {}; + + +/* +* Callback priorities +*/ + +promises.priority = { + HIGH: -1000, + MEDIUM: 0, + LOW: 1000, +}; + +const getHook = (hookName) => promises[hookName] || []; + +/* +* Add a callback function to a hook +* @param {String} hook - The name of the hook +* @param {Function} callback - The callback function +*/ + +promises.add = function(hook, callback, p = promises.priority.MEDIUM, id) { + callback.priority = _.isNumber(p) ? p : promises.priority.MEDIUM; + callback.id = id || Random.id(); + promises[hook] = getHook(hook); + if (promises[hook].find((cb) => cb.id === callback.id)) { + return; + } + promises[hook].push(callback); + promises[hook] = _.sortBy(promises[hook], (callback) => callback.priority || promises.priority.MEDIUM); +}; + + +/* +* Remove a callback from a hook +* @param {string} hook - The name of the hook +* @param {string} id - The callback's id +*/ + +promises.remove = function(hook, id) { + promises[hook] = getHook(hook).filter((callback) => callback.id !== id); +}; + + +/* +* Successively run all of a hook's callbacks on an item +* @param {String} hook - The name of the hook +* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks +* @param {Object} [constant] - An optional constant that will be passed along to each callback +* @returns {Object} Returns the item after it's been through all the callbacks for this hook +*/ + +promises.run = function(hook, item, constant) { + const callbacks = promises[hook]; + if (callbacks == null || callbacks.length === 0) { + return Promise.resolve(item); + } + return callbacks.reduce((previousPromise, callback) => previousPromise.then((result) => callback(result, constant)), Promise.resolve(item)); +}; + + +/* +* Successively run all of a hook's callbacks on an item, in async mode (only works on server) +* @param {String} hook - The name of the hook +* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks +* @param {Object} [constant] - An optional constant that will be passed along to each callback +*/ + +promises.runAsync = function(hook, item, constant) { + const callbacks = promises[hook]; + if (!Meteor.isServer || callbacks == null || callbacks.length === 0) { + return item; + } + Meteor.defer(() => callbacks.forEach((callback) => callback(item, constant))); +}; diff --git a/packages/rocketchat-promises/package.js b/packages/rocketchat-promises/package.js new file mode 100644 index 000000000000..98a03be8c724 --- /dev/null +++ b/packages/rocketchat-promises/package.js @@ -0,0 +1,14 @@ +Package.describe({ + name: 'rocketchat:promises', + version: '0.0.1', + summary: 'Rocketchat Promises', + git: '', +}); + +Package.onUse(function(api) { + api.use([ + 'ecmascript', + ]); + api.mainModule('client/index.js', 'client'); + api.mainModule('server/index.js', 'server'); +}); diff --git a/packages/rocketchat-promises/server/index.js b/packages/rocketchat-promises/server/index.js new file mode 100644 index 000000000000..999b1f62bc34 --- /dev/null +++ b/packages/rocketchat-promises/server/index.js @@ -0,0 +1,5 @@ +import { promises } from '../lib/promises'; + +export { + promises, +};