-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (46 loc) · 1.68 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import redis from 'redis';
import { Meteor } from 'meteor/meteor';
import { Context } from './lib/context';
FastMethods = {
fastMethodsContext: new Meteor.EnvironmentVariable(),
};
FastMethods.redisClient = redis.createClient(Meteor.settings.redisOplog.redis);
import { EJSON } from 'meteor/ejson';
import {
CHANGES_METHOD_NAME,
CHANGES_NAMESPACE,
reduceToMethodSubscriptionFormat,
} from './lib/helpers';
import { Vent } from 'meteor/cultofcoders:redis-oplog';
import { onPageLoad } from 'meteor/server-render';
import './lib/mongo-collections-map';
export { CachedValidatedMethod } from './lib/cached-method';
export function preloadData(cursorsFunction) {
onPageLoad(sink => {
const loginToken = sink.request.cookies['meteor_login_token'];
const context = new Context(loginToken, { headers: sink.request.headers });
FastMethods.fastMethodsContext.withValue(context, function () {
const cursors = cursorsFunction.call(context, sink);
cursors.forEach(cursor => {
const shouldEmitEvents = !cursor._cursorDescription.options
?.shouldNotEmitEvents;
Mongo.Collection.__getCollectionByName(
cursor._getCollectionName()
).shouldEmitEvents = shouldEmitEvents;
});
sink.appendToBody(
`<script>
var PRELOADED_DATA_METEOR_FAST_METHODS = JSON.parse(
unescape("${escape(
EJSON.stringify(reduceToMethodSubscriptionFormat(cursors))
)}")
);
var METEOR_FAST_METHODS_INITIAL_LOGGED_IN = ${!!context.userId};
</script>`
);
});
});
}
Vent.publish(CHANGES_METHOD_NAME, function publication({ namespace }) {
this.on(CHANGES_NAMESPACE(namespace), doc => doc);
});