import Event from '../../events/event.js';

function createMethod(methodName) {
  return function(selector, options) {
    let Class = this;
    let Collection = Class.getCollection();

    // Get selector and options from the arguments.
    selector = Mongo.Collection._rewriteSelector(selector || {});
    options = options || {};

    // Modify selector and options using the "beforeFind" event handlers.
    Class.dispatchEvent(new Event('beforeFind', {
      selector: selector,
      options: options
    }));

    // If it's an inherited class, then get only documents being instances of
    // the subclass.
    let typeField = Class.getTypeField();
    if (typeField) {
      selector[typeField] = Class.getName();
    }

    // Get default transform function if none is provided.
    if (options.transform === undefined) {
      options.transform = Class.getTransform();
    }

    // Execute the original method.
    let result = Collection[methodName](selector, options);

    // Modify a query result using the "afterFind" event handlers.
    Class.dispatchEvent(new Event('afterFind', {
      selector: selector,
      options: options,
      result: result
    }));

    return result;
  };
};

const find = createMethod('find');
const findOne = createMethod('findOne');

export { find, findOne };
@@ -393,32 +393,68 @@
 event handlers.%0A
+    if (!options.disableEvents) %7B%0A  
     Class.dispat
@@ -489,33 +489,25 @@
 d', %7B%0A      
-selector:
+ 
  selector,%0A 
@@ -507,32 +507,25 @@
 ctor,%0A      
-options:
+ 
  options%0A   
@@ -521,28 +521,36 @@
 options%0A    
+  
 %7D));
+%0A    %7D
 %0A%0A    // If 
@@ -1070,24 +1070,60 @@
 t handlers.%0A
+    if (!options.disableEvents) %7B%0A  
     Class.di
@@ -1169,17 +1169,9 @@
     
-selector:
+ 
  sel
@@ -1187,16 +1187,9 @@
     
-options:
+ 
  opt
@@ -1204,15 +1204,9 @@
     
-result:
+ 
  res
@@ -1213,20 +1213,28 @@
 ult%0A    
+  
 %7D));
+%0A    %7D
 %0A%0A    re

Add "disableEvents" find options