123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- 'use strict';
-
- var util = require('util');
- var define = require('define-property');
- var CacheBase = require('cache-base');
- var Emitter = require('component-emitter');
- var isObject = require('isobject');
- var merge = require('mixin-deep');
- var pascal = require('pascalcase');
- var cu = require('class-utils');
-
-
-
- function namespace(name) {
- var Cache = name ? CacheBase.namespace(name) : CacheBase;
- var fns = [];
-
-
-
-
- function Base(config, options) {
- if (!(this instanceof Base)) {
- return new Base(config, options);
- }
- Cache.call(this, config);
- this.is('base');
- this.initBase(config, options);
- }
-
-
-
-
- util.inherits(Base, Cache);
-
-
-
-
- Emitter(Base);
-
-
-
-
- Base.prototype.initBase = function(config, options) {
- this.options = merge({}, this.options, options);
- this.cache = this.cache || {};
- this.define('registered', {});
- if (name) this[name] = {};
-
-
- this.define('_callbacks', this._callbacks);
- if (isObject(config)) {
- this.visit('set', config);
- }
- Base.run(this, 'use', fns);
- };
-
-
-
-
- Base.prototype.is = function(name) {
- if (typeof name !== 'string') {
- throw new TypeError('expected name to be a string');
- }
- this.define('is' + pascal(name), true);
- this.define('_name', name);
- this.define('_appname', name);
- return this;
- };
-
-
-
-
- Base.prototype.isRegistered = function(name, register) {
- if (this.registered.hasOwnProperty(name)) {
- return true;
- }
- if (register !== false) {
- this.registered[name] = true;
- this.emit('plugin', name);
- }
- return false;
- };
-
-
-
-
- Base.prototype.use = function(fn) {
- fn.call(this, this);
- return this;
- };
-
-
-
-
- Base.prototype.define = function(key, val) {
- if (isObject(key)) {
- return this.visit('define', key);
- }
- define(this, key, val);
- return this;
- };
-
-
-
-
- Base.prototype.mixin = function(key, val) {
- Base.prototype[key] = val;
- return this;
- };
-
-
-
-
- Base.prototype.mixins = Base.prototype.mixins || [];
-
-
-
-
- Object.defineProperty(Base.prototype, 'base', {
- configurable: true,
- get: function() {
- return this.parent ? this.parent.base : this;
- }
- });
-
-
-
-
- define(Base, 'use', function(fn) {
- fns.push(fn);
- return Base;
- });
-
-
-
-
- define(Base, 'run', function(obj, prop, arr) {
- var len = arr.length, i = 0;
- while (len--) {
- obj[prop](arr[i++]);
- }
- return Base;
- });
-
-
-
-
- define(Base, 'extend', cu.extend(Base, function(Ctor, Parent) {
- Ctor.prototype.mixins = Ctor.prototype.mixins || [];
-
- define(Ctor, 'mixin', function(fn) {
- var mixin = fn(Ctor.prototype, Ctor);
- if (typeof mixin === 'function') {
- Ctor.prototype.mixins.push(mixin);
- }
- return Ctor;
- });
-
- define(Ctor, 'mixins', function(Child) {
- Base.run(Child, 'mixin', Ctor.prototype.mixins);
- return Ctor;
- });
-
- Ctor.prototype.mixin = function(key, value) {
- Ctor.prototype[key] = value;
- return this;
- };
- return Base;
- }));
-
-
-
-
- define(Base, 'mixin', function(fn) {
- var mixin = fn(Base.prototype, Base);
- if (typeof mixin === 'function') {
- Base.prototype.mixins.push(mixin);
- }
- return Base;
- });
-
-
-
-
- define(Base, 'mixins', function(Child) {
- Base.run(Child, 'mixin', Base.prototype.mixins);
- return Base;
- });
-
-
-
-
- define(Base, 'inherit', cu.inherit);
- define(Base, 'bubble', cu.bubble);
- return Base;
- }
-
-
-
- module.exports = namespace();
-
-
-
- module.exports.namespace = namespace;
|