Merge branch 'master' into devel

This commit is contained in:
Apollinaire 2018-12-19 16:10:54 +01:00
commit 1e7ae073a9
4 changed files with 36 additions and 28 deletions

View file

@ -1,21 +1,18 @@
import { addCallback, getActions } from 'meteor/vulcan:lib'; import { addCallback, getActions } from 'meteor/vulcan:lib';
/* /*
Core callbacks Core callbacks
*/ */
/** /**
* @summary Clear flash messages marked as seen when the route changes * @summary Clear flash messages marked as seen when the route changes
* @param {Object} Item needed by `runCallbacks` to iterate on, unused here * @param {Object} props
* @param {Object} Redux store reference instantiated on the current connected client * @param {Object} props.client Apollo Client reference instantiated on the current connected client
* @param {Object} Apollo Client reference instantiated on the current connected client
*/ */
function RouterClearMessages(unusedItem, nextRoute, store, apolloClient) { function RouterClearMessages({ client }) {
store.dispatch(getActions().messages.clearSeen()); client.store.dispatch(getActions().messages.clearSeen());
return unusedItem;
} }
addCallback('router.onUpdate', RouterClearMessages); addCallback('router.onUpdate.async', RouterClearMessages);

View file

@ -1,5 +1,5 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { registerComponent, runCallbacks } from 'meteor/vulcan:lib'; import { registerComponent, runCallbacks, runCallbacksAsync } from 'meteor/vulcan:lib';
import { withApollo } from 'react-apollo'; import { withApollo } from 'react-apollo';
class RouterHook extends PureComponent { class RouterHook extends PureComponent {
@ -8,15 +8,17 @@ class RouterHook extends PureComponent {
this.runOnUpdateCallback(props); this.runOnUpdateCallback(props);
} }
componentWillReceiveProps(nextProps) { componentDidUpdate(nextProps) {
this.runOnUpdateCallback(nextProps); this.runOnUpdateCallback(this.props, nextProps);
} }
runOnUpdateCallback = props => { runOnUpdateCallback = (props, nextProps = {}) => {
const { currentRoute, client } = props; const { currentRoute, client } = props;
// the first argument is an item to iterate on, needed by vulcan:lib/callbacks // the first argument is an item to iterate on, needed by vulcan:lib/callbacks
// note: this item is not used in this specific callback: router.onUpdate // note: this item is not used in this specific callback: router.onUpdate
runCallbacks('router.onUpdate', {}, currentRoute, client.store, client); runCallbacks('router.onUpdate', {}, currentRoute, client.store, client);
runCallbacksAsync('router.onUpdate.async', props, nextProps);
}; };
render() { render() {

View file

@ -37,7 +37,7 @@ export const addIdentifyFunction = f => {
}; };
export const addPageFunction = f => { export const addPageFunction = f => {
const f2 = (empty, route) => f(route); const f2 = ({ currentRoute }) => f(currentRoute);
// rename f2 to same name as f // rename f2 to same name as f
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
@ -45,5 +45,5 @@ export const addPageFunction = f => {
descriptor.value = f.name; descriptor.value = f.name;
Object.defineProperty(f2, 'name', descriptor); Object.defineProperty(f2, 'name', descriptor);
addCallback('router.onUpdate', f2); addCallback('router.onUpdate.async', f2);
}; };

View file

@ -1,3 +1,5 @@
import { Meteor } from 'meteor/meteor';
import { debug } from './debug.js'; import { debug } from './debug.js';
import { Utils } from './utils'; import { Utils } from './utils';
@ -182,17 +184,24 @@ export const runCallbacksAsync = function () {
const callbacks = Array.isArray(hook) ? hook : Callbacks[hook]; const callbacks = Array.isArray(hook) ? hook : Callbacks[hook];
if (Meteor.isServer && typeof callbacks !== 'undefined' && !!callbacks.length) { if (typeof callbacks !== 'undefined' && !!callbacks.length) {
const _runCallbacksAsync = () =>
Promise.all(
callbacks.map(callback => {
debug(`\x1b[32m>> Running async callback [${callback.name}] on hook [${hook}]\x1b[0m`);
return callback.apply(this, args);
}),
);
// use defer to avoid holding up client if (Meteor.isServer) {
Meteor.defer(function () { // TODO: find out if we can safely use promises on the server, too - https://github.com/VulcanJS/Vulcan/pull/2065
// run all post submit server callbacks on post object successively return new Promise(async (resolve, reject) => {
callbacks.forEach(function (callback) { Meteor.defer(function() {
debug(`\x1b[32m>> Running async callback [${callback.name}] on hook [${hook}]\x1b[0m`); _runCallbacksAsync().then(resolve).catch(reject);
callback.apply(this, args); });
}); });
}); }
return _runCallbacksAsync();
} }
return [];
}; };