Skip to content

Commit

Permalink
Updating launch logic to delay loading options until initiation
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeyls committed Jul 25, 2019
1 parent d37c23a commit 2ee5ceb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"@pika/plugin-standard-pkg": "^0.3.10",
"core-js": "^2.6.3",
"jest": "^24.8.0",
"npm": "^6.8.0"
"npm": "^6.8.0",
"prettier": "^1.18.2"
},
"peerDependencies": {
"jquery": ">=1.6.2"
Expand Down
45 changes: 26 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ class AlreadyRegisteredError extends Error {
const widgetQueue = queue();

const strategies = initiationStrategies({
nextTick(pluginFn, $$, options, ready) {
return window.setTimeout(() => pluginFn.call($$, options, ready), 0);
nextTick(pluginFn, $$) {
return window.setTimeout(() => pluginFn(), 0);
},

immediate(pluginFn, $$, options, ready) {
return pluginFn.call($$, options, ready) || {};
immediate(pluginFn, $$) {
return pluginFn() || {};
},

hover(pluginFn, $$, options, ready) {
return $$.one('mouseover', () => pluginFn.call($$, options, ready));
hover(pluginFn, $$) {
return $$.one('mouseover', () => pluginFn());
},
});

Expand All @@ -94,7 +94,23 @@ const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
registered[name] = plugin;
};

const loadWidget = function($$, name, data) {
const wrapPlugin = function wrapPlugin(name, pluginFn, $$) {
const ready = function ready() {
emit($$, 'vvidget:initialized');
};

return function() {
const pluginName = camelize(name);
const options = $.extend(
{},
pluginFn.defaults,
extractOptions($$.data(), pluginName)
);
pluginFn.call($$, options, ready);
};
};

const loadWidget = function($$, name) {
if (name) {
const existingPlugin = $$.data(`vvidget:${name}`);
const pluginFn = registered[name];
Expand All @@ -103,20 +119,11 @@ const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
return;
}

const ready = () => {
emit($$, 'vvidget:initialized')
};
const wrapped = wrapPlugin(name, pluginFn, $$);

if (!existingPlugin) {
const pluginName = camelize(name);
const options = $.extend(
{},
pluginFn.defaults,
extractOptions(data, pluginName)
);

widgetQueue.add(() => {
strategies.get(pluginFn.init)(pluginFn, $$, options, ready);
strategies.get(pluginFn.init)(wrapped, $$);
});
widgetQueue.flush();

Expand All @@ -130,7 +137,7 @@ const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
const $$ = $(this);
const names = `${$$.data(data) || ''} ${$$.attr(attr) || ''}`;

names.split(' ').forEach(name => loadWidget($$, name, $$.data()));
names.split(' ').forEach(name => loadWidget($$, name));
});
};

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5359,6 +5359,11 @@ preserve@^0.2.0:
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
integrity sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=

prettier@^1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==

pretty-format@^24.8.0:
version "24.8.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.8.0.tgz#8dae7044f58db7cb8be245383b565a963e3c27f2"
Expand Down

0 comments on commit 2ee5ceb

Please sign in to comment.