Skip to content

Commit

Permalink
linting, use emptyObject for serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl authored Mar 24, 2023
1 parent 51103c0 commit bd363b2
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import $ from 'jquery';

import { NativeView } from './nativeview';

import { JSONObject, JSONValue } from '@lumino/coreutils';
import { JSONObject, JSONValue, JSONExt } from '@lumino/coreutils';

import { Message, MessageLoop } from '@lumino/messaging';

Expand Down Expand Up @@ -37,7 +37,8 @@ const IPY_MODEL_ = 'IPY_MODEL_';
/**
* A best-effort method for performing deep copies.
*/
const deepcopy = globalThis.structuredClone || ((x: any) => JSON.parse(JSON.stringify(x)));
const deepcopy =
globalThis.structuredClone || ((x: any) => JSON.parse(JSON.stringify(x)));

/**
* Replace model ids with models recursively.
Expand All @@ -48,13 +49,13 @@ export function unpack_models(
): Promise<WidgetModel | Dict<WidgetModel> | WidgetModel[] | any> {
if (Array.isArray(value)) {
const unpacked: any[] = [];
for(const sub_value of value) {
for (const sub_value of value) {
unpacked.push(unpack_models(sub_value, manager));
}
return Promise.all(unpacked);
} else if (value instanceof Object && typeof value !== 'string') {
const unpacked: { [key: string]: any } = {};
for(const [key, sub_value] of Object.entries(value)) {
for (const [key, sub_value] of Object.entries(value)) {
unpacked[key] = unpack_models(sub_value, manager);
}
return utils.resolvePromisesDict(unpacked);
Expand All @@ -70,22 +71,22 @@ export function unpack_models(
*
* If the commonly-used `unpack_models` is given as the `seralize` method,
* and no `deserialize` is given, this will be used as a default.
*/
*/
export function pack_models(
value: WidgetModel | Dict<WidgetModel> | WidgetModel[] | any,
widget?: WidgetModel
): any | Dict<unknown> | string | (Dict<unknown> | string)[] {
if (Array.isArray(value)) {
const model_ids: string[] = [];
for(const model of value) {
for (const model of value) {
model_ids.push(pack_models(model, widget));
}
return model_ids;
} else if (value instanceof WidgetModel) {
return `${IPY_MODEL_}${value.model_id}`;
} else if (value instanceof Object && typeof value !== 'string' ) {
} else if (value instanceof Object && typeof value !== 'string') {
const packed: { [key: string]: string } = {};
for(const [key, sub_value] of Object.entries(value)) {
for (const [key, sub_value] of Object.entries(value)) {
packed[key] = pack_models(sub_value, widget);
}
} else {
Expand Down Expand Up @@ -565,7 +566,7 @@ export class WidgetModel extends Backbone.Model {
(this.constructor as typeof WidgetModel).serializers || {};
for (const k of Object.keys(state)) {
try {
let { serialize, deserialize } = serializers[k];
let { serialize, deserialize } = serializers[k] || JSONExt.emptyObject;

if (serialize == null && deserialize === unpack_models) {
// handle https://github.com/jupyter-widgets/ipywidgets/issues/3735
Expand Down

0 comments on commit bd363b2

Please sign in to comment.