JSFiddle - React, Tailwind, and code Playground
by Ryan Duffy
JavaScript
enyo.lock = {
_locks: {},
create: function(name, context) {
return {
name: name,
locked: false,
release: function() {
enyo.lock.release(name, context);
}
}
},
exists: function(name, context) {
context = context || this;
var lock = context._locks && context._locks[name];
return lock && lock.locked && lock || false;
},
acquire: function(name, context) {
if(!name) {
throw "lock name is required";
}
context = context || this;
context._locks = context._locks || {};
var lock = context._locks[name] = context._locks[name] || this.create(name, context);
if(lock.locked) {
return false;
} else {
lock.locked = true;
return lock;
}
},
release: function(name, context) {
if(context._locks[name]) {
context._locks[name].locked = false;
}
}
};
enyo.mutex = function(name, context, method /* + arguments */) {
var argsAt = 3;
// adapt input for (name, method) syntax
if(context instanceof Function || enyo.isString(context)) {
method = context;
context = enyo;
argsAt = 2;
}
// allow method names which are bound on the given context
if(enyo.isString(method)) {
method = enyo.bind(context, method);
}
var args = Array.prototype.splice.call(arguments, argsAt);
var f = function() {
method.apply(context, args);
};
var lock = enyo.lock.exists(name, context);
if(lock) {
lock.stack.push(f);
} else {
var lock = enyo.lock.acquire(name, context);
lock.stack = [f];
while(lock.stack.length > 0) {
lock.stack.pop()();
}
lock.release();
}
}
enyo.kind({
name:"ex.App",
components:[
{kind:"Button",...