UNPKG

@formidable-webview/webshell

Version:

🔥 Craft Robust React Native WebView-based components with ease.

165 lines (131 loc) • 8.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _Feature2 = _interopRequireDefault(require("./Feature")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } /** * A utility to create feature classes. * * @typeparam O - A type describing the shape of the JSON-serializable object that will be passed to the Web script. * @typeparam S - A type specifying the new properties added to the shell (capabilities to send message to the shell). * @typeparam W - A type specifying the Web handlers added to the shell (capabilities to send message to the Web script). * * @public */ var FeatureBuilder = /*#__PURE__*/function () { /** * * @param config - An object to specify attributes of the feature. */ function FeatureBuilder(config) { _classCallCheck(this, FeatureBuilder); _defineProperty(this, "config", void 0); this.config = config; if (typeof config.script === 'function') { throw new TypeError('[FeatureBuilder]: config.script must be a string. If you are trying to import a ' + 'webjs file such as in the docs, you need to setup babel-plugin-inline-import. ' + 'See our guide here: https://formidable-webview.github.io/webshell/docs/tooling#babel'); } } /** * Instruct that the shell will receive events from the Web, and provide a * new handler prop for that purpose. * * @param propName - The name of the handler prop added to the shell. * It is advised to follow the convention of prefixing all these handlers * with `onDOM` or `onWeb` to avoid collisions with `WebView` own props. * @param eventId - A unique identifier for the event received by the shell. * If none is provided, fallback to `"default"`. * * @typeparam N - A type to define the name of the prop. * @typeparam P - A type describing the shape of payloads sent to shell handlers. */ _createClass(FeatureBuilder, [{ key: "withShellHandler", value: function withShellHandler(propName) { var eventId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default'; var propDefinition = { eventId: eventId, name: propName, featureIdentifier: this.config.identifier, type: 'handler' }; var propSpec = _defineProperty({}, propName, propDefinition); return new FeatureBuilder(_objectSpread(_objectSpread({}, this.config), {}, { __propSpecs: _objectSpread(_objectSpread({}, this.config.__propSpecs || {}), propSpec) })); } /** * Instruct that the Web script will receive events from the shell. * See {@link WebshellInvariantProps.webHandleRef}, {@link WebHandle.postMessageToWeb} and {@link WebjsContext.onShellMessage}. * * @param eventId - A unique identifier for the event received by the Web script. * * @typeparam I - A type for the event identifier. * @typeparam P - A type describing the shape of payloads sent to Web handlers. */ }, { key: "withWebHandler", value: function withWebHandler(eventId) { return new FeatureBuilder(_objectSpread(_objectSpread({}, this.config), {}, { __webSpecs: _objectSpread(_objectSpread({}, this.config.__webSpecs || {}), {}, _defineProperty({}, eventId, { async: false, eventId: eventId })) })); } /** * Assemble this builder object into a feature class. */ }, { key: "build", value: function build() { var _class, _temp; var _this$config = this.config, script = _this$config.script, featureIdentifier = _this$config.identifier, propSpecs = _this$config.__propSpecs, webSpecs = _this$config.__webSpecs, defaultOptions = _this$config.defaultOptions; var ctor = (_temp = _class = /*#__PURE__*/function (_Feature) { _inherits(ctor, _Feature); var _super = _createSuper(ctor); function ctor() { _classCallCheck(this, ctor); return _super.call(this, { script: script, identifier: featureIdentifier, defaultOptions: defaultOptions, propSpecs: propSpecs || [], webSpecs: webSpecs || {} }, (arguments.length <= 0 ? undefined : arguments[0]) || {}); } return ctor; }(_Feature2["default"]), _defineProperty(_class, "identifier", featureIdentifier), _temp); Object.defineProperty(ctor, 'displayName', { configurable: true, enumerable: false, writable: false, value: "Feature(".concat(featureIdentifier, ")") }); return ctor; } }]); return FeatureBuilder; }(); exports["default"] = FeatureBuilder; //# sourceMappingURL=FeatureBuilder.js.map