From a6246d910b4f271be5b4611fa68d6c249f5b20f5 Mon Sep 17 00:00:00 2001 From: arthur791004 Date: Wed, 7 Oct 2020 11:18:07 +0800 Subject: [PATCH 1/2] Support customize browser api when new Store --- src/store/Store.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/store/Store.js b/src/store/Store.js index 6d33427..10a75d1 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -18,6 +18,7 @@ const defaultOpts = { portName: DEFAULT_PORT_NAME, state: {}, extensionId: null, + browserAPI: null, serializer: noop, deserializer: noop, patchStrategy: shallowDiff @@ -28,7 +29,15 @@ 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. */ - 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'); } @@ -46,7 +55,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); From 315a19688cf6c6b0a300106a2c05ec081d54f9ad Mon Sep 17 00:00:00 2001 From: arthur791004 Date: Wed, 7 Oct 2020 11:32:05 +0800 Subject: [PATCH 2/2] Update comment --- src/store/Store.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/store/Store.js b/src/store/Store.js index 10a75d1..225746a 100644 --- a/src/store/Store.js +++ b/src/store/Store.js @@ -27,7 +27,14 @@ 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,