|
| 1 | +import { types } from "mobx-state-tree"; |
| 2 | +import Types from "../../core/Types"; |
| 3 | +import { SharedStoreModel } from "./model"; |
| 4 | + |
| 5 | +/** |
| 6 | + * StoreIds and Stores act as a cache. |
| 7 | + * |
| 8 | + * The reason behind those is that we're creating a new store on the `preProcessSnapshot` when there's no |
| 9 | + * access to the State Tree. When the store is created, it's put into the cache and retrieved back in the |
| 10 | + * `afterCreate` hook of the model. |
| 11 | + * |
| 12 | + * StoreIds is just a map of existing store IDs to reference to during the `preProcessSnapshot`. |
| 13 | + */ |
| 14 | +export const Stores = new Map(); |
| 15 | +const StoreIds = new Set(); |
| 16 | + |
| 17 | +/** |
| 18 | + * Defines the ID to group SharedStores by. |
| 19 | + */ |
| 20 | +const SharedStoreID = types.optional(types.maybeNull(types.string), null); |
| 21 | + |
| 22 | +/** |
| 23 | + * Defines the Store model referenced from the Annotation Store |
| 24 | + */ |
| 25 | +const Store = types.optional(types.maybeNull(types.late(() => types.reference(SharedStoreModel))), null); |
| 26 | + |
| 27 | +/** |
| 28 | + * SharedStoreMixin, when injected into the model, provides an AnnotationStore level shared storages to |
| 29 | + * reduce the memory footprint and computation time. |
| 30 | + * |
| 31 | + * It was specifically designed to be used with Repeater tag where the memory issues are the most sound. |
| 32 | + * |
| 33 | + * This mixin provedes a `sharedStore` property to the model which is a reference to the shared store. |
| 34 | + * |
| 35 | + * The concept behind it is that whenever a model is parsing a snapshot, children are subtracted from the |
| 36 | + * initial snapshot, and put into the newly created SharedStore. |
| 37 | + * |
| 38 | + * The store is then put into the cache and attached to the model in the `afterCreate` hook. Any subsequent |
| 39 | + * models lookup the store in the cache first and use its id instead of creating a new one. |
| 40 | + * |
| 41 | + * When the store is fullfilled with children, it's locked and cannot be modified anymore. The allows the model |
| 42 | + * not to process children anymore and just use the store. |
| 43 | + * |
| 44 | + * Shared Stores live on the AnnotationStore level meaning that even if the user switches between annotations or |
| 45 | + * create new ones, they will all use the same shared store decreasing the memory footprint and computation time. |
| 46 | + */ |
| 47 | +export const SharedStoreMixin = types.model("SharedStoreMixin", { |
| 48 | + sharedstore: SharedStoreID, |
| 49 | + store: Store, |
| 50 | +}) |
| 51 | + .views((self) => ({ |
| 52 | + get children() { |
| 53 | + return self.sharedChildren; |
| 54 | + }, |
| 55 | + |
| 56 | + get locked() { |
| 57 | + return self.store?.locked ?? false; |
| 58 | + }, |
| 59 | + |
| 60 | + set children(val) { |
| 61 | + self.store?.lock(); |
| 62 | + self.store.setChildren(val); |
| 63 | + }, |
| 64 | + |
| 65 | + get sharedChildren() { |
| 66 | + return self.store.children ?? []; |
| 67 | + }, |
| 68 | + |
| 69 | + get storeId() { |
| 70 | + return self.sharedstore ?? self.name; |
| 71 | + }, |
| 72 | + })) |
| 73 | + .actions(self => ({ |
| 74 | + afterCreate() { |
| 75 | + if (!self.store) { |
| 76 | + const store = Stores.get(self.storeId); |
| 77 | + const annotationStore = Types.getParentOfTypeString(self, "AnnotationStore"); |
| 78 | + |
| 79 | + annotationStore.addSharedStore(store); |
| 80 | + StoreIds.add(self.storeId); |
| 81 | + self.store = self.storeId; |
| 82 | + } |
| 83 | + }, |
| 84 | + })) |
| 85 | + .preProcessSnapshot((sn) => { |
| 86 | + const storeId = sn.sharedstore ?? sn.name; |
| 87 | + |
| 88 | + if (StoreIds.has(storeId)) { |
| 89 | + sn.store = storeId; |
| 90 | + } else { |
| 91 | + Stores.set(storeId, SharedStoreModel.create({ |
| 92 | + id: storeId, |
| 93 | + children: sn._children ?? sn.children ?? [], |
| 94 | + })); |
| 95 | + } |
| 96 | + |
| 97 | + return sn; |
| 98 | + }); |
| 99 | + |
| 100 | +export const destroy = () => { |
| 101 | + console.log("destroying"); |
| 102 | + Stores.clear(); |
| 103 | + StoreIds.clear(); |
| 104 | +}; |
| 105 | + |
0 commit comments