UNPKG

@surface/custom-element

Version:

Provides support of directives and data binding on custom elements.

15 lines (14 loc) 618 B
import { getValue } from "@surface/core"; import AsyncObserver from "../reactivity/async-observer.js"; import { scheduler } from "../singletons.js"; export default function observe(target, observables, listener, lazy = false) { const subscriptions = []; for (const path of observables) { const observer = AsyncObserver.observe(target, path, scheduler); subscriptions.push(observer.subscribe(listener)); if (!lazy) { listener(getValue(target, ...path)); } } return { unsubscribe: () => subscriptions.splice(0).forEach(x => x.unsubscribe()) }; }