UNPKG

4.47 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.AddonStore = void 0;
7Object.defineProperty(exports, "Channel", {
8 enumerable: true,
9 get: function get() {
10 return _channels.Channel;
11 }
12});
13exports.addons = void 0;
14
15require("core-js/modules/es.object.assign.js");
16
17require("core-js/modules/es.object.to-string.js");
18
19require("core-js/modules/web.dom-collections.for-each.js");
20
21require("core-js/modules/es.object.values.js");
22
23require("core-js/modules/es.promise.js");
24
25var _global = _interopRequireDefault(require("global"));
26
27var _channels = require("@storybook/channels");
28
29var _clientLogger = require("@storybook/client-logger");
30
31var _storybookChannelMock = require("./storybook-channel-mock");
32
33var _types = require("./types");
34
35function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
36
37function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
38
39function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
40
41function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
42
43var AddonStore = /*#__PURE__*/_createClass(function AddonStore() {
44 var _this = this;
45
46 _classCallCheck(this, AddonStore);
47
48 this.loaders = {};
49 this.elements = {};
50 this.config = {};
51 this.channel = void 0;
52 this.serverChannel = void 0;
53 this.promise = void 0;
54 this.resolve = void 0;
55
56 this.getChannel = function () {
57 // this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), set a mock instead.
58 if (!_this.channel) {
59 _this.setChannel((0, _storybookChannelMock.mockChannel)());
60 }
61
62 return _this.channel;
63 };
64
65 this.getServerChannel = function () {
66 if (!_this.serverChannel) {
67 throw new Error('Accessing non-existent serverChannel');
68 }
69
70 return _this.serverChannel;
71 };
72
73 this.ready = function () {
74 return _this.promise;
75 };
76
77 this.hasChannel = function () {
78 return !!_this.channel;
79 };
80
81 this.hasServerChannel = function () {
82 return !!_this.serverChannel;
83 };
84
85 this.setChannel = function (channel) {
86 _this.channel = channel;
87
88 _this.resolve();
89 };
90
91 this.setServerChannel = function (channel) {
92 _this.serverChannel = channel;
93 };
94
95 this.getElements = function (type) {
96 if (!_this.elements[type]) {
97 _this.elements[type] = {};
98 }
99
100 return _this.elements[type];
101 };
102
103 this.addPanel = function (name, options) {
104 _this.add(name, Object.assign({
105 type: _types.types.PANEL
106 }, options));
107 };
108
109 this.add = function (name, addon) {
110 var type = addon.type;
111
112 var collection = _this.getElements(type);
113
114 collection[name] = Object.assign({
115 id: name
116 }, addon);
117 };
118
119 this.setConfig = function (value) {
120 Object.assign(_this.config, value);
121 };
122
123 this.getConfig = function () {
124 return _this.config;
125 };
126
127 this.register = function (name, registerCallback) {
128 if (_this.loaders[name]) {
129 _clientLogger.logger.warn("".concat(name, " was loaded twice, this could have bad side-effects"));
130 }
131
132 _this.loaders[name] = registerCallback;
133 };
134
135 this.loadAddons = function (api) {
136 Object.values(_this.loaders).forEach(function (value) {
137 return value(api);
138 });
139 };
140
141 this.promise = new Promise(function (res) {
142 _this.resolve = function () {
143 return res(_this.getChannel());
144 };
145 });
146}); // Enforce addons store to be a singleton
147
148
149exports.AddonStore = AddonStore;
150var KEY = '__STORYBOOK_ADDONS';
151
152function getAddonsStore() {
153 if (!_global.default[KEY]) {
154 _global.default[KEY] = new AddonStore();
155 }
156
157 return _global.default[KEY];
158} // Exporting this twice in order to to be able to import it like { addons } instead of 'addons'
159// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons'
160//
161// See public_api.ts
162
163
164var addons = getAddonsStore();
165exports.addons = addons;
\No newline at end of file