casparss / racket-meetup-mobile

Racket Meetup is a tennis social media app built in Typescript with Angular 5 on Ionic 4
0 stars 0 forks source link

Model enhancements #28

Closed casparss closed 6 years ago

casparss commented 7 years ago

Had an idea that would make the models much more powerful in reconciling data and Irgun the application. At the moment it's possible to create two separate models from a JSON object with the same ID, the data in both of these models is the same, however when one is updated the other will never know about the change and the state tree will diverge for that session.

Models should have one source of truth and there shouldn't be two separate instances of the same data.

I'm proposing that the model service maintains a collection of the models it was 'created' with the create method. The create method when invoked with a new model candidate object will run a find on the the models collection and try and resolve a model with he same ID as the the object passed in. If it finds an object already then, a diff is run on the object and the new Jain objects props are copied onto the incumbent and then the incumbent model is returned by the create. If no model is found on the collection then a new Model is instantiated (and also added to the model collection).

casparss commented 7 years ago

Another additional idea is the concept of a type property on models. Firstly a model factory when creating a model with a passed in object will need to check the type property to prevent the wrong model type is forced into a model instance. Secondly could streamline model creation by using a single service, that checks the model prop and automatically invoked the correct model instance.

casparss commented 6 years ago

Another cool feature for keeping models up-to-date in the case of when they are unable to receive websocket events (such as when the app goes offline / socket is dropped) would be to have a refresh event (when app goes online / socket reconnects) that maps through the models collection and creates and a collection of { _id, updatedAt }, and emits this to the server. Once emitted, a find() could be run with the array of IDs and the result filtered through using the updatedAt on models with a newer timestamp (updated models), which would be emitted back to the model service with the relevant models being updated.

casparss commented 6 years ago

To enable garbage collection of models that are no longer in use, was thinking of building a container to house collections for each component where the models are used, which registers with the model service - this is oppose to just using a plain array with a component where the Array object is not aware of any other references to these models. It would be straight forward checking when a model isn't referenced out of any existing containers and destroying.

casparss commented 6 years ago

This has been fully implemented.