Skip to content

Instantly share code, notes, and snippets.

View MichaelBelousov's full-sized avatar

Michael Belousov MichaelBelousov

View GitHub Profile
@MichaelBelousov
MichaelBelousov / Lazy.ts
Last active July 30, 2020 16:39
glorious lazy iterables in typescript/javascript
/** return whether arg is T or an iterable of T
* for the flat function */
function isIterable<T>(arg: T | Iterable<T>): arg is Iterable<T> {
return typeof arg === "object" && Symbol.iterator in arg;
}
/** iterable wrapper for functional programming with lazy composition */
export default class Lazy<T> implements Iterator<T> {
static from<T>(iterable: Iterable<T>) {
@MichaelBelousov
MichaelBelousov / PlainMarker.tsx
Last active October 30, 2020 13:43
A basic imodeljs marker integration with a React UI
import React from "react";
import ReactDOM from "react-dom";
import { BeButtonEvent, DecorateContext, Decorator, Marker, IModelApp } from "@bentley/imodeljs-frontend";
import { Point3d, XAndY, XYAndZ } from "@bentley/geometry-core";
// a weak map would probably be better to prevent leaks
const reactSetHoverStateMap = new Map<PinMarker, (val: boolean) => void>();
function InMarkerComponent(props: {markerInstance: PinMarker}) {
const [isHovered, setIsHovered] = React.useState(false);
@MichaelBelousov
MichaelBelousov / useMarker-Sample.tsx
Last active October 30, 2020 13:32
introducing imodel react hooks useMarker sample
import React, { useState } from "react";
import { Point3d } from "@bentley/geometry-core";
import { useMarker } from "@bentley/imodel-react-hooks";
const MyJsxMarker = (props: { worldLocation: Point3d }) => {
const [isHovered, setIsHovered] = useState(false);
useMarker({
worldLocation: props.worldLocation,
size: [70, 20],
onMouseEnter: () => setIsHovered(true),
@MichaelBelousov
MichaelBelousov / useFeatureOverrides-sample.tsx
Last active October 30, 2020 13:23
introducing imodel-react-hooks article useFeatureOverrides sample
import { ViewportComponent } from "@bentley/ui-components";
import { myIModel, myViewDefId, iModelInnerModels, useFetchAppMarkers, ElemMarkerData } from "./my-app-state";
import { FeatureOverrideReactProvider, useFeatureOverrides, useMarker, IModelJsViewProvider } from "@bentley/imodel-react-hooks";
import { FeatureAppearance } from "@bentley/imodeljs-common";
import { Id64Array } from "@bentley/bentleyjs-core";
function RedElementMarker(props: ElemMarkerData) {
const RED = {r: 255, g: 0, b: 0};
useMarker({ worldLocation: props.worldLocation, image: props.someSvgPath });
useFeatureOverrides({ // override the one element to be red
@MichaelBelousov
MichaelBelousov / join_meshes.py
Last active January 21, 2021 00:46
generated exploded combined mesh for selected to active bake blender
import bpy
lp_meshes = [obj for obj in bpy.data.collections['lp'].all_objects if obj.type == 'MESH']
bpy.ops.object.select_all(action='DESELECT')
for obj in bpy.data.collections['bake'].all_objects:
if (obj.type == 'MESH'
and obj.name not in ('combined_cage', 'combined_lp')):
bpy.data.objects.remove(obj, do_unlink=True)
@MichaelBelousov
MichaelBelousov / server.js
Last active January 27, 2021 15:29
even more hooks - create server script
const app = require("express")();
app.use(require('cors')());
app.get("/tank-params", (req, res) => {
res.json({
level: Math.random(),
pressure: Math.random(),
});
});
@MichaelBelousov
MichaelBelousov / install-deps-serve.sh
Created January 27, 2021 14:24
even more hooks - install deps and run server
yarn add express@^4.16.0 cors @bentley/imodel-react-hooks @bentley/react-hooks
node src/server.js
@MichaelBelousov
MichaelBelousov / TankMarker.tsx
Last active January 28, 2021 13:24
even more hooks - TankMarker
import React from "react";
import { Marker } from "@bentley/imodel-react-hooks";
import { useAsyncInterval } from "@bentley/react-hooks";
import { Point3d } from "@bentley/geometry-core";
export default function TankMarker({tankParamType}: {tankParamType: "level" | "pressure"}) {
const [tankParamValue, setTankParamValue] = React.useState<number>();
useAsyncInterval(
async ({ isStale, setCancel }) => {
@MichaelBelousov
MichaelBelousov / App.tsx
Last active January 28, 2021 13:23
even more hooks - show the marker in App
// at the top add these imports
import TankMarker from "./TankMarker.tsx";
import { Viewer } from "@bentley/itwin-viewer-react";
import { IModelJsViewProvider } from "@bentley/imodel-react-hooks";
const App = () => {//...
//... at the bottom of the file replace the return statement with this
const [imjsInited, setImjsInited] = useState(false);
const [tankParamType, setTankParamType] = useState<"level" | "pressure">("level");
@MichaelBelousov
MichaelBelousov / TankMarker.tsx
Last active January 28, 2021 13:20
even more hooks - use useAsyncInterval
import { useAsyncInterval } from "@bentley/react-hooks";
//...
useAsyncInterval(
async ({isStale, setCancel}) => {
const aborter = new AbortController();
setCancel(() => aborter.abort());
const response = await fetch("http://localhost:3001/tank-params", { signal: aborter.signal });
const result = await response.json();
if (!isStale()) setTankParamValue(result[tankParamType]);
},