diff --git a/package.json b/package.json index 4679413..b09a919 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@pb33f/saddlebag", - "version": "0.0.0", + "version": "0.1.0", "license": "MIT", "private": false, "description": "A tiny, pure JavaScript in-memory object store library for simple application state management", diff --git a/src/bag.manager.ts b/src/bag.manager.ts index 93d2096..486adb9 100644 --- a/src/bag.manager.ts +++ b/src/bag.manager.ts @@ -58,7 +58,7 @@ class saddlebagManager implements BagManager { } loadStatefulBags(): Promise { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { const request = indexedDB.open(BAG_DB_NAME, 1); request.onupgradeneeded = () => { // @ts-ignore @@ -66,10 +66,6 @@ class saddlebagManager implements BagManager { this._db.createObjectStore(BAG_OBJECT_STORE); }; - request.onerror = (event) => { - reject(event); - } - request.onsuccess = () => { // @ts-ignore this._db = request.result @@ -78,10 +74,6 @@ class saddlebagManager implements BagManager { const tx = this._db.transaction(BAG_OBJECT_STORE) const cursor = tx.objectStore(BAG_OBJECT_STORE).openCursor() - cursor.onerror = (event) => { - reject(event); - } - cursor.onsuccess = (event) => { // @ts-ignore let cursor = event.target.result; @@ -119,7 +111,7 @@ class saddlebagManager implements BagManager { if (this._bags.has(key)) { return this._bags.get(key); } - return CreateBag(key, ); + return this.createBag(key) } resetBags() { diff --git a/src/saddlebag.ts b/src/saddlebag.ts index 8ed1442..2f46f79 100644 --- a/src/saddlebag.ts +++ b/src/saddlebag.ts @@ -58,6 +58,7 @@ export interface Bag { * key is changed, the callback will be called with the new value. * @param {string} key to be monitored * @param {BagValueSubscriptionFunction} callback to fire on every change, updated value will be passed + * @return {Subscription} a handle to the subscription */ subscribe(key: string, callback: BagValueSubscriptionFunction): Subscription; @@ -65,6 +66,7 @@ export interface Bag { * onAllChanges will add a subscription to the bag for all changes. Any time that * any value changes in the bag, subscribers will be notified. * @param {BagAllChangeSubscriptionFunction} callback to fire on every change + * @return {Subscription} a handle to the subscription */ onAllChanges(callback: BagAllChangeSubscriptionFunction): Subscription; @@ -72,6 +74,7 @@ export interface Bag { * onPopulated will add a subscription to the bag for when the bag is populated with * any data. The entire contents of the bag will be passed to the callback. * @param {BagPopulatedSubscriptionFunction} callback + * @return {Subscription} a handle to the subscription */ onPopulated(callback: BagPopulatedSubscriptionFunction): Subscription; @@ -84,11 +87,13 @@ export interface Bag { /** * id is the unique identifier of the bag. + * @return {string} the unique identifier of the bag */ get id(): string; /** * db is the IndexedID database that the bag is associated with. + * @param db {IDBDatabase | undefined} indexedDB used to store the bag. */ set db(db: IDBDatabase | undefined); } diff --git a/src/saddlebag_engine.spec.ts b/src/saddlebag_engine.spec.ts index 7ade648..2a2281c 100644 --- a/src/saddlebag_engine.spec.ts +++ b/src/saddlebag_engine.spec.ts @@ -44,7 +44,20 @@ describe('store basics', () => { const bag2 = bm2.getBag('foo'); if (bag2) { expect(bag2.get('foo')).toEqual('bar'); - resolve(result) + + + // now reset the bag and check its gone from the db + bag2.reset(); + const bm3 = CreateBagManager(true) + expect(bm3).toBeDefined(); + + bm3.loadStatefulBags().then(() => { + + // should be gone. + expect(bag2.get('foo')).toBeUndefined() + resolve(result) + + }); } }) } diff --git a/src/saddlebag_engine.ts b/src/saddlebag_engine.ts index a8ac151..7914ff6 100644 --- a/src/saddlebag_engine.ts +++ b/src/saddlebag_engine.ts @@ -70,7 +70,6 @@ class bag { private _stateful: boolean; private _values: Map; private _db: IDBDatabase | undefined; - private _bagStore: IDBObjectStore | undefined; _subscriptions: Map[]>; _allChangesSubscriptions: BagAllChangeSubscriptionFunction[]; @@ -110,6 +109,12 @@ class bag { this.alertSubscribers(key, undefined); // the value is gone! }); this._values = new Map(); + // if there is a db, wipe out the bag in the db + if (this._stateful && this._db) { + this._db.transaction([BAG_OBJECT_STORE], 'readwrite') + .objectStore(BAG_OBJECT_STORE) + .delete(this._id); + } } private alertSubscribers(key: string, value: T | undefined): void {