-
Notifications
You must be signed in to change notification settings - Fork 2
commands: add OptableCommands command-queue addon #323
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
Merged
+112
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # Command Queue Addon | ||
|
|
||
| A command queue for wrappers loaded via an async script tag, in the style of `googletag.cmd` and `pbjs.que`. It lets a page interact with the wrapper before the script has loaded. | ||
|
|
||
| ## Usage | ||
|
|
||
| The page defines a plain-array stub and queues calls against it: | ||
|
|
||
| ```html | ||
| <script> | ||
| window.optable = window.optable || { cmd: [] }; | ||
| window.optable.cmd.push(() => { | ||
| // Runs once the wrapper has loaded. | ||
| }); | ||
| </script> | ||
| <script async src="https://.../wrapper.js"></script> | ||
| ``` | ||
|
|
||
| During initialization the wrapper swaps the stub for an instance: | ||
|
|
||
| ```js | ||
| import { OptableCommands } from "@optable/web-sdk/lib/dist/addons/commands"; | ||
|
|
||
| window.optable.cmd = new OptableCommands(window.optable.cmd || []); | ||
| ``` | ||
|
|
||
| The constructor drains everything queued while the script was loading. After the swap, `push()` executes its argument immediately and returns its value. | ||
|
|
||
| Non-function entries in the pre-load queue are ignored, a missing or non-array queue is tolerated, and a queued function that throws is logged to the console without stopping the rest of the queue — so a page that clobbers the stub or queues a broken function cannot break wrapper initialization. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { OptableCommands } from "./commands"; | ||
|
|
||
| describe("OptableCommands", () => { | ||
| it("executes functions queued before construction, in order", () => { | ||
| const calls: number[] = []; | ||
| new OptableCommands([() => calls.push(1), () => calls.push(2)]); | ||
| expect(calls).toEqual([1, 2]); | ||
| }); | ||
|
|
||
| it("ignores non-function entries in the queue", () => { | ||
| const fn = jest.fn(); | ||
| expect(() => new OptableCommands([null, "x", 42, fn])).not.toThrow(); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("logs a throwing queued function and continues draining", () => { | ||
| const spy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
| const after = jest.fn(); | ||
| const boom = new Error("boom"); | ||
| expect( | ||
| () => | ||
| new OptableCommands([ | ||
| () => { | ||
| throw boom; | ||
| }, | ||
| after, | ||
| ]) | ||
| ).not.toThrow(); | ||
| expect(after).toHaveBeenCalledTimes(1); | ||
| expect(spy).toHaveBeenCalledWith(boom); | ||
| spy.mockRestore(); | ||
| }); | ||
|
|
||
| it("tolerates a missing or non-array queue", () => { | ||
| expect(() => new OptableCommands()).not.toThrow(); | ||
| expect(() => new OptableCommands(undefined)).not.toThrow(); | ||
| expect(() => new OptableCommands({} as unknown)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("executes pushed functions immediately and returns their value", () => { | ||
| const cmd = new OptableCommands([]); | ||
| const fn = jest.fn(() => "done"); | ||
| expect(cmd.push(fn)).toBe("done"); | ||
| expect(fn).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Command queue for async script-tag wrappers, in the style of googletag.cmd | ||
| // and pbjs.que. Pages push functions onto a plain-array stub before the | ||
| // wrapper script loads; the wrapper replaces the stub with an instance, which | ||
| // drains the queue and executes later pushes immediately. | ||
| class OptableCommands { | ||
| constructor(cmds?: unknown) { | ||
| if (Array.isArray(cmds)) { | ||
| cmds.forEach((cmd) => { | ||
| if (typeof cmd !== "function") return; | ||
| try { | ||
| cmd(); | ||
| } catch (e) { | ||
| console.error(e); // eslint-disable-line no-console | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| push<T>(cmd: () => T): T { | ||
| return cmd(); | ||
| } | ||
| } | ||
|
|
||
| export { OptableCommands }; | ||
| export default OptableCommands; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.