Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move meta from this to external/contextual getter #36

Closed
fregante opened this issue Oct 6, 2021 · 1 comment
Closed

Move meta from this to external/contextual getter #36

fregante opened this issue Oct 6, 2021 · 1 comment

Comments

@fregante
Copy link
Collaborator

fregante commented Oct 6, 2021

Information about the sender/trace and options is currently stored on this. This is because:

  • Most methods don't need access to this information
  • It can be easily be left out of the method definition when not used
  • It can be easily used regardless of parameter length
  • It allows the the registration of any function, not just functions with specific parameters (e.g. arguments[0] is the meta")

this is fine for me, but maybe there alternative solutions. This is what I came up with:

import {getCurrentMeta} from "webext-messenger";

function whoAmI() {
	const meta = getCurrentMeta();
	return meta.trace[0];
}

This pattern:

  • Preserves the previous wins
  • Actually simplifies our types further (due to the lack of this)
  • Somewhat matches other patterns like:
     import.meta.url; // Only available in the current file, in ES Modules
    
     function onMessageHandler() {
     	if (chrome.runtime.lastError) { // Available only in this call
     		
     	}
     }

The implementation would look like:

- await handler.apply(meta, args)
+ currentMeta = meta;
+ await handler(...args)
+ currentMeta = undefined;

and

export function getCurrentMeta() {
	return currentMeta;
}
@fregante
Copy link
Collaborator Author

fregante commented Oct 6, 2021

Nope, I just realized that this could only work in sync functions, because a second message could replace currentMeta before first call returns. Even if I were to store each meta for each call, there's no way to match a meta with a call without passing a parameter… which would probably be this anyway:

const uid = getUID();
meta.set(uid, meta);
await handler.apply(uid, args)
meta.delete(uid);

and

function whoAmI() {
	const meta = getCurrentMeta(this);
	return meta.trace[0];
}

Which would be a marginal improvement over the current situation.

@fregante fregante closed this as completed Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant