Re: [w3ctag/design-reviews] ObservableArray, and its use by adoptedStyleSheets (Issue #693)

> > One request here would be to consider exposing this to web developers as well (make it a constructable object, instead of a trait) - and possibly bring it to TC39. WDYT?
> 
> @domenic WDYT?

I've been in favor of giving developers a nice way to create observable arrays since the beginning; see some of the discussion in https://github.com/whatwg/webidl/issues/796.

The latest shape (after the course corrections mentioned in https://github.com/w3ctag/design-reviews/issues/693#issuecomment-983930212) makes this a bit trickier. Part of the reason the current solution is so good is precisely because it is "just a (proxy for an) `Array`", i.e. it is not a constructible class. If it were a constructible class, with its own constructor, then it would have worse compatibility with `Array`s!

So this means the creation of these things would need to look a bit different, perhaps `Array.observable(...)` or `createObservableArray()`, instead of `new ObservableArray()`.

The other hard part of exposing this functionality to developers is that we'd then need to come up with a nice-to-use API that is good for all time, instead of just creating something that works for spec developers and can be tweaked any time. The spec-author facing hooks are:

- "set an indexed value"
- "delete an indexed value"
- a fully mutable "backing list"

but I'd be hesitant to say these are the best possible API, or can be expressed nicely in JS (as opposed to in spec language). As a simple example, in spec land we're representing an assignment, e.g. `observableArray[5] = "foo"`, as a delete-then-set; maybe developers would prefer that to be represented as a single change operation.

So overall I think it would be a significant project to make these developer-creatable, which would benefit from research, maybe some cowpath-paving libraries, and so on. Probably the first step, before investing too much, is to find out if there's demand for this sort of thing.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/w3ctag/design-reviews/issues/693#issuecomment-1080901960
You are receiving this because you are subscribed to this thread.

Message ID: <w3ctag/design-reviews/issues/693/1080901960@github.com>

Received on Monday, 28 March 2022 16:54:22 UTC