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

Support customized browser api to avoid conflict with global variable #260

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/store/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const defaultOpts = {
portName: DEFAULT_PORT_NAME,
state: {},
extensionId: null,
browserAPI: null,
serializer: noop,
deserializer: noop,
patchStrategy: shallowDiff
Expand All @@ -26,9 +27,24 @@ const defaultOpts = {
class Store {
/**
* Creates a new Proxy store
* @param {object} options An object of form {portName, state, extensionId, serializer, deserializer, diffStrategy}, where `portName` is a required string and defines the name of the port for state transition changes, `state` is the initial state of this store (default `{}`) `extensionId` is the extension id as defined by browserAPI when extension is loaded (default `''`), `serializer` is a function to serialize outgoing message payloads (default is passthrough), `deserializer` is a function to deserialize incoming message payloads (default is passthrough), and patchStrategy is one of the included patching strategies (default is shallow diff) or a custom patching function.
* @param {object} options An object of form {portName, state, extensionId, browserAPI, serializer, deserializer, diffStrategy}
* @param {string} options.portName A required string and defines the name of the port for state transition changes
* @param {object} options.state The initial state of this store (default `{}`)
* @param {string} options.extensionId The extension id as defined by browserAPI when extension is loaded (default `''`)
* @param {object} options.browserAPI A browser api (default is global browser api)
* @param {function} options.serializer A function to serialize outgoing message payloads (default is passthrough)
* @param {function} options.deserializer A function to deserialize incoming message payloads (default is passthrough)
* @param {function} options.patchStrategy One of the included patching strategies or a custom patching function (default is shallow diff)
*/
constructor({portName = defaultOpts.portName, state = defaultOpts.state, extensionId = defaultOpts.extensionId, serializer = defaultOpts.serializer, deserializer = defaultOpts.deserializer, patchStrategy = defaultOpts.patchStrategy} = defaultOpts) {
constructor({
portName = defaultOpts.portName,
state = defaultOpts.state,
extensionId = defaultOpts.extensionId,
browserAPI = defaultOpts.browserAPI,
serializer = defaultOpts.serializer,
deserializer = defaultOpts.deserializer,
patchStrategy = defaultOpts.patchStrategy
} = defaultOpts) {
if (!portName) {
throw new Error('portName is required in options');
}
Expand All @@ -46,7 +62,7 @@ class Store {
this.readyResolved = false;
this.readyPromise = new Promise(resolve => this.readyResolve = resolve);

this.browserAPI = getBrowserAPI();
this.browserAPI = browserAPI || getBrowserAPI();
this.extensionId = extensionId; // keep the extensionId as an instance variable
this.port = this.browserAPI.runtime.connect(this.extensionId, {name: portName});
this.safetyHandler = this.safetyHandler.bind(this);
Expand Down