From 04481b0b4b7a5fed1ebc6a8779e5402dc2a50103 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Fri, 10 May 2024 16:02:24 -0700 Subject: [PATCH] Add memletConfig option to makePluginIo 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. --- CHANGELOG.md | 1 + edge-currency-plugins.d.ts | 13 ++++++----- src/common/plugin/CurrencyPlugin.ts | 25 ++++++++++++++++++++- src/react-native.ts | 25 +++++++++++++++++---- test/common/plugin/CurrencyPlugin.spec.ts | 4 +++- test/common/utxobased/engine/engine.spec.ts | 4 +++- test/utils.ts | 13 +++++++++++ 7 files changed, 73 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6920ae42..8d7bc99c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/edge-currency-plugins.d.ts b/edge-currency-plugins.d.ts index 210508b2..c7b8e195 100644 --- a/edge-currency-plugins.d.ts +++ b/edge-currency-plugins.d.ts @@ -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, diff --git a/src/common/plugin/CurrencyPlugin.ts b/src/common/plugin/CurrencyPlugin.ts index 6c483339..75f82d58 100644 --- a/src/common/plugin/CurrencyPlugin.ts +++ b/src/common/plugin/CurrencyPlugin.ts @@ -6,7 +6,9 @@ import { EdgeCurrencyTools, EdgeWalletInfo } from 'edge-core-js/types' +import { setMemletConfig } from 'memlet' +import { EdgeCurrencyPluginNativeIo } from '../../react-native' import { asUtxoInitOptions, asUtxoUserSettings @@ -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({ @@ -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, diff --git a/src/react-native.ts b/src/react-native.ts index 3b0cc583..5fd58343 100644 --- a/src/react-native.ts +++ b/src/react-native.ts @@ -1,6 +1,5 @@ -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() @@ -8,6 +7,24 @@ 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> +} + +export function makePluginIo( + options: EdgeCurrencyPluginIoOptions = {} +): EdgeCurrencyPluginNativeIo { + const { memletConfig } = options + + return { + memletConfig, + getMemletConfig: async () => { + return memletConfig + } + } } diff --git a/test/common/plugin/CurrencyPlugin.spec.ts b/test/common/plugin/CurrencyPlugin.spec.ts index 7b7e6bf9..e53b47be 100644 --- a/test/common/plugin/CurrencyPlugin.spec.ts +++ b/test/common/plugin/CurrencyPlugin.spec.ts @@ -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', () => { @@ -23,6 +24,7 @@ describe('currencyPlugins.spec', () => { let tools: EdgeCurrencyTools const fakeIo = makeFakeIo() + const nativeIo = makeFakeNativeIo() const pluginOpts: EdgeCorePluginOptions = { initOptions: {}, io: { @@ -30,7 +32,7 @@ describe('currencyPlugins.spec', () => { random: () => Uint8Array.from(fixture.key) }, log: testLog, - nativeIo: {}, + nativeIo, pluginDisklet: fakeIo.disklet } const factory = edgeCorePlugins[fixture.pluginId] diff --git a/test/common/utxobased/engine/engine.spec.ts b/test/common/utxobased/engine/engine.spec.ts index 31f8ff1b..c1d4cb5d 100644 --- a/test/common/utxobased/engine/engine.spec.ts +++ b/test/common/utxobased/engine/engine.spec.ts @@ -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 @@ -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') @@ -58,7 +60,7 @@ describe('engine.spec', function () { fetch: fetchHack }, log: testLog, - nativeIo: {}, + nativeIo, pluginDisklet: fakeIoDisklet } const factory = edgeCorePlugins[tests.pluginId] diff --git a/test/utils.ts b/test/utils.ts index f7388d70..34ca7587 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -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 { @@ -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) + } + } + } +}