This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/helpers/create-store.js
David Taylor fc36ac6cde
DEV: Modernize Ember Resolver (#17353)
This switches us to use the modern ember resolver package, and re-implements a number of our custom resolution rules within it. The legacy resolver remains for now, and is used as a fallback if the modern resolver is unable to resolve a package. When this happens, a warning will be printed to the console.

Co-authored-by: Peter Wagenet <peter.wagenet@gmail.com>
2022-07-06 14:20:00 +01:00

68 lines
2.2 KiB
JavaScript

import KeyValueStore from "discourse/lib/key-value-store";
import RestAdapter from "discourse/adapters/rest";
import Store from "discourse/services/store";
import TopicListAdapter from "discourse/adapters/topic-list";
import TopicTrackingState from "discourse/models/topic-tracking-state";
import { buildResolver } from "discourse-common/resolver";
import { currentSettings } from "discourse/tests/helpers/site-settings";
import Site from "discourse/models/site";
const CatAdapter = RestAdapter.extend({
primaryKey: "cat_id",
});
export default function (customLookup = () => {}) {
const resolver = buildResolver("discourse").create({
namespace: { modulePrefix: "discourse" },
});
// Normally this would happen in inject-discourse-objects.
// However, `create-store` is used by unit tests which do not init the application.
Site.current();
return Store.create({
register: {
lookup(type) {
if (type === "adapter:cat") {
this._catAdapter =
this._catAdapter || CatAdapter.create({ owner: this });
return this._catAdapter;
}
if (type === "adapter:rest") {
if (!this._restAdapter) {
this._restAdapter = RestAdapter.create({ owner: this });
}
return this._restAdapter;
}
if (type === "adapter:topicList") {
this._topicListAdapter =
this._topicListAdapter || TopicListAdapter.create({ owner: this });
return this._topicListAdapter;
}
if (type === "key-value-store:main") {
this._kvs = this._kvs || new KeyValueStore();
return this._kvs;
}
if (type === "topic-tracking-state:main") {
this._tracker = this._tracker || TopicTrackingState.create();
return this._tracker;
}
if (type === "site-settings:main") {
this._settings = this._settings || currentSettings();
return this._settings;
}
return customLookup(type);
},
lookupFactory(type) {
const split = type.split(":");
return resolver.resolveOther({
type: split[0],
fullNameWithoutType: split[1],
root: {},
});
},
},
});
}