-
Notifications
You must be signed in to change notification settings - Fork 2
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
pattern for namespacing #378
Comments
(1) Pattern similar to @jonathanolson repos (scenery, kite, dot,... ) // functonBuilder.js
define( function( require ) {
'use strict';
return ( phet.functionBuilder = {} );
} )
// requires a require
var functionBuilder = require( 'FUNCTION_BUILDER/functionBuilder' );
// immediately after constructor
functionBuilder.PatternsModel = PatternsModel;
// wrap inherit, if you don't mind using the expression result of assignment
return functionBuilder.PatternsModel = inherit( Object, PatternsModel, ... );
// or if the above is distasteful...
functionBuilder.PatternsModel = inherit( Object, PatternsModel, ... );
return functionBuilder.PatternsModel; |
(2) Create global function // after constructor
phet.namespace( 'functionBuilder.PatternsModel', PatternsModel );
// wrap the inherit
return phet.namespace( 'functionBuilder.PatternsModel', inherit( Object, PatternsModel, ... ) ); |
@jonathanolson and I are leaning towards (1). |
To be clear on why I'm leaning towards (1).... • The |
@samreid @jbphet @aaronsamuel137 @jessegreenberg please comment. Propose additional patterns, and vote on your preferred pattern. |
+1 for pattern (1) |
Sure you could, there might be a copy paste error like this: var functionBuilder = require( 'SCENERY_PHET/sceneryPhet' );
Yes, but if you forget both, you won't see any lint errors. (However, we could add custom ESLint rules to catch this issue).
Assignment using
The following phet.namespace( 'functionBuilder.PatternsModel', PatternsModel ); doesn't require any inherit call, and works for any type. Why can't we use Tandem.addInstance for this? It seems like it has very similar goals? |
I'm also interested in discussing our plan for namespacing files:
If we are using (2) - (3) we may be incentivized to look into ESLint to check that files are namespaced (we may even be able to check the filename somehow?) |
I'd prefer to namespace all of my files at once. Will take me a couple of hours, then I won't have to deal with it again. |
9/29/15 dev meeting: Each developer will try out patterns 1, 2, 3 (or propose new patterns) and report back at Tue 10/6/15 developer meeting. |
Pattern 3: // Namespace.js
define( require ) {
'use strict';
function Namespace( name ) {
this._name = name;
if ( window.phet ) {
window.phet[ name ] = this;
}
}
return inherit( Object, Namespace, {
register: function( name, value ) {
assert && assert( !this[ name ], 'Duplicate ...' );
this[ name ] = value;
console.log( this._name + ' ' + name + ' loaded' );
}
} );
}
////////////////
// functionBuilder.js
define( require ) {
'use strict';
var Namespace = require( '.../Namespace' );
return new Namespace( 'functionBuilder' );
}
////////////////
var functionBuilder = require( 'FUNCTION_BUILDER/functionBuilder' ); <-- add
function SomeThing() {
...
}
functionBuilder.register( 'SomeThing', SomeThing ); // pattern 3
functionBuilder.SomeThing = SomeThing; // pattern 1 |
Unassigning. Will be discussed at 10/6/15 developer meeting after we individually test-drive. |
I took pattern (3) for a test-drive, see these (committed) files in function-builder: Namespace.js - namespace abstraction, would be moved to common code (phet-core?) |
Thoughts and reflections after testing this out? |
The term |
I like this pattern - I think it combines the best elements of the other 2. I'm not crazy about duplication of the name of the thing being registered (e.g. PatternsModel). But that's an issue with all patterns that we've considered. |
One could eliminate one duplication of the thing being registered (e.g. PatternsModel) if the
But I don't like the look of this, and it somewhat obfuscates the value of what's being registered. |
BRAINSTORMING_EMOTICON_NOT_FOUND: We could move inherit to Namespace and do something like this (only needs a require statement for functionBuilder, not for inherit): return functionBuilder.inherit( 'PatternsModel', Object, PatternsModel, { //... |
Not advocating, just brainstorming still: Namespace could be renamed to ModuleSystem which provides namespacing and inheritance functions (related functions for creating a module). |
How is this a module system? AMD is a module system, and is API specification is implemented by requirejs. If it's indeed a module system, then why do we need requirejs? Or why would we want to have 2 module systems? |
And what is the advantage (other than the syntactic convenience described in #378 (comment)) of combining namespace and inheritance responsibilities? |
This issue is getting a bit long, I'll create separate issue(s) for creating the eslint rule(s) for checking namespacing. @pixelzoom anything else to do here? |
We should probably prioritize adding namespace to common repos, and divide up the work. |
Looks like @jonathanolson took care of axon, phet-core, dot, kite, scenery. Here are the common repos that don't currently have a namespace. Let's put names next to these at developer meeting.
|
I've created individual issues (see above) for all of the common repos that need a namespace. After assigning those issues, we can close this issue. |
@jbphet says that sun will be done today. Closing. |
This came up in the context of together, where having namespacing is required for deserialization, JS type verification, and documentation of associated JS types.
Come up with a pattern that we like and apply it to existing code (sims and common).
The text was updated successfully, but these errors were encountered: