Create new events-internal package to hold internal Mongo event tracking code

This commit is contained in:
SachaG 2017-12-17 20:59:26 +09:00
parent 4d55c2f788
commit 104b630448
13 changed files with 64 additions and 18 deletions

View file

@ -5,18 +5,20 @@ import { withApollo } from 'react-apollo';
class RouterHook extends PureComponent {
constructor(props) {
super(props);
const { currentRoute, client } = props;
console.log(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);
this.runOnUpdateCallback(props);
}
componentWillReceiveProps(nextProps) {
const { currentRoute, client } = nextProps;
this.runOnUpdateCallback(nextProps);
}
runOnUpdateCallback = props => {
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);
}
};
render() {
return null;
}

View file

@ -0,0 +1 @@
Vulcan events package, used internally.

View file

@ -0,0 +1,12 @@
import { addTrackFunction } from 'meteor/vulcan:events';
import { ApolloClient } from 'apollo-client';
import { getRenderContext } from 'meteor/vulcan:lib';
import gql from 'graphql-tag';
function trackInternal() {
const { apolloClient, store } = getRenderContext();
console.log(apolloClient)
apolloClient.query({ query: gql`{ hello }` }).then(console.log);
}
addTrackFunction(trackInternal);

View file

@ -0,0 +1,3 @@
export * from '../modules/index.js';
import './internal.js';

View file

@ -0,0 +1 @@
export * from './collection.js';

View file

@ -0,0 +1 @@
export * from '../modules/index.js';

View file

@ -0,0 +1,19 @@
Package.describe({
name: "vulcan:events-internal",
summary: "Vulcan internal event tracking package",
version: '1.8.0',
git: "https://github.com/VulcanJS/Vulcan.git"
});
Package.onUse(function(api) {
api.versionsFrom('METEOR@1.5.2');
api.use([
'vulcan:core@1.8.0',
]);
api.mainModule("lib/server/main.js", "server");
api.mainModule('lib/client/main.js', 'client');
});

View file

@ -4,14 +4,14 @@ export const initFunctions = [];
export const trackFunctions = [];
export const addInitFunction = func => {
initFunctions.push(func);
export const addInitFunction = f => {
initFunctions.push(f);
// execute init function as soon as possible
func();
f();
};
export const addTrackFunction = func => {
trackFunctions.push(func);
export const addTrackFunction = f => {
trackFunctions.push(f);
};
export const track = (eventName, eventProperties) => {
@ -20,10 +20,18 @@ export const track = (eventName, eventProperties) => {
});
}
export const addIdentifyFunction = func => {
addCallback('events.identify', func);
export const addIdentifyFunction = f => {
addCallback('events.identify', f);
};
export const addPageFunction = func => {
addCallback('router.onUpdate', (empty, route) => func(route));
export const addPageFunction = f => {
const f2 = (empty, route) => f(route);
// rename f2 to same name as f
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
const descriptor = Object.create(null); // no inherited properties
descriptor.value = f.name;
Object.defineProperty(f2, 'name', descriptor)
addCallback('router.onUpdate', f2);
};

View file

@ -1,2 +1 @@
export * from './collection.js';
export * from './events';

View file

@ -87,7 +87,7 @@ export const runCallbacks = function () {
if (typeof result === 'undefined') {
// if result of current iteration is undefined, don't pass it on
console.log(`// Warning: Sync callback [${callback.name}] in hook [${hook}] didn't return a result!`)
debug(`// Warning: Sync callback [${callback.name}] in hook [${hook}] didn't return a result!`)
return accumulator
} else {
return result;