Skip to content

Commit

Permalink
uses OrderedMap for MapSchema items. closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed May 12, 2019
1 parent 7986393 commit ee7d156
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
3 changes: 3 additions & 0 deletions example/openfl/Source/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Main extends Sprite {
*/
this.room.onJoin = function() {
this.room.state.players.onAdd = function(player, key) {
trace("PLAYER ADDED AT: ", key);
var cat = Assets.getMovieClip("library:NyanCatAnimation");
this.cats[key] = cat;
cat.x = player.x;
Expand All @@ -66,11 +67,13 @@ class Main extends Sprite {
}

this.room.state.players.onChange = function(player, key) {
trace("PLAYER CHANGED AT: ", key);
this.cats[key].x = player.x;
this.cats[key].y = player.y;
}

this.room.state.players.onRemove = function(player, key) {
trace("PLAYER REMOVED AT: ", key);
removeChild(this.cats[key]);
}
};
Expand Down
8 changes: 4 additions & 4 deletions example/openfl/Source/Player.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
//
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.4.30
//
//
// GENERATED USING @colyseus/schema 0.4.32
//


import io.colyseus.serializer.schema.Schema;
Expand Down
2 changes: 1 addition & 1 deletion example/openfl/Source/State.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// THIS FILE HAS BEEN GENERATED AUTOMATICALLY
// DO NOT CHANGE IT MANUALLY UNLESS YOU KNOW WHAT YOU'RE DOING
//
// GENERATED USING @colyseus/schema 0.4.30
// GENERATED USING @colyseus/schema 0.4.32
//


Expand Down
65 changes: 53 additions & 12 deletions src/io/colyseus/serializer/schema/Schema.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.colyseus.serializer.schema;

import haxe.io.Bytes;
import haxe.Constraints.IMap;

// begin macros / decorator
#if macro
import haxe.macro.Context;
Expand Down Expand Up @@ -367,10 +369,50 @@ class ArraySchema<T> {

}

class OrderedMapIterator<K,V> {
var map : OrderedMap<K,V>;
var index : Int = 0;
public function new(omap:OrderedMap<K,V>) { map = omap; }
public function hasNext() : Bool { return index < map._keys.length;}
public function next() : V { return map.get(map._keys[index++]); }
}

// class OrderedMap<K, V> implements IMap<K, V> {
class OrderedMap<K, V> {
var map:Map<K, V>;

@:allow(OrderedMapIterator) // TODO: why this doesn't seem to work?
public var _keys:Array<K>; // FIXME: this should be private
var idx = 0;

public function new(_map) {
_keys = [];
map = _map;
}

public function set(key, value) {
if(!map.exists(key)) _keys.push(key);
map[key] = value;
}

public function toString() {
var _ret = ''; var _cnt = 0; var _len = _keys.length;
for(k in _keys) _ret += '$k => ${map.get(k)}${(_cnt++<_len-1?", ":"")}';
return '{$_ret}';
}

public function iterator() return new OrderedMapIterator<K,V>(this);
public function remove(key) return map.remove(key) && _keys.remove(key);
public function exists(key) return map.exists(key);
public function get(key) return map.get(key);
public inline function keys() return _keys.iterator();
}


@:keep
@:generic
class MapSchema<T> {
public var items:Map<String, T> = new Map<String, T>();
public var items:OrderedMap<String, T> = new OrderedMap<String, T>(new Map<String, T>());

public dynamic function onAdd(item:T, key:String):Void {}
public dynamic function onChange(item:T, key:String):Void {}
Expand All @@ -381,14 +423,13 @@ class MapSchema<T> {
public function clone():MapSchema<T> {
var cloned = new MapSchema<T>();

#if haxe4
cloned.items = this.items.copy();
#else
var newMap = new Map<String, T>();
// #if haxe4
// cloned.items = this.items.copy();
// #else
for (key in this.items.keys()) {
cloned.items[key] = this.items[key];
cloned.items.set(key, this.items.get(key));
}
#end
// #end

cloned.onAdd = this.onAdd;
cloned.onChange = this.onChange;
Expand All @@ -402,19 +443,19 @@ class MapSchema<T> {

@:arrayAccess
public inline function get(key:String) {
return this.items[key];
return this.items.get(key);
}

@:arrayAccess
public inline function arrayWrite(key:String, value:T):T {
this.items[key] = value;
this.items.set(key, value);
return value;
}

public function toString () {
var data = [];
for (key in this.items.keys()) {
data.push(key + " => " + this.items[key]);
data.push(key + " => " + this.items.get(key));
}
return "MapSchema ("+ Lambda.count(this.items) +") { " + data.join(", ") + " }";
}
Expand Down Expand Up @@ -603,7 +644,7 @@ class Schema {
var newKey = (hasMapIndex) ? previousKeys[decoder.number(bytes, it)] : decoder.string(bytes, it);

var item:Dynamic;
var isNew = (!hasIndexChange && !cast(valueRef.items, Map<String, Dynamic>).exists(newKey))
var isNew = (!hasIndexChange && !valueRef.items.exists(newKey))
|| (hasIndexChange && previousKey == "" && hasMapIndex);

if (isNew && isSchemaType) {
Expand Down Expand Up @@ -651,7 +692,7 @@ class Schema {

if (hasChange) {
changes.push({
field: field,
field: field,
value: (change == null) ? value : change,
previousValue: Reflect.getProperty(this, field)
});
Expand Down

0 comments on commit ee7d156

Please sign in to comment.