Skip to content

Commit

Permalink
Cleaned up state, seems to be working well.
Browse files Browse the repository at this point in the history
coverage at 100%

Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Feb 24, 2024
1 parent c3785f1 commit a9d4d54
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 2 additions & 10 deletions src/bag.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,14 @@ class saddlebagManager implements BagManager {
}

loadStatefulBags(): Promise<BagDB> {
return new Promise<BagDB>((resolve, reject) => {
return new Promise<BagDB>((resolve) => {
const request = indexedDB.open(BAG_DB_NAME, 1);
request.onupgradeneeded = () => {
// @ts-ignore
this._db = request.result
this._db.createObjectStore(BAG_OBJECT_STORE);
};

request.onerror = (event) => {
reject(event);
}

request.onsuccess = () => {
// @ts-ignore
this._db = request.result
Expand All @@ -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;
Expand Down Expand Up @@ -119,7 +111,7 @@ class saddlebagManager implements BagManager {
if (this._bags.has(key)) {
return this._bags.get(key);
}
return CreateBag<T>(key, );
return this.createBag(key)
}

resetBags() {
Expand Down
5 changes: 5 additions & 0 deletions src/saddlebag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ export interface Bag<T = any> {
* key is changed, the callback will be called with the new value.
* @param {string} key to be monitored
* @param {BagValueSubscriptionFunction<T>} callback to fire on every change, updated value will be passed
* @return {Subscription} a handle to the subscription
*/
subscribe(key: string, callback: BagValueSubscriptionFunction<T>): Subscription;

/**
* 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<T>} callback to fire on every change
* @return {Subscription} a handle to the subscription
*/
onAllChanges(callback: BagAllChangeSubscriptionFunction<T>): Subscription;

/**
* 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<T>} callback
* @return {Subscription} a handle to the subscription
*/
onPopulated(callback: BagPopulatedSubscriptionFunction<T>): Subscription;

Expand All @@ -84,11 +87,13 @@ export interface Bag<T = any> {

/**
* 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);
}
Expand Down
15 changes: 14 additions & 1 deletion src/saddlebag_engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ describe('store basics', () => {
const bag2 = bm2.getBag<string>('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)

});
}
})
}
Expand Down
7 changes: 6 additions & 1 deletion src/saddlebag_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class bag<T> {
private _stateful: boolean;
private _values: Map<string, T>;
private _db: IDBDatabase | undefined;
private _bagStore: IDBObjectStore | undefined;

_subscriptions: Map<string, BagValueSubscriptionFunction<T>[]>;
_allChangesSubscriptions: BagAllChangeSubscriptionFunction<T>[];
Expand Down Expand Up @@ -110,6 +109,12 @@ class bag<T> {
this.alertSubscribers(key, undefined); // the value is gone!
});
this._values = new Map<string, T>();
// 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 {
Expand Down

0 comments on commit a9d4d54

Please sign in to comment.