MCPcopy
hub / github.com/clauderic/dnd-kit / Entity

Class Entity

packages/abstract/src/core/entities/entity/entity.ts:44–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42 * @template T - The type of data associated with the entity.
43 */
44export class Entity<
45 T extends Data = Data,
46 U extends DragDropManager<any, any> = DragDropManager<any, any>,
47> {
48 static pendingIdChanges: Map<Entity, UniqueIdentifier> | null = null;
49
50 static #flushIdChanges() {
51 const changes = Entity.pendingIdChanges;
52 Entity.pendingIdChanges = null;
53
54 if (changes) {
55 batch(() => {
56 for (const [entity, id] of changes) {
57 entity.#idSignal.value = id;
58 }
59 });
60 }
61 }
62
63 /**
64 * Creates a new instance of the `Entity` class.
65 *
66 * @param input - An object containing the initial properties of the entity.
67 * @param manager - The manager that controls the drag and drop operations.
68 */
69 constructor(input: Input<T>, manager: U | undefined) {
70 const {effects, id, data = {} as T, disabled = false, register = true} = input;
71
72 let previousId = id;
73
74 this.#idSignal = signal(id);
75 this.manager = manager;
76 this.data = data;
77 this.disabled = disabled;
78 this.effects = () => [
79 () => {
80 const {id, manager} = this;
81
82 if (id === previousId) {
83 return;
84 }
85
86 previousId = id;
87 manager?.registry.register(this);
88
89 return () => manager?.registry.unregister(this);
90 },
91 ...(effects?.() ?? []),
92 ];
93
94 this.register = this.register.bind(this);
95 this.unregister = this.unregister.bind(this);
96 this.destroy = this.destroy.bind(this);
97
98 if (manager && register) {
99 queueMicrotask(this.register);
100 }
101 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected