From ee7d1560de793369fc487ea077564211109ef193 Mon Sep 17 00:00:00 2001 From: Endel Dreyer Date: Sun, 12 May 2019 20:27:44 -0300 Subject: [PATCH] uses OrderedMap for MapSchema items. closes #10 --- example/openfl/Source/Main.hx | 3 + example/openfl/Source/Player.hx | 8 +-- example/openfl/Source/State.hx | 2 +- src/io/colyseus/serializer/schema/Schema.hx | 65 +++++++++++++++++---- 4 files changed, 61 insertions(+), 17 deletions(-) diff --git a/example/openfl/Source/Main.hx b/example/openfl/Source/Main.hx index f538d88..ba0aa90 100644 --- a/example/openfl/Source/Main.hx +++ b/example/openfl/Source/Main.hx @@ -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; @@ -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]); } }; diff --git a/example/openfl/Source/Player.hx b/example/openfl/Source/Player.hx index 25a1fce..e7a413c 100644 --- a/example/openfl/Source/Player.hx +++ b/example/openfl/Source/Player.hx @@ -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; diff --git a/example/openfl/Source/State.hx b/example/openfl/Source/State.hx index 6602f38..a675d39 100644 --- a/example/openfl/Source/State.hx +++ b/example/openfl/Source/State.hx @@ -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 // diff --git a/src/io/colyseus/serializer/schema/Schema.hx b/src/io/colyseus/serializer/schema/Schema.hx index 919c4ad..8f6ff9c 100644 --- a/src/io/colyseus/serializer/schema/Schema.hx +++ b/src/io/colyseus/serializer/schema/Schema.hx @@ -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; @@ -367,10 +369,50 @@ class ArraySchema { } +class OrderedMapIterator { + var map : OrderedMap; + var index : Int = 0; + public function new(omap:OrderedMap) { map = omap; } + public function hasNext() : Bool { return index < map._keys.length;} + public function next() : V { return map.get(map._keys[index++]); } +} + +// class OrderedMap implements IMap { +class OrderedMap { + var map:Map; + + @:allow(OrderedMapIterator) // TODO: why this doesn't seem to work? + public var _keys:Array; // 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(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 { - public var items:Map = new Map(); + public var items:OrderedMap = new OrderedMap(new Map()); public dynamic function onAdd(item:T, key:String):Void {} public dynamic function onChange(item:T, key:String):Void {} @@ -381,14 +423,13 @@ class MapSchema { public function clone():MapSchema { var cloned = new MapSchema(); -#if haxe4 - cloned.items = this.items.copy(); -#else - var newMap = new Map(); +// #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; @@ -402,19 +443,19 @@ class MapSchema { @: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(", ") + " }"; } @@ -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).exists(newKey)) + var isNew = (!hasIndexChange && !valueRef.items.exists(newKey)) || (hasIndexChange && previousKey == "" && hasMapIndex); if (isNew && isSchemaType) { @@ -651,7 +692,7 @@ class Schema { if (hasChange) { changes.push({ - field: field, + field: field, value: (change == null) ? value : change, previousValue: Reflect.getProperty(this, field) });