Skip to content

Commit

Permalink
Merge pull request #153 from near/feat/deprecate-widget
Browse files Browse the repository at this point in the history
feat: deprecate Widget
  • Loading branch information
andy-haynes authored Jan 3, 2024
2 parents 042ab80 + 9b6ab43 commit d13980a
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 29 deletions.
4 changes: 1 addition & 3 deletions packages/application/src/components/SandboxedIframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ ${scriptSrc}
const initContainer = ${initContainer.toString()};
// placeholder function to bind to Component/Widget references
// placeholder function to bind Component references in BOS Component source
function Component() {}
function Widget() {}
function useComponentCallback(cb, args) {
const [value, setValue] = useState(undefined);
Expand Down Expand Up @@ -99,7 +98,6 @@ ${scriptSrc}
Preact.render(createElement(BWEComponent, props), document.body);
}
},
Widget,
},
});
Expand Down
12 changes: 6 additions & 6 deletions packages/compiler/src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function parseWidgetRenders(transpiledComponent: string) {
function parseComponentRenders(transpiledComponent: string) {
const functionOffset = 'createElement'.length;
const componentRegex =
/createElement\((?:Widget|Component),\s*\{(?:[\w\W]*?)(?:\s*src:\s*["|'](?<src>((([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+)\/[\w.-]+))["|']/gi;
/createElement\(Component,\s*\{(?:[\w\W]*?)(?:\s*src:\s*["|'](?<src>((([a-z\d]+[\-_])*[a-z\d]+\.)*([a-z\d]+[\-_])*[a-z\d]+)\/[\w.-]+))["|']/gi;
return [...transpiledComponent.matchAll(componentRegex)].map((match) => {
const openParenIndex = match.index! + functionOffset;
let parenCount = 1;
Expand Down Expand Up @@ -36,9 +36,9 @@ export interface ParsedChildComponent {
export function parseChildComponents(
transpiledComponent: string
): ParsedChildComponent[] {
const widgetRenders = parseWidgetRenders(transpiledComponent);
widgetRenders.sort((a, b) => a.index - b.index);
return widgetRenders.map(({ expression, index, source }) => {
const componentRenders = parseComponentRenders(transpiledComponent);
componentRenders.sort((a, b) => a.index - b.index);
return componentRenders.map(({ expression, index, source }) => {
const [trustMatch] = [
...expression.matchAll(
/trust(?:\s*:\s*{(?:[\w\W])*?mode\s*:\s*['"](trusted-author|trusted|sandboxed))/gi
Expand All @@ -55,7 +55,7 @@ export function parseChildComponents(
if (!propsMatch?.index) {
return componentSource.replaceAll(
expression,
expression.replace(/(Widget|Component)/, componentName)
expression.replace(/Component/, componentName)
);
}

Expand Down
3 changes: 0 additions & 3 deletions packages/container/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export function initContainer({
parentContainerId,
trust,
updateContainerProps,
Widget, // TODO remove when <Widget /> no longer supported
},
}: InitContainerParams) {
const callbacks: { [key: string]: Function } = {};
Expand All @@ -39,7 +38,6 @@ export function initContainer({
buildRequest,
callbacks,
isComponent: (c) => c === Component,
isWidget: (c) => c === Widget, // TODO remove when <Widget /> no longer supported
parentContainerId,
postCallbackInvocationMessage,
requests,
Expand All @@ -50,7 +48,6 @@ export function initContainer({
isComponent: (c) => c === Component,
isFragment: (c) => c === Fragment,
isRootComponent: (c) => c === BWEComponent,
isWidget: (c) => c === Widget, // TODO remove when <Widget /> no longer supported
postComponentRenderMessage,
serializeNode,
trust,
Expand Down
7 changes: 1 addition & 6 deletions packages/container/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export const composeRenderMethods: ComposeRenderMethodsCallback = ({
isRootComponent,
isComponent,
isFragment,
isWidget, // TODO remove when <Widget /> no longer supported
postComponentRenderMessage,
serializeNode,
trust,
Expand Down Expand Up @@ -151,11 +150,7 @@ export const composeRenderMethods: ComposeRenderMethodsCallback = ({
)
: node.props;

if (
typeof node.type === 'function' &&
!isComponent(node.type) &&
!isWidget(node.type)
) {
if (typeof node.type === 'function' && !isComponent(node.type)) {
if (isBWEComponent(node) && !isRootComponent(node.type)) {
const componentNode = buildBWEComponentNode(
node as BWEComponentNode,
Expand Down
9 changes: 1 addition & 8 deletions packages/container/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
buildRequest,
callbacks,
isComponent,
isWidget,
parentContainerId,
postCallbackInvocationMessage,
requests,
Expand Down Expand Up @@ -297,16 +296,10 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
});

if (typeof type === 'function') {
if (!isWidget(type) && !isComponent(type)) {
if (!isComponent(type)) {
throw new Error(`unrecognized Component function ${type.name}`);
}

if (isWidget(type)) {
console.warn(
'<Widget /> will be deprecated in upcoming versions of BOS Web Engine. Please update your code to reference <Component /> instead.'
);
}

const { child, placeholder } = serializeChildComponent({
parentId,
props,
Expand Down
3 changes: 0 additions & 3 deletions packages/container/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ interface ComposeRenderMethodsParams {
isComponent: (component: Function) => boolean;
isFragment: (component: Function) => boolean;
isRootComponent: (component: Function) => boolean;
isWidget: (component: Function) => boolean; // TODO remove when <Widget /> no longer supported
postComponentRenderMessage: PostMessageComponentRenderCallback;
serializeNode: SerializeNodeCallback;
trust: ComponentTrust;
Expand All @@ -101,7 +100,6 @@ export interface ComposeSerializationMethodsParams {
buildRequest: BuildRequestCallback;
callbacks: CallbackMap;
isComponent: (component: Function) => boolean;
isWidget: (component: Function) => boolean; // TODO remove when <Widget /> no longer supported
parentContainerId: string | null;
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
requests: RequestMap;
Expand Down Expand Up @@ -165,7 +163,6 @@ export interface InitContainerParams {
props: any;
trust: ComponentTrust;
updateContainerProps: UpdateContainerPropsCallback;
Widget: Function; // TODO remove when <Widget /> no longer supported
};
}

Expand Down

0 comments on commit d13980a

Please sign in to comment.