Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
fix(plugin): layers would not be marshalled correctly (#126)
Browse files Browse the repository at this point in the history
* fix(plugin): layers would not be marshalled correctly

* improve type
  • Loading branch information
rot1024 committed Nov 9, 2021
1 parent 7c65d0c commit 886302f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/atoms/Plugin/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type Options = {
};

// restrict any classes
const defaultIsMarshalable = (obj: any): boolean => {
export const defaultIsMarshalable = (obj: any): boolean => {
return (
((typeof obj !== "object" || obj === null) && typeof obj !== "function") ||
Array.isArray(obj) ||
Expand Down
5 changes: 3 additions & 2 deletions src/components/atoms/Plugin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { IframeHTMLAttributes, ReactNode } from "react";

import useHook, { IFrameAPI as IFrameAPIType } from "./hooks";
import useHook, { IFrameAPI } from "./hooks";
import IFrame from "./IFrame";

export type IFrameAPI = IFrameAPIType;
export { defaultIsMarshalable } from "./hooks";
export type { IFrameAPI } from "./hooks";

export type Props = {
className?: string;
Expand Down
4 changes: 4 additions & 0 deletions src/components/molecules/Visualizer/Layers/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export class LayerStore {
return l;
}

isLayer = (obj: any): obj is PluginLayer => {
return typeof obj === "object" && Object.getPrototypeOf(obj) === this.#prototype;
};

findById = (id: string): PluginLayer | undefined => {
return this.#pmap.get(id);
};
Expand Down
3 changes: 3 additions & 0 deletions src/components/molecules/Visualizer/Plugin/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ export function commonReearth({
get overrideProperty() {
return overrideLayerProperty;
},
get isLayer() {
return layers().isLayer;
},
get layers() {
return layers().root.children ?? [];
},
Expand Down
17 changes: 15 additions & 2 deletions src/components/molecules/Visualizer/Plugin/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Options } from "quickjs-emscripten-sync";
import { useCallback, useEffect, useMemo, useRef } from "react";

import { IFrameAPI } from "@reearth/components/atoms/Plugin";
import type { IFrameAPI } from "@reearth/components/atoms/Plugin";
import { defaultIsMarshalable } from "@reearth/components/atoms/Plugin";
import events, { EventEmitter, Events, mergeEvents } from "@reearth/util/event";

import { exposed } from "./api";
Expand Down Expand Up @@ -119,6 +120,18 @@ export function useAPI({
event.current?.[1]("message", msg);
}, []);

const isMarshalable = useCallback(
(target: any) => {
if (defaultIsMarshalable(target)) return true;
if (ctx?.reearth.layers.isLayer(target)) return true;
if (typeof ctx?.engine.isMarshalable === "function") {
return ctx.engine.isMarshalable(target);
}
return ctx?.engine.isMarshalable || false;
},
[ctx?.engine, ctx?.reearth.layers],
);

const staticExposed = useMemo((): ((api: IFrameAPI) => GlobalThis) | undefined => {
if (!ctx?.reearth) return;
return ({ postMessage, render }: IFrameAPI) => {
Expand Down Expand Up @@ -157,7 +170,7 @@ export function useAPI({

return {
staticExposed,
isMarshalable: ctx?.engine.isMarshalable,
isMarshalable,
onMessage,
onPreInit,
onDispose,
Expand Down
1 change: 1 addition & 0 deletions src/components/molecules/Visualizer/Plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export type Layers = {
readonly walk: <T>(
fn: (layer: Layer, index: number, parents: Layer[]) => T | void,
) => T | undefined;
readonly isLayer: (obj: any) => obj is Layer;
readonly overrideProperty: (id: string, property: any) => void;
};

Expand Down

0 comments on commit 886302f

Please sign in to comment.