func cached()

in Sources/TwitterTextEditor/TextView.swift [146:173]


        func cached(_ block: () -> T) -> T {
            if pasteboardChangedNotificationObserverToken == nil {
                pasteboardChangedNotificationObserverToken =
                    NotificationCenter.default.addObserver(forName: UIPasteboard.changedNotification,
                                                           object: nil,
                                                           queue: nil) { [weak self] _ in
                        self?.cache = nil
                    }
            }
            if applicationWillEnterForegroundNotificationObserverToken == nil {
                applicationWillEnterForegroundNotificationObserverToken =
                    NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification,
                                                           object: nil,
                                                           queue: nil) { [weak self] _ in
                        self?.cache = nil
                    }
            }

            let now = CFAbsoluteTimeGetCurrent()
            if let cache = cache, now - cache.time < 1.0 {
                return cache.value
            }

            let value = block()
            cache = (value, now)

            return value
        }