Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: import Shiny from module #74

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions inst/www/shiny.react/shiny-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -10624,7 +10624,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(prop_types__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react */ "react");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _isShiny__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./isShiny */ "./src/react/isShiny.ts");
/* harmony import */ var _Shiny__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./Shiny */ "./src/react/Shiny.ts");
/* harmony import */ var _mapReactData__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./mapReactData */ "./src/react/mapReactData.js");
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! lodash */ "./node_modules/lodash/lodash.js");
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_4__);
Expand Down Expand Up @@ -10663,8 +10663,8 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var updateHandlers = {};

if ((0,_isShiny__WEBPACK_IMPORTED_MODULE_2__["default"])()) {
window.Shiny.addCustomMessageHandler('updateReactInput', function (_ref) {
if ((0,_Shiny__WEBPACK_IMPORTED_MODULE_2__.isShiny)()) {
_Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.addCustomMessageHandler('updateReactInput', function (_ref) {
var inputId = _ref.inputId,
data = _ref.data;

Expand Down Expand Up @@ -10708,7 +10708,7 @@ function useValue(inputId, defaultValue, rateLimit) {
var ref = (0,react__WEBPACK_IMPORTED_MODULE_1__.useRef)();
(0,react__WEBPACK_IMPORTED_MODULE_1__.useEffect)(function () {
var setInputValue = function setInputValue(v) {
return window.Shiny.setInputValue(inputId, v);
return _Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.setInputValue(inputId, v);
};

if (rateLimit === undefined) {
Expand Down Expand Up @@ -10826,14 +10826,14 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _onceShinyInitialized__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./onceShinyInitialized */ "./src/react/onceShinyInitialized.js");
/* harmony import */ var _mapReactData__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./mapReactData */ "./src/react/mapReactData.js");
/* harmony import */ var _isShiny__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./isShiny */ "./src/react/isShiny.ts");
/* harmony import */ var _Shiny__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./Shiny */ "./src/react/Shiny.ts");





if ((0,_isShiny__WEBPACK_IMPORTED_MODULE_3__["default"])()) {
var binding = new window.Shiny.OutputBinding();
if ((0,_Shiny__WEBPACK_IMPORTED_MODULE_3__.isShiny)()) {
var binding = new _Shiny__WEBPACK_IMPORTED_MODULE_3__.Shiny.OutputBinding();

binding.find = function (scope) {
return scope.find('.react-container');
Expand All @@ -10842,11 +10842,11 @@ if ((0,_isShiny__WEBPACK_IMPORTED_MODULE_3__["default"])()) {
binding.renderValue = function (container, _ref) {
var data = _ref.data,
deps = _ref.deps;
window.Shiny.renderDependencies(deps);
_Shiny__WEBPACK_IMPORTED_MODULE_3__.Shiny.renderDependencies(deps);
react_dom__WEBPACK_IMPORTED_MODULE_0___default().render((0,_mapReactData__WEBPACK_IMPORTED_MODULE_2__["default"])(data), container);
};

window.Shiny.outputBindings.register(binding);
_Shiny__WEBPACK_IMPORTED_MODULE_3__.Shiny.outputBindings.register(binding);
}

function unmountContainersAtNode(node) {
Expand Down Expand Up @@ -10926,6 +10926,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _shinyBindings__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./shinyBindings */ "./src/react/shinyBindings.jsx");
/* harmony import */ var _Shiny__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./Shiny */ "./src/react/Shiny.ts");
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
Expand All @@ -10940,6 +10941,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }




var dataMappers = {};
function mapReactData(data) {
var type = data.type;
Expand Down Expand Up @@ -11059,7 +11061,7 @@ dataMappers.element = function (_ref7) {
dataMappers.event = function (_ref8) {
var id = _ref8.id;
return function () {
window.Shiny.setInputValue(id, true, {
_Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.setInputValue(id, true, {
priority: 'event'
});
};
Expand All @@ -11076,7 +11078,7 @@ dataMappers.input = function (_ref9) {
args[_key] = arguments[_key];
}

window.Shiny.setInputValue(id, getValue(args), {
_Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.setInputValue(id, getValue(args), {
priority: 'event'
});
};
Expand All @@ -11095,12 +11097,12 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (/* binding */ onceShinyInitialized)
/* harmony export */ });
/* harmony import */ var _isShiny__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./isShiny */ "./src/react/isShiny.ts");
/* harmony import */ var _Shiny__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Shiny */ "./src/react/Shiny.ts");
// Shiny initializes some functions after a delay:
// https://github.com/rstudio/shiny/blob/cda59da698eba1deda20ba09ca8b7f0b0b149f87/srcts/src/shiny/index.ts#L101

function shinyInitialized() {
return (0,_isShiny__WEBPACK_IMPORTED_MODULE_0__["default"])() ? window.Shiny.setInputValue !== undefined : true;
return (0,_Shiny__WEBPACK_IMPORTED_MODULE_0__.isShiny)() ? _Shiny__WEBPACK_IMPORTED_MODULE_0__.Shiny.setInputValue !== undefined : true;
} // Run `callback` and keep retrying with exponential backoff until it returns true.


Expand Down Expand Up @@ -11149,6 +11151,8 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(prop_types__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react */ "react");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _Shiny__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./Shiny */ "./src/react/Shiny.ts");



var regex = new RegExp([// `shiny::actionButton()` and `shiny::actionLink()`.
Expand All @@ -11165,10 +11169,10 @@ function ShinyBindingWrapper(_ref) {
var ref = (0,react__WEBPACK_IMPORTED_MODULE_1__.useRef)();
(0,react__WEBPACK_IMPORTED_MODULE_1__.useEffect)(function () {
var wrapper = ref.current;
window.Shiny.initializeInputs(wrapper);
window.Shiny.bindAll(wrapper);
_Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.initializeInputs(wrapper);
_Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.bindAll(wrapper);
return function () {
return window.Shiny.unbindAll(wrapper);
return _Shiny__WEBPACK_IMPORTED_MODULE_2__.Shiny.unbindAll(wrapper);
};
}, []);
return (
Expand Down Expand Up @@ -11476,20 +11480,22 @@ __webpack_require__.r(__webpack_exports__);

/***/ }),

/***/ "./src/react/isShiny.ts":
/*!******************************!*\
!*** ./src/react/isShiny.ts ***!
\******************************/
/***/ "./src/react/Shiny.ts":
/*!****************************!*\
!*** ./src/react/Shiny.ts ***!
\****************************/
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isShiny = exports.Shiny = void 0;
function isShiny() {
console.log(!!window.Shiny);
return !!window.Shiny;
}
exports["default"] = isShiny;
exports.isShiny = isShiny;
const Shiny = window.Shiny;
exports.Shiny = Shiny;


/***/ }),
Expand Down
2 changes: 1 addition & 1 deletion inst/www/shiny.react/shiny-react.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions js/src/react/Shiny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function isShiny(): boolean {
return !!window.Shiny
}

const Shiny = window.Shiny;

export { Shiny, isShiny }
6 changes: 3 additions & 3 deletions js/src/react/adapters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React, { useEffect, useState, useRef } from 'react';
import isShiny from './isShiny';
import { Shiny, isShiny } from './Shiny';
import mapReactData from './mapReactData';

export { throttle, debounce } from 'lodash';
Expand All @@ -15,7 +15,7 @@ export { throttle, debounce } from 'lodash';
const updateHandlers = {};

if (isShiny()) {
window.Shiny.addCustomMessageHandler('updateReactInput', ({ inputId, data }) => {
Shiny.addCustomMessageHandler('updateReactInput', ({ inputId, data }) => {
if (inputId in updateHandlers) {
updateHandlers[inputId](mapReactData(data));
} else throw new Error(`Attempted to update non-existent React input '${inputId}'`);
Expand Down Expand Up @@ -49,7 +49,7 @@ function useValue(inputId, defaultValue, rateLimit) {
const [value, setValue] = useState(defaultValue);
const ref = useRef();
useEffect(() => {
const setInputValue = (v) => window.Shiny.setInputValue(inputId, v);
const setInputValue = (v) => Shiny.setInputValue(inputId, v);
if (rateLimit === undefined) {
ref.current = setInputValue;
// No cleanup effect
Expand Down
8 changes: 4 additions & 4 deletions js/src/react/findAndRenderReactData.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import ReactDOM from 'react-dom';
import onceShinyInitialized from './onceShinyInitialized';
import mapReactData from './mapReactData';
import isShiny from './isShiny';
import { Shiny, isShiny } from './Shiny';

if (isShiny()) {
const binding = new window.Shiny.OutputBinding();
const binding = new Shiny.OutputBinding();
binding.find = (scope) => scope.find('.react-container');
binding.renderValue = (container, { data, deps }) => {
window.Shiny.renderDependencies(deps);
Shiny.renderDependencies(deps);
ReactDOM.render(mapReactData(data), container);
};
window.Shiny.outputBindings.register(binding);
Shiny.outputBindings.register(binding);
}

function unmountContainersAtNode(node) {
Expand Down
3 changes: 0 additions & 3 deletions js/src/react/isShiny.ts

This file was deleted.

5 changes: 3 additions & 2 deletions js/src/react/mapReactData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import { needsBindingWrapper, ShinyBindingWrapper } from './shinyBindings';
import { Shiny } from './Shiny';

const dataMappers = {};

Expand Down Expand Up @@ -86,14 +87,14 @@ dataMappers.element = ({ module, name, props: propsData }) => {
// on every call (this works thanks to `priority: 'event'`).
dataMappers.event = ({ id }) => (
() => {
window.Shiny.setInputValue(id, true, { priority: 'event' });
Shiny.setInputValue(id, true, { priority: 'event' });
}
);

// Used to implement `setInput()` R function.
dataMappers.input = ({ id, jsAccessor }) => {
const getValue = eval(`(args) => (args${jsAccessor})`); // eslint-disable-line no-eval
return (...args) => {
window.Shiny.setInputValue(id, getValue(args), { priority: 'event' });
Shiny.setInputValue(id, getValue(args), { priority: 'event' });
};
};
4 changes: 2 additions & 2 deletions js/src/react/onceShinyInitialized.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import isShiny from './isShiny';
import { Shiny, isShiny } from './Shiny';

// Shiny initializes some functions after a delay:
// https://github.com/rstudio/shiny/blob/cda59da698eba1deda20ba09ca8b7f0b0b149f87/srcts/src/shiny/index.ts#L101
function shinyInitialized() {
return isShiny() ? window.Shiny.setInputValue !== undefined : true;
return isShiny() ? Shiny.setInputValue !== undefined : true;
}

// Run `callback` and keep retrying with exponential backoff until it returns true.
Expand Down
7 changes: 4 additions & 3 deletions js/src/react/shinyBindings.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import PropTypes from 'prop-types';
import React, { useEffect, useRef } from 'react';
import { Shiny } from './Shiny';

const regex = new RegExp([
// `shiny::actionButton()` and `shiny::actionLink()`.
Expand All @@ -22,9 +23,9 @@ export function ShinyBindingWrapper({ children }) {
const ref = useRef();
useEffect(() => {
const wrapper = ref.current;
window.Shiny.initializeInputs(wrapper);
window.Shiny.bindAll(wrapper);
return () => window.Shiny.unbindAll(wrapper);
Shiny.initializeInputs(wrapper);
Shiny.bindAll(wrapper);
return () => Shiny.unbindAll(wrapper);
}, []);

return (
Expand Down