@anywidget/deno
Advanced tools
Comparing version 0.0.5 to 0.1.0
# @anywidget/deno | ||
## 0.1.0 | ||
### Minor Changes | ||
- Switch to sync API to define widget ([#487](https://github.com/manzt/anywidget/pull/487)) | ||
## 0.0.5 | ||
@@ -4,0 +10,0 @@ |
40
mod.ts
@@ -1,3 +0,2 @@ | ||
import * as path from "https://deno.land/std@0.203.0/path/mod.ts"; | ||
import mitt, { type Emitter } from "npm:mitt@3"; | ||
import * as path from "jsr:@std/path@0.220.1"; | ||
import { find_data_dir } from "./jupyter_paths.ts"; | ||
@@ -113,11 +112,8 @@ | ||
class Model<State> { | ||
_state: State; | ||
_emitter: Emitter<ChangeEvents<State>>; | ||
private _state: State; | ||
private _target: EventTarget; | ||
constructor(state: State) { | ||
this._state = state; | ||
// types are messed up for mitt. | ||
this._emitter = (mitt as unknown as typeof mitt.default)< | ||
ChangeEvents<State> | ||
>(); | ||
this._target = new EventTarget(); | ||
} | ||
@@ -129,3 +125,3 @@ get<K extends keyof State>(key: K): State[K] { | ||
// @ts-expect-error can't convince TS that K is a key of State | ||
this._emitter.emit(`change:${key}`, value); | ||
this._emit(`change:${key}`, value); | ||
this._state[key] = value; | ||
@@ -135,6 +131,10 @@ } | ||
name: Event, | ||
callback: (data: ChangeEvents<State>[Event]) => void, | ||
callback: () => void, | ||
): void { | ||
this._emitter.on(name, callback); | ||
this._target.addEventListener(name, callback); | ||
} | ||
private _emit(name: string, data: unknown) { | ||
this._target.dispatchEvent(new CustomEvent(name, { detail: data })); | ||
} | ||
} | ||
@@ -172,3 +172,3 @@ | ||
export async function widget<State>({ | ||
export function widget<State>({ | ||
state, | ||
@@ -181,7 +181,10 @@ render, | ||
let comm = new Comm({ anywidget_version: version }); | ||
await comm.init(); | ||
await comm.send_state({ ...state, _esm: to_esm({ imports, render }) }); | ||
let init_promise = comm | ||
.init() | ||
.then(() => | ||
comm.send_state({ ...state, _esm: to_esm({ imports, render }) }) | ||
); | ||
for (let key in state) { | ||
model.on(`change:${key}`, (data) => { | ||
comm.send_state({ [key]: data }); | ||
model.on(`change:${key}`, () => { | ||
comm.send_state({ [key]: model.get(key) }); | ||
}); | ||
@@ -192,3 +195,6 @@ } | ||
if (prop === Symbol.for("Jupyter.display")) { | ||
return comm.mimebundle.bind(comm); | ||
return async () => { | ||
await init_promise; | ||
return comm.mimebundle(); | ||
}; | ||
} | ||
@@ -195,0 +201,0 @@ return Reflect.get(target, prop, receiver); |
{ | ||
"name": "@anywidget/deno", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "A stub-package for making releases of the Deno anywidget mod.ts with changesets" | ||
} |
@@ -12,3 +12,3 @@ # anywidget (deno) | ||
const model = await widget({ | ||
const model = widget({ | ||
state: { letters: "abcd" }, | ||
@@ -15,0 +15,0 @@ imports: `\ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18348
515