Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
},
props: {
members: {}
}
});
// Let's set some defaults:
const defaults = stampit().props({
name: 'The Saloon',
specials: 'Whisky, Gin, Tequila'
});
// Classical inheritance has nothing on this. No parent/child coupling. No deep inheritance hierarchies.
// Just good, clean code reusability.
const bar = stampit.compose(defaults, membership);
// Note that you can override props on instantiation:
const myBar = bar({ name: 'Moe\'s' });
// Silly, but proves that everything is as it should be.
myBar.add({ name: 'Homer' }).open().getMember('Homer');
const myStamp = stampit().methods({
foo() {
return 'foo';
},
methodOverride() {
return false;
}
}).methods({
bar() {
return 'bar';
},
const a = options.args[0];
this.getA = () => {
return a;
};
});
a(); // Object -- so far so good.
a().getA(); // "a"
const b = stampit().init(function() {
const a = 'b';
this.getB = () => {
return a;
};
});
const c = stampit.compose(a, b);
const foo = c(); // we won't throw this one away...
foo.getA(); // "a"
foo.getB(); // "b"
// Here's a mixin with public methods, and some props:
const membership = stampit({
methods: {
// members: {},
add(member: any) {
this.members[member.name] = member;
return this;
},
getMember(name: any) {
return this.members[name];
}
},
import { compose } from 'stampit';
import isStamp from 'stampit/isStamp';
import FastArray from 'fast-array';
import EventEmitter from './EventEmitter';
import Pool from './Pool';
import { isNonEmptyString } from './helpers';
/**
* This module manages the state of entities, components and systems. The heart of Entropy.
*
* @class Engine
* @extends EventEmitter
*/
const Engine = compose({
init(opts) {
// entity ids start from 1, 0 means uninitailized or disabled entity
let greatestEntityID = 1;
/**
* When entity is removed, it's ID can be reused by new entities. This pool stores old IDs ready to reuse.
*
* @private
* @name _entitiesIdsPool
* @memberof Engine#
* @type Pool
*/
this._entitiesIdsPool = Pool({
_new() {
return greatestEntityID++;
},
register(descriptor) {
this._factories[descriptor.type] = compose(Component, {
methods: pickBy(descriptor, value => isFunction(value)),
});
// generating `id` for class of components
this._componentsIdsMap[descriptor.type] = this._greatestComponentId;
this._greatestComponentId += 1;
this._pools[descriptor.type] = Pool({
_new: (...args) => {
const component = this._factories[descriptor.type]({
type: descriptor.type,
id: this._componentsIdsMap[descriptor.type],
});
component.onCreate(...args);
assert.plan(1);
const record = irecord({
a: 'a',
b: 'b'
});
const stamp = stampit({
methods: {
countProps () {
return Object.keys(this.toJS()).length;
}
}
});
const composedRecord = stampit.compose(
record.stamp,
stamp
).create();
assert.equal(composedRecord.countProps(), 2,
'should expose irecord methods');
});
const shooter = stampit()
.props({
bullets: 6
})
.methods({
shoot(direction) {
if (this.bullets > 0) {
--this.bullets;
console.log(`${this.name} shoot to the ${direction}`);
}
}
})
;
const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);
const gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');
const shooter = stampit()
.props({
bullets: 6
})
.methods({
shoot(direction) {
if (this.bullets > 0) {
--this.bullets;
console.log(`${this.name} shoot to the ${direction}`);
}
}
})
;
const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);
const gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');
const shooter = stampit().
props({
bullets: 6
}).
methods({
shoot(direction) {
if (this.bullets > 0) {
--this.bullets;
console.log(`${this.name} shoot to the ${direction}`);
}
}
})
;
const runner = stampit.compose(character, mover);
const samurai = stampit.compose(character, mover, slasher);
const sniper = stampit.compose(character, shooter);
const gunslinger = stampit.compose(character, mover, shooter);
const westernSamurai = stampit.compose(gunslinger, samurai);
let gojiro = westernSamurai();
gojiro.name = 'Gojiro Kiryu';
gojiro.move(1,0);
gojiro.slash('left');
gojiro.shoot('right');