-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Singletonable mixin #3
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Add lazy singletons to Constructors. This adds a makeSingleton class | ||
// method, which will create a singleton property of the given name, passing | ||
// the remaining arguments to the constructor. | ||
// | ||
// The singleton is constructed on first access, through | ||
// Object.defineProperty. This defers initialization, simplifying dependency | ||
// management. | ||
|
||
module.exports = function (Constructor) { | ||
Constructor.makeSingleton = function (name, varargs) { | ||
// Singleton should be of whatever makeSingleton was called on, not | ||
// necessarily what was passed to singletonable() | ||
var Child = this; | ||
varargs = arguments._.tail(); | ||
|
||
// Storage for the the singleton object, once it is created | ||
var singleton; | ||
Object.defineProperty(Child, name, { | ||
get: function () { | ||
if (!singleton) singleton = _.applyConstructor(Child, varargs) | ||
return singleton; | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
var init = require('./init'); | ||
var singletonable = require('../lib/singletonable'); | ||
|
||
describe('Singletonable', function () { | ||
var constructorCalls = 0; | ||
var Klass = function (a, b, c) { | ||
var self = this; | ||
constructorCalls++; | ||
self.a = a; | ||
self.b = b; | ||
self.c = c; | ||
} | ||
|
||
singletonable(Klass); | ||
|
||
describe('when you create a singleton', function () { | ||
before(function () { | ||
Klass.makeSingleton('singleton', 1, 2, 3); | ||
}) | ||
|
||
it('should not call the constructor', function () { | ||
return expect(constructorCalls).equal(0); | ||
}) | ||
|
||
it('should have a property for the singleton', function () { | ||
return expect(Klass.hasOwnProperty('singleton')).true; | ||
}) | ||
|
||
describe('when you access the constructor', function () { | ||
before(function () { | ||
Klass.singleton; | ||
Klass.singleton; | ||
}) | ||
|
||
it('should have called the constructor once', function () { | ||
return expect(constructorCalls).equal(1); | ||
}) | ||
|
||
it('should have the correct properties', function () { | ||
return expect(Klass.singleton._.pick('a', 'b', 'c')).deep.equal({a: 1, b: 2, c: 3}); | ||
}) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally I think this would be Constructor._singleton or something like that so that people could mock/override it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm... I guess that won't really work because of the name you're using, but you could provide a setter method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a previous iteration, I had set configurable: true, thinking that would make it so subclasses could override it. But then I realized that that's not necessary for the subclass case, and took it off. Mocking is a good point though, so I think you're right.
I have a trick for this kind of situation where you define a property as a configurable property, but then set it when it's accessed, so it acts exactly like normal property. That'll be good because then we can use autoMock too.