Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy(tree): comments and other nits #17981

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ export class LazySequence<TTypes extends AllowedTypes>
}
}
}
const count = sourceEnd - sourceStart;
const movedCount = sourceEnd - sourceStart;
let destinationIndex = index;
if (sourceField === this) {
if (destinationIndex > sourceStart) {
destinationIndex =
destinationIndex < sourceEnd
? sourceStart // distination overlaps with source range -> slide to left
: (destinationIndex -= count); // destination after source range -> subtract count
? sourceStart // destination overlaps with source range -> slide to left
: (destinationIndex -= movedCount); // destination after source range -> subtract moved count
}
}
assertValidIndex(destinationIndex, this, true);
Expand All @@ -362,7 +362,7 @@ export class LazySequence<TTypes extends AllowedTypes>
this.context.editor.move(
sourceFieldPath,
sourceStart,
count,
movedCount,
destinationFieldPath,
destinationIndex,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ export function createObjectProxy<TSchema extends ObjectNodeSchema, TTypes exten
schema: TSchema,
): SharedTreeObject<TSchema> {
// To satisfy 'deepEquals' level scrutiny, the target of the proxy must be an object with the same
// 'null prototype' you would get from on object literal '{}' or 'Object.create(null)'. This is
// because 'deepEquals' uses 'Object.getPrototypeOf' as a way to quickly reject objects with different
// prototype chains.
// prototype as an object literal '{}'. This is because 'deepEquals' uses 'Object.getPrototypeOf'
// as a way to quickly reject objects with different prototype chains.
//
// (Note that the prototype of an object literal appears as '[Object: null prototype] {}', not because
// the prototype is null, but because the prototype object itself has a null prototype.)

// TODO: Although the target is an object literal, it's still worthwhile to try experimenting with
// a dispatch object to see if it improves performance.
Expand Down Expand Up @@ -205,7 +207,7 @@ export function createObjectProxy<TSchema extends ObjectNodeSchema, TTypes exten
}

/**
* Given the a list proxy, returns it's underlying LazySequence field.
* Given the a list proxy, returns its underlying LazySequence field.
*/
const getSequenceField = <TTypes extends AllowedTypes>(
list: SharedTreeList<AllowedTypes, "javaScript">,
Expand All @@ -227,15 +229,17 @@ function itemsAsContextuallyTyped(
iterable: Iterable<ProxyNodeUnion<AllowedTypes, "javaScript">>,
): Iterable<ContextuallyTypedNodeData> {
// If the iterable is not already an array, copy it into an array to use '.map()' below.
const asArray = Array.isArray(iterable) ? iterable : Array.from(iterable);

return asArray.map(asContextuallyTypedData);
return Array.isArray(iterable)
? iterable.map(asContextuallyTypedData)
: Array.from(iterable, asContextuallyTypedData);
}

// TODO: Experiment with alternative dispatch methods to see if we can improve performance.

/** PropertyDescriptorMap used to build the prototype for our dispatch object. */
const prototypeProperties: PropertyDescriptorMap = {
/**
* PropertyDescriptorMap used to build the prototype for our SharedListNode dispatch object.
*/
const listPrototypeProperties: PropertyDescriptorMap = {
// We manually add [Symbol.iterator] to the dispatch map rather than use '[fn.name] = fn' as
// above because 'Array.prototype[Symbol.iterator].name' returns "values" (i.e., Symbol.iterator
// is an alias for the '.values()' function.)
Expand Down Expand Up @@ -383,12 +387,12 @@ const prototypeProperties: PropertyDescriptorMap = {
// Array.prototype.unshift,
Array.prototype.values,
].forEach((fn) => {
prototypeProperties[fn.name] = { value: fn };
listPrototypeProperties[fn.name] = { value: fn };
});

/* eslint-enable @typescript-eslint/unbound-method */

const prototype = Object.create(Object.prototype, prototypeProperties);
const prototype = Object.create(Object.prototype, listPrototypeProperties);

/**
* Helper to coerce property keys to integer indexes (or undefined if not an in-range integer).
Expand Down