Skip to content

Commit

Permalink
feat: add runTimes property to PluginInstance (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 authored Jan 25, 2023
1 parent e8647af commit 17d7870
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/components/molecules/Visualizer/Plugin/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import { exposed } from "./api";
import { useContext } from "./context";
import type { PluginModalInfo } from "./ModalContainer";
import type { PluginPopupInfo } from "./PopupContainer";
import type { Layer, Widget, Block, GlobalThis, ReearthEventType } from "./types";
import type {
Layer,
Widget,
Block,
GlobalThis,
ReearthEventType,
WidgetLocationOptions,
} from "./types";

export default function ({
pluginId,
Expand Down Expand Up @@ -389,17 +396,16 @@ export function useAPI({
},
startEventLoop,
overrideSceneProperty: ctx.overrideSceneProperty,
moveWidget: ctx.moveWidget,
moveWidget: (widgetId: string, options: WidgetLocationOptions) => {
ctx.moveWidget?.(widgetId, options);
ctx.pluginInstances.runTimesCache.increment(widgetId);
},
pluginPostMessage: ctx.pluginInstances.postMessage,
clientStorage: ctx.clientStorage,
});
};
}, [
ctx?.reearth,
ctx?.overrideSceneProperty,
ctx?.moveWidget,
ctx?.pluginInstances,
ctx?.clientStorage,
ctx,
extensionId,
extensionType,
pluginId,
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 @@ -105,6 +105,7 @@ export type PluginExtensionInstance = {
readonly name: string;
readonly extensionId: string;
readonly extensionType: "widget" | "block";
readonly runTimes: number | undefined; // Count number of plugin is run
};

export type Plugins = {
Expand Down
7 changes: 7 additions & 0 deletions src/components/molecules/Visualizer/storybook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export const context: ProviderProps = {
postMessage: () => {},
addPluginMessageSender: () => {},
removePluginMessageSender: () => {},
runTimesCache: {
get: () => 1,
increment: () => {},
decrement: () => {},
clear: () => {},
clearAll: () => {},
},
},
clientStorage: {
getAsync: act("clientStorage.getAsync"),
Expand Down
33 changes: 31 additions & 2 deletions src/components/molecules/Visualizer/usePluginInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,30 @@ export type PluginInstances = {
postMessage: (id: string, msg: any, sender: string) => void;
addPluginMessageSender: (id: string, msgSender: (msg: string) => void) => void;
removePluginMessageSender: (id: string) => void;
runTimesCache: {
get: (id: string) => number | undefined;
increment: (id: string) => void;
decrement: (id: string) => void;
clear: (id: string) => void;
clearAll: () => void;
};
};

export default ({ alignSystem, floatingWidgets, blocks }: Props) => {
const pluginInstancesMeta = useRef<PluginExtensionInstance[]>([]);

const runTimesCache = useMemo<Map<string, number>>(() => new Map(), []);
const runTimesCacheHandler = useMemo(
() => ({
get: (id: string) => runTimesCache.get(id),
increment: (id: string) => runTimesCache.set(id, (runTimesCache.get(id) || 0) + 1),
decrement: (id: string) => runTimesCache.set(id, (runTimesCache.get(id) || 0) - 1),
clear: (id: string) => runTimesCache.set(id, 0),
clearAll: () => runTimesCache.clear(),
}),
[runTimesCache],
);

useEffect(() => {
const instances: PluginExtensionInstance[] = [];

Expand All @@ -40,6 +59,9 @@ export default ({ alignSystem, floatingWidgets, blocks }: Props) => {
name: getExtensionInstanceName(widget.pluginId ?? ""),
extensionId: widget.extensionId ?? "",
extensionType: "widget",
get runTimes() {
return runTimesCache.get(widget.id);
},
});
});
}
Expand All @@ -58,6 +80,9 @@ export default ({ alignSystem, floatingWidgets, blocks }: Props) => {
name: getExtensionInstanceName(widget.pluginId ?? ""),
extensionId: widget.extensionId ?? "",
extensionType: "widget",
get runTimes() {
return runTimesCache.get(widget.id);
},
});
});
}
Expand All @@ -70,12 +95,15 @@ export default ({ alignSystem, floatingWidgets, blocks }: Props) => {
name: getExtensionInstanceName(block.pluginId ?? ""),
extensionId: block.extensionId ?? "",
extensionType: "block",
get runTimes() {
return runTimesCache.get(block.id);
},
});
});
}

pluginInstancesMeta.current = instances;
}, [alignSystem, floatingWidgets, blocks]);
}, [alignSystem, floatingWidgets, blocks, runTimesCache]);

const pluginMessageSenders = useRef<Map<string, (msg: any) => void>>(new Map());

Expand All @@ -102,8 +130,9 @@ export default ({ alignSystem, floatingWidgets, blocks }: Props) => {
removePluginMessageSender: (id: string) => {
pluginMessageSenders.current?.delete(id);
},
runTimesCache: runTimesCacheHandler,
};
}, [pluginInstancesMeta]);
}, [pluginInstancesMeta, runTimesCacheHandler]);

return pluginInstances;
};
Expand Down

0 comments on commit 17d7870

Please sign in to comment.