All files / src/component component.js

0% Statements 0/67
0% Branches 0/22
0% Functions 0/9
0% Lines 0/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123                                                                                                                                                                                                                                                     
import { compile } from "../compiler/compiler";
import { m } from "../util/m";
 
const create = function(root) {
	this._view[0](root);
	this.emit("create");
};
 
const update = function(key, value) {
	if (key !== undefined) {
		if (typeof key === "object") {
			for (let childKey in key) {
				this[childKey] = key[childKey];
			}
		} else {
			this[key] = value;
		}
	}
 
	if (this._queued === false) {
		this._queued = true;
 
		const instance = this;
		setTimeout(() => {
			instance._view[1]();
			instance._queued = false;
			instance.emit("update");
		}, 0);
	}
};
 
const destroy = function() {
	this._view[2]();
	this.emit("destroy");
};
 
const on = function(type, handler) {
	let events = this._events;
	let handlers = events[type];
 
	if (handlers === undefined) {
		events[type] = [handler.bind(this)];
	} else {
		handlers.push(handler.bind(this));
	}
};
 
const off = function(type, handler) {
	if (type === undefined) {
		this._events = {};
	} else if (handler === undefined) {
		this._events[type] = [];
	} else {
		let handlers = this._events[type];
		handlers.splice(handlers.indexOf(handler), 1);
	}
};
 
const emit = function(type, data) {
	let handlers = this._events[type];
 
	if (handlers !== undefined) {
		for (let i = 0; i < handlers.length; i++) {
			handlers[i](data);
		}
	}
};
 
export const component = (name, data) => {
	// View
	let view = data.view;
	if (typeof view === "string") {
		view = new Function("m", "instance", "locals", compile(view));
	}
 
	delete data.view;
 
	// Events
	let onCreate = data.onCreate;
	let onUpdate = data.onUpdate;
	let onDestroy = data.onDestroy;
 
	delete data.onCreate;
	delete data.onUpdate;
	delete data.onDestroy;
 
	// Constructor
	function MoonComponent() {
		this._view = view(m, this, {});
 
		this._events = {};
 
		if (onCreate !== undefined) {
			this.on("create", onCreate);
		}
 
		if (onUpdate !== undefined) {
			this.on("update", onUpdate);
		}
 
		if (onDestroy !== undefined) {
			this.on("destroy", onDestroy);
		}
	}
 
	// Initialize
	MoonComponent.prototype = data;
 
	// Properties
	data._name = name;
	data._queued = false;
 
	// Methods
	data.create = create;
	data.update = update;
	data.destroy = destroy;
	data.on = on;
	data.off = off;
	data.emit = emit;
 
	return MoonComponent;
};