From fbd5f1e4110059726118a3728141753848d10c19 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 27 Oct 2017 13:59:22 -0400 Subject: [PATCH] REFACTOR: Rename `store:main` to `service:store` so we can inject it --- .../subscribe-user-notifications.js.es6 | 2 +- .../discourse/lib/plugin-api.js.es6 | 2 +- .../discourse/models/nav-item.js.es6 | 2 +- .../javascripts/discourse/models/rest.js.es6 | 2 +- .../javascripts/discourse/models/site.js.es6 | 2 +- .../discourse/models/topic-list.js.es6 | 2 +- .../javascripts/discourse/models/user.js.es6 | 2 +- .../inject-discourse-objects.js.es6 | 47 ++++++++++--------- .../discourse/widgets/widget.js.es6 | 2 +- .../javascripts/helpers/component-test.js.es6 | 2 +- test/javascripts/models/store-test.js.es6 | 2 +- 11 files changed, 34 insertions(+), 33 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 index 2a85136b0c7..a477463d953 100644 --- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 +++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 @@ -33,7 +33,7 @@ export default { } bus.subscribe(`/notification/${user.get('id')}`, data => { - const store = container.lookup('store:main'); + const store = container.lookup('service:store'); const oldUnread = user.get('unread_notifications'); const oldPM = user.get('unread_private_messages'); diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 081a1286e84..38772203aee 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -441,7 +441,7 @@ class PluginApi { * will issue a request to `/mice.json` **/ addStorePluralization(thing, plural) { - this.container.lookup("store:main").addPluralization(thing, plural); + this.container.lookup("service:store").addPluralization(thing, plural); } /** diff --git a/app/assets/javascripts/discourse/models/nav-item.js.es6 b/app/assets/javascripts/discourse/models/nav-item.js.es6 index c41f33b544d..f71842cd6e1 100644 --- a/app/assets/javascripts/discourse/models/nav-item.js.es6 +++ b/app/assets/javascripts/discourse/models/nav-item.js.es6 @@ -122,7 +122,7 @@ NavItem.reopenClass({ _.merge(args, extra); }); - const store = Discourse.__container__.lookup('store:main'); + const store = Discourse.__container__.lookup('service:store'); return store.createRecord('nav-item', args); }, diff --git a/app/assets/javascripts/discourse/models/rest.js.es6 b/app/assets/javascripts/discourse/models/rest.js.es6 index 7dff27171e4..6c267a0e152 100644 --- a/app/assets/javascripts/discourse/models/rest.js.es6 +++ b/app/assets/javascripts/discourse/models/rest.js.es6 @@ -84,7 +84,7 @@ RestModel.reopenClass({ if (!args.store) { const container = Discourse.__container__; // Ember.warn('Use `store.createRecord` to create records instead of `.create()`'); - args.store = container.lookup('store:main'); + args.store = container.lookup('service:store'); } args.__munge = this.munge; diff --git a/app/assets/javascripts/discourse/models/site.js.es6 b/app/assets/javascripts/discourse/models/site.js.es6 index 5b421c46bee..ace9f7176ce 100644 --- a/app/assets/javascripts/discourse/models/site.js.es6 +++ b/app/assets/javascripts/discourse/models/site.js.es6 @@ -94,7 +94,7 @@ Site.reopenClass(Singleton, { // The current singleton will retrieve its attributes from the `PreloadStore`. createCurrent() { - const store = Discourse.__container__.lookup('store:main'); + const store = Discourse.__container__.lookup('service:store'); return store.createRecord('site', PreloadStore.get('site')); }, diff --git a/app/assets/javascripts/discourse/models/topic-list.js.es6 b/app/assets/javascripts/discourse/models/topic-list.js.es6 index b9076f078d0..3e39ae5c895 100644 --- a/app/assets/javascripts/discourse/models/topic-list.js.es6 +++ b/app/assets/javascripts/discourse/models/topic-list.js.es6 @@ -140,7 +140,7 @@ TopicList.reopenClass({ }, find(filter, params) { - const store = Discourse.__container__.lookup('store:main'); + const store = Discourse.__container__.lookup('service:store'); return store.findFiltered('topicList', {filter, params}); }, diff --git a/app/assets/javascripts/discourse/models/user.js.es6 b/app/assets/javascripts/discourse/models/user.js.es6 index 3296c5fbe7c..17e7f9bfc11 100644 --- a/app/assets/javascripts/discourse/models/user.js.es6 +++ b/app/assets/javascripts/discourse/models/user.js.es6 @@ -544,7 +544,7 @@ User.reopenClass(Singleton, { createCurrent() { const userJson = PreloadStore.get('currentUser'); if (userJson) { - const store = Discourse.__container__.lookup('store:main'); + const store = Discourse.__container__.lookup('service:store'); return store.createRecord('user', userJson); } return null; diff --git a/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 b/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 index 34181e46b41..ca60c3c311f 100644 --- a/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 +++ b/app/assets/javascripts/discourse/pre-initializers/inject-discourse-objects.js.es6 @@ -8,17 +8,7 @@ import SearchService from 'discourse/services/search'; import { startTracking, default as TopicTrackingState } from 'discourse/models/topic-tracking-state'; import ScreenTrack from 'discourse/lib/screen-track'; -function inject() { - const app = arguments[0], - name = arguments[1], - singletonName = Ember.String.underscore(name).replace(/_/g, '-') + ':main'; - - Array.prototype.slice.call(arguments, 2).forEach(dest => app.inject(dest, name, singletonName)); -} - -function injectAll(app, name) { - inject(app, name, 'controller', 'component', 'route', 'model', 'adapter'); -} +const ALL_TARGETS = ['controller', 'component', 'route', 'model', 'adapter']; export default { name: "inject-discourse-objects", @@ -26,51 +16,62 @@ export default { initialize(container, app) { const appEvents = AppEvents.create(); app.register('app-events:main', appEvents, { instantiate: false }); - injectAll(app, 'appEvents'); + ALL_TARGETS.forEach(t => app.inject(t, 'appEvents', 'app-events:main')); DiscourseURL.appEvents = appEvents; + // backwards compatibility: remove when plugins have updated app.register('store:main', Store); - inject(app, 'store', 'route', 'controller', 'service'); + + app.register('service:store', Store); + ALL_TARGETS.forEach(t => app.inject(t, 'store', 'service:store')); const messageBus = window.MessageBus; app.register('message-bus:main', messageBus, { instantiate: false }); - injectAll(app, 'messageBus'); + ALL_TARGETS.forEach(t => app.inject(t, 'messageBus', 'message-bus:main')); const currentUser = Discourse.User.current(); app.register('current-user:main', currentUser, { instantiate: false }); const topicTrackingState = TopicTrackingState.create({ messageBus, currentUser }); app.register('topic-tracking-state:main', topicTrackingState, { instantiate: false }); - injectAll(app, 'topicTrackingState'); + ALL_TARGETS.forEach(t => app.inject(t, 'topicTrackingState', 'topic-tracking-state:main')); const site = Discourse.Site.current(); app.register('site:main', site, { instantiate: false }); - injectAll(app, 'site'); + ALL_TARGETS.forEach(t => app.inject(t, 'site', 'site:main')); const siteSettings = Discourse.SiteSettings; app.register('site-settings:main', siteSettings, { instantiate: false }); - injectAll(app, 'siteSettings'); + ALL_TARGETS.forEach(t => app.inject(t, 'siteSettings', 'site-settings:main')); app.register('search-service:main', SearchService); - injectAll(app, 'searchService'); + ALL_TARGETS.forEach(t => app.inject(t, 'searchService', 'search-service:main')); const session = Session.current(); app.register('session:main', session, { instantiate: false }); - injectAll(app, 'session'); + ALL_TARGETS.forEach(t => app.inject(t, 'session', 'session:main')); + + const screenTrack = new ScreenTrack( + topicTrackingState, + siteSettings, + session, + currentUser + ); - const screenTrack = new ScreenTrack(topicTrackingState, siteSettings, session, currentUser); app.register('screen-track:main', screenTrack, { instantiate: false }); - inject(app, 'screenTrack', 'component', 'route'); + ['component', 'route'].forEach(t => app.inject(t, 'screenTrack', 'screen-track:main')); if (currentUser) { - inject(app, 'currentUser', 'component', 'route', 'controller'); + ['component', 'route', 'controller'].forEach(t => { + app.inject(t, 'currentUser', 'current-user:main'); + }); } app.register('location:discourse-location', DiscourseLocation); const keyValueStore = new KeyValueStore("discourse_"); app.register('key-value-store:main', keyValueStore, { instantiate: false }); - injectAll(app, 'keyValueStore'); + ALL_TARGETS.forEach(t => app.inject(t, 'keyValueStore', 'key-value-store:main')); startTracking(topicTrackingState); } diff --git a/app/assets/javascripts/discourse/widgets/widget.js.es6 b/app/assets/javascripts/discourse/widgets/widget.js.es6 index d81b2b53959..022625168f4 100644 --- a/app/assets/javascripts/discourse/widgets/widget.js.es6 +++ b/app/assets/javascripts/discourse/widgets/widget.js.es6 @@ -153,7 +153,7 @@ export default class Widget { this.siteSettings = register.lookup('site-settings:main'); this.currentUser = register.lookup('current-user:main'); this.capabilities = register.lookup('capabilities:main'); - this.store = register.lookup('store:main'); + this.store = register.lookup('service:store'); this.appEvents = register.lookup('app-events:main'); this.keyValueStore = register.lookup('key-value-store:main'); diff --git a/test/javascripts/helpers/component-test.js.es6 b/test/javascripts/helpers/component-test.js.es6 index 4154a461219..24c99aa63ce 100644 --- a/test/javascripts/helpers/component-test.js.es6 +++ b/test/javascripts/helpers/component-test.js.es6 @@ -33,7 +33,7 @@ export default function(name, opts) { { instantiate: false }); } - this.registry.register('store:main', store, { instantiate: false }); + this.registry.register('service:store', store, { instantiate: false }); if (opts.beforeEach) { opts.beforeEach.call(this, store); diff --git a/test/javascripts/models/store-test.js.es6 b/test/javascripts/models/store-test.js.es6 index 6499262e8b1..232055bb285 100644 --- a/test/javascripts/models/store-test.js.es6 +++ b/test/javascripts/models/store-test.js.es6 @@ -1,4 +1,4 @@ -QUnit.module('store:main'); +QUnit.module('service:store'); import createStore from 'helpers/create-store';