@flumens/models 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Model.d.ts +2 -2
- package/dist/Model.js +1 -1
- package/package.json +1 -1
package/dist/Model.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export default class Model<T extends Data = Data> {
|
|
|
20
20
|
/**
|
|
21
21
|
* Remote server document ID.
|
|
22
22
|
*/
|
|
23
|
-
id
|
|
23
|
+
id?: string;
|
|
24
24
|
/**
|
|
25
25
|
* Key name of the model used to save and fetch the model from.
|
|
26
26
|
*/
|
|
@@ -77,7 +77,7 @@ export default class Model<T extends Data = Data> {
|
|
|
77
77
|
* Returns a clean (no observables) JSON representation of the model.
|
|
78
78
|
*/
|
|
79
79
|
toJSON(): {
|
|
80
|
-
id: string;
|
|
80
|
+
id: string | undefined;
|
|
81
81
|
cid: string;
|
|
82
82
|
createdAt: number;
|
|
83
83
|
updatedAt: number;
|
package/dist/Model.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),s=require("mobx-utils"),i=require("@flumens/utils");const d=t=>JSON.parse(JSON.stringify(t)),r={};exports.copyObject=d,exports.default=class{constructor({id:t
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),s=require("mobx-utils"),i=require("@flumens/utils");const d=t=>JSON.parse(JSON.stringify(t)),r={};exports.copyObject=d,exports.default=class{constructor({id:t,cid:a="",createdAt:o,updatedAt:h,syncedAt:c,data:n={},attrs:u={},store:l}){this.ready=new i.Deferred,this.debouncedValue=3e3,this._observerPaused=!1,this.id=t,this.cid=a||t||i.UUIDv7();const v=Date.now();let y;this.createdAt=o||v,this.updatedAt=h||v,this.syncedAt=c,this.data=d(Object.assign(Object.assign(Object.assign({},r),n),u)),e.makeObservable(this,{createdAt:e.observable,updatedAt:e.observable,syncedAt:e.observable,data:e.observable}),this.store=l;const b=(...t)=>{clearTimeout(y),y=setTimeout((()=>this.sync.apply(this,t)),this.debouncedValue)};s.deepObserve(this.data,(()=>{this._observerPaused||(this.updatedAt=Date.now(),b())}))}setUpdatedAtTimestamp(t){this.updatedAt=t}fetch(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)return void this.ready.resolve(!1);const t=yield this.store.find(this.cid);if(!t)return yield this.save(),void this.ready.resolve(!0);t.id&&(this.id=t.id),t.cid&&(this.cid=t.cid),this._observerPaused=!0,e.set(this.data,t.data),this._observerPaused=!1,this.createdAt=t.createdAt,this.updatedAt=t.updatedAt,this.syncedAt=t.syncedAt,this.ready.resolve(!0)}))}save(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to save locally without a store");const t=this.toJSON();yield this.store.save(t)}))}destroy(){return t.__awaiter(this,void 0,void 0,(function*(){if(!this.store)throw new Error("Trying to delete locally without a store");yield this.store.delete(this.cid)}))}sync(...e){return t.__awaiter(this,void 0,void 0,(function*(){this.store&&(yield this.save())}))}toJSON(){return{id:this.id,cid:this.cid,createdAt:this.createdAt,updatedAt:this.updatedAt,syncedAt:this.syncedAt,data:e.toJS(this.data)}}reset(t){this.id="";const s=JSON.parse(JSON.stringify(Object.assign(Object.assign({},r),t)));e.set(this.data,s);const i=Object.keys(s);return Object.keys(this.data).forEach((t=>{i.includes(t)||(this.data[t]=null,delete this.data[t])})),this.save()}get isStored(){return!!this.store}};
|