y-op-sqlite
Version:
<p align="center"> <img src="https://cdn.malts.me/6Wu0Fj.svg" /> </p>
3 lines (2 loc) • 2.5 kB
JavaScript
var yOpSqlite=function(n,i,c){"use strict";var g=Object.defineProperty;var v=(n,i,c)=>i in n?g(n,i,{enumerable:!0,configurable:!0,writable:!0,value:c}):n[i]=c;var d=(n,i,c)=>(v(n,typeof i!="symbol"?i+"":i,c),c);function u(o){const s=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(o){for(const t in o)if(t!=="default"){const e=Object.getOwnPropertyDescriptor(o,t);Object.defineProperty(s,t,e.get?e:{enumerable:!0,get:()=>o[t]})}}return s.default=o,Object.freeze(s)}const h=u(i),f=o=>new Promise(o);Promise.all.bind(Promise);const a=()=>new Map,l=(o,s,t)=>{let e=o.get(s);return e===void 0&&o.set(s,e=t()),e},b=()=>new Set,y=Array.from;class p{constructor(){this._observers=a()}on(s,t){return l(this._observers,s,b).add(t),t}once(s,t){const e=(...r)=>{this.off(s,e),t(...r)};this.on(s,e)}off(s,t){const e=this._observers.get(s);e!==void 0&&(e.delete(t),e.size===0&&this._observers.delete(s))}emit(s,t){return y((this._observers.get(s)||a()).values()).forEach(e=>e(...t))}destroy(){this._observers=a()}}const _=500,E="opssqlite-persistence-";class S extends p{constructor(t,e){super();d(this,"doc");d(this,"name");d(this,"whenSynced");d(this,"db");d(this,"synced",!1);d(this,"_dbref",0);d(this,"_dbsize",0);d(this,"_destroyed",!1);this.doc=e,this.name=t,this.whenSynced=f(r=>this.on("synced",()=>r(this))),this.db=c.open({name:E+t+".sqlite"}),this.db.executeAsync("CREATE TABLE IF NOT EXISTS updates (id INTEGER PRIMARY KEY, content BLOB)").then(()=>{const r=h.encodeStateAsUpdate(e);r.length>0&&this._storeUpdate(r,null)}).then(()=>this.db.executeAsync("SELECT content FROM updates").catch(r=>console.error("error loading updates",r))).then(r=>{if(r!=null&&r.rows!=null)for(const R of r.rows._array)h.applyUpdate(e,new Uint8Array(R.content));this.synced=!0,this.emit("synced",[this])}),this.destroy=this.destroy.bind(this),this._storeUpdate.bind(this),e.on("update",this._storeUpdate.bind(this)),e.on("destroy",this.destroy.bind(this))}_storeUpdate(t,e){if(!this.db||e==this){!this.db&&console.error("trying to store update without db");return}return this.db.executeAsync("INSERT INTO updates (content) VALUES (?)",[t]).catch(r=>console.error("error storing update",r))}destroy(){return this.doc.off("update",this._storeUpdate),this.doc.off("destroy",this.destroy),this._destroyed=!0,this.db.close()}}return n.OPSQLitePersistence=S,n.PREFERRED_TRIM_SIZE=_,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"}),n}({},Y,opSqlite);
//# sourceMappingURL=y-op-sqlite.iife.js.map