BigW Consortium Gitlab
Decide on and document a convention for singletons > The singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The simplest implementation uses an object literal to contain the logic. ```javascript gl.MyThing = { prop1: 'hello', method1: () => {} }; ``` A live example of this is [GfmAutoComplete](https://gitlab.com/gitlab-org/gitlab-ce/blob/172aab108b875e8dc9a5f1d3c2d53018eff76ea1/app/assets/javascripts/gfm_auto_complete.js.es6) Another approach makes use of ES6 `class` syntax. ```javascript let singleton; class MyThing { constructor() { if (!singleton) { singleton = this; singleton.init(); } return singleton; } init() { this.prop1 = 'hello'; } method1() {} } gl.MyThing = MyThing; ``` A live example of this is [Sidebar](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/app/assets/javascripts/sidebar.js.es6) Another functional approach to define Singleton using `Javascript Revealing Module Pattern` is like below ```javascript /** * 1. It instantiates only a single object * 2. It is safe – it keeps the reference to the singleton inside a variable, which lives inside a lexical closure, so it is not accessible by the outside world * 3. It allows privacy – you can define all private methods of your singleton inside the lexical closure of the first module pattern * 4. No this keyword trap (no wrong context referring) * 5. No use of new keyword * 6. Easy to write test code */ // const Singleton = (function () { // Instance stores a reference to the Singleton var instance; function init() { // Singleton // Private methods and variables function privateMethod(){ console.log( "I am private" ); } var privateVariable = "Im also private"; var privateRandomNumber = Math.random(); return { // Public methods and variables publicMethod: function () { console.log( "The public can see me!" ); }, publicProperty: "I am also public", getRandomNumber: function() { return privateRandomNumber; } }; }; return { // Get the Singleton instance if one exists // or create one if it doesn't getInstance: function () { if ( !instance ) { instance = init(); } return instance; } }; })(); const singletonObj = Singleton.getInstance() ``` ## Are there points in the code the reviewer needs to double check? ## What does this MR do? Creates a space for discussion and contribution for interested devs. ## Why was this MR needed? ## Screenshots (if relevant) ## Does this MR meet the acceptance criteria? - [x] [Documentation created/updated](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/development/doc_styleguide.md) - [x] All builds are passing (http://docs.gitlab.com/ce/development/merge_request_performance_guidelines.html) - [x] Conform by the [style guides](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/CONTRIBUTING.md#style-guides) - [x] [Squashed related commits together](https://git-scm.com/book/en/Git-Tools-Rewriting-History#Squashing-Commits) ## What are the relevant issue numbers? See merge request !6620
Name |
Last commit
|
Last update |
---|---|---|
.. | ||
ux_guide | Loading commit data... | |
README.md | Loading commit data... | |
adding_database_indexes.md | Loading commit data... | |
api_styleguide.md | Loading commit data... | |
architecture.md | Loading commit data... | |
changelog.md | Loading commit data... | |
ci_setup.md | Loading commit data... | |
code_review.md | Loading commit data... | |
db_dump.md | Loading commit data... | |
doc_styleguide.md | Loading commit data... | |
frontend.md | Loading commit data... | |
gitlab_architecture_diagram.png | Loading commit data... | |
gitlab_diagram_overview.odg | Loading commit data... | |
gotchas.md | Loading commit data... | |
instrumentation.md | Loading commit data... | |
licensing.md | Loading commit data... | |
limit_ee_conflicts.md | Loading commit data... | |
merge_request_performance_guidelines.md | Loading commit data... | |
migration_style_guide.md | Loading commit data... | |
newlines_styleguide.md | Loading commit data... | |
omnibus.md | Loading commit data... | |
performance.md | Loading commit data... | |
post_deployment_migrations.md | Loading commit data... | |
profiling.md | Loading commit data... | |
rake_tasks.md | Loading commit data... | |
scss_styleguide.md | Loading commit data... | |
shared_files.md | Loading commit data... | |
shell_commands.md | Loading commit data... | |
sidekiq_debugging.md | Loading commit data... | |
sidekiq_style_guide.md | Loading commit data... | |
sql.md | Loading commit data... | |
testing.md | Loading commit data... | |
ui_guide.md | Loading commit data... | |
what_requires_downtime.md | Loading commit data... |