UNPKG

3.35 kBJavaScriptView Raw
1import "core-js/modules/es.object.assign.js";
2import "core-js/modules/web.dom-collections.for-each.js";
3import "core-js/modules/es.object.values.js";
4import "core-js/modules/es.promise.js";
5import "core-js/modules/es.object.to-string.js";
6
7function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
9import global from 'global';
10import { Channel } from '@storybook/channels';
11import { logger } from '@storybook/client-logger';
12import { types } from './types';
13export { Channel };
14export var AddonStore = function AddonStore() {
15 var _this = this;
16
17 _classCallCheck(this, AddonStore);
18
19 this.loaders = {};
20 this.elements = {};
21 this.config = {};
22 this.channel = void 0;
23 this.serverChannel = void 0;
24 this.promise = void 0;
25 this.resolve = void 0;
26
27 this.getChannel = function () {
28 // this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), throw.
29 if (!_this.channel) {
30 throw new Error('Accessing non-existent addons channel, see https://storybook.js.org/basics/faq/#why-is-there-no-addons-channel');
31 }
32
33 return _this.channel;
34 };
35
36 this.getServerChannel = function () {
37 if (!_this.serverChannel) {
38 throw new Error('Accessing non-existent serverChannel');
39 }
40
41 return _this.serverChannel;
42 };
43
44 this.ready = function () {
45 return _this.promise;
46 };
47
48 this.hasChannel = function () {
49 return !!_this.channel;
50 };
51
52 this.hasServerChannel = function () {
53 return !!_this.serverChannel;
54 };
55
56 this.setChannel = function (channel) {
57 _this.channel = channel;
58
59 _this.resolve();
60 };
61
62 this.setServerChannel = function (channel) {
63 _this.serverChannel = channel;
64 };
65
66 this.getElements = function (type) {
67 if (!_this.elements[type]) {
68 _this.elements[type] = {};
69 }
70
71 return _this.elements[type];
72 };
73
74 this.addPanel = function (name, options) {
75 _this.add(name, Object.assign({
76 type: types.PANEL
77 }, options));
78 };
79
80 this.add = function (name, addon) {
81 var type = addon.type;
82
83 var collection = _this.getElements(type);
84
85 collection[name] = Object.assign({
86 id: name
87 }, addon);
88 };
89
90 this.setConfig = function (value) {
91 Object.assign(_this.config, value);
92 };
93
94 this.getConfig = function () {
95 return _this.config;
96 };
97
98 this.register = function (name, registerCallback) {
99 if (_this.loaders[name]) {
100 logger.warn("".concat(name, " was loaded twice, this could have bad side-effects"));
101 }
102
103 _this.loaders[name] = registerCallback;
104 };
105
106 this.loadAddons = function (api) {
107 Object.values(_this.loaders).forEach(function (value) {
108 return value(api);
109 });
110 };
111
112 this.promise = new Promise(function (res) {
113 _this.resolve = function () {
114 return res(_this.getChannel());
115 };
116 });
117}; // Enforce addons store to be a singleton
118
119var KEY = '__STORYBOOK_ADDONS';
120
121function getAddonsStore() {
122 if (!global[KEY]) {
123 global[KEY] = new AddonStore();
124 }
125
126 return global[KEY];
127} // Exporting this twice in order to to be able to import it like { addons } instead of 'addons'
128// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons'
129//
130// See public_api.ts
131
132
133export var addons = getAddonsStore();
\No newline at end of file