Skip to content

Commit

Permalink
Upgrade @types/backbone
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongrout authored and jtpio committed Jun 16, 2021
1 parent 5dcc7fc commit 871f2da
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
24 changes: 13 additions & 11 deletions packages/base-manager/src/manager-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
PROTOCOL_VERSION,
IWidgetManager,
IModelOptions,
IWidgetOptions
IWidgetOptions,
IBackboneModelOptions
} from '@jupyter-widgets/base';

import { base64ToBuffer, bufferToBase64, hexToBuffer } from './utils';
Expand Down Expand Up @@ -52,6 +53,11 @@ export interface IBase64Buffers extends PartialJSONObject {
encoding: 'base64';
}

/**
* Make all properties in K (of T) required
*/
export type RequiredSome<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;

/**
* Manager abstract base class
*/
Expand Down Expand Up @@ -270,25 +276,21 @@ export abstract class ManagerBase implements IWidgetManager {
options: IModelOptions,
serialized_state: any = {}
): Promise<WidgetModel> {
let model_id;
if (options.model_id) {
model_id = options.model_id;
} else if (options.comm) {
model_id = options.model_id = options.comm.comm_id;
} else {
const model_id = options.model_id ?? options.comm?.comm_id;
if (!model_id) {
throw new Error(
'Neither comm nor model_id provided in options object. At least one must exist.'
);
}

const modelPromise = this._make_model(options, serialized_state);
options.model_id = model_id;
const modelPromise = this._make_model(options as RequiredSome<IModelOptions, 'model_id'>, serialized_state);
// this call needs to happen before the first `await`, see note in `set_state`:
this.register_model(model_id, modelPromise);
return await modelPromise;
}

async _make_model(
options: IModelOptions,
options: RequiredSome<IModelOptions, 'model_id'>,
serialized_state: any = {}
): Promise<WidgetModel> {
const model_id = options.model_id;
Expand All @@ -315,7 +317,7 @@ export abstract class ManagerBase implements IWidgetManager {
serialized_state,
this
);
const modelOptions = {
const modelOptions: IBackboneModelOptions = {
widget_manager: this,
model_id: model_id,
comm: options.comm
Expand Down
2 changes: 1 addition & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@lumino/coreutils": "^1.2.0",
"@lumino/messaging": "^1.2.1",
"@lumino/widgets": "^1.3.0",
"@types/backbone": "1.4.1",
"@types/backbone": "1.4.10",
"@types/lodash": "^4.14.134",
"backbone": "1.4.0",
"jquery": "^3.1.1",
Expand Down
14 changes: 7 additions & 7 deletions packages/base/src/nativeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class NativeView<T extends Backbone.Model> extends Backbone.View<T> {
_setAttributes(attrs: Backbone.ObjectHash): void {
for (const attr in attrs) {
attr in this.el
? (this.el[attr] = attrs[attr])
? ((this.el as any)[attr] = attrs[attr])
: this.el.setAttribute(attr, attrs[attr]);
}
}
Expand All @@ -99,17 +99,17 @@ export class NativeView<T extends Backbone.Model> extends Backbone.View<T> {
* https://github.com/jquery/jquery/blob/7d21f02b9ec9f655583e898350badf89165ed4d5/src/event.js#L442
* for some similar exceptional cases).
*/
delegate(eventName: string, listener: Function): Backbone.View<T>;
delegate(eventName: string, listener: Function): this;
delegate(
eventName: string,
selector: string,
listener: Function
): Backbone.View<T>;
): this;
delegate(
eventName: string,
selector: string | Function,
listener?: any
): Backbone.View<T> {
): this {
if (typeof selector !== 'string') {
listener = selector;
selector = null!;
Expand Down Expand Up @@ -150,8 +150,8 @@ export class NativeView<T extends Backbone.Model> extends Backbone.View<T> {
eventName: string,
selector?: string,
listener?: Function
): NativeView<T>;
undelegate(selector: string, listener?: Function): NativeView<T>;
): this;
undelegate(selector: string, listener?: Function): this;
undelegate(
eventName: string,
selector?: string | Function,
Expand Down Expand Up @@ -185,7 +185,7 @@ export class NativeView<T extends Backbone.Model> extends Backbone.View<T> {
}

// Remove all events created with `delegate` from `el`
undelegateEvents(): NativeView<T> {
undelegateEvents(): this {
if (this.el && this._domEvents) {
const len = this._domEvents.length;
for (let i = 0; i < len; i++) {
Expand Down
6 changes: 5 additions & 1 deletion packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export interface ISerializers {
};
}

export interface IBackboneModelOptions extends Backbone.ModelSetOptions {
model_id: string; comm?: any; widget_manager: any
}

export class WidgetModel extends Backbone.Model {
/**
* The default attributes.
Expand Down Expand Up @@ -107,7 +111,7 @@ export class WidgetModel extends Backbone.Model {
*/
initialize(
attributes: Backbone.ObjectHash,
options: { model_id: string; comm?: any; widget_manager: any }
options: IBackboneModelOptions
): void {
super.initialize(attributes, options);

Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,14 @@
"@types/jquery" "*"
"@types/underscore" "*"

"@types/[email protected]":
version "1.4.10"
resolved "https://registry.npmjs.org/@types/backbone/-/backbone-1.4.10.tgz#042e72ffc966fe920ed02ff92afa66984a036844"
integrity sha512-X6UM8N9i4WFtO1F53Z3DE7mjI7UxEfxyFtMTYHOPFhYFvExDuu0UJENstnA023+/FnVOdxltMIKc4picZxW4dA==
dependencies:
"@types/jquery" "*"
"@types/underscore" "*"

"@types/base64-js@^1.2.5":
version "1.3.0"
resolved "https://registry.npmjs.org/@types/base64-js/-/base64-js-1.3.0.tgz#c939fdba49846861caf5a246b165dbf5698a317c"
Expand Down

0 comments on commit 871f2da

Please sign in to comment.