-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.js
167 lines (137 loc) · 3.31 KB
/
core.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var uuid = require('node-uuid'),
ObjectId = require('mongoose').Types.ObjectId,
util = require('util'),
events = require('events');
var forEach = Array.prototype.forEach;
function mixin(trait, overwrite) {
if (typeof overwrite === 'undefined') {
overwrite = true;
}
for (var prop in trait) {
if (typeof this[prop] === 'undefined') {
if (prop !== 'events' && prop !== '__init') {
var desc = Object.getOwnPropertyDescriptor(trait, prop);
if ('get' in desc || 'set' in desc) {
Object.defineProperty(this, prop, desc);
}
else {
this[prop] = trait[prop];
}
}
}
else {
if (overwrite) {
this[prop] = trait[prop];
}
}
}
if (typeof trait.events === 'object') {
for (var eventName in trait.events) {
this.on(eventName, trait.events[eventName]);
}
}
if (typeof trait.__init !== 'undefined') {
trait.__init(this);
}
}
function queueEvent(type, name) {
if (!(name in this._eventQueue)) {
this._eventQueue[name] = [];
this._eventQueue[name].type = type;
}
else {
throw new Error('event "' + name + '" already queued,');
}
var self = this;
var queue = {
type: type,
name: name,
add: function() {
forEach.call(arguments, function(arg) {
self._eventQueue[name].push(arg);
});
},
emit: function() {
self.completeEvent(this.name);
}
};
return queue;
}
function completeEvent(nameOrQueue) {
//allows passing in of either queue objects or the event name.
if (typeof nameOrQueue === 'object') {
var name = nameOrQueue.name;
}
else {
var name = nameOrQueue;
}
if (!(name in this._eventQueue)) {
throw new Error('event for "' + name + '" not queued.');
}
var queuedEvents = this._eventQueue[name];
queuedEvents.unshift(queuedEvents.type);
var self = this;
this.emit.apply(this, queuedEvents);
delete this._eventQueue[name];
}
function is(trait) {
var hasAllTraits = true;
for (var c = 0; c < arguments.length; c++) {
var arg = arguments[c];
hasAllTraits = this._traits.indexOf(arg) !== -1;
if (!hasAllTraits) return false;
}
return true;
}
function ensurePrototype(Schema) {
Schema.prototype.is = is;
Schema.prototype.mixin = mixin;
Schema.prototype.queueEvent = queueEvent;
Schema.prototype.completeEvent = completeEvent;
}
exports.createObject = function() {
var traits = Array.prototype.slice.call(arguments);
var Schema = traits[0];
ensurePrototype(Schema);
traits = traits.slice(1);
function MUDObject() {
this.memid = uuid();
this._eventQueue = {};
}
MUDObject.prototype = new Schema;
var obj = new MUDObject;
traits.forEach(function(trait) {
if (typeof trait !== 'undefined') {
obj.mixin(trait);
}
});
obj._traits = traits;
obj._traits.push(Schema);
return obj;
}
exports.createFromModel = function() {
var traits = Array.prototype.slice.call(arguments);
var model = traits[0];
traits = traits.slice(1);
function MUDObject() {
this.memid = uuid();
this._eventQueue = {};
}
MUDObject.prototype = model;
ensurePrototype(MUDObject);
var obj = new MUDObject;
traits.forEach(function(trait) {
if (typeof trait !== 'undefined') {
obj.mixin(trait, false);
}
});
obj._traits = traits;
return obj;
}
exports.ensureObjectId = function(mudObj) {
if ('_id' in mudObj) {
if (!(mudObj._id instanceof ObjectId)) {
mudObj._id = new ObjectId(mudObj._id);
}
}
}