@graspologic/memstore
Advanced tools
Comparing version 0.7.0-0 to 0.7.0-1
{ | ||
"name": "@graspologic/memstore", | ||
"version": "0.7.0-0", | ||
"version": "0.7.0-1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -91,4 +91,3 @@ /*! | ||
public *scan(): IterableIterator<ReaderItem<P>> { | ||
let idx: number | ||
public scan(callback: (item: ReaderItem<P>) => boolean | void): void { | ||
let item: P | undefined | ||
@@ -99,22 +98,20 @@ if (this.count > 0) { | ||
if (item) { | ||
for (idx of this.itemIds()) { | ||
this.itemIdsScan(idx => { | ||
if (!this.propertyBags[idx]) { | ||
this.propertyBags[idx] = {} | ||
} | ||
item.connect(idx, this) | ||
yield item | ||
} | ||
item!.connect(idx, this) | ||
return callback(item!) | ||
}) | ||
} | ||
} | ||
public *filter( | ||
ids: Set<string>, | ||
scan = false, | ||
): IterableIterator<ReaderItem<P>> { | ||
const iterator = scan ? this.scan() : this | ||
for (const item of iterator) { | ||
public filter(ids: Set<string>): ReaderItem<P>[] { | ||
const items: ReaderItem<P>[] = [] | ||
for (const item of this) { | ||
if (item.id != null && ids.has(item.id)) { | ||
yield item | ||
items.push(item) | ||
} | ||
} | ||
return items | ||
} | ||
@@ -121,0 +118,0 @@ |
@@ -140,7 +140,7 @@ /*! | ||
/** | ||
* Creates an iterator that efficiently scans through the items contained in this store | ||
* Scans through the store calling __callback__ for each item. | ||
* | ||
* _NOTE_ Items returned from this iterator should not be stored, as references are re-used for iteration | ||
* _NOTE_ Items should not be used after the callback is invoked, as the item reference is reused | ||
*/ | ||
scan(): IterableIterator<ReaderItem<P>> | ||
scan(callback: (item: ReaderItem<P>) => boolean | void): void | ||
@@ -150,5 +150,10 @@ /** | ||
* @param ids The ids of the items to retrieve | ||
* @param scan Use scan | ||
*/ | ||
filter(ids: Set<string>, scan?: boolean): IterableIterator<ReaderItem<P>> | ||
filter(ids: Set<string>): ReaderItem<P>[] | ||
/** | ||
* Performs a bulk update against this store | ||
* @param update The function which performs the update | ||
*/ | ||
bulkUpdate(update: () => void): void | ||
} |
@@ -75,3 +75,6 @@ /*! | ||
*/ | ||
notify(storeId: number, attribute: AttributeName): void | ||
notify( | ||
storeId: number | undefined, | ||
attribute: AttributeName | undefined, | ||
): void | ||
} | ||
@@ -87,2 +90,3 @@ | ||
private _count = 0 | ||
private inBulkUpdate = 0 | ||
@@ -136,2 +140,23 @@ /** a map of available storage slots in the buffer, modeled as alinked list */ | ||
/** | ||
* Scans through the list of used item ids | ||
*/ | ||
public itemIdsScan(callback: (id: number) => boolean | void) { | ||
this.slotAllocator.usedScan(callback) | ||
} | ||
/** | ||
* Performs a bulk update | ||
* @param update The update function | ||
*/ | ||
public bulkUpdate(update: () => void) { | ||
this.inBulkUpdate++ | ||
try { | ||
update() | ||
} finally { | ||
this.inBulkUpdate-- | ||
this.notify(undefined, undefined) | ||
} | ||
} | ||
// #region pubsub events | ||
@@ -212,5 +237,3 @@ /** | ||
for (const id of this.itemIds()) { | ||
this.fireRemoveHandlers(id) | ||
} | ||
this.itemIdsScan(id => this.fireRemoveHandlers(id)) | ||
@@ -238,8 +261,10 @@ this._store.resize(numItems) | ||
*/ | ||
public notify(id: number, attribute?: AttributeName) { | ||
for (const handler of this.onUpdateHandlers) { | ||
try { | ||
handler(id, attribute) | ||
} catch (e) { | ||
console.error('caught error', e) | ||
public notify(id: number | undefined, attribute?: AttributeName | undefined) { | ||
if (this.inBulkUpdate === 0) { | ||
for (const handler of this.onUpdateHandlers) { | ||
try { | ||
handler(id, attribute) | ||
} catch (e) { | ||
console.error('caught error', e) | ||
} | ||
} | ||
@@ -246,0 +271,0 @@ } |
@@ -131,2 +131,28 @@ /*! | ||
/** | ||
* Scans through the list of used indices | ||
* @param callback The callback which gets invoked for each id in the used list | ||
*/ | ||
public usedScan(callback: (id: number) => boolean | void): void { | ||
// Shortcut | ||
if ( | ||
this.availableIndices.size === 0 && | ||
(this.nextAvailableIndex === -1 || this.nextAvailableIndex === undefined) | ||
) { | ||
for (let i = 0; i < this.capacity; ++i) { | ||
if (callback(i) === false) { | ||
break | ||
} | ||
} | ||
} else { | ||
for (let i = 0; i < this.capacity; ++i) { | ||
if (!this.availableIndices.has(i) && this.nextAvailableIndex !== i) { | ||
if (callback(i) === false) { | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Returns true if the given index has been allocated | ||
@@ -133,0 +159,0 @@ * @param index The index to check |
@@ -47,3 +47,3 @@ /*! | ||
* A handler for when an attribute has been updated | ||
* @param index The index of the item | ||
* @param index The index of the item, if undefined, assume all items have changed | ||
* @param attribute The name of the attribute that was updated, if undefined, all attributes have been changed | ||
@@ -53,4 +53,4 @@ * @param value The new value of the attribute | ||
export type AttributeUpdatedHandler = ( | ||
index: number, | ||
attribute?: AttributeName, | ||
index: number | undefined, | ||
attribute?: AttributeName | undefined, | ||
) => void | ||
@@ -57,0 +57,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
598028
11286