Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move rocketchat promises #13039

Merged
merged 28 commits into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
eb36f8b
Move rocketchat settings to specific package
MarcosSpessatto Dec 21, 2018
4428a97
WIP: Move models from rocketchat-lib to a specific package (server)
MarcosSpessatto Dec 24, 2018
157edc0
Move function from rocketchat:lib to rocketchat:utils to use it in ro…
MarcosSpessatto Dec 24, 2018
7b65a21
Merge branch 'develop' into globals/remove-rocketchat-settings
MarcosSpessatto Dec 24, 2018
4a26cba
Merge branch 'globals/remove-rocketchat-settings' into globals/move-r…
MarcosSpessatto Dec 24, 2018
24cfb4f
Move client models from rocketchat:lib to rocketchat:models
MarcosSpessatto Dec 26, 2018
0683f57
Fix lint
MarcosSpessatto Dec 26, 2018
b26e192
Move rocketchat.info from lib to utils
MarcosSpessatto Dec 27, 2018
8eeac90
Remove directly dependency between lib and migrations
MarcosSpessatto Dec 27, 2018
7080105
Move statistics Model to rocketchat:models
MarcosSpessatto Dec 27, 2018
3d2395c
Create rocketchat:metrics to be able to depacking rocketchat callbacks
MarcosSpessatto Dec 27, 2018
289c478
Move callbacks to specific package
MarcosSpessatto Dec 27, 2018
ccba459
Remove unused dependency
MarcosSpessatto Dec 27, 2018
869c15d
Move rocketchat-notifications to a specific package
MarcosSpessatto Dec 27, 2018
af817df
Move rocketchat-promises to a specific package
MarcosSpessatto Dec 28, 2018
4f1947d
Merge branch 'develop' into globals/remove-rocketchat-settings
MarcosSpessatto Jan 2, 2019
52e95cc
Merge branch 'globals/remove-rocketchat-settings' into globals/move-r…
MarcosSpessatto Jan 2, 2019
dfbcf6b
Merge branch 'globals/move-rocketchat-models' into globals/move-rocke…
MarcosSpessatto Jan 3, 2019
e60d248
Merge branch 'globals/move-rocketchat-metrics' into globals/move-rock…
MarcosSpessatto Jan 3, 2019
2c34342
Merge branch 'globals/move-rocketchat-callbacks' into globals/move-ro…
MarcosSpessatto Jan 3, 2019
e71d2b5
Merge branch 'globals/move-rocketchat-notifications' into globals/mov…
MarcosSpessatto Jan 3, 2019
b40ea2d
Merge branch 'develop' into globals/move-rocketchat-callbacks
MarcosSpessatto Jan 10, 2019
ac03e2d
Merge branch 'develop' into globals/move-rocketchat-callbacks
MarcosSpessatto Jan 10, 2019
483650d
Merge branch 'globals/move-rocketchat-callbacks' into globals/move-ro…
MarcosSpessatto Jan 10, 2019
63bb0a4
Merge branch 'globals/move-rocketchat-notifications' into globals/mov…
MarcosSpessatto Jan 10, 2019
db361ac
Merge branch 'develop' into globals/move-rocketchat-notifications
rodrigok Jan 10, 2019
30df39b
Merge branch 'globals/move-rocketchat-notifications' into globals/mov…
MarcosSpessatto Jan 10, 2019
b4c055f
Merge branch 'develop' into globals/move-rocketchat-promises
rodrigok Jan 10, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .meteor/packages
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,4 @@ rocketchat:models
rocketchat:metrics
rocketchat:callbacks
rocketchat:notifications
rocketchat:promises
1 change: 1 addition & 0 deletions .meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 2 additions & 83 deletions packages/rocketchat-lib/lib/promises.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-promises/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { promises } from '../lib/promises';

export {
promises,
};
84 changes: 84 additions & 0 deletions packages/rocketchat-promises/lib/promises.js
Original file line number Diff line number Diff line change
@@ -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)));
};
14 changes: 14 additions & 0 deletions packages/rocketchat-promises/package.js
Original file line number Diff line number Diff line change
@@ -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');
});
5 changes: 5 additions & 0 deletions packages/rocketchat-promises/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { promises } from '../lib/promises';

export {
promises,
};