From c697682865e993808fa7e1a15a9cef5129e1e499 Mon Sep 17 00:00:00 2001 From: Benjamin Pannell Date: Tue, 17 Nov 2015 08:33:36 +0200 Subject: [PATCH] docs: Added some comments regarding promise performance under async conditions --- dist/lib/ModelHandlers.js.map | 2 +- lib/ModelHandlers.ts | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/dist/lib/ModelHandlers.js.map b/dist/lib/ModelHandlers.js.map index f71b95b..efd3de1 100644 --- a/dist/lib/ModelHandlers.js.map +++ b/dist/lib/ModelHandlers.js.map @@ -1 +1 @@ -{"version":3,"sources":["lib/ModelHandlers.ts"],"names":["ModelHandlers","ModelHandlers.constructor","ModelHandlers.documentReceived","ModelHandlers.creatingDocuments","ModelHandlers.savingDocument"],"mappings":"AAOA,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAE7B,IAAO,QAAQ,WAAW,UAAU,CAAC,CAAC;AAEtC;;;;;;;GAOG;AACH;IACIA,uBAAmBA,KAAkCA;QAAlCC,UAAKA,GAALA,KAAKA,CAA6BA;IAErDA,CAACA;IAEDD,wCAAgBA,GAAhBA,UAA0BA,UAAeA,EACrCA,MAAiBA,EACjBA,OAA+EA,EAC/EA,OAAuCA;QAH3CE,iBA6BCA;QA1BGA,uBAAuCA,GAAvCA,YAAuCA;QACvCA,CAACA,CAACA,QAAQA,CAACA,OAAOA,EAAEA;YAChBA,KAAKA,EAAEA,IAAIA;YACXA,OAAOA,EAAEA,KAAKA;SACjBA,CAACA,CAACA;QAEHA,MAAMA,CAACA,QAAQA,CAACA,OAAOA,CAACA,MAAMA,CAACA,CAACA,IAAIA,CAACA,UAACA,MAAWA;YAC7CA,MAAMA,CAAoBA,QAAQA;iBAC7BA,OAAOA,CAACA,KAAIA,CAACA,KAAKA,CAACA,KAAKA,CAACA,WAAWA,IAAIA,KAAIA,CAACA,KAAKA,CAACA,KAAKA,CAACA,WAAWA,CAACA,MAAMA,CAACA,CAACA;iBAC7EA,IAAIA,CAACA;gBACFA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AACA,CAACA;gBACpDA,IAAIA,UAAUA,GAAkBA,KAAIA,CAACA,KAAKA,CAACA,OAAOA,CAACA,QAAQA,CAACA,QAAQA,CAACA,CAACA;gBACtEA,EAAEA,CAACA,CAACA,UAAUA,CAACA,MAAMA,CAACA;oBAACA,MAAMA,CAACA,QAAQA,CAACA,MAAMA,CAACA,UAAUA,CAACA,KAAKA,CAACA,CAACA;gBAEhEA,MAAMA,CAACA,QAAQA,CAACA;YACpBA,CAACA,CAACA,CAACA;QACXA,CAACA,CAACA,CAACA,CAACA;IACRA,CAACA;IAEDH,sCAAcA,GAAdA,UAAeA,QAAmBA,EAAEA,OAAYA;QAC5CI,MAAMA,CAACA,QAAQA;aACVA,OAAOA,CAACA,IAAIA,CAACA,KAAKA,CAACA,KAAKA,CAACA,QAAQA,IAAIA,IAAIA,CAACA,KAAKA,CAACA,KAAKA,CAACA,QAAQA,CAACA,QAAQA,EAAEA,OAAOA,CAACA,CAACA;aAClFA,IAAIA,CAACA;YACFA,MAAMA,CAACA,QAAQA,CAACA;QACpBA,CAACA,CAACA,CAACA;IACXA,CAACA;IACLJ,oBAACA;AAADA,CAzDA,AAyDCA,IAAA;AAzDY,qBAAa,gBAyDzB,CAAA","file":"lib/ModelHandlers.js","sourcesContent":["import {Core} from './Core';\r\nimport {Schema} from './Schema';\r\nimport {Model} from './Model';\r\nimport {ModelCache} from './ModelCache';\r\nimport * as ModelOptions from './ModelOptions';\r\n\r\nimport Skmatc = require('skmatc');\r\nimport _ = require('lodash');\r\nimport MongoDB = require('mongodb');\r\nimport Bluebird = require('bluebird');\r\n\r\n/**\r\n * Provides a number of methods which are used to handle events that occur within\r\n * the Iridium workflow - such as what happens when a document is received from\r\n * the database, or how to handle the creation of new documents and saving of instances.\r\n *\r\n * Mostly this is for cache support, wrapping and hook triggering.\r\n * @internal\r\n */\r\nexport class ModelHandlers {\r\n constructor(public model: Model) {\r\n\r\n }\r\n\r\n documentReceived(conditions: any,\r\n result: TDocument,\r\n wrapper: (document: TDocument, isNew?: boolean, isPartial?: boolean) => TResult,\r\n options: ModelOptions.QueryOptions = {}): Bluebird {\r\n _.defaults(options, {\r\n cache: true,\r\n partial: false\r\n });\r\n\r\n return Bluebird.resolve(result).then((target: any) => {\r\n return >Bluebird\r\n .resolve(this.model.hooks.onRetrieved && this.model.hooks.onRetrieved(target))\r\n .then(() => {\r\n // Cache the document if caching is enabled\r\n if (this.model.core.cache && options.cache && !options.fields) {\r\n this.model.cache.set(target); // Does not block execution pipeline - fire and forget\r\n }\r\n\r\n // Wrap the document and trigger the ready hook\r\n let wrapped: TResult = wrapper(target, false, !!options.fields);\r\n\r\n // Only incur the additional promise's performance penalty if this hook is being used\r\n if (this.model.hooks.onReady)\r\n return Bluebird\r\n .resolve(this.model.hooks.onReady(wrapped))\r\n .then(() => wrapped);\r\n return wrapped;\r\n });\r\n });\r\n }\r\n\r\n creatingDocuments(documents: TDocument[]): Bluebird {\r\n return Bluebird.all(documents.map((document: any) => {\r\n return Bluebird\r\n .resolve(this.model.hooks.onCreating && this.model.hooks.onCreating(document))\r\n .then(() => {\r\n document = this.model.helpers.convertToDB(document);\r\n let validation: Skmatc.Result = this.model.helpers.validate(document);\r\n if (validation.failed) return Bluebird.reject(validation.error);\r\n\r\n return document;\r\n });\r\n }));\r\n }\r\n\r\n savingDocument(instance: TInstance, changes: any): Bluebird {\r\n return Bluebird\r\n .resolve(this.model.hooks.onSaving && this.model.hooks.onSaving(instance, changes))\r\n .then(() => {\r\n return instance;\r\n });\r\n }\r\n}\r\n"],"sourceRoot":"/source/"} \ No newline at end of file +{"version":3,"sources":["lib/ModelHandlers.ts"],"names":["ModelHandlers","ModelHandlers.constructor","ModelHandlers.documentReceived","ModelHandlers.creatingDocuments","ModelHandlers.savingDocument"],"mappings":"AAOA,IAAO,CAAC,WAAW,QAAQ,CAAC,CAAC;AAE7B,IAAO,QAAQ,WAAW,UAAU,CAAC,CAAC;AAEtC;;;;;;;GAOG;AACH;IACIA,uBAAmBA,KAAk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AACA;YACFA,MAAMA,CAACA,QAAQA,CAACA;QACpBA,CAACA,CAACA,CAACA;IACXA,CAACA;IACLJ,oBAACA;AAADA,CA5DA,AA4DCA,IAAA;AA5DY,qBAAa,gBA4DzB,CAAA","file":"lib/ModelHandlers.js","sourcesContent":["import {Core} from './Core';\r\nimport {Schema} from './Schema';\r\nimport {Model} from './Model';\r\nimport {ModelCache} from './ModelCache';\r\nimport * as ModelOptions from './ModelOptions';\r\n\r\nimport Skmatc = require('skmatc');\r\nimport _ = require('lodash');\r\nimport MongoDB = require('mongodb');\r\nimport Bluebird = require('bluebird');\r\n\r\n/**\r\n * Provides a number of methods which are used to handle events that occur within\r\n * the Iridium workflow - such as what happens when a document is received from\r\n * the database, or how to handle the creation of new documents and saving of instances.\r\n *\r\n * Mostly this is for cache support, wrapping and hook triggering.\r\n * @internal\r\n */\r\nexport class ModelHandlers {\r\n constructor(public model: Model) {\r\n\r\n }\r\n\r\n documentReceived(conditions: any,\r\n result: TDocument,\r\n wrapper: (document: TDocument, isNew?: boolean, isPartial?: boolean) => TResult,\r\n options: ModelOptions.QueryOptions = {}): Bluebird {\r\n _.defaults(options, {\r\n cache: true,\r\n partial: false\r\n });\r\n\r\n return Bluebird.resolve(result).then((target: any) => {\r\n return >Bluebird\r\n // If onRetrieved returns a Bluebird promise then there is no significant performance overhead here\r\n .resolve(this.model.hooks.onRetrieved && this.model.hooks.onRetrieved(target))\r\n .then(() => {\r\n // Cache the document if caching is enabled\r\n if (this.model.core.cache && options.cache && !options.fields) {\r\n this.model.cache.set(target); // Does not block execution pipeline - fire and forget\r\n }\r\n\r\n // Wrap the document and trigger the ready hook\r\n let wrapped: TResult = wrapper(target, false, !!options.fields);\r\n\r\n // Only incur the additional promise's performance penalty if this hook is being used\r\n if (this.model.hooks.onReady)\r\n return Bluebird\r\n .resolve(this.model.hooks.onReady(wrapped))\r\n .then(() => wrapped);\r\n return wrapped;\r\n });\r\n });\r\n }\r\n\r\n creatingDocuments(documents: TDocument[]): Bluebird {\r\n return Bluebird.all(documents.map((document: any) => {\r\n return Bluebird\r\n // If onCreating returns a Bluebird promise then there is no significant performance overhead here\r\n .resolve(this.model.hooks.onCreating && this.model.hooks.onCreating(document))\r\n .then(() => {\r\n document = this.model.helpers.convertToDB(document);\r\n let validation: Skmatc.Result = this.model.helpers.validate(document);\r\n if (validation.failed) return Bluebird.reject(validation.error);\r\n\r\n return document;\r\n });\r\n }));\r\n }\r\n\r\n savingDocument(instance: TInstance, changes: any): Bluebird {\r\n return Bluebird\r\n // If onSaving returns a Bluebird promise then there is no significant performance overhead here\r\n .resolve(this.model.hooks.onSaving && this.model.hooks.onSaving(instance, changes))\r\n .then(() => {\r\n return instance;\r\n });\r\n }\r\n}\r\n"],"sourceRoot":"/source/"} \ No newline at end of file diff --git a/lib/ModelHandlers.ts b/lib/ModelHandlers.ts index 17a0a14..9aa5377 100644 --- a/lib/ModelHandlers.ts +++ b/lib/ModelHandlers.ts @@ -34,6 +34,7 @@ export class ModelHandlers { let wrapped: TResult; return Bluebird.resolve(result).then((target: any) => { return >Bluebird + // If onRetrieved returns a Bluebird promise then there is no significant performance overhead here .resolve(this.model.hooks.onRetrieved && this.model.hooks.onRetrieved(target)) .then(() => { // Cache the document if caching is enabled @@ -57,6 +58,7 @@ export class ModelHandlers { creatingDocuments(documents: TDocument[]): Bluebird { return Bluebird.all(documents.map((document: any) => { return Bluebird + // If onCreating returns a Bluebird promise then there is no significant performance overhead here .resolve(this.model.hooks.onCreating && this.model.hooks.onCreating(document)) .then(() => { document = this.model.helpers.convertToDB(document); @@ -70,6 +72,7 @@ export class ModelHandlers { savingDocument(instance: TInstance, changes: any): Bluebird { return Bluebird + // If onSaving returns a Bluebird promise then there is no significant performance overhead here .resolve(this.model.hooks.onSaving && this.model.hooks.onSaving(instance, changes)) .then(() => { return instance;