I am learning Svelte by creating simple app.
The logic is written using classes. The idea is, that all the data needed comes from class instance properties. Instances should not be instantiated more than once. I am using stores to provide components this instances.
The problem is I can't get reactivity using this approach. I tried readable and writable stores and nothing helps. It is still possible to get reactivity using OOP and what can I do? Reassignment and creating new instances will be expensive.
Edit
I can't make up the example in REPL cause the class is too big.
Parser.js
export default class Parser {
constructor() {
this._history = [];
}
parse(string) {
this._history.push(string)
}
get history() {
return this._history;
}
}
Here I pass instance to the store.
parserStore.js
import writable from "svelte/store";
import Parser from "Parser.js"
export const parserStore = writable(new Parser());
In this component I get the instance and use reactively a method.
Component_1.svelte*
import { parserStore } from "parserStore.js";
$: result = parserStore.parse(binded_input_value);
What I want to get is the up to time history property that was updated from using class method:
Component_2.svelte
import { parserStore } from "parserStore.js";
$: history = parserStore.history;
{#each history as ... }
I know, it is not the best example, but what I want is reactive class instance available through the store. Actually the values are up to date, but it is not causing the re-render of the components. When the component is mounted - data of the latest, but after nothing re-renders at all even so the properties of the instance is changed.