forked from GMOD/Apollo3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ApolloRefNameAliasAdapter (GMOD#417)
* ref name alias apollo configuration * Fix error message --------- Co-authored-by: Garrett Stevens <stevens.garrett.j@gmail.com>
- Loading branch information
1 parent
4ecf6e3
commit cb1aced
Showing
9 changed files
with
234 additions
and
10 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
packages/jbrowse-plugin-apollo/src/ApolloRefNameAliasAdapter/ApolloRefNameAliasAdapter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { RefNameAliases } from './../BackendDrivers/BackendDriver' | ||
import { | ||
BaseRefNameAliasAdapter, | ||
BaseAdapter, | ||
} from '@jbrowse/core/data_adapters/BaseAdapter' | ||
import { readConfObject } from '@jbrowse/core/configuration' | ||
import { ApolloSessionModel } from '../session' | ||
import { BackendDriver } from '../BackendDrivers' | ||
import { nanoid } from 'nanoid' | ||
import RpcServer from 'librpc-web-mod/dist/server' | ||
|
||
declare global { | ||
const rpcServer: RpcServer | ||
} | ||
|
||
interface ApolloRefNameAliasMessage { | ||
apollo: true | ||
messageId: string | ||
refNameAliases: RefNameAliases[] | ||
} | ||
|
||
function isApolloRefNameAliasMessage( | ||
data?: unknown, | ||
): data is ApolloRefNameAliasMessage { | ||
return ( | ||
typeof data === 'object' && | ||
data !== null && | ||
'apollo' in data && | ||
data.apollo === true && | ||
'refNameAliases' in data | ||
) | ||
} | ||
|
||
const isInWebWorker = typeof sessionStorage === 'undefined' | ||
|
||
export default class RefNameAliasAdapter | ||
extends BaseAdapter | ||
implements BaseRefNameAliasAdapter | ||
{ | ||
private refNameAliases: RefNameAliases[] | undefined | ||
|
||
async getRefNameAliases() { | ||
const assemblyId = readConfObject(this.config, 'assemblyId') as string | ||
if (!isInWebWorker) { | ||
const dataStore = ( | ||
this.pluginManager?.rootModel?.session as ApolloSessionModel | undefined | ||
)?.apolloDataStore | ||
if (!dataStore) { | ||
throw new Error('No Apollo data store found') | ||
} | ||
const backendDriver = dataStore.getBackendDriver( | ||
assemblyId, | ||
) as BackendDriver | ||
const refNameAliases = await backendDriver.getRefNameAliases(assemblyId) | ||
return refNameAliases | ||
} | ||
const refNameAliases = await new Promise( | ||
( | ||
resolve: (refNameAliases: RefNameAliases[]) => void, | ||
reject: (reason: Error) => void, | ||
) => { | ||
const timeoutId = setTimeout(() => { | ||
reject(new Error('timeout')) | ||
}, 20_000) | ||
const messageId = nanoid() | ||
const messageListener = (event: MessageEvent) => { | ||
const data = event.data as ApolloRefNameAliasMessage | ||
if (!isApolloRefNameAliasMessage(data)) { | ||
return | ||
} | ||
if (data.messageId !== messageId) { | ||
return | ||
} | ||
clearTimeout(timeoutId) | ||
removeEventListener('message', messageListener) | ||
resolve(data.refNameAliases) | ||
} | ||
addEventListener('message', messageListener) | ||
rpcServer.emit('apollo', { | ||
apollo: true, | ||
method: 'getRefNameAliases', | ||
assembly: assemblyId, | ||
messageId, | ||
}) | ||
}, | ||
) | ||
this.refNameAliases = refNameAliases | ||
return refNameAliases | ||
} | ||
|
||
async freeResources() { | ||
// no resources to free | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/jbrowse-plugin-apollo/src/ApolloRefNameAliasAdapter/configSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ConfigurationSchema } from '@jbrowse/core/configuration' | ||
|
||
export default ConfigurationSchema( | ||
'ApolloRefNameAliasAdapter', | ||
{ | ||
assemblyId: { | ||
type: 'string', | ||
defaultValue: '', | ||
}, | ||
}, | ||
{ explicitlyTyped: true }, | ||
) |
21 changes: 21 additions & 0 deletions
21
packages/jbrowse-plugin-apollo/src/ApolloRefNameAliasAdapter/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import AdapterType from '@jbrowse/core/pluggableElementTypes/AdapterType' | ||
import PluginManager from '@jbrowse/core/PluginManager' | ||
|
||
import configSchema from './configSchema' | ||
import ApolloRefNameAliasAdapter from './ApolloRefNameAliasAdapter' | ||
|
||
export function installApolloRefNameAliasAdapter(pluginManager: PluginManager) { | ||
pluginManager.addAdapterType( | ||
() => | ||
new AdapterType({ | ||
name: 'ApolloRefNameAliasAdapter', | ||
configSchema, | ||
adapterMetadata: { | ||
category: undefined, | ||
hiddenFromGUI: true, | ||
description: undefined, | ||
}, | ||
AdapterClass: ApolloRefNameAliasAdapter, | ||
}), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters