MVCoconut / coconut.data

Observable Data Layer.
The Unlicense
20 stars 8 forks source link

Callback when a model goes out of use. #59

Open back2dos opened 4 years ago

back2dos commented 4 years ago

It should be possible to detect when none of a model's observable/editable properties are in use and so that any underlying subscriptions (to a websocket or something) can be cleaned up.

kevinresol commented 4 years ago

Will it be like "Suspendable" i.e. can be deactivated/reactivated repeatedly?

kevinresol commented 4 years ago

In react hook there is useEffect, I wonder if it make sense to have something like this?

@:effect function subscribe():CallbackLink;

this would track observables used in the function and re-call when necessary.

back2dos commented 4 years ago

Not sure I follow 100%. Can you please elaborate on how this would be used?

kevinresol commented 4 years ago

Roughly something like this:

@:observable var id:String;
@:editable var currentValue:Int;
@:effect function subscribe():CallbackLink {
    // when `id` changes, this function will be called again, the last CallbackLink produced will be dissolved at the same time
    return subscribeForExternalChange(id, value -> currentValue = value);
}

@:effect is a function that will produce side effects, and returns a callback link to undo it, and when the model goes out of use, the callback link will be called to cleanup resources. maybe it is just equivalent to untilUnmounted in a view

kevinresol commented 1 year ago

I am interested in implementing this, are there any tips where I should get started?