Skip to content

Commit

Permalink
Hooks and caches are now not waited on for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Apr 27, 2015
1 parent 6602edc commit a47e8bb
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 48 deletions.
4 changes: 2 additions & 2 deletions lib/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Bluebird = require('bluebird');
export = ICache;

interface ICache {
set<T>(key: string, value: T): Bluebird<T>;
set<T>(key: string, value: T): void;
get<T>(key: string): Bluebird<T>;
clear(key: string): Bluebird<boolean>
clear(key: string): void
}
8 changes: 4 additions & 4 deletions lib/Hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import instance = require('./Instance');
export = IHooks;

interface IHooks<TDocument, TInstance> {
creating? (document: TDocument);
retrieved? (document: TDocument);
ready? (instance: TInstance);
saving? (instance: TInstance, changes: any);
creating? (document: TDocument): void;
retrieved? (document: TDocument): void;
ready? (instance: TInstance): void;
saving? (instance: TInstance, changes: any): void;
}
7 changes: 4 additions & 3 deletions lib/Model.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Model.js.map

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class Model<TDocument extends { _id?: any }, TInstance> implements ModelInterfac

if (conditions.hasOwnProperty('_id'))
conditions['_id'] = this.options.identifier.reverse(conditions['_id']);

var cursor = this.collection.find(conditions, {
fields: fields
});
Expand Down Expand Up @@ -488,6 +488,8 @@ class Model<TDocument extends { _id?: any }, TInstance> implements ModelInterfac
options = {};
}

options = options || {};

if (!_.isPlainObject(conditions)) conditions = {
_id: conditions
};
Expand Down Expand Up @@ -584,8 +586,7 @@ class Model<TDocument extends { _id?: any }, TInstance> implements ModelInterfac
});
});
}).then((count) => {
if (count === 1)
return this._cache.clear(conditions).then(() => count);
if (count === 1) this._cache.clear(conditions);
return Bluebird.resolve(count);
}).nodeify(callback);
}
Expand Down Expand Up @@ -652,10 +653,10 @@ class Model<TDocument extends { _id?: any }, TInstance> implements ModelInterfac
index = _(<Index.IndexSpecification>specification).map((direction, key) => key + '_' + direction).reduce<string>((x, y) => x + '_' + y);
}

return new Bluebird<any>((resolve, reject) => {
this.collection.dropIndex(index,(err, count) => {
return new Bluebird<boolean>((resolve, reject) => {
this.collection.dropIndex(index,(err, result: { ok: number }) => {
if (err) return reject(err);
return resolve(count);
return resolve(<any>!!result.ok);
});
}).nodeify(callback);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/ModelCache.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ModelCache.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/ModelCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ class ModelCache {

}

set<T>(value: T): Bluebird<T> {
if (!this.model.cacheDirector || !this.model.cacheDirector.valid(value)) return Bluebird.resolve(value);
return this.model.core.cache.set(this.model.cacheDirector.buildKey(value), value);
set<T>(value: T): void {
if (!this.model.cacheDirector || !this.model.cacheDirector.valid(value)) return;;
this.model.core.cache.set(this.model.cacheDirector.buildKey(value), value);
}

get<T>(conditions: any): Bluebird<T> {
if (!this.model.cacheDirector || !this.model.cacheDirector.validQuery(conditions)) return Bluebird.resolve(<T>null);
return this.model.core.cache.get<T>(this.model.cacheDirector.buildQueryKey(conditions));
}

clear(conditions: any): Bluebird<boolean> {
if (!this.model.cacheDirector || !this.model.cacheDirector.validQuery(conditions)) return Bluebird.resolve(false);
return this.model.core.cache.clear(this.model.cacheDirector.buildQueryKey(conditions));
clear(conditions: any): void {
if (!this.model.cacheDirector || !this.model.cacheDirector.validQuery(conditions)) return;
this.model.core.cache.clear(this.model.cacheDirector.buildQueryKey(conditions));
}
}
17 changes: 7 additions & 10 deletions lib/ModelHandlers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/ModelHandlers.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions lib/ModelHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ class ModelHandlers<TDocument extends { _id?: any }, TInstance> {
return Bluebird.resolve(result).then((target: any) => {
return <Bluebird<TResult>>Bluebird.resolve().then(() => {
// Trigger the received hook
if (this.model.options.hooks.retrieved) return this.model.options.hooks.retrieved(target);
}).then(() => {
if (this.model.options.hooks.retrieved) this.model.options.hooks.retrieved(target);

// Cache the document if caching is enabled
if (this.model.core.cache && options.cache && !options.fields) {
var cacheDoc = _.cloneDeep(target);
return this.model.cache.set(cacheDoc);
this.model.cache.set(target); // Does not block execution pipeline - fire and forget
}
}).then(() => {

// Wrap the document and trigger the ready hook
var wrapped: TResult = wrapper(target, false, !!options.fields);

if (this.model.options.hooks.ready && wrapped instanceof this.model.Instance) return Bluebird.resolve(this.model.options.hooks.ready(<TInstance><any>wrapped)).then(() => wrapped);
if (this.model.options.hooks.ready && wrapped instanceof this.model.Instance) this.model.options.hooks.ready(<TInstance><any>wrapped);
return wrapped;
});
});
Expand All @@ -47,8 +46,7 @@ class ModelHandlers<TDocument extends { _id?: any }, TInstance> {
creatingDocuments(documents: TDocument[]): Bluebird<any[]> {
return Bluebird.all(documents.map((document: any) => {
return Bluebird.resolve().then(() => {
if (this.model.options.hooks.retrieved) return this.model.options.hooks.creating(document);
}).then(() => {
if (this.model.options.hooks.retrieved) this.model.options.hooks.creating(document);
var validation: SkmatcCore.IResult = this.model.helpers.validate(document);
if (validation.failed) return Bluebird.reject(validation.error);
return document;
Expand All @@ -58,7 +56,8 @@ class ModelHandlers<TDocument extends { _id?: any }, TInstance> {

savingDocument(instance: TInstance, changes: any): Bluebird<TInstance> {
return Bluebird.resolve().then(() => {
if (this.model.options.hooks.saving) return this.model.options.hooks.saving(instance, changes);
}).then(() => instance);
if (this.model.options.hooks.saving) this.model.options.hooks.saving(instance, changes);
return instance;
});
}
}

0 comments on commit a47e8bb

Please sign in to comment.