Merge pull request #2065 from OrigenStudio/feature/router-hook-props

Add async hook to RouterHook and provide props as argument
This commit is contained in:
Sacha Greif 2018-12-16 22:52:00 +09:00 committed by GitHub
commit d51ae7591b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 28 deletions

View file

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

View file

@ -1,5 +1,5 @@
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';
class RouterHook extends PureComponent {
@ -8,15 +8,17 @@ class RouterHook extends PureComponent {
this.runOnUpdateCallback(props);
}
componentWillReceiveProps(nextProps) {
this.runOnUpdateCallback(nextProps);
componentDidUpdate(nextProps) {
this.runOnUpdateCallback(this.props, nextProps);
}
runOnUpdateCallback = props => {
runOnUpdateCallback = (props, nextProps = {}) => {
const { currentRoute, client } = props;
// 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
runCallbacks('router.onUpdate', {}, currentRoute, client.store, client);
runCallbacksAsync('router.onUpdate.async', props, nextProps);
};
render() {

View file

@ -37,7 +37,7 @@ export const addIdentifyFunction = f => {
};
export const addPageFunction = f => {
const f2 = (empty, route) => f(route);
const f2 = ({ currentRoute }) => f(currentRoute);
// rename f2 to same name as f
// 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;
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 { Utils } from './utils';
@ -169,17 +171,24 @@ export const runCallbacksAsync = function () {
const callbacks = Array.isArray(hook) ? hook : Callbacks[hook];
if (Meteor.isServer && typeof callbacks !== 'undefined' && !!callbacks.length) {
// use defer to avoid holding up client
Meteor.defer(function () {
// run all post submit server callbacks on post object successively
callbacks.forEach(function (callback) {
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`);
callback.apply(this, args);
});
});
return callback.apply(this, args);
}),
);
if (Meteor.isServer) {
// TODO: find out if we can safely use promises on the server, too - https://github.com/VulcanJS/Vulcan/pull/2065
return new Promise(async (resolve, reject) => {
Meteor.defer(function() {
_runCallbacksAsync().then(resolve).catch(reject);
});
});
}
return _runCallbacksAsync();
}
return [];
};