@grapes-agency/apollo-link-webworker
Advanced tools
Comparing version 1.0.0-alpha.5 to 1.0.0-alpha.6
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.0.0-alpha.6](https://github.com/grapes-agency/graphql/compare/@grapes-agency/apollo-link-webworker@1.0.0-alpha.5...@grapes-agency/apollo-link-webworker@1.0.0-alpha.6) (2021-08-18) | ||
### Features | ||
* **apollo-link-webworker:** Options can be updated after link creation ([d753ace](https://github.com/grapes-agency/graphql/commit/d753ace2ebae6a6170ad1c28ae4bb6e8fd87ca75)) | ||
# [1.0.0-alpha.5](https://github.com/grapes-agency/graphql/compare/@grapes-agency/apollo-link-webworker@1.0.0-alpha.4...@grapes-agency/apollo-link-webworker@1.0.0-alpha.5) (2021-08-17) | ||
@@ -8,0 +19,0 @@ |
@@ -6,6 +6,6 @@ /// <reference types="zen-observable" /> | ||
import './RemoteOperation'; | ||
export declare class ApolloWebWorkerLink extends ApolloLink { | ||
protected options: Record<string, any>; | ||
export declare class ApolloWebWorkerLink<Options = Record<string, any>> extends ApolloLink { | ||
private apolloWorker; | ||
constructor(webWorker: Worker, options?: Record<string, any>); | ||
constructor(webWorker: Worker, options?: Options); | ||
updateOptions(options: Options): void; | ||
request(operation: Operation, forward?: NextLink): Observable<FetchResult<{ | ||
@@ -15,2 +15,2 @@ [key: string]: any; | ||
} | ||
export declare const createWebWorkerLink: (webWorker: Worker, options?: Record<string, any> | undefined) => ApolloWebWorkerLink; | ||
export declare const createWebWorkerLink: <Options = Record<string, any>>(webWorker: Worker, options?: Options | undefined) => ApolloWebWorkerLink<Options>; |
@@ -7,8 +7,15 @@ import { ApolloLink, Observable } from '@apollo/client'; | ||
class ApolloWebWorkerLink extends ApolloLink { | ||
constructor(webWorker, options = {}) { | ||
constructor(webWorker, options) { | ||
if (!(webWorker instanceof Worker)) { | ||
throw new Error('ApolloWebWorkerLink needs an initialized Worker'); | ||
} | ||
super(); | ||
this.options = options; | ||
this.apolloWorker = wrap(webWorker); | ||
this.apolloWorker.setup(this.options); | ||
if (options) { | ||
this.apolloWorker.setup(options); | ||
} | ||
} | ||
updateOptions(options) { | ||
this.apolloWorker.setup(options); | ||
} | ||
request(operation, forward) { | ||
@@ -15,0 +22,0 @@ return new Observable(observer => { |
@@ -11,3 +11,3 @@ /// <reference types="zen-observable" /> | ||
} | ||
export declare const createApolloWorker: (apolloLink: ApolloLink | ((options: Record<string, any>) => ApolloLink | Promise<ApolloLink>)) => void; | ||
export declare const createApolloWorker: <Options = Record<string, any>>(apolloLink: ApolloLink | ((options: Options) => ApolloLink | Promise<ApolloLink>)) => new () => Worker; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
import { Observable, ApolloLink } from '@apollo/client'; | ||
import { ApolloLink, Observable } from '@apollo/client'; | ||
import { expose } from 'comlink'; | ||
@@ -6,4 +6,27 @@ import './RemoteObservable.js'; | ||
const createAsyncLink = () => { | ||
let release = null; | ||
let linkPromise = new Promise(resolve => { | ||
release = link => { | ||
release = null; | ||
resolve(link); | ||
}; | ||
}); | ||
return { | ||
get: () => linkPromise, | ||
set: (link) => { | ||
if (release) { | ||
release(link); | ||
} | ||
else { | ||
linkPromise = Promise.resolve(link); | ||
} | ||
}, | ||
}; | ||
}; | ||
const createApolloWorker = (apolloLink) => { | ||
let link; | ||
const link = createAsyncLink(); | ||
if (apolloLink instanceof ApolloLink) { | ||
link.set(apolloLink); | ||
} | ||
const remoteRequestHandler = async (operation, forward) => { | ||
@@ -23,10 +46,19 @@ let next; | ||
} | ||
return (await link).request(operation, next); | ||
return (await link.get()).request(operation, next); | ||
}; | ||
expose({ | ||
setup: (options) => { | ||
link = Promise.resolve(apolloLink instanceof ApolloLink ? apolloLink : apolloLink(options)); | ||
if (apolloLink instanceof ApolloLink) { | ||
throw new Error('cannot setup apollo worker without create function'); | ||
} | ||
link.set(apolloLink(options)); | ||
}, | ||
request: remoteRequestHandler, | ||
}); | ||
class FakeWorker { | ||
constructor() { | ||
throw new Error('You did not properly loaded the link as Worker'); | ||
} | ||
} | ||
return FakeWorker; | ||
}; | ||
@@ -33,0 +65,0 @@ |
@@ -101,8 +101,15 @@ 'use strict'; | ||
class ApolloWebWorkerLink extends client.ApolloLink { | ||
constructor(webWorker, options = {}) { | ||
constructor(webWorker, options) { | ||
if (!(webWorker instanceof Worker)) { | ||
throw new Error('ApolloWebWorkerLink needs an initialized Worker'); | ||
} | ||
super(); | ||
this.options = options; | ||
this.apolloWorker = Comlink.wrap(webWorker); | ||
this.apolloWorker.setup(this.options); | ||
if (options) { | ||
this.apolloWorker.setup(options); | ||
} | ||
} | ||
updateOptions(options) { | ||
this.apolloWorker.setup(options); | ||
} | ||
request(operation, forward) { | ||
@@ -128,4 +135,27 @@ return new client.Observable(observer => { | ||
const createAsyncLink = () => { | ||
let release = null; | ||
let linkPromise = new Promise(resolve => { | ||
release = link => { | ||
release = null; | ||
resolve(link); | ||
}; | ||
}); | ||
return { | ||
get: () => linkPromise, | ||
set: (link) => { | ||
if (release) { | ||
release(link); | ||
} | ||
else { | ||
linkPromise = Promise.resolve(link); | ||
} | ||
}, | ||
}; | ||
}; | ||
const createApolloWorker = (apolloLink) => { | ||
let link; | ||
const link = createAsyncLink(); | ||
if (apolloLink instanceof client.ApolloLink) { | ||
link.set(apolloLink); | ||
} | ||
const remoteRequestHandler = async (operation, forward) => { | ||
@@ -145,10 +175,19 @@ let next; | ||
} | ||
return (await link).request(operation, next); | ||
return (await link.get()).request(operation, next); | ||
}; | ||
Comlink.expose({ | ||
setup: (options) => { | ||
link = Promise.resolve(apolloLink instanceof client.ApolloLink ? apolloLink : apolloLink(options)); | ||
if (apolloLink instanceof client.ApolloLink) { | ||
throw new Error('cannot setup apollo worker without create function'); | ||
} | ||
link.set(apolloLink(options)); | ||
}, | ||
request: remoteRequestHandler, | ||
}); | ||
class FakeWorker { | ||
constructor() { | ||
throw new Error('You did not properly loaded the link as Worker'); | ||
} | ||
} | ||
return FakeWorker; | ||
}; | ||
@@ -155,0 +194,0 @@ |
{ | ||
"name": "@grapes-agency/apollo-link-webworker", | ||
"version": "1.0.0-alpha.5", | ||
"version": "1.0.0-alpha.6", | ||
"description": "WebWorker for @apollo/client", | ||
@@ -25,3 +25,3 @@ "main": "./dist/index.cjs.js", | ||
}, | ||
"gitHead": "5cfa3d8d4a8d81696dc7ae8110e6efa3e97a89a2" | ||
"gitHead": "bd6d8b4f2eae00880b26bebe8b2ff2bf0a04ba3f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29495
395