@kubun/with-relay 0.17.1 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,10 +1,22 @@
1
+ import { type SubscriptionRecoveryOptions } from '@kubun/client';
1
2
  import type { GraphQLSource } from '@kubun/protocol';
2
3
  import { type CacheConfig, Environment, type INetwork, Store } from 'relay-runtime';
3
- export type NetworkParams = {
4
+ export type NetworkParams = SubscriptionRecoveryOptions & {
4
5
  source: GraphQLSource;
6
+ /**
7
+ * Makes the store consistent again after a subscription recovered: events sent while it
8
+ * was down are lost. Without it a subscription does not recover, and its interruption
9
+ * reaches the subscriber as an error.
10
+ */
11
+ rebaseline?: () => Promise<unknown>;
5
12
  };
6
- export type EnvironmentParams = NetworkParams & {
13
+ export type EnvironmentParams = Omit<NetworkParams, 'rebaseline'> & {
7
14
  store?: Store;
15
+ /**
16
+ * Makes the store consistent again after a subscription recovered. Defaults to
17
+ * invalidating the store, so each query is fetched again the next time it is read.
18
+ */
19
+ rebaseline?: (environment: Environment) => Promise<unknown>;
8
20
  };
9
21
  /**
10
22
  * The connector operation-envelope ids a mutation carries (see
package/lib/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { Environment, Network, Observable, RecordSource, Store } from 'relay-runtime';
1
+ import { runRecoveringSubscription } from '@kubun/client';
2
+ import { commitLocalUpdate, Environment, Network, Observable, RecordSource, Store } from 'relay-runtime';
2
3
  /**
3
4
  * The Relay `cacheConfig` that carries an operation envelope to the network
4
5
  * layer, for a mutation commit (`commitMutation` / `useMutation`).
@@ -26,7 +27,7 @@ function operationEnvelopeFrom(cacheConfig) {
26
27
  };
27
28
  }
28
29
  export function createNetwork(params) {
29
- const { source } = params;
30
+ const { source, rebaseline, ...recovery } = params;
30
31
  return Network.create((request, variables, cacheConfig)=>{
31
32
  const text = request.text;
32
33
  if (text == null) {
@@ -50,58 +51,35 @@ export function createNetwork(params) {
50
51
  sink.error(new Error('Relay request is missing query text'));
51
52
  });
52
53
  }
53
- return Observable.create((sink)=>{
54
- let disposed = false;
55
- let generator;
56
- source.subscribe({
57
- text,
58
- variables: variables
59
- }).then((result)=>{
60
- if (Symbol.asyncIterator in result) {
61
- generator = result;
62
- // Dispose may win the race against this resolution: close the
63
- // server subscription immediately rather than draining it forever.
64
- if (disposed) {
65
- void generator.return?.(undefined);
66
- return;
67
- }
68
- ;
69
- (async ()=>{
70
- try {
71
- for await (const value of generator){
72
- if (disposed) break;
73
- sink.next(value);
74
- }
75
- if (!disposed) {
76
- sink.complete();
77
- }
78
- } catch (error) {
79
- if (!disposed) {
80
- sink.error(error instanceof Error ? error : new Error(String(error)));
81
- }
82
- }
83
- })();
84
- return;
85
- }
86
- sink.next(result);
87
- sink.complete();
88
- }, (error)=>{
89
- sink.error(error instanceof Error ? error : new Error(String(error)));
90
- });
91
- // Closing the consumer generator propagates through the client and
92
- // transport to tear down the server-side subscription. The `disposed`
93
- // flag still guards sink.next/complete/error after teardown.
94
- return ()=>{
95
- disposed = true;
96
- void generator?.return?.(undefined);
97
- };
98
- });
54
+ const subscriptionParams = {
55
+ text,
56
+ variables: variables
57
+ };
58
+ // The returned close also wins a race against the subscribe resolving, and closing
59
+ // the consumer generator propagates through the client and transport to tear down
60
+ // the server-side subscription.
61
+ return Observable.create((sink)=>runRecoveringSubscription({
62
+ ...recovery,
63
+ subscribe: ()=>source.subscribe(subscriptionParams),
64
+ rebaseline,
65
+ next: (value)=>sink.next(value),
66
+ error: (error)=>sink.error(error),
67
+ complete: ()=>sink.complete()
68
+ }));
99
69
  });
100
70
  }
71
+ function invalidateStore(environment) {
72
+ commitLocalUpdate(environment, (store)=>store.invalidateStore());
73
+ return Promise.resolve();
74
+ }
101
75
  export function createEnvironment(params) {
102
- const { store, ...networkParams } = params;
103
- return new Environment({
104
- network: createNetwork(networkParams),
76
+ const { store, rebaseline = invalidateStore, ...networkParams } = params;
77
+ const environment = new Environment({
78
+ network: createNetwork({
79
+ ...networkParams,
80
+ rebaseline: ()=>rebaseline(environment)
81
+ }),
105
82
  store: store ?? new Store(new RecordSource())
106
83
  });
84
+ return environment;
107
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/with-relay",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,
@@ -15,16 +15,21 @@
15
15
  "LICENSE.md"
16
16
  ],
17
17
  "dependencies": {
18
+ "@kubun/client": "^0.18.0",
18
19
  "relay-runtime": "^21.0.1"
19
20
  },
20
21
  "devDependencies": {
21
- "@enkaku/transport": "^0.21.0",
22
- "@kubun/client": "^0.17.2",
23
- "@kubun/engine": "^0.17.2",
24
- "@kubun/models": "^0.17.0",
25
- "@kubun/plugin-service-server": "^0.17.1",
26
- "@kubun/protocol": "^0.17.1",
27
- "@kubun/service-graph-api": "^0.17.2",
22
+ "@enkaku/transport": "^0.21.5",
23
+ "@kubun/engine": "^0.18.0",
24
+ "@kubun/models-calendar": "^0.18.0",
25
+ "@kubun/models-contacts": "^0.18.0",
26
+ "@kubun/models-content": "^0.18.0",
27
+ "@kubun/models-core": "^0.18.0",
28
+ "@kubun/models-messaging": "^0.18.0",
29
+ "@kubun/models-tasks": "^0.18.0",
30
+ "@kubun/plugin-service-server": "^0.18.0",
31
+ "@kubun/protocol": "^0.18.0",
32
+ "@kubun/service-graph-api": "^0.18.0",
28
33
  "@kubun/test-utils": "^0.0.0",
29
34
  "graphql": "^17.0.2"
30
35
  },