Skip to content

Commit

Permalink
Fix type errors, add type definitions and remove overview.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Oct 26, 2023
1 parent 2849020 commit e73b9ce
Show file tree
Hide file tree
Showing 28 changed files with 1,095 additions and 254 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ node_modules: package.json package-lock.json
npm install

build: node_modules
npm run types
npm run build

dist: build

check: node_modules build eslint
npm run types
npm run test

.PHONY: eslint
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Modernized Backbone with web components",
"url": "https://github.com/conversejs/skeletor",
"main": "src/index.js",
"types": "src/types/index.d.ts",
"browser": "dist/skeletor.js",
"keywords": [
"model",
Expand Down
111 changes: 52 additions & 59 deletions src/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Collection extends EventEmitter(Object) {
this._reset();
this.initialize.apply(this, arguments);
if (models) this.reset(models, Object.assign({ silent: true }, options));

this[Symbol.iterator] = this.values;
}

/**
Expand Down Expand Up @@ -280,10 +282,10 @@ class Collection extends EventEmitter(Object) {
console.error(e);
resolve();
},
})
}),
);
});
})
}),
);
await this.browserStorage.clear();
this.reset();
Expand Down Expand Up @@ -402,7 +404,7 @@ class Collection extends EventEmitter(Object) {
sortBy(iteratee) {
return sortBy(
this.models,
isFunction(iteratee) ? iteratee : (m) => (isString(iteratee) ? m.get(iteratee) : m.matches(iteratee))
isFunction(iteratee) ? iteratee : (m) => (isString(iteratee) ? m.get(iteratee) : m.matches(iteratee)),
);
}

Expand Down Expand Up @@ -452,7 +454,7 @@ class Collection extends EventEmitter(Object) {
findLastIndex(pred, fromIndex) {
return this.models.findLastIndex(
isFunction(pred) ? pred : (m) => (isString(pred) ? m.get(pred) : m.matches(pred)),
fromIndex
fromIndex,
);
}

Expand Down Expand Up @@ -718,7 +720,7 @@ class Collection extends EventEmitter(Object) {
_prepareModel(attrs, options) {
if (this._isModel(attrs)) {
if (!attrs.collection) attrs.collection = this;
return /** @type {Model} */(attrs);
return /** @type {Model} */ (attrs);
}
options = options ? clone(options) : {};
options.collection = this;
Expand Down Expand Up @@ -822,69 +824,60 @@ class Collection extends EventEmitter(Object) {
}
}

// Defining an @@iterator method implements JavaScript's Iterable protocol.
// In modern ES2015 browsers, this value is found at Symbol.iterator.
const $$iterator = typeof Symbol === 'function' && Symbol.iterator;
if ($$iterator) {
Collection.prototype[$$iterator] = Collection.prototype.values;
}

// CollectionIterator
// ------------------

// A CollectionIterator implements JavaScript's Iterator protocol, allowing the
// use of `for of` loops in modern browsers and interoperation between
// Collection and other JavaScript functions and third-party libraries
// which can operate on Iterables.
const CollectionIterator = function (collection, kind) {
this._collection = collection;
this._kind = kind;
this._index = 0;
};

// This "enum" defines the three possible kinds of values which can be emitted
// by a CollectionIterator that correspond to the values(), keys() and entries()
// methods on Collection, respectively.
const ITERATOR_VALUES = 1;
const ITERATOR_KEYS = 2;
const ITERATOR_KEYSVALUES = 3;

// All Iterators should themselves be Iterable.
if ($$iterator) {
CollectionIterator.prototype[$$iterator] = function () {
return this;
};
}

CollectionIterator.prototype.next = function () {
if (this._collection) {
// Only continue iterating if the iterated collection is long enough.
if (this._index < this._collection.length) {
const model = this._collection.at(this._index);
this._index++;

// Construct a value depending on what kind of values should be iterated.
let value;
if (this._kind === ITERATOR_VALUES) {
value = model;
} else {
const id = this._collection.modelId(model.attributes);
if (this._kind === ITERATOR_KEYS) {
value = id;
} else {
// ITERATOR_KEYSVALUES
value = [id, model];
class CollectionIterator {
/**
* A CollectionIterator implements JavaScript's Iterator protocol, allowing the
* use of `for of` loops in modern browsers and interoperation between
* Collection and other JavaScript functions and third-party libraries
* which can operate on Iterables.
*/
constructor(collection, kind) {
this._collection = collection;
this._kind = kind;
this._index = 0;
}

[Symbol.iterator]() {
return {
next: () => {
if (this._collection) {
// Only continue iterating if the iterated collection is long enough.
if (this._index < this._collection.length) {
const model = this._collection.at(this._index);
this._index++;

// Construct a value depending on what kind of values should be iterated.
let value;
if (this._kind === ITERATOR_VALUES) {
value = model;
} else {
const id = this._collection.modelId(model.attributes);
if (this._kind === ITERATOR_KEYS) {
value = id;
} else {
// ITERATOR_KEYSVALUES
value = [id, model];
}
}
return { value: value, done: false };
}

// Once exhausted, remove the reference to the collection so future
// calls to the next method always return done.
this._collection = undefined;
}
}
return { value: value, done: false };
}

// Once exhausted, remove the reference to the collection so future
// calls to the next method always return done.
this._collection = undefined;
return { value: undefined, done: true };
},
};
}

return { value: undefined, done: true };
};
}

export { Collection };
5 changes: 5 additions & 0 deletions src/eventemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { eventsApi, onApi, offApi, onceMap, tryCatchOn, triggerApi } from './uti
// A private global variable to share between listeners and listenees.
let _listening;

/**
* @function
* @template {new(...args: any[]) => {}} ClassConstructor
* @param {ClassConstructor} Base
*/
export function EventEmitter(Base) {
return class EventEmitter extends Base {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class Model extends EventEmitter(Object) {
* If the server returns an attributes hash that differs, the model's
* state will be `set` again.
* @param {string} key
* @param {string|Options} val
* @param {string|Options} [val]
* @param {Options} [options]
*/
save(key, val, options) {
Expand Down
191 changes: 0 additions & 191 deletions src/overview.js

This file was deleted.

Loading

0 comments on commit e73b9ce

Please sign in to comment.