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

RFC: initial version of globalState hook #124

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCache } from "./lib/data/cache";
import * as utils from "./lib/data/utils";
import { CommitButton } from "./lib/components/Commit";
import { EthersProviderContext } from "./lib/components/ethers";

import { GlobalStateContext } from "./lib/components/globalState";
export {
Widget,
CommitButton,
Expand All @@ -17,4 +17,5 @@ export {
useAccountId,
utils,
EthersProviderContext,
GlobalStateContext,
};
4 changes: 4 additions & 0 deletions src/lib/components/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useAccountId } from "../data/account";
import Big from "big.js";
import uuid from "react-uuid";
import { EthersProviderContext } from "./ethers";
import { GlobalStateContext } from "./globalState";

const computeSrcOrCode = (src, code, configs) => {
let srcOrCode = src ? { src } : code ? { code } : null;
Expand Down Expand Up @@ -74,6 +75,7 @@ export const Widget = React.forwardRef((props, forwardedRef) => {
const [configs, setConfigs] = useState(null);
const [srcOrCode, setSrcOrCode] = useState(null);
const ethersProviderContext = useContext(EthersProviderContext);
const globalStateContext = useContext(GlobalStateContext);

const networkId =
configs &&
Expand Down Expand Up @@ -189,6 +191,7 @@ export const Widget = React.forwardRef((props, forwardedRef) => {
version: uuid(),
widgetConfigs: configs,
ethersProviderContext,
globalStateContext,
});
setVm(vm);
return () => {
Expand All @@ -203,6 +206,7 @@ export const Widget = React.forwardRef((props, forwardedRef) => {
confirmTransactions,
configs,
ethersProviderContext,
globalStateContext
]);

useEffect(() => {
Expand Down
3 changes: 3 additions & 0 deletions src/lib/components/globalState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from "react";

export const GlobalStateContext = React.createContext(null);
26 changes: 25 additions & 1 deletion src/lib/vm/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const ApprovedTagsSimple = {
const ApprovedTagsCustom = {
Widget: false,
CommitButton: true,
GlobalStateProvider: true,
IpfsImageUpload: false,
Markdown: false,
Fragment: true,
Expand Down Expand Up @@ -621,6 +622,16 @@ class VmStack {
);
}

if (element === "GlobalStateProvider") {
if(!attributes.value) {
throw new Error("GlobalStateProvider requires a value prop");
}
if(!isArray(attributes.value) || attributes.value.length !== 2) {
throw new Error("Invalid value prop for GlobalStateProvider");
}
this.vm.globalStateContext = attributes.value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I don't understand this part. Shouldn't we just create a wrapper of React.Context?

  2. Why inject [state, setState] here instead of context by name?

}

const children = code.children.map((child, i) => {
this.vm.gIndex = i;
return this.executeExpression(child);
Expand Down Expand Up @@ -995,6 +1006,18 @@ class VmStack {
return f(...args);
}
return this.vm.cachedEthersCall(callee, args);
} else if (callee === "useGlobalState") {
if(args.length < 1 || !isString(args[0])) {
throw new Error("Method: useGlobalState. Requires string argument");
}
const [globalState, setGlobalState] = this.vm.globalStateContext
return [
globalState[args[0]],
(value) => setGlobalState({
...globalState,
[args[0]]: value,
})
];
} else if (keyword === "WebSocket") {
if (callee === "WebSocket") {
const websocket = new WebSocket(...args);
Expand Down Expand Up @@ -1875,6 +1898,7 @@ export default class VM {
version,
widgetConfigs,
ethersProviderContext,
globalStateContext,
isModule,
} = options;

Expand Down Expand Up @@ -1931,7 +1955,7 @@ export default class VM {
this.ethersProvider = ethersProviderContext?.provider
? new ethers.providers.Web3Provider(ethersProviderContext.provider)
: null;

this.globalStateContext = globalStateContext;
this.timeouts = new Set();
this.intervals = new Set();
this.websockets = [];
Expand Down