Skip to content

Commit 754a3d5

Browse files
committed
simplify
1 parent 00c8b06 commit 754a3d5

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

src/dataLoaders.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default class DataLoaders {
106106

107107
/**
108108
* Create DataLoader for events in dynamic collections `events:<projectId>` stored in the events DB
109+
*
109110
* @param eventsDb - MongoDB connection to the events database
110111
* @param projectId - project id used to pick a dynamic collection
111112
*/

src/models/eventsFactory.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class EventsFactory extends Factory {
9696
}
9797

9898
this.projectId = projectId;
99-
this.eventById = createProjectEventsByIdLoader(mongo.databases.events, this.projectId);
99+
this.eventsDataLoader = createProjectEventsByIdLoader(mongo.databases.events, this.projectId);
100100
}
101101

102102
/**
@@ -159,7 +159,7 @@ class EventsFactory extends Factory {
159159
* @returns {Event|null}
160160
*/
161161
async findById(id) {
162-
const searchResult = await this.eventById.load(id);
162+
const searchResult = await this.eventsDataLoader.load(id);
163163

164164
const event = searchResult ? new Event(searchResult) : null;
165165

@@ -603,7 +603,7 @@ class EventsFactory extends Factory {
603603
* If originalEventId equals repetitionId than user wants to get first repetition which is original event
604604
*/
605605
if (repetitionId === originalEventId) {
606-
const originalEvent = await this.eventById.load(originalEventId);
606+
const originalEvent = await this.eventsDataLoader.load(originalEventId);
607607

608608
if (!originalEvent) {
609609
return null;
@@ -627,7 +627,7 @@ class EventsFactory extends Factory {
627627
_id: ObjectID(repetitionId),
628628
});
629629

630-
const originalEvent = await this.eventById.load(originalEventId);
630+
const originalEvent = await this.eventsDataLoader.load(originalEventId);
631631

632632
if (!originalEvent) {
633633
return null;
@@ -712,7 +712,7 @@ class EventsFactory extends Factory {
712712
async toggleEventMark(eventId, mark) {
713713
const collection = this.getCollection(this.TYPES.EVENTS);
714714

715-
const event = await this.eventsDataLoader.eventById.load(eventId);
715+
const event = await this.eventsDataLoader.load(eventId);
716716

717717
if (!event) {
718718
throw new Error(`Event not found for eventId: ${eventId}`);

src/resolvers/event.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ const sendPersonalNotification = require('../utils/personalNotifications').defau
55
/**
66
* Returns a per-request, per-project EventsFactory instance
77
* Uses context.eventsFactoryCache to memoize by projectId
8-
* @param {any} context - GraphQL resolver context
9-
* @param {string|ObjectID} ctorProjectId - value passed to EventsFactory constructor
10-
* @param {string|ObjectID} keyProjectId - value used as cache key (string id is preferred)
11-
* @returns {EventsFactory}
8+
*
9+
* @param {ResolverContextBase} context - resolver context
10+
* @param {string} projectId - project id to get EventsFactory instance for
11+
* @returns {EventsFactory} - EventsFactory instance bound to a specific project object
1212
*/
13-
function getEventsFactoryForProjectId(context, ctorProjectId, keyProjectId) {
13+
function getEventsFactoryForProjectId(context, projectId) {
1414
const cache = context.eventsFactoryCache || (context.eventsFactoryCache = new Map());
15-
const cacheKey = (keyProjectId || ctorProjectId).toString();
15+
const cacheKey = projectId.toString();
1616

1717
if (!cache.has(cacheKey)) {
18-
cache.set(cacheKey, new EventsFactory(ctorProjectId));
18+
cache.set(cacheKey, new EventsFactory(projectId));
1919
}
2020

2121
return cache.get(cacheKey);
@@ -49,7 +49,7 @@ module.exports = {
4949
* @return {RepetitionsPortion}
5050
*/
5151
async repetitionsPortion({ projectId, originalEventId }, { limit, cursor }, context) {
52-
const factory = getEventsFactoryForProjectId(context, projectId, projectId);
52+
const factory = getEventsFactoryForProjectId(context, projectId);
5353

5454
return factory.getEventRepetitions(originalEventId, limit, cursor);
5555
},
@@ -104,7 +104,7 @@ module.exports = {
104104
* @returns {Promise<ProjectChartItem[]>}
105105
*/
106106
async chartData({ projectId, groupHash }, { days, timezoneOffset }, context) {
107-
const factory = getEventsFactoryForProjectId(context, new ObjectID(projectId), projectId);
107+
const factory = getEventsFactoryForProjectId(context, projectId);
108108

109109
return factory.findChartData(days, timezoneOffset, groupHash);
110110
},
@@ -117,7 +117,7 @@ module.exports = {
117117
* @returns {Promise<Release>}
118118
*/
119119
async release({ projectId, id: eventId }, _args, context) {
120-
const factory = getEventsFactoryForProjectId(context, new ObjectID(projectId), projectId);
120+
const factory = getEventsFactoryForProjectId(context, projectId);
121121
const release = await factory.getEventRelease(eventId);
122122

123123
return release;
@@ -134,7 +134,7 @@ module.exports = {
134134
* @return {Promise<boolean>}
135135
*/
136136
async visitEvent(_obj, { projectId, eventId }, { user, ...context }) {
137-
const factory = getEventsFactoryForProjectId(context, projectId, projectId);
137+
const factory = getEventsFactoryForProjectId(context, projectId);
138138

139139
const { result } = await factory.visitEvent(eventId, user.id);
140140

@@ -151,7 +151,7 @@ module.exports = {
151151
* @return {Promise<boolean>}
152152
*/
153153
async toggleEventMark(_obj, { project, eventId, mark }, context) {
154-
const factory = getEventsFactoryForProjectId(context, project, project);
154+
const factory = getEventsFactoryForProjectId(context, project);
155155

156156
const { result } = await factory.toggleEventMark(eventId, mark);
157157

@@ -176,7 +176,7 @@ module.exports = {
176176
*/
177177
async updateAssignee(_obj, { input }, { factories, user, ...context }) {
178178
const { projectId, eventId, assignee } = input;
179-
const factory = getEventsFactoryForProjectId(context, projectId, projectId);
179+
const factory = getEventsFactoryForProjectId(context, projectId);
180180

181181
const userExists = await factories.usersFactory.findById(assignee);
182182

@@ -227,7 +227,7 @@ module.exports = {
227227
*/
228228
async removeAssignee(_obj, { input }, context) {
229229
const { projectId, eventId } = input;
230-
const factory = getEventsFactoryForProjectId(context, projectId, projectId);
230+
const factory = getEventsFactoryForProjectId(context, projectId);
231231

232232
const { result } = await factory.updateAssignee(eventId, '');
233233

src/resolvers/project.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,28 @@ const REPETITIONS_GROUP_HASH_INDEX_NAME = 'groupHash_hashed';
1414
const REPETITIONS_USER_ID_INDEX_NAME = 'userId';
1515
const MAX_SEARCH_QUERY_LENGTH = 50;
1616

17-
/**
18-
* Symbol key for memoizing per-project EventsFactory instance
19-
*/
20-
const PROJECT_EVENTS_FACTORY = Symbol('PROJECT_EVENTS_FACTORY');
21-
2217
/**
2318
* Returns a singleton EventsFactory instance bound to a specific project object.
24-
* Also mirrors the instance into request-scoped cache to share across nested resolvers.
25-
* @param {ProjectDBScheme|Object} project
26-
* @param {ResolverContextBase} context
27-
* @returns {EventsFactory}
19+
* Uses request-scoped cache to share across nested resolvers.
20+
*
21+
* @param {ProjectDBScheme|Object} project - project instance to make a instance of EventsFactory
22+
* @param {ResolverContextBase} context - resolver context
23+
* @returns {EventsFactory} - EventsFactory instance bound to a specific project object
2824
*/
2925
function getEventsFactoryForProject(project, context) {
30-
if (!project[PROJECT_EVENTS_FACTORY]) {
31-
const factory = new EventsFactory(project._id);
26+
const cache = context && context.eventsFactoryCache;
27+
const key = project._id.toString();
3228

33-
Object.defineProperty(project, PROJECT_EVENTS_FACTORY, {
34-
value: factory,
35-
enumerable: false,
36-
configurable: false,
37-
});
38-
39-
if (context && context.eventsFactoryCache) {
40-
context.eventsFactoryCache.set(project._id.toString(), factory);
29+
if (cache) {
30+
if (!cache.has(key)) {
31+
cache.set(key, new EventsFactory(project._id));
4132
}
33+
34+
return cache.get(key);
4235
}
4336

44-
return project[PROJECT_EVENTS_FACTORY];
37+
// Fallback (shouldn't happen in normal resolver flow): return a fresh instance
38+
return new EventsFactory(project._id);
4539
}
4640

4741
/**

0 commit comments

Comments
 (0)