( beforeSubscribe?: () => Set<dia.Cell.ID> | undefined )
| 13 | * @group utils |
| 14 | */ |
| 15 | export function subscribeHandler( |
| 16 | beforeSubscribe?: () => Set<dia.Cell.ID> | undefined |
| 17 | ): SubscribeHandler { |
| 18 | let isScheduled = false; |
| 19 | const subscribers = new Set<(changedIds?: Set<dia.Cell.ID>) => void>(); |
| 20 | |
| 21 | return { |
| 22 | subscribe: (onStoreChange: (changedIds?: Set<dia.Cell.ID>) => void) => { |
| 23 | subscribers.add(onStoreChange); |
| 24 | return () => { |
| 25 | subscribers.delete(onStoreChange); |
| 26 | }; |
| 27 | }, |
| 28 | notifySubscribers: () => { |
| 29 | if (!isScheduled) { |
| 30 | isScheduled = true; |
| 31 | requestAnimationFrame(() => { |
| 32 | const changedIds = beforeSubscribe?.(); |
| 33 | for (const subscriber of subscribers) { |
| 34 | subscriber(changedIds); |
| 35 | } |
| 36 | isScheduled = false; |
| 37 | }); |
| 38 | } |
| 39 | }, |
| 40 | }; |
| 41 | } |
no test coverage detected