wrap incremental index with computed cache

pull/3685/head
David Sheldrick 2024-05-07 14:10:42 +01:00
rodzic 004945611c
commit b3a57a6366
2 zmienionych plików z 10 dodań i 14 usunięć

Wyświetl plik

@ -778,10 +778,6 @@ export class Editor extends EventEmitter<TLEventMap> {
findCommonAncestor(shapes: TLShape[] | TLShapeId[], predicate?: (shape: TLShape) => boolean): TLShapeId | undefined;
findShapeAncestor(shape: TLShape | TLShapeId, predicate: (parent: TLShape) => boolean): TLShape | undefined;
flipShapes(shapes: TLShape[] | TLShapeId[], operation: 'horizontal' | 'vertical'): this;
// (undocumented)
getAllBindingsFromShape(shape: TLShape | TLShapeId): TLBinding[];
// (undocumented)
getAllBindingsToShape(shape: TLShape | TLShapeId): TLBinding[];
getAncestorPageId(shape?: TLShape | TLShapeId): TLPageId | undefined;
getArrowInfo(shape: TLArrowShape | TLShapeId): TLArrowInfo | undefined;
getArrowsBoundTo(shapeId: TLShapeId): TLArrowShape[];
@ -792,9 +788,11 @@ export class Editor extends EventEmitter<TLEventMap> {
// (undocumented)
getBinding(id: TLBindingId): TLBinding | undefined;
// (undocumented)
getBindingsFromShape<Binding extends TLUnknownBinding = TLBinding>(shape: TLShape | TLShapeId, type: Binding['type']): Binding[];
getBindingsFromShape<Binding extends TLUnknownBinding = TLBinding>(shape: TLShape | TLShapeId, type?: Binding['type']): Binding[];
// (undocumented)
getBindingsToShape<Binding extends TLUnknownBinding = TLBinding>(shape: TLShape | TLShapeId, type: Binding['type']): Binding[];
getBindingsInvolvingShape<Binding extends TLUnknownBinding = TLBinding>(shape: TLShape | TLShapeId, type?: Binding['type']): Binding[];
// (undocumented)
getBindingsToShape<Binding extends TLUnknownBinding = TLBinding>(shape: TLShape | TLShapeId, type?: Binding['type']): Binding[];
getBindingUtil<S extends TLUnknownBinding>(binding: S | TLBindingPartial<S>): BindingUtil<S>;
// (undocumented)
getBindingUtil<S extends TLUnknownBinding>(type: S['type']): BindingUtil<S>;

Wyświetl plik

@ -5040,19 +5040,17 @@ export class Editor extends EventEmitter<TLEventMap> {
/* -------------------- Bindings -------------------- */
@computed
private _getBindingsIndex() {
return bindingsIndex(this)
}
private getBindingsIndex() {
return this._getBindingsIndex().get()
private _getBindingsIndexCache() {
const index = bindingsIndex(this)
return this.store.createComputedCache<TLBinding[], TLShape>('bindingsIndex', (shape) => {
return index.get()[shape.id]
})
}
getBinding(id: TLBindingId): TLBinding | undefined {
return this.store.get(id) as TLBinding | undefined
}
// TODO(alex) #bindings - cache `allBindings` getters and derive type-specific ones from them
getBindingsFromShape<Binding extends TLUnknownBinding = TLBinding>(
shape: TLShape | TLShapeId,
type?: Binding['type']
@ -5072,7 +5070,7 @@ export class Editor extends EventEmitter<TLEventMap> {
type?: Binding['type']
): Binding[] {
const id = typeof shape === 'string' ? shape : shape.id
const result = this.getBindingsIndex()[id] ?? EMPTY_ARRAY
const result = this._getBindingsIndexCache().get(id) ?? EMPTY_ARRAY
if (!type) return result as Binding[]
return result.filter((b) => b.type === type) as Binding[]
}