diff --git a/lib/index.js b/lib/index.js index 76530ed..e80a6d5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -206,7 +206,7 @@ const SESSIONS = new Map(); const CONFIGS = {}; let GENERATION = 0; - +let WAREHOUSED = false; class Goblin { static buildApplication(appId, config) { return appBuilder(appId, config); @@ -266,6 +266,9 @@ class Goblin { } static registerQuest(goblinName, questName, quest, options) { + if (goblinName === 'warehouse') { + WAREHOUSED = true; + } if (!QUESTSMETA[goblinName]) { QUESTSMETA[goblinName] = {}; } @@ -1062,9 +1065,19 @@ class Goblin { if (!this.lastUpsertData) { this.lastUpsertData = payload.data; - yield quest.warehouse.upsert(payload); + if (WAREHOUSED) { + const warehouse = GOBLINS.get('warehouse').get('warehouse'); + warehouse._do('upsert', payload); + } else { + yield quest.warehouse.upsert(payload); + } } else if (!this.lastUpsertData.equals(payload.data)) { - yield quest.warehouse.upsert(payload); + if (WAREHOUSED) { + const warehouse = GOBLINS.get('warehouse').get('warehouse'); + warehouse._do('upsert', payload); + } else { + yield quest.warehouse.upsert(payload); + } this.lastUpsertData = payload.data; } } @@ -1214,8 +1227,12 @@ class Goblin { * TODO: think about a way to remove properly garbage after a * crash. */ - yield quest.warehouse.upsert(payload); - + if (WAREHOUSED) { + const warehouse = GOBLINS.get('warehouse').get('warehouse'); + warehouse._do('upsert', payload); + } else { + yield quest.warehouse.upsert(payload); + } if ( this.TTL === 0 && this._goblinConfig && @@ -1233,7 +1250,6 @@ class Goblin { } result = yield QUESTS[this._goblinName][questName](quest, msg); - //Create must return the goblin id if not provided if (questName === 'create' && !result) { result = this._goblinId; @@ -1256,7 +1272,6 @@ class Goblin { `Fatal error ${msg.topic} missing 'parent' parameter` ); } - let toUpsert = this.getState().state; if (toUpsert.size === 0) { toUpsert = Shredder.fromJS({id: this._goblinId}); @@ -1325,13 +1340,27 @@ class Goblin { /* If an error occurs while the goblin is created, then we must * delete its instance. */ - yield quest.warehouse.deleteBranch({ - branch: this._goblinId, - }); + if (WAREHOUSED) { + const warehouse = GOBLINS.get('warehouse').get('warehouse'); + warehouse._do('delete-branch', { + branch: this._goblinId, + }); + } else { + yield quest.warehouse.deleteBranch({ + branch: this._goblinId, + }); + } } else if (isCreating === true) { - yield quest.warehouse.delCreator({ - branch: this._goblinId, - }); + if (WAREHOUSED) { + const warehouse = GOBLINS.get('warehouse').get('warehouse'); + warehouse._do('del-creator', { + branch: this._goblinId, + }); + } else { + yield quest.warehouse.delCreator({ + branch: this._goblinId, + }); + } } if (questName === 'create') {