Skip to content

Commit

Permalink
Remove StorageProxy dependency from handle.ts (PolymerLabs#4225)
Browse files Browse the repository at this point in the history
This reduces the counted number of cycles to 8, and the real number to 5.

* Remove StorageProxy dependency from handle.ts

* Add a type for the no-op storage proxy allocator
  • Loading branch information
raulverag authored Dec 6, 2019
1 parent 56dbbd2 commit 1c8e255
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/platform/loader-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {Dictionary} from '../runtime/hot.js';
import '../runtime/schema-from-literal.js';
import '../runtime/type-from-literal.js';
import '../runtime/handle-constructors.js';
import '../runtime/noop-proxy.js';
import '../runtime/storageNG/store-constructors.js';

type ParticleCtor = typeof Particle;
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import {ParticleExecutionContext} from './particle-execution-context.js';
import {Reference} from './reference.js';
import {TypeChecker} from './recipe/type-checker.js';
import {Storable} from './handle.js';
import {SerializedEntity} from './storage-proxy.js';
import {Id, IdGenerator} from './id.js';
import {Dictionary, Consumer} from './hot.js';
import {SYMBOL_INTERNALS} from './symbols.js';

export type EntityRawData = {};

export type SerializedEntity = {id: string, rawData: EntityRawData};

/**
* Represents mutable entity data. Instances will have mutable properties defined on them for all
* of the fields defined in the schema for the entity. This type permits indexing by all strings,
Expand Down
20 changes: 14 additions & 6 deletions src/runtime/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import {SystemException, UserException} from './arc-exceptions.js';
import {ParticleSpec} from './particle-spec.js';
import {Particle} from './particle.js';
import {Reference} from './reference.js';
import {SerializedEntity, StorageProxy} from './storage-proxy.js';
import {BigCollectionType, CollectionType, EntityType, InterfaceType, ReferenceType} from './type.js';
import {EntityClass, Entity} from './entity.js';
import {BigCollectionType, CollectionType, EntityType, InterfaceType, ReferenceType, Type} from './type.js';
import {EntityClass, Entity, SerializedEntity} from './entity.js';
import {Store, SingletonStore, CollectionStore, BigCollectionStore} from './store.js';
import {IdGenerator, Id} from './id.js';
import {SYMBOL_INTERNALS} from './symbols.js';
Expand Down Expand Up @@ -57,6 +56,8 @@ function restore(entry: SerializedEntity, entityClass: EntityClass) {

export interface HandleOptions {keepSynced: boolean; notifySync: boolean; notifyUpdate: boolean; notifyDesync: boolean;}

type NoOpStorageAllocator = (id: string, type: Type) => Store;

/**
* Base class for Collections and Singletons.
*/
Expand All @@ -70,6 +71,8 @@ export abstract class HandleOld {
readonly options: HandleOptions;
entityClass: EntityClass|null;

static noOpStorageAllocator : NoOpStorageAllocator = null;

abstract _notify(kind: string, particle: Particle, details: {});

// TODO type particleId, marked as string, but called with number
Expand Down Expand Up @@ -156,12 +159,17 @@ export abstract class HandleOld {
* storage proxy
*/
disable(particle?: Particle): void {
if (this.storage instanceof StorageProxy) {
this.storage.deregister(particle, this);
// This used to check that storage is a StorageProxy, but we are trying
// to remove all references to StorageProxy to break cyclic dependencies.
// tslint:disable-next-line: no-any
if (typeof (this.storage as any).deregister === 'function') {
// tslint:disable-next-line: no-any
(this.storage as any).deregister(particle, this);
}
// Set this handle's storage to a no-operation storage proxy so any actions that need to be
// taken by this handle in the future (due to some async operations) will do nothing and finish quietly
this._storage = StorageProxy.newNoOpProxy(this.storage.id, this.storage.type);
//this._storage = StorageProxy.newNoOpProxy(this.storage.id, this.storage.type);
this._storage = HandleOld.noOpStorageAllocator(this.storage.id, this.storage.type);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/runtime/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ import {StorageKeyParser} from './storageNG/storage-key-parser.js';
import {VolatileStorageKey} from './storageNG/drivers/volatile.js';
import {RamDiskStorageKey} from './storageNG/drivers/ramdisk.js';
import {CRDTSingletonTypeRecord} from './crdt/crdt-singleton.js';
import {Entity} from './entity.js';
import {SerializedEntity} from './storage-proxy.js';
import {Entity, SerializedEntity} from './entity.js';
import {Runtime} from './runtime.js';

export enum ErrorSeverity {
Expand Down
16 changes: 16 additions & 0 deletions src/runtime/noop-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @license
* Copyright (c) 2017 Google Inc. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* Code distributed by Google as part of this project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/

import {StorageProxy} from './storage-proxy.js';
import {HandleOld} from './handle.js';

// Inject the function to allocate a NoOpStorageProxy, in order to remove
// the dependency on StorageProxy from handle.js.
HandleOld.noOpStorageAllocator = StorageProxy.newNoOpProxy;
4 changes: 2 additions & 2 deletions src/runtime/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {assert} from '../platform/assert-web.js';
import {Collection, Storable, unifiedHandleFor} from './handle.js';
import {ParticleExecutionContext} from './particle-execution-context.js';
import {ReferenceType, EntityType} from './type.js';
import {Entity} from './entity.js';
import {SerializedEntity, StorageProxy} from './storage-proxy.js';
import {Entity, SerializedEntity} from './entity.js';
import {StorageProxy} from './storage-proxy.js';
import {SYMBOL_INTERNALS} from './symbols.js';
import {CollectionHandle} from './storageNG/handle.js';

Expand Down
4 changes: 1 addition & 3 deletions src/runtime/storage-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import {ParticleExecutionContext} from './particle-execution-context.js';
import {Particle} from './particle.js';
import {CrdtCollectionModel, SerializedModelEntry, ModelValue} from './storage/crdt-collection-model.js';
import {BigCollectionType, CollectionType, Type} from './type.js';
import {EntityRawData} from './entity.js';
import {SerializedEntity} from './entity.js';
import {Store, SingletonStore, CollectionStore, BigCollectionStore} from './store.js';
import {Id} from './id.js';

enum SyncState {none, pending, full}

export type SerializedEntity = {id: string, rawData: EntityRawData};

/**
* Mediates between one or more Handles and the backing store outside the PEC.
*
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/storageNG/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import {CRDTOperation, CRDTTypeRecord, VersionMap} from '../crdt/crdt.js';
import {CollectionOperation, CollectionOpTypes, CRDTCollectionTypeRecord, Referenceable} from '../crdt/crdt-collection.js';
import {CRDTSingletonTypeRecord, SingletonOperation, SingletonOpTypes} from '../crdt/crdt-singleton.js';
import {Particle} from '../particle.js';
import {Entity, EntityClass} from '../entity.js';
import {Entity, EntityClass, SerializedEntity} from '../entity.js';
import {IdGenerator, Id} from '../id.js';
import {EntityType, Type} from '../type.js';
import {StorageProxy, NoOpStorageProxy} from './storage-proxy.js';
import {SYMBOL_INTERNALS} from '../symbols.js';
import {SerializedEntity} from '../storage-proxy.js';

export interface HandleOptions {
keepSynced: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/storageNG/reference-mode-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {Producer, Consumer, Runnable, Dictionary} from '../hot.js';
import {PropagatedException} from '../arc-exceptions.js';
import {Store} from './store.js';
import {noAwait} from '../util.js';
import {SerializedEntity} from '../storage-proxy.js';
import {SerializedEntity} from '../entity.js';
import {ReferenceModeStorageKey} from './reference-mode-storage-key.js';

// ReferenceMode store uses an expanded notion of Reference that also includes a version. This allows stores to block on
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/storageNG/tests/handle-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import {CollectionHandle, SingletonHandle, handleNGFor} from '../handle.js';
import {StorageProxy} from '../storage-proxy.js';
import {ProxyMessageType} from '../store.js';
import {MockParticle, MockStore} from '../testing/test-storage.js';
import {SerializedEntity} from '../../storage-proxy.js';
import {Manifest} from '../../manifest.js';
import {EntityClass, Entity} from '../../entity.js';
import {EntityClass, Entity, SerializedEntity} from '../../entity.js';


async function getCollectionHandle(primitiveType: Type, particle?: MockParticle, canRead=true, canWrite=true):
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/storageNG/tests/reference-mode-store-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {CRDTCollection, CollectionOpTypes, CollectionData, CollectionOperation,
import {CRDTSingleton} from '../../crdt/crdt-singleton.js';
import {CountType, CollectionType, EntityType, SingletonType} from '../../type.js';
import {Schema} from '../../schema.js';
import {SerializedEntity} from '../../storage-proxy.js';
import {SerializedEntity} from '../../entity.js';
import {ReferenceModeStorageKey} from '../reference-mode-storage-key.js';

/* eslint-disable no-async-promise-executor */
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/testing/handle-for-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import {Referenceable, CRDTCollectionTypeRecord} from '../crdt/crdt-collection.j
import {CRDTTypeRecord} from '../crdt/crdt.js';
import {CRDTSingletonTypeRecord} from '../crdt/crdt-singleton.js';
import {Manifest} from '../manifest.js';
import {SerializedEntity} from '../storage-proxy.js';
import {Entity} from '../entity.js';
import {Entity, SerializedEntity} from '../entity.js';
import {VolatileStorageKey} from '../../runtime/storageNG/drivers/volatile.js';
import {StorageKey} from '../../runtime/storageNG/storage-key.js';
import {ArcId} from '../../runtime/id.js';
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CircularDependencyPlugin = require('circular-dependency-plugin');
const lib = './shells/lib';

// Decrease MAX_CYCLES every time you eliminate circular dependencies from the codebase.
const MAX_CYCLES = 11;
const MAX_CYCLES = 8;
let numCyclesDetected = 0;

module.exports = {
Expand Down

0 comments on commit 1c8e255

Please sign in to comment.