Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eyston committed Jan 16, 2016
1 parent 301bc5c commit 4405fa7
Show file tree
Hide file tree
Showing 12 changed files with 998 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/RelayPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const RelayQL = require('RelayQL');
const RelayRootContainer = require('RelayRootContainer');
const RelayRoute = require('RelayRoute');
const RelayStore = require('RelayStore');
const RelaySubscription = require('RelaySubscription');
const RelayTaskScheduler = require('RelayTaskScheduler');
const RelayInternals = require('RelayInternals');

Expand All @@ -38,6 +39,7 @@ if (typeof global.__REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined') {
*/
var RelayPublic = {
Mutation: RelayMutation,
Subscription: RelaySubscription,
PropTypes: RelayPropTypes,
QL: RelayQL,
RootContainer: RelayRootContainer,
Expand Down
9 changes: 9 additions & 0 deletions src/network-layer/default/RelayDefaultNetworkLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import type RelayMutationRequest from 'RelayMutationRequest';
import type RelayQueryRequest from 'RelayQueryRequest';
import type RelaySubscriptionRequest from 'RelaySubscriptionRequest';
import type {Subscription} from 'RelayTypes';

const fetch = require('fetch');
const fetchWithRetries = require('fetchWithRetries');
Expand Down Expand Up @@ -92,6 +94,13 @@ class RelayDefaultNetworkLayer {
)));
}

sendSubscription(request: RelaySubscriptionRequest): Subscription {
throw new Error(
'RelayDefaultNetworkLayer: `sendSubscription` is not implemented in the ' +
'default network layer. A custom network layer must be injected.'
);
}

supports(...options: Array<string>): boolean {
// Does not support the only defined option, "defer".
return false;
Expand Down
28 changes: 28 additions & 0 deletions src/network/RelayNetworkLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
import type RelayMutationRequest from 'RelayMutationRequest';
const RelayProfiler = require('RelayProfiler');
import type RelayQueryRequest from 'RelayQueryRequest';
import type RelaySubscriptionRequest from 'RelaySubscriptionRequest';
import type {Subscription} from 'RelayTypes';

const invariant = require('invariant');
const warning = require('warning');

type NetworkLayer = {
sendMutation: (mutationRequest: RelayMutationRequest) => ?Promise;
Expand Down Expand Up @@ -53,6 +56,31 @@ var RelayNetworkLayer = {
}
},

sendSubscription(subscriptionRequest: RelaySubscriptionRequest): Subscription {
const networkLayer = getCurrentNetworkLayer();
const result = networkLayer.sendSubscription(subscriptionRequest);

if (result) {
if (typeof result === 'function') {
return {
dispose: result,
};
} else if (result.dispose && typeof result.dispose === 'function') {
return result;
} else {
warning(
false,
'NetworkLayer: `sendSubscription` should return a disposable or a ' +
'function.'
);
}
}

return {
dispose() { }, // noop
};
},

supports(...options: Array<string>): boolean {
var networkLayer = getCurrentNetworkLayer();
return networkLayer.supports(...options);
Expand Down
118 changes: 118 additions & 0 deletions src/network/RelaySubscriptionRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Copyright 2013-2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule RelaySubscriptionRequest
* @typechecks
* @flow
*/

'use strict';

import type {PrintedQuery} from 'RelayInternalTypes';
import type RelayQuery from 'RelayQuery';
import type {SubscriptionResult, SubscriptionCallbacks, Variables} from 'RelayTypes';

const printRelayQuery = require('printRelayQuery');

/**
* @internal
*
* Instances of these are made available via `RelayNetworkLayer.sendSubscription`.
*/
class RelaySubscriptionRequest {
_subscription: RelayQuery.Subscription;
_printedQuery: ?PrintedQuery;
_observer: SubscriptionCallbacks<SubscriptionResult>;

constructor(
subscription: RelayQuery.Subscription,
observer: SubscriptionCallbacks<SubscriptionResult>
) {
this._subscription = subscription;
this._observer = observer;
this._printedQuery = null;
}

/**
* @public
*
* Gets a string name used to refer to this request for printing debug output.
*/
getDebugName(): string {
return this._subscription.getName();
}

/**
* @public
*
* Gets the variables used by the subscription. These variables should be
* serialized and sent in the GraphQL request.
*/
getVariables(): Variables {
var printedQuery = this._printedQuery;
if (!printedQuery) {
printedQuery = printRelayQuery(this._subscription);
this._printedQuery = printedQuery;
}
return printedQuery.variables;
}

/**
* @public
*
* Gets a string representation of the GraphQL subscription.
*/
getQueryString(): string {
var printedQuery = this._printedQuery;
if (!printedQuery) {
printedQuery = printRelayQuery(this._subscription);
this._printedQuery = printedQuery;
}
return printedQuery.text;
}

/**
* @public
*
* Called when new event data is received for the subscription.
*/
onNext(result: SubscriptionResult): void {
this._observer.onNext(result);
}

/**
* @public
*
* Called when there is an error with the subscription. Ends the
* subscription.
*/
onError(err: any): void {
this._observer.onError(err);
}

/**
* @public
*
* Called when no more data will be provided to the subscriptions. Ends
* the subscription.
*/
onCompleted(): void {
this._observer.onCompleted();
}

/**
* @public
* @unstable
*/
getSubscription(): RelayQuery.Subscription {
return this._subscription;
}

}

module.exports = RelaySubscriptionRequest;
11 changes: 11 additions & 0 deletions src/store/RelayStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

const GraphQLFragmentPointer = require('GraphQLFragmentPointer');
import type RelayMutation from 'RelayMutation';
import type RelaySubscription from 'RelaySubscription';
const RelayMutationTransaction = require('RelayMutationTransaction');
const RelayQuery = require('RelayQuery');
const RelayQueryResultObservable = require('RelayQueryResultObservable');
const RelayStoreData = require('RelayStoreData');

const createSubscription = require('createSubscription');
const forEachRootCallArg = require('forEachRootCallArg');
const readRelayQueryData = require('readRelayQueryData');
const warning = require('warning');
Expand All @@ -28,9 +30,11 @@ import type {
Abortable,
Observable,
RelayMutationTransactionCommitCallbacks,
SubscriptionCallbacks,
ReadyStateChangeCallback,
StoreReaderData,
StoreReaderOptions,
Subscription,
} from 'RelayTypes';

import type {
Expand Down Expand Up @@ -205,6 +209,13 @@ var RelayStore = {
);
this.commitUpdate(mutation, callbacks);
},

subscribe(
subscription: RelaySubscription,
callbacks?: SubscriptionCallbacks
): Subscription {
return createSubscription(storeData, subscription, callbacks);
},
};

module.exports = RelayStore;
Loading

0 comments on commit 4405fa7

Please sign in to comment.