Saving Functions as module settings

@wasp I remember you asked about saving functions as settings (which quickly turned into stringifying them IIRC?), did you end up doing something about that ? I am sort of in dire need of the same haaha EDIT I'll thread this to keep the convo focused
9 Replies
ccjmk
ccjmkOP•3y ago
In particular, I need to save the Commands for Commander, that have several inner functions (looking for an example)
Wasp
Wasp•3y ago
I did not, no - but if you were to stringify the function and then eval(settingString)() you can at least call it
ccjmk
ccjmkOP•3y ago
is it just plain
const myFunction = () => {...}
const funcStr = JSON.stringify(myFunction)
// save funcStr ?
const myFunction = () => {...}
const funcStr = JSON.stringify(myFunction)
// save funcStr ?
??? I recalled we had to do some processing to it like rehydrating the function using a new Function(..) and stuff
Wasp
Wasp•3y ago
Yeah, eval and new Function I believe will both work
ccjmk
ccjmkOP•3y ago
for ref, this is what I want to save (objects with this shape)
{
name: "tae",
description: "Token Active Effect",
schema: "tae $effect",
args: [{
name: 'effect',
type: 'string',
suggestions: (...) => {...}, // function
}],
allow: (...) => {...} // function
handler: (...) => {...} // function
}
{
name: "tae",
description: "Token Active Effect",
schema: "tae $effect",
args: [{
name: 'effect',
type: 'string',
suggestions: (...) => {...}, // function
}],
allow: (...) => {...} // function
handler: (...) => {...} // function
}
hmm ok, I will play a little bit and report back probably later today 😆 thanks!
Leo The League Lion
Leo The League Lion•3y ago
@ccjmk gave vote LeaguePoints™ to @wasp (#6 • 386)
ccjmk
ccjmkOP•3y ago
I landed here https://stackoverflow.com/a/40876342 looks like the same approach, will give this a try later today! 🙂 this pesky thing called work got in the middle 😆
Stack Overflow
JavaScript - Save object with methods as a string
I've been looking around for a way to do this but can't seem to find anything, I have different configuration objects that I need to save as a text in variables for some processing later on, here i...
ccjmk
ccjmkOP•3y ago
In case you were still looking into it Wasp, this is what I did:
export function persistCommandInLocalStorage(command: Command) {
const serializedCommand = JSON.stringify(command, replacer, 2);
const key = `cmd-${getCommandSchemaWithoutArguments(command)}`;

const storedCommands = new Map(JSON.parse(localStorage.getItem(LS_KEY) ?? '[]'));
storedCommands.set(key, serializedCommand);
localStorage.setItem(LS_KEY, JSON.stringify([...storedCommands]));
}

export function retrieveCommandsFromModuleSetting(): Command[] {
const storedCommands = new Map(JSON.parse(localStorage.getItem(LS_KEY) ?? '[]'));
return Array.from(storedCommands.values())
.map(serializedCommand => JSON.parse(serializedCommand as string, reviver));
}

const replacer = (key: string, value: unknown) => {
return typeof value === 'function' ? value.toString() : value;
};

const reviver = (key: string, value: string) => {
return ['handler', 'allow', 'suggestions'].includes(key) ? new Function(`(${value})`) : value;
};
export function persistCommandInLocalStorage(command: Command) {
const serializedCommand = JSON.stringify(command, replacer, 2);
const key = `cmd-${getCommandSchemaWithoutArguments(command)}`;

const storedCommands = new Map(JSON.parse(localStorage.getItem(LS_KEY) ?? '[]'));
storedCommands.set(key, serializedCommand);
localStorage.setItem(LS_KEY, JSON.stringify([...storedCommands]));
}

export function retrieveCommandsFromModuleSetting(): Command[] {
const storedCommands = new Map(JSON.parse(localStorage.getItem(LS_KEY) ?? '[]'));
return Array.from(storedCommands.values())
.map(serializedCommand => JSON.parse(serializedCommand as string, reviver));
}

const replacer = (key: string, value: unknown) => {
return typeof value === 'function' ? value.toString() : value;
};

const reviver = (key: string, value: string) => {
return ['handler', 'allow', 'suggestions'].includes(key) ? new Function(`(${value})`) : value;
};
Oh it's not really working as expected yet 😦
Want results from more Discord servers?
Add your server