: ViewModelMarker> dynamicLifecycleProxy(viewModel: VM, vmInterface: Class<*>, view: Observable<View>): VM { val handler = InvocationHandler { proxy, method, args -> if (method.returnType == rx.Observable::class.java) { (method.invoke(viewModel, args) as Observable<*>) .observeOn(AndroidSchedulers.mainThread()) .delaySubscription(view.flatMap(View::attaches)) .takeUntil(view.flatMap(View::detaches)) .repeatWhen { view.flatMap(View::attaches) } } else { method.invoke(viewModel, args) } } return Proxy.newProxyInstance( classLoader = vmInterface.classLoader, interfaces = arrayOf(vmInterface), invocationHandler = handler ) as VM }