()
| 19 | } |
| 20 | |
| 21 | export default function createRegistry(): Registry { |
| 22 | const entries: EntryMap = { |
| 23 | draggables: {}, |
| 24 | droppables: {}, |
| 25 | }; |
| 26 | |
| 27 | const subscribers: Subscriber[] = []; |
| 28 | |
| 29 | function subscribe(cb: Subscriber): Unsubscribe { |
| 30 | subscribers.push(cb); |
| 31 | |
| 32 | return function unsubscribe(): void { |
| 33 | const index: number = subscribers.indexOf(cb); |
| 34 | |
| 35 | // might have been removed by a clean |
| 36 | if (index === -1) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | subscribers.splice(index, 1); |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | function notify(event: RegistryEvent) { |
| 45 | if (subscribers.length) { |
| 46 | subscribers.forEach((cb) => cb(event)); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | function findDraggableById(id: DraggableId): DraggableEntry | null { |
| 51 | return entries.draggables[id] || null; |
| 52 | } |
| 53 | |
| 54 | function getDraggableById(id: DraggableId): DraggableEntry { |
| 55 | const entry: DraggableEntry | null = findDraggableById(id); |
| 56 | invariant(entry, `Cannot find draggable entry with id [${id}]`); |
| 57 | return entry; |
| 58 | } |
| 59 | |
| 60 | const draggableAPI: DraggableAPI = { |
| 61 | register: (entry: DraggableEntry) => { |
| 62 | entries.draggables[entry.descriptor.id] = entry; |
| 63 | notify({ type: 'ADDITION', value: entry }); |
| 64 | }, |
| 65 | update: (entry: DraggableEntry, last: DraggableEntry) => { |
| 66 | const current: DraggableEntry | null = |
| 67 | entries.draggables[last.descriptor.id]; |
| 68 | |
| 69 | // item already removed |
| 70 | if (!current) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // id already used for another mount |
| 75 | if (current.uniqueId !== entry.uniqueId) { |
| 76 | return; |
| 77 | } |
| 78 |
searching dependent graphs…