Skip to content

Commit

Permalink
Create Interfaces for MultiActorEnvironment and ActorSpecificEnvironment
Browse files Browse the repository at this point in the history
Reviewed By: kassens

Differential Revision: D27678869

fbshipit-source-id: 01407e987846863f78ad45dab662523dadb4ac90
  • Loading branch information
alunyov authored and facebook-github-bot committed Apr 14, 2021
1 parent 636118d commit 9fc0fb6
Show file tree
Hide file tree
Showing 6 changed files with 689 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/relay-runtime/multi-actor-environment/ActorIdentifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

'use strict';

/**
* A unique identifier of the current actor.
*/
export opaque type ActorIdentifier = string;

module.exports = {
getActorIdentifier(actorID: string): ActorIdentifier {
return (actorID: ActorIdentifier);
},

getDefaultActorIdentifier(): ActorIdentifier {
throw new Error('Not Implemented');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

import type {ActorIdentifier} from './ActorIdentifier';
import type {
IActorEnvironment,
IMultiActorEnvironment,
} from './MultiActorEnvironmentTypes';
import type {
RenderPolicy,
Disposable,
Observable as RelayObservable,
LogFunction,
OptimisticResponseConfig,
OptimisticUpdateFunction,
OperationDescriptor,
OperationAvailability,
Snapshot,
PayloadData,
INetwork,
Store,
SelectorStoreUpdater,
GraphQLResponse,
SingularReaderSelector,
OperationTracker as RelayOperationTracker,
StoreUpdater,
ExecuteMutationConfig,
RequiredFieldLogger,
} from 'relay-runtime';

function todo() {
throw new Error('Not implementd');
}

export type ActorSpecificEnvironmentConfig = $ReadOnly<{
actorIdentifier: ActorIdentifier,
multiActorEnvironment: IMultiActorEnvironment,
logFn: LogFunction,
requiredFieldLogger: RequiredFieldLogger,
}>;

class ActorSpecificEnvironment implements IActorEnvironment {
+options: mixed;
__log: LogFunction;
requiredFieldLogger: RequiredFieldLogger;
+store: Store;

// Actor specific properties
+actorIdentifier: ActorIdentifier;
+multiActorEnvironment: IMultiActorEnvironment;

constructor(config: ActorSpecificEnvironmentConfig) {
this.actorIdentifier = config.actorIdentifier;
this.multiActorEnvironment = config.multiActorEnvironment;

this.__log = config.logFn;
this.requiredFieldLogger = config.requiredFieldLogger;
}

UNSTABLE_getDefaultRenderPolicy(): RenderPolicy {
return todo();
}

applyMutation(optimisticConfig: OptimisticResponseConfig): Disposable {
return this.multiActorEnvironment.applyMutation(
this.actorIdentifier,
optimisticConfig,
);
}

applyUpdate(optimisticUpdate: OptimisticUpdateFunction): Disposable {
return this.multiActorEnvironment.applyUpdate(
this.actorIdentifier,
optimisticUpdate,
);
}

check(operation: OperationDescriptor): OperationAvailability {
return this.multiActorEnvironment.check(this.actorIdentifier, operation);
}

subscribe(
snapshot: Snapshot,
callback: (snapshot: Snapshot) => void,
): Disposable {
return this.multiActorEnvironment.subscribe(
this.actorIdentifier,
snapshot,
callback,
);
}

retain(operation: OperationDescriptor): Disposable {
return this.multiActorEnvironment.retain(this.actorIdentifier, operation);
}

commitUpdate(updater: StoreUpdater): void {
return this.multiActorEnvironment.commitUpdate(
this.actorIdentifier,
updater,
);
}

/**
* Commit a payload to the environment using the given operation selector.
*/
commitPayload(
operationDescriptor: OperationDescriptor,
payload: PayloadData,
): void {
return this.multiActorEnvironment.commitPayload(
this.actorIdentifier,
operationDescriptor,
payload,
);
}

getNetwork(): INetwork {
return this.multiActorEnvironment.getNetwork(this.actorIdentifier);
}

getStore(): Store {
return this.store;
}

getOperationTracker(): RelayOperationTracker {
return this.multiActorEnvironment.getOperationTracker(this.actorIdentifier);
}

lookup(selector: SingularReaderSelector): Snapshot {
return this.multiActorEnvironment.lookup(this.actorIdentifier, selector);
}

execute(config: {
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
}): RelayObservable<GraphQLResponse> {
return todo();
}

executeMutation(
options: ExecuteMutationConfig,
): RelayObservable<GraphQLResponse> {
return this.multiActorEnvironment.executeMutation(
this.actorIdentifier,
options,
);
}

executeWithSource(options: {
operation: OperationDescriptor,
source: RelayObservable<GraphQLResponse>,
}): RelayObservable<GraphQLResponse> {
return this.multiActorEnvironment.executeWithSource(
this.actorIdentifier,
options,
);
}

isRequestActive(requestIdentifier: string): boolean {
return this.multiActorEnvironment.isRequestActive(
this.actorIdentifier,
requestIdentifier,
);
}

isServer(): boolean {
return todo();
}
}

module.exports = ActorSpecificEnvironment;
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

'use strict';

const ActorSpecificEnvironment = require('./ActorSpecificEnvironment');

import type {ActorIdentifier} from './ActorIdentifier';
import type {
IActorEnvironment,
IMultiActorEnvironment,
} from './MultiActorEnvironmentTypes';
import type {
IEnvironment,
Disposable,
Observable as RelayObservable,
OptimisticResponseConfig,
OptimisticUpdateFunction,
OperationDescriptor,
OperationAvailability,
Snapshot,
PayloadData,
INetwork,
Store,
SelectorStoreUpdater,
GraphQLResponse,
SingularReaderSelector,
OperationTracker as RelayOperationTracker,
StoreUpdater,
RequiredFieldLogger,
ExecuteMutationConfig,
LogFunction,
} from 'relay-runtime';

function todo() {
throw new Error('Not implementd');
}

export type MultiActorEnvironmentConfig = $ReadOnly<{
network: INetwork,
logFn: LogFunction,
requiredFieldLogger: RequiredFieldLogger,
}>;

class MultiActorEnvironment implements IMultiActorEnvironment {
+__logFn: LogFunction;
+__requiredFieldLogger: RequiredFieldLogger;

+_actorEnvironments: Map<ActorIdentifier, IEnvironment & IActorEnvironment>;

constructor(config: MultiActorEnvironmentConfig) {
this._actorEnvironments = new Map();
this.__logFn = config.logFn;
this.__requiredFieldLogger = config.requiredFieldLogger;
}

/**
* This method will create an actor specfic environment. It will create a new instance
* and store it in the internal maps. If will return a memozied version
* of the environment if we already created one for actor.
*/
forActor(actorIdentifier: ActorIdentifier): IEnvironment & IActorEnvironment {
const environment = this._actorEnvironments.get(actorIdentifier);
if (environment == null) {
const newEnvironment = new ActorSpecificEnvironment({
actorIdentifier,
multiActorEnvironment: this,
logFn: this.__logFn,
requiredFieldLogger: this.__requiredFieldLogger,
});
this._actorEnvironments.set(actorIdentifier, newEnvironment);
return newEnvironment;
} else {
return environment;
}
}

check(
actorIdentifier: ActorIdentifier,
operation: OperationDescriptor,
): OperationAvailability {
return todo();
}

subscribe(
actorIdentifier: ActorIdentifier,
snapshot: Snapshot,
callback: (snapshot: Snapshot) => void,
): Disposable {
return todo();
}

retain(
actorIdentifier: ActorIdentifier,
operation: OperationDescriptor,
): Disposable {
return todo();
}

applyUpdate(
actorIdentifier: ActorIdentifier,
optimisticUpdate: OptimisticUpdateFunction,
): Disposable {
return todo();
}

applyMutation(
actorIdentifier: ActorIdentifier,
optimisticConfig: OptimisticResponseConfig,
): Disposable {
return todo();
}

commitUpdate(actorIdentifier: ActorIdentifier, updater: StoreUpdater): void {
return todo();
}

commitPayload(
actorIdentifier: ActorIdentifier,
operationDescriptor: OperationDescriptor,
payload: PayloadData,
): void {
return todo();
}

getNetwork(actorIdentifier: ActorIdentifier): INetwork {
return todo();
}

getStore(actorIdentifier: ActorIdentifier): Store {
return todo();
}

getOperationTracker(actorIdentifier: ActorIdentifier): RelayOperationTracker {
return todo();
}

lookup(
actorIdentifier: ActorIdentifier,
selector: SingularReaderSelector,
): Snapshot {
return todo();
}

execute(
actorIdentifier: ActorIdentifier,
config: {
operation: OperationDescriptor,
updater?: ?SelectorStoreUpdater,
},
): RelayObservable<GraphQLResponse> {
return todo();
}

executeMutation(
actorIdentifier: ActorIdentifier,
config: ExecuteMutationConfig,
): RelayObservable<GraphQLResponse> {
return todo();
}

executeWithSource(
actorIdentifier: ActorIdentifier,
config: {
operation: OperationDescriptor,
source: RelayObservable<GraphQLResponse>,
},
): RelayObservable<GraphQLResponse> {
return todo();
}

isRequestActive(
actorIdentifier: ActorIdentifier,
requestIdentifier: string,
): boolean {
return todo();
}
}

module.exports = MultiActorEnvironment;
Loading

0 comments on commit 9fc0fb6

Please sign in to comment.