Skip to content

Commit e1c8579

Browse files
committed
Refactor into initialize function
1 parent 5d857b0 commit e1c8579

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

src/index.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as React from "react";
22
import { useState, useEffect } from "react";
33
import { createRender, useModelState, useModel } from "@anywidget/react";
4-
import type { Initialize, Render } from "@anywidget/types";
4+
import type { Initialize, InitializeProps, Render } from "@anywidget/types";
55
import Map from "react-map-gl/maplibre";
66
import DeckGL from "@deck.gl/react/typed";
77
import { MapViewState, type Layer } from "@deck.gl/core/typed";
88
import { BaseLayerModel, initializeLayer } from "./model/index.js";
99
import type { WidgetModel } from "@jupyter-widgets/base";
10-
import { useParquetWasm } from "./parquet.js";
10+
import { initParquetWasmFromBinary } from "./parquet.js";
1111
import { getTooltip } from "./tooltip/index.js";
1212
import { isDefined, loadChildModels } from "./util.js";
1313
import { v4 as uuidv4 } from "uuid";
@@ -63,10 +63,6 @@ async function getChildModelState(
6363
function App() {
6464
let model = useModel();
6565

66-
let [parquetWasmBinary] = useModelState<DataView | null>(
67-
"_parquet_wasm_content",
68-
);
69-
let [parquetWasmReady] = useParquetWasm(parquetWasmBinary);
7066
let [mapStyle] = useModelState<string>("basemap_style");
7167
let [mapHeight] = useModelState<number>("_height");
7268
let [showTooltip] = useModelState<boolean>("show_tooltip");
@@ -110,15 +106,7 @@ function App() {
110106
let [stateCounter, setStateCounter] = useState<Date>(new Date());
111107

112108
useEffect(() => {
113-
if (!parquetWasmReady) {
114-
return;
115-
}
116-
117109
const callback = async () => {
118-
if (!parquetWasmReady) {
119-
throw new Error("inside callback but parquetWasm not ready!");
120-
}
121-
122110
const childModels = await loadChildModels(
123111
model.widget_manager,
124112
childLayerIds,
@@ -132,7 +120,7 @@ function App() {
132120
setSubModelState(newSubModelState);
133121
};
134122
callback().catch(console.error);
135-
}, [parquetWasmReady, childLayerIds]);
123+
}, [childLayerIds]);
136124

137125
const layers: Layer[] = [];
138126
for (const subModel of Object.values(subModelState)) {
@@ -200,7 +188,13 @@ function App() {
200188
);
201189
}
202190

191+
async function initialize({ model }: InitializeProps): Promise<void> {
192+
const parquetWasmBinary: DataView = model.get("_parquet_wasm_content");
193+
await initParquetWasmFromBinary(parquetWasmBinary);
194+
}
195+
203196
const module: { render: Render; initialize?: Initialize } = {
197+
initialize,
204198
render: createRender(App),
205199
};
206200

src/parquet.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useEffect, useState } from "react";
21
import { initSync, readParquet } from "parquet-wasm/esm/arrow2";
32
import * as arrow from "apache-arrow";
43

@@ -17,20 +16,13 @@ async function decompressBlob(blob: Blob) {
1716
*
1817
* @return Whether initialization succeeded
1918
*/
20-
export async function initParquetWasmFromBinary(
21-
view: DataView | null,
22-
): Promise<boolean> {
23-
if (!view) {
24-
return false;
25-
}
26-
19+
export async function initParquetWasmFromBinary(view: DataView): Promise<void> {
2720
let blob = new Blob([view]);
2821
const decompressedBlob = await decompressBlob(blob);
2922
const decompressedBuffer = await decompressedBlob.arrayBuffer();
3023

3124
initSync(decompressedBuffer);
3225
WASM_READY = true;
33-
return true;
3426
}
3527

3628
/**
@@ -74,21 +66,3 @@ export function parseParquetBuffers(dataViews: DataView[]): arrow.Table {
7466

7567
return new arrow.Table(batches);
7668
}
77-
78-
export function useParquetWasm(view: DataView | null): [boolean] {
79-
const [wasmReady, setWasmReady] = useState<boolean>(false);
80-
81-
// Init parquet wasm
82-
useEffect(() => {
83-
const callback = async () => {
84-
const succeeded = await initParquetWasmFromBinary(view);
85-
if (succeeded) {
86-
setWasmReady(true);
87-
}
88-
};
89-
90-
callback();
91-
}, []);
92-
93-
return [wasmReady];
94-
}

0 commit comments

Comments
 (0)