Skip to content

Commit

Permalink
Add memletConfig option to makePluginIo
Browse files Browse the repository at this point in the history
I'd much rather have a push strategy for this state, but the best I
could achieve and test to work is a pulling strategy.
The plugin factory will pull for the first instance.
  • Loading branch information
samholmes committed May 10, 2024
1 parent 840c9a8 commit 04481b0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- added: Support for ElectrumX server WebSocket connections under 'electrumws(s):' protocol scheme.
- added: Query for UTXO data from ElectrumX servers (sans transaction data).
- added: `memletConfig` option to new `makePluginIo` API to config memlet memory usage.
- changed: Plugin package now builds itself into a react-native module using webpack (see README for installation guide).

## 2.6.2 (2024-04-24)
Expand Down
13 changes: 8 additions & 5 deletions edge-currency-plugins.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { EdgeCorePluginOptions, EdgeCurrencyPlugin } from 'edge-core-js/types'

import {
EdgeCorePluginOptions,
EdgeCurrencyPlugin,
EdgeOtherMethods
} from 'edge-core-js/types'
EdgeCurrencyPluginIoOptions,
EdgeCurrencyPluginNativeIo
} from './src/react-native'

/**
* Add this to your `nativeIo` object on React Native,
* as `{ 'edge-currency-plugins': makePluginIo() }`
*/
export function makePluginIo(): EdgeOtherMethods
export function makePluginIo(
options?: EdgeCurrencyPluginIoOptions
): EdgeCurrencyPluginNativeIo

/**
* Debugging-URI to use on React Native,
Expand Down
25 changes: 24 additions & 1 deletion src/common/plugin/CurrencyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
EdgeCurrencyTools,
EdgeWalletInfo
} from 'edge-core-js/types'
import { setMemletConfig } from 'memlet'

import { EdgeCurrencyPluginNativeIo } from '../../react-native'
import {
asUtxoInitOptions,
asUtxoUserSettings
Expand All @@ -17,12 +19,14 @@ import { makeEngineEmitter } from './EngineEmitter'
import { makePluginState } from './PluginState'
import { EngineConfig, PluginInfo } from './types'

let hasMemletBeenSet = false

export function makeCurrencyPlugin(
pluginOptions: EdgeCorePluginOptions,
pluginInfo: PluginInfo
): EdgeCurrencyPlugin {
const { currencyInfo } = pluginInfo
const { io, log, pluginDisklet, initOptions } = pluginOptions
const { initOptions, io, log, nativeIo, pluginDisklet } = pluginOptions
const currencyTools = makeCurrencyTools(io, pluginInfo)
const { defaultSettings, pluginId, currencyCode } = currencyInfo
const pluginState = makePluginState({
Expand All @@ -33,6 +37,25 @@ export function makeCurrencyPlugin(
log,
defaultSettings: asUtxoUserSettings(defaultSettings)
})

if (!hasMemletBeenSet) {
hasMemletBeenSet = true

const nativeMethods = nativeIo[
'edge-currency-plugins'
] as EdgeCurrencyPluginNativeIo

nativeMethods
.getMemletConfig()
.then(config => {
if (config == null) return
setMemletConfig(config)
})
.catch(e => {
log.error(e)
})
}

return {
currencyInfo,

Expand Down
25 changes: 21 additions & 4 deletions src/react-native.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { EdgeOtherMethods } from 'edge-core-js/types'
import { MemletConfig } from 'memlet'
import { NativeModules } from 'react-native'
// import { bridgifyObject, emit, onMethod } from 'yaob'

const { EdgeCurrencyPluginsModule } = NativeModules
const { sourceUri } = EdgeCurrencyPluginsModule.getConstants()

export const pluginUri = sourceUri
export const debugUri = 'http://localhost:8101/edge-currency-plugins.js'

export function makePluginIo(): EdgeOtherMethods {
return {}
export interface EdgeCurrencyPluginIoOptions {
readonly memletConfig?: MemletConfig
}

export interface EdgeCurrencyPluginNativeIo {
readonly memletConfig?: MemletConfig
readonly getMemletConfig: () => Promise<Partial<MemletConfig | undefined>>
}

export function makePluginIo(
options: EdgeCurrencyPluginIoOptions = {}
): EdgeCurrencyPluginNativeIo {
const { memletConfig } = options

return {
memletConfig,
getMemletConfig: async () => {
return memletConfig
}
}
}
4 changes: 3 additions & 1 deletion test/common/plugin/CurrencyPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { before, describe, it } from 'mocha'

import edgeCorePlugins from '../../../src/index'
import { testLog } from '../../util/testLog'
import { makeFakeNativeIo } from '../../utils'
import { fixtures } from './CurrencyPlugin.fixtures/index'

describe('currencyPlugins.spec', () => {
Expand All @@ -23,14 +24,15 @@ describe('currencyPlugins.spec', () => {
let tools: EdgeCurrencyTools

const fakeIo = makeFakeIo()
const nativeIo = makeFakeNativeIo()
const pluginOpts: EdgeCorePluginOptions = {
initOptions: {},
io: {
...fakeIo,
random: () => Uint8Array.from(fixture.key)
},
log: testLog,
nativeIo: {},
nativeIo,
pluginDisklet: fakeIo.disklet
}
const factory = edgeCorePlugins[fixture.pluginId]
Expand Down
4 changes: 3 additions & 1 deletion test/common/utxobased/engine/engine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import request from 'request'
import edgeCorePlugins from '../../../../src/index'
import { objectKeys } from '../../../util/objectKeys'
import { noOp, testLog } from '../../../util/testLog'
import { makeFakeNativeIo } from '../../../utils'
import { fixtures } from './engine.fixtures/index'

const fetchHack: EdgeFetchFunction = fetch as any
Expand All @@ -45,6 +46,7 @@ describe('engine.spec', function () {
const fakeIo = makeFakeIo()
const fixtureDisklet = makeNodeDisklet(tests.dummyDataPath)
const fakeIoDisklet = makeMemoryDisklet()
const nativeIo = makeFakeNativeIo()
// FOR DEBUGGING:
// const fakeIoDisklet = makeNodeDisklet(
// join(__dirname, 'engine.fixtures/bitcoinTestnet2')
Expand All @@ -58,7 +60,7 @@ describe('engine.spec', function () {
fetch: fetchHack
},
log: testLog,
nativeIo: {},
nativeIo,
pluginDisklet: fakeIoDisklet
}
const factory = edgeCorePlugins[tests.pluginId]
Expand Down
13 changes: 13 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'edge-core-js/types'

import { PluginInfo } from '../src/common/plugin/types'
import { EdgeCurrencyPluginNativeIo } from '../src/react-native'

export const makeFakePluginInfo = (): PluginInfo => {
return {
Expand Down Expand Up @@ -153,3 +154,15 @@ const makeFakeFetch = (): EdgeFetchFunction => async (
}
}
}

export const makeFakeNativeIo = (): {
'edge-currency-plugins': EdgeCurrencyPluginNativeIo
} => {
return {
'edge-currency-plugins': {
async getMemletConfig() {
return await Promise.resolve(undefined)
}
}
}
}

0 comments on commit 04481b0

Please sign in to comment.