Skip to content

Commit

Permalink
refactoring, add support for pointer entries
Browse files Browse the repository at this point in the history
  • Loading branch information
benStre committed Jan 25, 2024
1 parent e8b85a8 commit 897b285
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export async function init() {
// has only local endpoint id (%0000) or global id?
if (endpoint != LOCAL_ENDPOINT) {
Pointer.is_local = false;
// update items that contain pointers with unresolved local origin in storage
Storage.updateItemsWithUnresolvedLocalDependencies();
// update storage entries that contain pointers with unresolved @@local origin
Storage.updateEntriesWithUnresolvedLocalDependencies();
// init subscriber cache as soon as endpoint is available
initSubscriberCache();
}
Expand Down
42 changes: 28 additions & 14 deletions runtime/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LOCAL_ENDPOINT } from "../types/addressing.ts";
import { ESCAPE_SEQUENCES } from "../utils/logger.ts";
import { StorageMap } from "../types/storage_map.ts";
import { StorageSet } from "../types/storage_set.ts";
import { IterableWeakSet } from "../utils/iterable-weak-set.ts";


// displayInit();
Expand Down Expand Up @@ -378,6 +379,7 @@ export class Storage {
}

static #unresolvedLocalItems = new Map<string, unknown>()
static #unresolvedLocalPointers = new IterableWeakSet<Pointer>()


static setItem(key:string, value:any, listen_for_pointer_changes = true, location:StorageLocation|null|undefined = this.#primary_location):Promise<boolean>|boolean {
Expand All @@ -397,7 +399,7 @@ export class Storage {
this.setDirty(location, true)
// store value (might be pointer reference)
const dependencies = await location.setItem(key, value);
if (Pointer.is_local) this.checkUnresolvedLocalDependencies(key, value, dependencies);
if (Pointer.is_local) this.checkUnresolvedLocalDependenciesForItem(key, value, dependencies);
this.updateItemDependencies(key, [...dependencies].map(p=>p.id));
await this.saveDependencyPointersAsync(dependencies, listen_for_pointer_changes, location);
this.setDirty(location, false)
Expand All @@ -406,35 +408,45 @@ export class Storage {

static setItemSync(location:SyncStorageLocation, key: string, value: unknown,listen_for_pointer_changes: boolean) {
const dependencies = location.setItem(key, value);
if (Pointer.is_local) this.checkUnresolvedLocalDependencies(key, value, dependencies);
if (Pointer.is_local) this.checkUnresolvedLocalDependenciesForItem(key, value, dependencies);
this.updateItemDependencies(key, [...dependencies].map(p=>p.id));
this.saveDependencyPointersSync(dependencies, listen_for_pointer_changes, location);
return true;
}

/**
* Collects all pointer dependencies of an item with a @@local origin.
* Once the endpoint is initialized, the item is updated with the correct pointer ids.
* @param itemKey
* @param value
* @param dependencies
* Collects all pointer dependencies of an itemr entry with a @@local origin.
* Once the endpoint is initialized, the entry is updated with the correct pointer ids.
*/
private static checkUnresolvedLocalDependencies(itemKey: string, value:unknown, dependencies: Set<Pointer>) {
private static checkUnresolvedLocalDependenciesForItem(key: string, value:unknown, dependencies: Set<Pointer>) {
const hasUnresolvedLocalDependency = [...dependencies].some(p=>p.origin == LOCAL_ENDPOINT);
if (hasUnresolvedLocalDependency) {
this.#unresolvedLocalItems.set(itemKey, value)
}
if (hasUnresolvedLocalDependency) this.#unresolvedLocalItems.set(key, value);
}

/**
* Collects all pointer dependencies of a pointer entry with a @@local origin.
* Once the endpoint is initialized, the entry is updated with the correct pointer ids.
*/
private static checkUnresolvedLocalDependenciesForPointer(pointer: Pointer, dependencies: Set<Pointer>) {
const hasUnresolvedLocalDependency = [...dependencies].some(p=>p.origin == LOCAL_ENDPOINT);
if (hasUnresolvedLocalDependency) this.#unresolvedLocalPointers.add(pointer);
}

/**
* Updates all items in storage that still are stored with unresolved @@local pointers
* Updates all item/pointer entries in storage that still are stored with unresolved @@local pointers
*/
static updateItemsWithUnresolvedLocalDependencies() {
static updateEntriesWithUnresolvedLocalDependencies() {
for (const [key, value] of this.#unresolvedLocalItems) {
logger.debug("update item containing pointers with @@local origin: " + key)
logger.debug("update item containing pointers with @@local origin!: " + key)
this.setItem(key, value)
}
this.#unresolvedLocalItems.clear()

for (const ptr of this.#unresolvedLocalPointers) {
logger.debug("update pointer containing pointers with @@local origin: " + ptr.idString())
this.setPointer(ptr as Pointer)
}
this.#unresolvedLocalPointers.clear()
}


Expand All @@ -458,6 +470,7 @@ export class Storage {

const dependencies = this.updatePointerSync(location, pointer, partialUpdateKey);
dependencies.delete(pointer);
if (Pointer.is_local) this.checkUnresolvedLocalDependenciesForPointer(pointer, dependencies);
this.updatePointerDependencies(pointer.id, [...dependencies].map(p=>p.id));
this.saveDependencyPointersSync(dependencies, listen_for_changes, location);

Expand All @@ -478,6 +491,7 @@ export class Storage {

const dependencies = await this.updatePointerAsync(location, pointer, partialUpdateKey);
dependencies.delete(pointer);
if (Pointer.is_local) this.checkUnresolvedLocalDependenciesForPointer(pointer, dependencies);
this.updatePointerDependencies(pointer.id, [...dependencies].map(p=>p.id));
await this.saveDependencyPointersAsync(dependencies, listen_for_changes, location);

Expand Down

0 comments on commit 897b285

Please sign in to comment.