vevet
Version:
Vevet is a JavaScript library for creative development that simplifies crafting rich interactions like split text animations, carousels, marquees, preloading, and more.
191 lines • 7.78 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Callbacks = void 0;
var noopIfDestroyed_1 = require("../../internal/noopIfDestroyed");
var safeAction_1 = require("../../internal/safeAction");
var common_1 = require("../../utils/common");
__exportStar(require("./types"), exports);
/**
* Manages event callbacks with features like one-time execution, protection, and delays.
*
* @group Base
*/
var Callbacks = /** @class */ (function () {
function Callbacks(_props) {
if (_props === void 0) { _props = {}; }
this._props = _props;
/** Whether the instance has been destroyed. */
this._isDestroyed = false;
/** Storage for registered callbacks. */
this._list = [];
}
Object.defineProperty(Callbacks.prototype, "list", {
/** Returns the list of all registered callbacks. */
get: function () {
return this._list;
},
enumerable: false,
configurable: true
});
/**
* Registers a callback for an event.
* @param target - Event name to associate the callback with.
* @param action - Function to execute on the event.
* @param settings - Optional callback settings (e.g., timeout, one-time).
* @returns Callback ID and a removal function.
*/
Callbacks.prototype.add = function (target, action, settings) {
var _this = this;
if (settings === void 0) { settings = {}; }
var id = (0, common_1.uid)('callback');
this._list.push(__assign(__assign({}, settings), { id: id, target: target, action: action }));
return { id: id, remove: function () { return _this.remove(id); } };
};
/**
* Adds a callback and returns a destructor to remove it.
* @param target - Event name to associate the callback with.
* @param action - Function to execute on the event.
* @param settings - Optional callback settings (e.g., timeout, one-time).
* @returns A function to remove the callback.
*/
Callbacks.prototype.on = function (target, action, settings) {
if (settings === void 0) { settings = {}; }
var callback = this.add(target, action, settings);
return function () {
callback.remove();
};
};
/**
* Removes a callback by its ID.
* @param id - ID of the callback to remove.
* @returns `true` if the callback was removed, `false` otherwise.
*/
Callbacks.prototype.remove = function (id) {
return this._remove(id);
};
/**
* Removes a callback, with an option to force removal of protected callbacks.
* @param callbackId - ID of the callback to remove.
* @param canRemoveProtected - Whether to forcibly remove protected callbacks.
* @returns `true` if the callback was removed, `false` otherwise.
*/
Callbacks.prototype._remove = function (callbackId, canRemoveProtected) {
if (canRemoveProtected === void 0) { canRemoveProtected = false; }
this._list = this._list.filter(function (callback) {
if (callback.id !== callbackId) {
return true;
}
if (callback.protected && !canRemoveProtected) {
return true;
}
return false;
});
var hasCallback = this._list.some(function (_a) {
var id = _a.id;
return id === callbackId;
});
return !hasCallback;
};
/** Removes all callbacks, including protected ones. */
Callbacks.prototype._removeAll = function () {
while (this._list.length > 0) {
this._remove(this._list[0].id, true);
}
};
/**
* Executes a callback and removes it if marked as `isOnce`.
* @param callback - Callback to execute.
* @param parameter - Argument to pass to the callback.
*/
Callbacks.prototype._callAction = function (_a, parameter) {
var id = _a.id, timeout = _a.timeout, action = _a.action, callback = __rest(_a, ["id", "timeout", "action"]);
var ctx = this._props.ctx;
if (timeout) {
setTimeout(function () { return (0, safeAction_1.safeAction)(function () { return action(parameter, ctx); }); }, timeout);
}
else {
(0, safeAction_1.safeAction)(function () { return action(parameter, ctx); });
}
if (callback.once) {
this._remove(id, true);
}
};
/**
* Triggers all callbacks for a given event.
* @param target - Event name to trigger.
* @param arg - Argument to pass to the callbacks.
*/
Callbacks.prototype.emit = function (target, arg) {
var _this = this;
this._list.forEach(function (callback) {
if (callback.target === target) {
_this._callAction(callback, arg);
}
});
};
/** Removes all registered callbacks. */
Callbacks.prototype.destroy = function () {
this._removeAll();
this._isDestroyed = true;
};
__decorate([
noopIfDestroyed_1.noopIfDestroyed
], Callbacks.prototype, "add", null);
__decorate([
noopIfDestroyed_1.noopIfDestroyed
], Callbacks.prototype, "on", null);
__decorate([
noopIfDestroyed_1.noopIfDestroyed
], Callbacks.prototype, "remove", null);
__decorate([
noopIfDestroyed_1.noopIfDestroyed
], Callbacks.prototype, "emit", null);
__decorate([
noopIfDestroyed_1.noopIfDestroyed
], Callbacks.prototype, "destroy", null);
return Callbacks;
}());
exports.Callbacks = Callbacks;
//# sourceMappingURL=index.js.map